mirror of
https://github.com/facebook/docusaurus.git
synced 2025-07-22 11:07:51 +02:00
fix(core): Use proper swc loader options (#10605)
This commit is contained in:
parent
6eeab427bb
commit
1a2b8b7d05
4 changed files with 78 additions and 58 deletions
|
@ -9,8 +9,32 @@ import Rspack from '@rspack/core';
|
|||
import * as lightningcss from 'lightningcss';
|
||||
import browserslist from 'browserslist';
|
||||
import {minify as swcHtmlMinifier} from '@swc/html';
|
||||
import type {RuleSetRule} from 'webpack';
|
||||
import type {JsMinifyOptions} from '@swc/core';
|
||||
import type {JsMinifyOptions, Options as SwcOptions} from '@swc/core';
|
||||
|
||||
export const swcLoader = require.resolve('swc-loader');
|
||||
|
||||
export const getSwcLoaderOptions = ({
|
||||
isServer,
|
||||
}: {
|
||||
isServer: boolean;
|
||||
}): SwcOptions => {
|
||||
return {
|
||||
env: {
|
||||
targets: getBrowserslistQueries({isServer}),
|
||||
},
|
||||
jsc: {
|
||||
parser: {
|
||||
syntax: 'typescript',
|
||||
tsx: true,
|
||||
},
|
||||
transform: {
|
||||
react: {
|
||||
runtime: 'automatic',
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
export const rspack = Rspack;
|
||||
|
||||
|
@ -18,33 +42,6 @@ export function getSwcHtmlMinifier(): typeof swcHtmlMinifier {
|
|||
return swcHtmlMinifier;
|
||||
}
|
||||
|
||||
export function getSwcJsLoaderFactory({
|
||||
isServer,
|
||||
}: {
|
||||
isServer: boolean;
|
||||
}): RuleSetRule {
|
||||
return {
|
||||
loader: require.resolve('swc-loader'),
|
||||
options: {
|
||||
jsc: {
|
||||
parser: {
|
||||
syntax: 'typescript',
|
||||
tsx: true,
|
||||
},
|
||||
transform: {
|
||||
react: {
|
||||
runtime: 'automatic',
|
||||
},
|
||||
},
|
||||
target: 'es2017',
|
||||
},
|
||||
module: {
|
||||
type: isServer ? 'commonjs' : 'es6',
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
// Note: these options are similar to what we use in core
|
||||
// They should rather be kept in sync for now to avoid any unexpected behavior
|
||||
// The goal of faster minifier is not to fine-tune options but only to be faster
|
||||
|
@ -68,7 +65,15 @@ export function getSwcJsMinimizerOptions(): JsMinifyOptions {
|
|||
|
||||
// We need this because of Rspack built-in LightningCSS integration
|
||||
// See https://github.com/orgs/browserslist/discussions/846
|
||||
export function getBrowserslistQueries(): string[] {
|
||||
export function getBrowserslistQueries({
|
||||
isServer,
|
||||
}: {
|
||||
isServer: boolean;
|
||||
}): string[] {
|
||||
if (isServer) {
|
||||
return [`node ${process.versions.node}`];
|
||||
}
|
||||
|
||||
const queries = browserslist.loadConfig({path: process.cwd()}) ?? [
|
||||
...browserslist.defaults,
|
||||
];
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue