From 035fe51d368cd10309b89c46fbd761d9b96d3b97 Mon Sep 17 00:00:00 2001 From: RiESAEX <2597245950@qq.com> Date: Thu, 29 Dec 2022 08:18:33 +0000 Subject: [PATCH] fix: cjs --- .../plugins/commands/webpackConfig/index.js | 38 ++++++++++++++++--- .../commands/webpackConfig/swcOptions.js | 12 +++++- 2 files changed, 43 insertions(+), 7 deletions(-) diff --git a/packages/fes-builder-webpack/src/plugins/commands/webpackConfig/index.js b/packages/fes-builder-webpack/src/plugins/commands/webpackConfig/index.js index dc16ae5a..409015d6 100644 --- a/packages/fes-builder-webpack/src/plugins/commands/webpackConfig/index.js +++ b/packages/fes-builder-webpack/src/plugins/commands/webpackConfig/index.js @@ -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({ diff --git a/packages/fes-builder-webpack/src/plugins/commands/webpackConfig/swcOptions.js b/packages/fes-builder-webpack/src/plugins/commands/webpackConfig/swcOptions.js index 9be21c92..3f262db3 100644 --- a/packages/fes-builder-webpack/src/plugins/commands/webpackConfig/swcOptions.js +++ b/packages/fes-builder-webpack/src/plugins/commands/webpackConfig/swcOptions.js @@ -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 || {}, );