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
20 changes: 1 addition & 19 deletions e2e/cases/server/ssr-type-module/rsbuild.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,32 +55,14 @@ export default defineConfig({
},
node: {
output: {
module: true,
target: 'node',
},
source: {
entry: {
index: './src/index.server',
},
},
tools: {
rspack: (config) => {
return {
...config,
experiments: {
...config.experiments,
outputModule: true,
},
output: {
...config.output,
chunkFormat: 'module',
chunkLoading: 'import',
library: {
type: 'module',
},
},
};
},
},
},
},
html: {
Expand Down
10 changes: 1 addition & 9 deletions e2e/cases/server/ssr/rsbuild.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export default defineConfig({
},
node: {
output: {
module: true,
target: 'node',
},
source: {
Expand All @@ -67,19 +68,10 @@ export default defineConfig({
if (process.env.TEST_ESM_LIBRARY) {
return {
...config,
experiments: {
...config.experiments,
outputModule: true,
},
output: {
...config.output,
filename: '[name].mjs',
chunkFilename: '[name].mjs',
chunkFormat: 'module',
chunkLoading: 'import',
library: {
type: 'module',
},
},
};
}
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/createRsbuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { pluginCleanOutput } from './plugins/cleanOutput';
import { pluginCss } from './plugins/css';
import { pluginDefine } from './plugins/define';
import { pluginEntry } from './plugins/entry';
import { pluginEsm } from './plugins/esm';
import { pluginExternals } from './plugins/externals';
import { pluginFileSize } from './plugins/fileSize';
import { pluginHtml } from './plugins/html';
Expand Down Expand Up @@ -94,6 +95,7 @@ function applyDefaultPlugins(
pluginMinimize(),
pluginProgress(),
pluginSwc(),
pluginEsm(),
pluginExternals(),
pluginSplitChunks(),
pluginInlineChunk(),
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/defaultConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ const getDefaultOutputConfig = (): NormalizedOutputConfig => ({
legalComments: 'linked',
injectStyles: false,
minify: true,
module: false,
manifest: false,
sourceMap: {
js: undefined,
Expand Down
36 changes: 36 additions & 0 deletions packages/core/src/plugins/esm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import type { RsbuildPlugin } from '../types';

export const pluginEsm = (): RsbuildPlugin => ({
name: 'rsbuild:esm',

setup(api) {
api.modifyBundlerChain((chain, { environment, isServer }) => {
const { config } = environment;

if (!config.output.module) {
return;
}

if (!isServer) {
throw new Error(
'[rsbuild:config] `output.module` is only supported for Node.js targets.',
);
}

chain.output
.module(true)
.chunkFormat('module')
.chunkLoading('import')
.workerChunkLoading('import')
.library({
...chain.output.get('library'),
type: 'module',
});

chain.experiments({
...chain.get('experiments'),
outputModule: true,
});
});
},
});
8 changes: 8 additions & 0 deletions packages/core/src/types/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1230,6 +1230,13 @@ export interface OutputConfig {
* @default false
*/
manifest?: ManifestConfig;
/**
* Whether to output JavaScript files in ES modules format. This feature is currently
* experimental and only available when `output.target` is `'node'`.
* @experimental
* @default false
*/
module?: boolean;
/**
* Whether to generate source map files, and which format of source map to generate.
*
Expand Down Expand Up @@ -1308,6 +1315,7 @@ export interface NormalizedOutputConfig extends OutputConfig {
assetPrefix: string;
dataUriLimit: number | NormalizedDataUriLimit;
manifest: ManifestConfig;
module: boolean;
minify: Minify;
inlineScripts: InlineChunkConfig;
inlineStyles: InlineChunkConfig;
Expand Down
9 changes: 9 additions & 0 deletions packages/core/tests/__snapshots__/environments.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ exports[`environment config > should normalize environment config correctly 1`]
"legalComments": "linked",
"manifest": false,
"minify": true,
"module": false,
"polyfill": "off",
"sourceMap": {
"css": false,
Expand Down Expand Up @@ -217,6 +218,7 @@ exports[`environment config > should normalize environment config correctly 2`]
"legalComments": "linked",
"manifest": false,
"minify": true,
"module": false,
"polyfill": "off",
"sourceMap": {
"css": false,
Expand Down Expand Up @@ -362,6 +364,7 @@ exports[`environment config > should print environment config when inspect confi
"legalComments": "linked",
"manifest": false,
"minify": true,
"module": false,
"polyfill": "off",
"sourceMap": {
"css": false,
Expand Down Expand Up @@ -503,6 +506,7 @@ exports[`environment config > should print environment config when inspect confi
"legalComments": "linked",
"manifest": false,
"minify": true,
"module": false,
"polyfill": "off",
"sourceMap": {
"css": false,
Expand Down Expand Up @@ -664,6 +668,7 @@ exports[`environment config > should support modify environment config by api.mo
"legalComments": "linked",
"manifest": false,
"minify": true,
"module": false,
"polyfill": "off",
"sourceMap": {
"css": false,
Expand Down Expand Up @@ -805,6 +810,7 @@ exports[`environment config > should support modify environment config by api.mo
"legalComments": "linked",
"manifest": false,
"minify": true,
"module": false,
"polyfill": "off",
"sourceMap": {
"css": false,
Expand Down Expand Up @@ -947,6 +953,7 @@ exports[`environment config > should support modify environment config by api.mo
"legalComments": "linked",
"manifest": false,
"minify": true,
"module": false,
"polyfill": "off",
"sourceMap": {
"css": false,
Expand Down Expand Up @@ -1092,6 +1099,7 @@ exports[`environment config > should support modify single environment config by
"legalComments": "linked",
"manifest": false,
"minify": true,
"module": false,
"polyfill": "off",
"sourceMap": {
"css": false,
Expand Down Expand Up @@ -1233,6 +1241,7 @@ exports[`environment config > should support modify single environment config by
"legalComments": "linked",
"manifest": false,
"minify": true,
"module": false,
"polyfill": "off",
"sourceMap": {
"css": false,
Expand Down
Loading