Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: target default to node #398

Merged
merged 3 commits into from
Nov 11, 2024
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
3 changes: 0 additions & 3 deletions examples/express-plugin/rslib.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,4 @@ export default defineConfig({
},
},
],
output: {
target: 'node',
},
});
3 changes: 3 additions & 0 deletions examples/module-federation/mf-react-component/rslib.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,8 @@ export default defineConfig({
],
},
],
output: {
target: 'web',
},
plugins: [pluginReact()],
});
3 changes: 3 additions & 0 deletions examples/react-component-bundle-false/rslib.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ export default defineConfig({
},
},
],
output: {
target: 'web',
},
plugins: [
pluginReact({
swcReactOptions: {
Expand Down
3 changes: 3 additions & 0 deletions examples/react-component-bundle/rslib.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ export default defineConfig({
},
},
],
output: {
target: 'web',
},
plugins: [
pluginReact({
swcReactOptions: {
Expand Down
3 changes: 3 additions & 0 deletions examples/react-component-umd/rslib.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ export default defineConfig({
},
},
],
output: {
target: 'web',
},
plugins: [
pluginReact({
swcReactOptions: {
Expand Down
1 change: 1 addition & 0 deletions packages/core/rslib.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export default defineConfig({
},
},
output: {
// TODO: Remove this after bumping Rslib
target: 'node',
externals: {
picocolors: '../compiled/picocolors/index.js',
Expand Down
48 changes: 28 additions & 20 deletions packages/core/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,7 @@ export async function createConstantRsbuildConfig(): Promise<RsbuildConfig> {
},
},
output: {
target: 'node',
filenameHash: false,
distPath: {
js: './',
Expand Down Expand Up @@ -558,7 +559,6 @@ const composeFormatConfig = ({
},
output: {
asyncChunks: false,

library: umdName
? {
type: 'umd',
Expand Down Expand Up @@ -713,8 +713,8 @@ const composeAutoExtensionConfig = (
};

const composeSyntaxConfig = (
target: RsbuildConfigOutputTarget,
syntax?: Syntax,
target?: RsbuildConfigOutputTarget,
): RsbuildConfig => {
// Defaults to ESNext, Rslib will assume all of the latest JavaScript and CSS features are supported.
if (syntax) {
Expand Down Expand Up @@ -941,31 +941,40 @@ const composeDtsConfig = async (
};

const composeTargetConfig = (
target: RsbuildConfigOutputTarget = 'web',
): RsbuildConfig => {
target: RsbuildConfigOutputTarget = 'node',
): {
config: RsbuildConfig;
target: RsbuildConfigOutputTarget;
} => {
switch (target) {
case 'web':
return {
tools: {
rspack: {
target: ['web'],
config: {
tools: {
rspack: {
target: ['web'],
},
},
},
target: 'web',
};
case 'node':
return {
tools: {
rspack: {
target: ['node'],
config: {
tools: {
rspack: {
target: ['node'],
},
},
output: {
// When output.target is 'node', Node.js's built-in will be treated as externals of type `node-commonjs`.
// Simply override the built-in modules to make them external.
// https://github.com/webpack/webpack/blob/dd44b206a9c50f4b4cb4d134e1a0bd0387b159a3/lib/node/NodeTargetPlugin.js#L81
externals: nodeBuiltInModules,
target: 'node',
},
},
output: {
// When output.target is 'node', Node.js's built-in will be treated as externals of type `node-commonjs`.
// Simply override the built-in modules to make them external.
// https://github.com/webpack/webpack/blob/dd44b206a9c50f4b4cb4d134e1a0bd0387b159a3/lib/node/NodeTargetPlugin.js#L81
externals: nodeBuiltInModules,
target: 'node',
},
target: 'node',
};
// TODO: Support `neutral` target, however Rsbuild don't list it as an option in the target field.
// case 'neutral':
Expand Down Expand Up @@ -1067,11 +1076,10 @@ async function composeLibRsbuildConfig(config: LibConfig, configPath: string) {
cssModulesAuto,
bundle,
);
const targetConfig = composeTargetConfig(config.output?.target);
const syntaxConfig = composeSyntaxConfig(
config?.syntax,
const { config: targetConfig, target } = composeTargetConfig(
config.output?.target,
);
const syntaxConfig = composeSyntaxConfig(target, config?.syntax);
const autoExternalConfig = composeAutoExternalConfig({
autoExternal,
pkgJson,
Expand Down
6 changes: 1 addition & 5 deletions packages/core/src/utils/syntax.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@ export const LATEST_TARGET_VERSIONS: Record<
};

const calcEsnextBrowserslistByTarget = (target: RsbuildConfigOutputTarget) => {
if (!target) {
return [...LATEST_TARGET_VERSIONS.node, ...LATEST_TARGET_VERSIONS.web];
}

if (target === 'node') {
return LATEST_TARGET_VERSIONS.node;
}
Expand Down Expand Up @@ -195,7 +191,7 @@ export function transformSyntaxToRspackTarget(

export function transformSyntaxToBrowserslist(
syntax: Syntax,
target?: NonNullable<RsbuildConfig['output']>['target'],
target: NonNullable<RsbuildConfig['output']>['target'],
): NonNullable<NonNullable<RsbuildConfig['output']>['overrideBrowserslist']> {
const handleSyntaxItem = (
syntaxItem: EcmaScriptVersion | string,
Expand Down
Loading
Loading