Skip to content

Commit 3356743

Browse files
feat: Handle content to inject in worker
1 parent 1555c98 commit 3356743

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

lib/build/bundlers/esbuild/index.js

+11
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,17 @@ class Esbuild {
3434
config.plugins = [NodeModulesPolyfillPlugin(), ...config.plugins];
3535
}
3636

37+
// inject content in worker initial code.
38+
if (this.builderConfig.contentToInject) {
39+
const workerInitContent = this.builderConfig.contentToInject;
40+
41+
if (config.banner?.js) {
42+
config.banner.js = `${config.banner.js} ${workerInitContent}`;
43+
} else {
44+
config.banner = { js: workerInitContent };
45+
}
46+
}
47+
3748
try {
3849
await esbuild.build(config);
3950
} catch (error) {

lib/build/bundlers/webpack/index.js

+30
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,36 @@ class Webpack {
3030
config = merge(this.customConfig, config);
3131
}
3232

33+
// inject content in worker initial code.
34+
if (this.builderConfig.contentToInject) {
35+
const workerInitContent = this.builderConfig.contentToInject;
36+
37+
const bannerPluginIndex = config.plugins.findIndex(
38+
(plugin) => plugin instanceof webpack.BannerPlugin,
39+
);
40+
41+
if (bannerPluginIndex !== -1) {
42+
const oldContent = config.plugins[bannerPluginIndex].options.banner;
43+
const pluginToRemove = config.plugins[bannerPluginIndex];
44+
config.plugins = config.plugins.filter(
45+
(plugin) => plugin !== pluginToRemove,
46+
);
47+
config.plugins.push(
48+
new webpack.BannerPlugin({
49+
banner: `${oldContent} ${workerInitContent}`,
50+
raw: true,
51+
}),
52+
);
53+
} else {
54+
config.plugins.push(
55+
new webpack.BannerPlugin({
56+
banner: workerInitContent,
57+
raw: true,
58+
}),
59+
);
60+
}
61+
}
62+
3363
try {
3464
const stats = await runWebpack(config);
3565

0 commit comments

Comments
 (0)