Skip to content

Commit 0372ecd

Browse files
authored
fix(faster): fix server build SWC / browserslist node target (facebook#11496)
1 parent e133e8d commit 0372ecd

File tree

5 files changed

+46
-17
lines changed

5 files changed

+46
-17
lines changed

packages/docusaurus-bundler/src/loaders/jsLoader.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ async function createSwcJsLoaderFactory(): Promise<
2828
return ({isServer}) => {
2929
return {
3030
loader,
31-
options: getOptions({isServer}),
31+
options: getOptions({isServer, bundlerName: 'webpack'}),
3232
};
3333
};
3434
}
@@ -42,7 +42,7 @@ async function createRspackSwcJsLoaderFactory(): Promise<
4242
return ({isServer}) => {
4343
return {
4444
loader,
45-
options: getOptions({isServer}),
45+
options: getOptions({isServer, bundlerName: 'rspack'}),
4646
};
4747
};
4848
}

packages/docusaurus-bundler/src/minification.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,10 @@ async function getRspackMinimizers({
142142
}: MinimizersConfig): Promise<WebpackPluginInstance[]> {
143143
const rspack = getCurrentBundlerAsRspack({currentBundler});
144144
const getBrowserslistQueries = await importGetBrowserslistQueries();
145-
const browserslistQueries = getBrowserslistQueries({isServer: false});
145+
const browserslistQueries = getBrowserslistQueries({
146+
isServer: false,
147+
bundlerName: 'rspack',
148+
});
146149
const swcJsMinimizerOptions = await importSwcJsMinimizerOptions();
147150
return [
148151
// See https://rspack.dev/plugins/rspack/swc-js-minimizer-rspack-plugin

packages/docusaurus-faster/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"@swc/html": "^1.13.5",
2525
"browserslist": "^4.24.2",
2626
"lightningcss": "^1.27.0",
27+
"semver": "^7.5.4",
2728
"swc-loader": "^0.2.6",
2829
"tslib": "^2.6.0",
2930
"webpack": "^5.95.0"

packages/docusaurus-faster/src/index.ts

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,22 @@ import Rspack from '@rspack/core';
99
import * as lightningcss from 'lightningcss';
1010
import browserslist from 'browserslist';
1111
import {minify as swcHtmlMinifier} from '@swc/html';
12+
import semver from 'semver';
1213
import type {JsMinifyOptions, Options as SwcOptions} from '@swc/core';
14+
import type {CurrentBundler} from '@docusaurus/types';
1315

1416
export const swcLoader = require.resolve('swc-loader');
1517

1618
export const getSwcLoaderOptions = ({
1719
isServer,
20+
bundlerName,
1821
}: {
1922
isServer: boolean;
23+
bundlerName: CurrentBundler['name'];
2024
}): SwcOptions => {
2125
return {
2226
env: {
23-
targets: getBrowserslistQueries({isServer}),
27+
targets: getBrowserslistQueries({isServer, bundlerName}),
2428
},
2529
jsc: {
2630
parser: {
@@ -63,20 +67,53 @@ export function getSwcJsMinimizerOptions(): JsMinifyOptions {
6367
};
6468
}
6569

70+
// TODO this is not accurate
71+
// for Rspack we should read from the built-in browserslist data
72+
// see https://github.com/facebook/docusaurus/pull/11496
73+
function getLastBrowserslistKnownNodeVersion(
74+
bundlerName: CurrentBundler['name'],
75+
): string {
76+
if (bundlerName === 'rspack') {
77+
// TODO hardcoded value until Rspack exposes its Browserslist data
78+
// see https://github.com/facebook/docusaurus/pull/11496
79+
return '22.0.0';
80+
}
81+
// browserslist('last 1 node versions')[0]!.replace('node ', '')
82+
return browserslist.nodeVersions.at(-1)!;
83+
}
84+
85+
function getMinVersion(v1: string, v2: string): string {
86+
return semver.lt(v1, v2) ? v1 : v2;
87+
}
88+
6689
// We need this because of Rspack built-in LightningCSS integration
6790
// See https://github.com/orgs/browserslist/discussions/846
6891
export function getBrowserslistQueries({
6992
isServer,
93+
bundlerName,
7094
}: {
7195
isServer: boolean;
96+
bundlerName: CurrentBundler['name'];
7297
}): string[] {
7398
if (isServer) {
74-
return [`node ${process.versions.node}`];
99+
// Escape hatch env variable
100+
if (process.env.DOCUSAURUS_SERVER_NODE_TARGET) {
101+
return [`node ${process.env.DOCUSAURUS_SERVER_NODE_TARGET}`];
102+
}
103+
// For server builds, we want to use the current Node version as target
104+
// But we can't pass a target that Browserslist doesn't know about yet
105+
const nodeTarget = getMinVersion(
106+
process.versions.node,
107+
getLastBrowserslistKnownNodeVersion(bundlerName),
108+
);
109+
110+
return [`node ${nodeTarget}`];
75111
}
76112

77113
const queries = browserslist.loadConfig({path: process.cwd()}) ?? [
78114
...browserslist.defaults,
79115
];
116+
80117
return queries;
81118
}
82119

packages/docusaurus-theme-mermaid/src/index.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,6 @@ export default async function themeMermaid(): Promise<Plugin<void>> {
4949
),
5050
}),
5151
],
52-
53-
// Workaround for weird Rspack/SWC issue
54-
// See https://github.com/facebook/docusaurus/issues/11430
55-
resolve: {
56-
alias: {
57-
...(elkLayoutEnabled
58-
? {}
59-
: {
60-
'@mermaid-js/layout-elk': false,
61-
}),
62-
},
63-
},
6452
};
6553
},
6654
};

0 commit comments

Comments
 (0)