Skip to content
Open
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
6 changes: 6 additions & 0 deletions .changeset/service-worker-module-output.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@modern-js/builder': patch
---

fix: respect output.module for service-worker environment output
fix: 在 service-worker 环境中遵循 output.module 输出配置
7 changes: 6 additions & 1 deletion packages/cli/builder/src/plugins/environmentDefaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,12 @@ export const pluginEnvironmentDefaults = (
const isServiceWorker =
environment.name === SERVICE_WORKER_ENVIRONMENT_NAME;

if (isServiceWorker) {
if (isServiceWorker && chain.output.get('module') === true) {
chain.output.library({
...(chain.output.get('library') || {}),
type: 'module',
});
Comment on lines +89 to +92

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Drop library names before switching to module output

When a service-worker build has output.module: true and an existing output.library.name from user/environment config, this spread preserves that name while changing the library type to module. Rspack requires module libraries to leave the library name unset, so this turns that previously valid commonjs2 worker configuration into a build-time validation error instead of producing ESM output. Strip or ignore name when selecting module output.

Useful? React with 👍 / 👎.

} else if (isServiceWorker) {
chain.output.library({
...(chain.output.get('library') || {}),
type: 'commonjs2',
Expand Down
38 changes: 38 additions & 0 deletions packages/cli/builder/tests/environment.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { join } from 'path';
import { afterEach, describe, expect, it, rstest } from '@rstest/core';
import { after } from 'lodash';
import { createBuilder } from '../src';
import { pluginEnvironmentDefaults } from '../src/plugins/environmentDefaults';

describe('builder environment compat', () => {
afterEach(() => {
Expand Down Expand Up @@ -42,4 +43,41 @@ describe('builder environment compat', () => {

expect(bundlerConfigs).toMatchSnapshot();
});

it('should use module library output when service-worker output module is enabled', async () => {
let handler: ((chain: any, utils: any) => Promise<void>) | undefined;

pluginEnvironmentDefaults().setup({
modifyBundlerChain: (registeredHandler: any) => {
handler =
typeof registeredHandler === 'function'
? registeredHandler
: registeredHandler.handler;
},
modifyEnvironmentConfig: () => {},
modifyRsbuildConfig: () => {},
} as any);

if (!handler) {
throw new Error('Expected environment defaults bundler-chain handler.');
}

const libraryCalls: any[] = [];
const chain = {
output: {
get: (key: string) => (key === 'module' ? true : undefined),
library: (value: any) => {
libraryCalls.push(value);
},
},
};

await handler(chain, {
environment: {
name: 'workerSSR',
},
});

expect(libraryCalls).toEqual([{ type: 'module' }]);
});
});