Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions packages/core/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,9 @@ const composeFormatConfig = ({
importMeta: false,
importDynamic: false,
},
others: {
worker: false,
},
} as const;

switch (format) {
Expand All @@ -588,6 +591,7 @@ const composeFormatConfig = ({
javascript: {
...jsParserOptions.esm,
...jsParserOptions.cjs,
...jsParserOptions.others,
},
},
},
Expand Down Expand Up @@ -618,7 +622,11 @@ const composeFormatConfig = ({
rspack: {
module: {
parser: {
javascript: { ...jsParserOptions.esm, ...jsParserOptions.cjs },
javascript: {
...jsParserOptions.esm,
...jsParserOptions.cjs,
...jsParserOptions.others,
},
},
},
output: {
Expand Down Expand Up @@ -701,8 +709,8 @@ const composeFormatConfig = ({
}
};

const formatRsbuildPlugin = (): RsbuildPlugin => ({
name: 'rsbuild:format',
const disableUrlParseRsbuildPlugin = (): RsbuildPlugin => ({
name: 'rsbuild:disable-url-parse',
setup(api) {
api.modifyBundlerChain((config, { CHAIN_ID }) => {
// Fix for https://github.com/web-infra-dev/rslib/issues/499.
Expand Down Expand Up @@ -761,7 +769,7 @@ const composeShimsConfig = (
},
plugins: [
resolvedShims.esm.require && pluginEsmRequireShim(),
formatRsbuildPlugin(),
disableUrlParseRsbuildPlugin(),
].filter(Boolean),
};
break;
Expand All @@ -770,6 +778,7 @@ const composeShimsConfig = (
rsbuildConfig = {
plugins: [
resolvedShims.cjs['import.meta.url'] && pluginCjsImportMetaUrlShim(),
disableUrlParseRsbuildPlugin(),
].filter(Boolean),
};
break;
Expand Down
8 changes: 7 additions & 1 deletion packages/core/tests/__snapshots__/config.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config i
},
"plugins": [
{
"name": "rsbuild:format",
"name": "rsbuild:disable-url-parse",
"setup": [Function],
},
{
Expand Down Expand Up @@ -189,6 +189,7 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config i
"requireAsExpression": false,
"requireDynamic": false,
"requireResolve": false,
"worker": false,
},
},
},
Expand Down Expand Up @@ -365,6 +366,10 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config i
"name": "rsbuild:cjs-import-meta-url-shim",
"setup": [Function],
},
{
"name": "rsbuild:disable-url-parse",
"setup": [Function],
},
{
"name": "rsbuild:lib-asset",
"pre": [
Expand Down Expand Up @@ -441,6 +446,7 @@ exports[`Should compose create Rsbuild config correctly > Merge Rsbuild config i
"requireAsExpression": false,
"requireDynamic": false,
"requireResolve": false,
"worker": false,
},
},
},
Expand Down
2 changes: 2 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions tests/integration/worker/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { buildAndGetResults, queryContent } from 'test-helper';
import { expect, test } from 'vitest';

test('new Worker(new URL(...)) should be preserved', async () => {
process.env.NODE_ENV = 'production';
const fixturePath = __dirname;
const { contents } = await buildAndGetResults({
fixturePath,
});

expect(contents.esm).toMatchInlineSnapshot(`
{
"<ROOT>/tests/integration/worker/dist/esm/index.js": "const worker = new Worker(new URL('./worker.js', import.meta.url), {
name: 'my-worker'
});
export { worker };
",
"<ROOT>/tests/integration/worker/dist/esm/worker.js": "console.log('Hello from worker', self.name);
",
}
`);

expect(queryContent(contents.cjs, /\/index\.js/).content).toContain(
"new Worker(new URL('./worker.js', __rslib_import_meta_url__)",
);
});
5 changes: 5 additions & 0 deletions tests/integration/worker/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "worker-test",
"version": "1.0.0",
"private": true
}
25 changes: 25 additions & 0 deletions tests/integration/worker/rslib.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { defineConfig } from '@rslib/core';
import { generateBundleCjsConfig, generateBundleEsmConfig } from 'test-helper';

export default defineConfig({
lib: [
generateBundleEsmConfig({
autoExtension: false,
source: {
entry: {
index: './src/index.ts',
worker: './src/worker.ts',
},
},
}),
generateBundleCjsConfig({
autoExtension: false,
source: {
entry: {
index: './src/index.ts',
worker: './src/worker.ts',
},
},
}),
],
});
3 changes: 3 additions & 0 deletions tests/integration/worker/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const worker = new Worker(new URL('./worker.js', import.meta.url), {
name: 'my-worker',
});
1 change: 1 addition & 0 deletions tests/integration/worker/src/worker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('Hello from worker', self.name);
Loading