fix(core): Use proper swc loader options (#10605)

This commit is contained in:
Sébastien Lorber 2024-10-23 14:47:14 +02:00 committed by GitHub
parent 6eeab427bb
commit 1a2b8b7d05
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 78 additions and 58 deletions

View file

@ -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,
];