Skip to content

Commit 1a2b8b7

Browse files
authored
fix(core): Use proper swc loader options (#10605)
1 parent 6eeab42 commit 1a2b8b7

File tree

4 files changed

+73
-53
lines changed

4 files changed

+73
-53
lines changed

packages/docusaurus-bundler/src/importFaster.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
import type {ConfigureWebpackUtils} from '@docusaurus/types';
98
import type {
109
MinimizerOptions as JsMinimizerOptions,
1110
CustomOptions,
@@ -34,11 +33,16 @@ export async function importRspack(): Promise<FasterModule['rspack']> {
3433
return faster.rspack;
3534
}
3635

37-
export async function importSwcJsLoaderFactory(): Promise<
38-
ConfigureWebpackUtils['getJSLoader']
36+
export async function importSwcLoader(): Promise<string> {
37+
const faster = await ensureFaster();
38+
return faster.swcLoader;
39+
}
40+
41+
export async function importGetSwcLoaderOptions(): Promise<
42+
FasterModule['getSwcLoaderOptions']
3943
> {
4044
const faster = await ensureFaster();
41-
return faster.getSwcJsLoaderFactory;
45+
return faster.getSwcLoaderOptions;
4246
}
4347

4448
export async function importSwcJsMinimizerOptions(): Promise<
@@ -55,9 +59,11 @@ export async function importSwcHtmlMinifier(): Promise<
5559
return faster.getSwcHtmlMinifier();
5660
}
5761

58-
export async function importBrowserslistQueries(): Promise<string[]> {
62+
export async function importGetBrowserslistQueries(): Promise<
63+
FasterModule['getBrowserslistQueries']
64+
> {
5965
const faster = await ensureFaster();
60-
return faster.getBrowserslistQueries();
66+
return faster.getBrowserslistQueries;
6167
}
6268

6369
export async function importLightningCssMinimizerOptions(): Promise<

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

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77

88
import {getBabelOptions} from '@docusaurus/babel';
9-
import {importSwcJsLoaderFactory} from '../importFaster';
9+
import {importSwcLoader, importGetSwcLoaderOptions} from '../importFaster';
1010
import {getCurrentBundler} from '../currentBundler';
1111
import type {ConfigureWebpackUtils, DocusaurusConfig} from '@docusaurus/types';
1212

@@ -20,24 +20,32 @@ const BabelJsLoaderFactory: ConfigureWebpackUtils['getJSLoader'] = ({
2020
};
2121
};
2222

23-
const RspackJsLoaderFactory: ConfigureWebpackUtils['getJSLoader'] = () => {
24-
return {
25-
loader: 'builtin:swc-loader',
26-
options: {
27-
jsc: {
28-
parser: {
29-
syntax: 'typescript',
30-
tsx: true,
31-
},
32-
transform: {
33-
react: {
34-
runtime: 'automatic',
35-
},
36-
},
37-
},
38-
},
23+
async function createSwcLoaderFactory(): Promise<
24+
ConfigureWebpackUtils['getJSLoader']
25+
> {
26+
const loader = await importSwcLoader();
27+
const getOptions = await importGetSwcLoaderOptions();
28+
return ({isServer}) => {
29+
return {
30+
loader,
31+
options: getOptions({isServer}),
32+
};
3933
};
40-
};
34+
}
35+
36+
// Same as swcLoader, except we use the built-in SWC loader
37+
async function createRspackLoaderFactory(): Promise<
38+
ConfigureWebpackUtils['getJSLoader']
39+
> {
40+
const loader = 'builtin:swc-loader';
41+
const getOptions = await importGetSwcLoaderOptions();
42+
return ({isServer}) => {
43+
return {
44+
loader,
45+
options: getOptions({isServer}),
46+
};
47+
};
48+
}
4149

4250
// Confusing: function that creates a function that creates actual js loaders
4351
// This is done on purpose because the js loader factory is a public API
@@ -61,7 +69,7 @@ export async function createJsLoaderFactory({
6169
'When using Rspack bundler, it is required to enable swcJsLoader too',
6270
);
6371
}
64-
return RspackJsLoaderFactory;
72+
return createRspackLoaderFactory();
6573
}
6674
const jsLoader = siteConfig.webpack?.jsLoader ?? 'babel';
6775
if (
@@ -76,7 +84,7 @@ export async function createJsLoaderFactory({
7684
return ({isServer}) => jsLoader(isServer);
7785
}
7886
if (siteConfig.future?.experimental_faster.swcJsLoader) {
79-
return importSwcJsLoaderFactory();
87+
return createSwcLoaderFactory();
8088
}
8189
if (jsLoader === 'babel') {
8290
return BabelJsLoaderFactory;

packages/docusaurus-bundler/src/minification.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import CssMinimizerPlugin from 'css-minimizer-webpack-plugin';
1010
import {
1111
importSwcJsMinimizerOptions,
1212
importLightningCssMinimizerOptions,
13-
importBrowserslistQueries,
13+
importGetBrowserslistQueries,
1414
} from './importFaster';
1515
import {getCurrentBundlerAsRspack} from './currentBundler';
1616
import type {CustomOptions, CssNanoOptions} from 'css-minimizer-webpack-plugin';
@@ -141,7 +141,8 @@ async function getRspackMinimizers({
141141
currentBundler,
142142
}: MinimizersConfig): Promise<WebpackPluginInstance[]> {
143143
const rspack = getCurrentBundlerAsRspack({currentBundler});
144-
const browserslistQueries = await importBrowserslistQueries();
144+
const getBrowserslistQueries = await importGetBrowserslistQueries();
145+
const browserslistQueries = getBrowserslistQueries({isServer: false});
145146
const swcJsMinimizerOptions = await importSwcJsMinimizerOptions();
146147
return [
147148
// See https://rspack.dev/plugins/rspack/swc-js-minimizer-rspack-plugin

packages/docusaurus-faster/src/index.ts

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,40 +9,37 @@ 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 type {RuleSetRule} from 'webpack';
13-
import type {JsMinifyOptions} from '@swc/core';
12+
import type {JsMinifyOptions, Options as SwcOptions} from '@swc/core';
1413

15-
export const rspack = Rspack;
16-
17-
export function getSwcHtmlMinifier(): typeof swcHtmlMinifier {
18-
return swcHtmlMinifier;
19-
}
14+
export const swcLoader = require.resolve('swc-loader');
2015

21-
export function getSwcJsLoaderFactory({
16+
export const getSwcLoaderOptions = ({
2217
isServer,
2318
}: {
2419
isServer: boolean;
25-
}): RuleSetRule {
20+
}): SwcOptions => {
2621
return {
27-
loader: require.resolve('swc-loader'),
28-
options: {
29-
jsc: {
30-
parser: {
31-
syntax: 'typescript',
32-
tsx: true,
33-
},
34-
transform: {
35-
react: {
36-
runtime: 'automatic',
37-
},
38-
},
39-
target: 'es2017',
22+
env: {
23+
targets: getBrowserslistQueries({isServer}),
24+
},
25+
jsc: {
26+
parser: {
27+
syntax: 'typescript',
28+
tsx: true,
4029
},
41-
module: {
42-
type: isServer ? 'commonjs' : 'es6',
30+
transform: {
31+
react: {
32+
runtime: 'automatic',
33+
},
4334
},
4435
},
4536
};
37+
};
38+
39+
export const rspack = Rspack;
40+
41+
export function getSwcHtmlMinifier(): typeof swcHtmlMinifier {
42+
return swcHtmlMinifier;
4643
}
4744

4845
// Note: these options are similar to what we use in core
@@ -68,7 +65,15 @@ export function getSwcJsMinimizerOptions(): JsMinifyOptions {
6865

6966
// We need this because of Rspack built-in LightningCSS integration
7067
// See https://github.com/orgs/browserslist/discussions/846
71-
export function getBrowserslistQueries(): string[] {
68+
export function getBrowserslistQueries({
69+
isServer,
70+
}: {
71+
isServer: boolean;
72+
}): string[] {
73+
if (isServer) {
74+
return [`node ${process.versions.node}`];
75+
}
76+
7277
const queries = browserslist.loadConfig({path: process.cwd()}) ?? [
7378
...browserslist.defaults,
7479
];

0 commit comments

Comments
 (0)