This commit is contained in:
RiESAEX 2022-12-29 08:18:33 +00:00
parent 2d92ad883f
commit 862ba3cf9c
2 changed files with 43 additions and 7 deletions

View File

@ -127,35 +127,42 @@ export default async function getConfig({ api, cwd, config, env, entry = {}, mod
.rule('js')
.test(/\.(js|mjs)$/)
// Don't transpile node_modules
.exclude.add((filepath) => /node_modules/.test(filepath))
.exclude.add(/node_modules/)
.end()
.use('swc-loader')
.loader(require.resolve('swc-loader'))
.options(buildSwcOptions(targets, config, false, false));
.options(buildSwcOptions(targets, config, false, false, true));
webpackConfig.module
.rule('jsx')
.test(/\.jsx$/)
.exclude.add(/node_modules/)
.end()
.use('swc-loader')
.loader(require.resolve('swc-loader'))
.options(buildSwcOptions(targets, config, true, false));
.options(buildSwcOptions(targets, config, true, false, true));
webpackConfig.module
.rule('ts')
.test(/\.ts$/)
.exclude.add(/node_modules/)
.end()
.use('swc-loader')
.loader(require.resolve('swc-loader'))
.options(buildSwcOptions(targets, config, false, true));
webpackConfig.module
.rule('tsx')
.test(/\.tsx$/)
.exclude.add(/node_modules/)
.end()
.use('swc-loader')
.loader(require.resolve('swc-loader'))
.options(buildSwcOptions(targets, config, true, true));
// 为了避免第三方依赖包编译不充分导致线上问题,默认对 node_modules 也进行全编译,只在生产构建的时候进行
if (isProd) {
const cjsReg = [/css-loader/].concat(config.swcLoader?.cjsPkg || []);
const transpileDepRegex = genTranspileDepRegex(config.nodeModulesTransform.exclude);
webpackConfig.module
.rule('js-in-node_modules')
.rule('esm-in-node_modules')
.test(/\.(js|mjs)$/)
.include.add(/node_modules/)
.end()
@ -163,13 +170,34 @@ export default async function getConfig({ api, cwd, config, env, entry = {}, mod
if (transpileDepRegex && transpileDepRegex.test(filepath)) {
return true;
}
if (cjsReg.some((reg) => reg.test(filepath))) {
return true;
}
return false;
})
.end()
.use('swc-loader')
.loader(require.resolve('swc-loader'))
.options(buildSwcOptions(targets, config, false, false, true));
webpackConfig.module
.rule('cjs-in-node_modules')
.test(/\.(js)$/)
.include.add(/node_modules/)
.end()
.exclude.add((filepath) => {
if (transpileDepRegex && transpileDepRegex.test(filepath)) {
return true;
}
if (cjsReg.every((reg) => !reg.test(filepath))) {
return true;
}
return false;
})
.end()
.use('swc-loader')
.loader(require.resolve('swc-loader'))
.options(buildSwcOptions(targets, config, false, false));
.options(buildSwcOptions(targets, config, false, false, false));
}
} else {
const babelOpts = await getBabelOpts({

View File

@ -1,8 +1,12 @@
import { deepmerge } from '@fesjs/utils';
export function buildSwcOptions(targets, config, isJsx, isTs) {
export function buildSwcOptions(targets, config, isJsx, isTs, isEsm) {
if (config.swcLoader?.cjsPkg) {
delete config.swcLoader.cjsPkg;
}
return deepmerge(
{
// sync: true,
env: {
targets,
mode: 'usage',
@ -16,8 +20,12 @@ export function buildSwcOptions(targets, config, isJsx, isTs) {
experimental: {
plugins: [['swc-plugin-vue-jsx', {}]],
},
// preserveAllComments: true,
},
minify: true,
module: {
type: isEsm ? 'es6' : 'commonjs',
},
// minify: true,
},
config.swcLoader || {},
);