Skip to content

Commit 1a67571

Browse files
authored
fix: preserve worker syntax (#959)
1 parent 6e82126 commit 1a67571

File tree

8 files changed

+82
-5
lines changed

8 files changed

+82
-5
lines changed

packages/core/src/config.ts

+13-4
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,9 @@ const composeFormatConfig = ({
576576
importMeta: false,
577577
importDynamic: false,
578578
},
579+
others: {
580+
worker: false,
581+
},
579582
} as const;
580583

581584
switch (format) {
@@ -588,6 +591,7 @@ const composeFormatConfig = ({
588591
javascript: {
589592
...jsParserOptions.esm,
590593
...jsParserOptions.cjs,
594+
...jsParserOptions.others,
591595
},
592596
},
593597
},
@@ -618,7 +622,11 @@ const composeFormatConfig = ({
618622
rspack: {
619623
module: {
620624
parser: {
621-
javascript: { ...jsParserOptions.esm, ...jsParserOptions.cjs },
625+
javascript: {
626+
...jsParserOptions.esm,
627+
...jsParserOptions.cjs,
628+
...jsParserOptions.others,
629+
},
622630
},
623631
},
624632
output: {
@@ -701,8 +709,8 @@ const composeFormatConfig = ({
701709
}
702710
};
703711

704-
const formatRsbuildPlugin = (): RsbuildPlugin => ({
705-
name: 'rsbuild:format',
712+
const disableUrlParseRsbuildPlugin = (): RsbuildPlugin => ({
713+
name: 'rsbuild:disable-url-parse',
706714
setup(api) {
707715
api.modifyBundlerChain((config, { CHAIN_ID }) => {
708716
// Fix for https://github.com/web-infra-dev/rslib/issues/499.
@@ -761,7 +769,7 @@ const composeShimsConfig = (
761769
},
762770
plugins: [
763771
resolvedShims.esm.require && pluginEsmRequireShim(),
764-
formatRsbuildPlugin(),
772+
disableUrlParseRsbuildPlugin(),
765773
].filter(Boolean),
766774
};
767775
break;
@@ -770,6 +778,7 @@ const composeShimsConfig = (
770778
rsbuildConfig = {
771779
plugins: [
772780
resolvedShims.cjs['import.meta.url'] && pluginCjsImportMetaUrlShim(),
781+
disableUrlParseRsbuildPlugin(),
773782
].filter(Boolean),
774783
};
775784
break;

packages/core/tests/__snapshots__/config.test.ts.snap

+7-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config i
111111
},
112112
"plugins": [
113113
{
114-
"name": "rsbuild:format",
114+
"name": "rsbuild:disable-url-parse",
115115
"setup": [Function],
116116
},
117117
{
@@ -189,6 +189,7 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config i
189189
"requireAsExpression": false,
190190
"requireDynamic": false,
191191
"requireResolve": false,
192+
"worker": false,
192193
},
193194
},
194195
},
@@ -365,6 +366,10 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config i
365366
"name": "rsbuild:cjs-import-meta-url-shim",
366367
"setup": [Function],
367368
},
369+
{
370+
"name": "rsbuild:disable-url-parse",
371+
"setup": [Function],
372+
},
368373
{
369374
"name": "rsbuild:lib-asset",
370375
"pre": [
@@ -441,6 +446,7 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config i
441446
"requireAsExpression": false,
442447
"requireDynamic": false,
443448
"requireResolve": false,
449+
"worker": false,
444450
},
445451
},
446452
},

pnpm-lock.yaml

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { buildAndGetResults, queryContent } from 'test-helper';
2+
import { expect, test } from 'vitest';
3+
4+
test('new Worker(new URL(...)) should be preserved', async () => {
5+
process.env.NODE_ENV = 'production';
6+
const fixturePath = __dirname;
7+
const { contents } = await buildAndGetResults({
8+
fixturePath,
9+
});
10+
11+
expect(contents.esm).toMatchInlineSnapshot(`
12+
{
13+
"<ROOT>/tests/integration/worker/dist/esm/index.js": "const worker = new Worker(new URL('./worker.js', import.meta.url), {
14+
name: 'my-worker'
15+
});
16+
export { worker };
17+
",
18+
"<ROOT>/tests/integration/worker/dist/esm/worker.js": "console.log('Hello from worker', self.name);
19+
",
20+
}
21+
`);
22+
23+
expect(queryContent(contents.cjs, /\/index\.js/).content).toContain(
24+
"new Worker(new URL('./worker.js', __rslib_import_meta_url__)",
25+
);
26+
});

tests/integration/worker/package.json

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"name": "worker-test",
3+
"version": "1.0.0",
4+
"private": true
5+
}
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { defineConfig } from '@rslib/core';
2+
import { generateBundleCjsConfig, generateBundleEsmConfig } from 'test-helper';
3+
4+
export default defineConfig({
5+
lib: [
6+
generateBundleEsmConfig({
7+
autoExtension: false,
8+
source: {
9+
entry: {
10+
index: './src/index.ts',
11+
worker: './src/worker.ts',
12+
},
13+
},
14+
}),
15+
generateBundleCjsConfig({
16+
autoExtension: false,
17+
source: {
18+
entry: {
19+
index: './src/index.ts',
20+
worker: './src/worker.ts',
21+
},
22+
},
23+
}),
24+
],
25+
});

tests/integration/worker/src/index.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export const worker = new Worker(new URL('./worker.js', import.meta.url), {
2+
name: 'my-worker',
3+
});
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log('Hello from worker', self.name);

0 commit comments

Comments
 (0)