This commit is contained in:
RiESAEX 2022-12-29 08:18:33 +00:00
parent 2d98538777
commit 035fe51d36
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') .rule('js')
.test(/\.(js|mjs)$/) .test(/\.(js|mjs)$/)
// Don't transpile node_modules // Don't transpile node_modules
.exclude.add((filepath) => /node_modules/.test(filepath)) .exclude.add(/node_modules/)
.end() .end()
.use('swc-loader') .use('swc-loader')
.loader(require.resolve('swc-loader')) .loader(require.resolve('swc-loader'))
.options(buildSwcOptions(targets, config, false, false)); .options(buildSwcOptions(targets, config, false, false, true));
webpackConfig.module webpackConfig.module
.rule('jsx') .rule('jsx')
.test(/\.jsx$/) .test(/\.jsx$/)
.exclude.add(/node_modules/)
.end()
.use('swc-loader') .use('swc-loader')
.loader(require.resolve('swc-loader')) .loader(require.resolve('swc-loader'))
.options(buildSwcOptions(targets, config, true, false)); .options(buildSwcOptions(targets, config, true, false, true));
webpackConfig.module webpackConfig.module
.rule('ts') .rule('ts')
.test(/\.ts$/) .test(/\.ts$/)
.exclude.add(/node_modules/)
.end()
.use('swc-loader') .use('swc-loader')
.loader(require.resolve('swc-loader')) .loader(require.resolve('swc-loader'))
.options(buildSwcOptions(targets, config, false, true)); .options(buildSwcOptions(targets, config, false, true));
webpackConfig.module webpackConfig.module
.rule('tsx') .rule('tsx')
.test(/\.tsx$/) .test(/\.tsx$/)
.exclude.add(/node_modules/)
.end()
.use('swc-loader') .use('swc-loader')
.loader(require.resolve('swc-loader')) .loader(require.resolve('swc-loader'))
.options(buildSwcOptions(targets, config, true, true)); .options(buildSwcOptions(targets, config, true, true));
// 为了避免第三方依赖包编译不充分导致线上问题,默认对 node_modules 也进行全编译,只在生产构建的时候进行 // 为了避免第三方依赖包编译不充分导致线上问题,默认对 node_modules 也进行全编译,只在生产构建的时候进行
if (isProd) { if (isProd) {
const cjsReg = [/css-loader/].concat(config.swcLoader?.cjsPkg || []);
const transpileDepRegex = genTranspileDepRegex(config.nodeModulesTransform.exclude); const transpileDepRegex = genTranspileDepRegex(config.nodeModulesTransform.exclude);
webpackConfig.module webpackConfig.module
.rule('js-in-node_modules') .rule('esm-in-node_modules')
.test(/\.(js|mjs)$/) .test(/\.(js|mjs)$/)
.include.add(/node_modules/) .include.add(/node_modules/)
.end() .end()
@ -163,13 +170,34 @@ export default async function getConfig({ api, cwd, config, env, entry = {}, mod
if (transpileDepRegex && transpileDepRegex.test(filepath)) { if (transpileDepRegex && transpileDepRegex.test(filepath)) {
return true; 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; return false;
}) })
.end() .end()
.use('swc-loader') .use('swc-loader')
.loader(require.resolve('swc-loader')) .loader(require.resolve('swc-loader'))
.options(buildSwcOptions(targets, config, false, false)); .options(buildSwcOptions(targets, config, false, false, false));
} }
} else { } else {
const babelOpts = await getBabelOpts({ const babelOpts = await getBabelOpts({

View File

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