feat: 只在prod开压缩

This commit is contained in:
RiESAEX 2023-01-06 08:56:44 +00:00
parent 862ba3cf9c
commit bca727338b
2 changed files with 13 additions and 11 deletions

View File

@ -131,7 +131,7 @@ export default async function getConfig({ api, cwd, config, env, entry = {}, mod
.end()
.use('swc-loader')
.loader(require.resolve('swc-loader'))
.options(buildSwcOptions(targets, config, false, false, true));
.options(buildSwcOptions(targets, config, false, false, true, false));
webpackConfig.module
.rule('jsx')
.test(/\.jsx$/)
@ -139,7 +139,7 @@ export default async function getConfig({ api, cwd, config, env, entry = {}, mod
.end()
.use('swc-loader')
.loader(require.resolve('swc-loader'))
.options(buildSwcOptions(targets, config, true, false, true));
.options(buildSwcOptions(targets, config, true, false, true, false));
webpackConfig.module
.rule('ts')
@ -148,7 +148,7 @@ export default async function getConfig({ api, cwd, config, env, entry = {}, mod
.end()
.use('swc-loader')
.loader(require.resolve('swc-loader'))
.options(buildSwcOptions(targets, config, false, true));
.options(buildSwcOptions(targets, config, false, true, false));
webpackConfig.module
.rule('tsx')
.test(/\.tsx$/)
@ -156,7 +156,7 @@ export default async function getConfig({ api, cwd, config, env, entry = {}, mod
.end()
.use('swc-loader')
.loader(require.resolve('swc-loader'))
.options(buildSwcOptions(targets, config, true, true));
.options(buildSwcOptions(targets, config, true, true, false));
// 为了避免第三方依赖包编译不充分导致线上问题,默认对 node_modules 也进行全编译,只在生产构建的时候进行
if (isProd) {
const cjsReg = [/css-loader/].concat(config.swcLoader?.cjsPkg || []);
@ -178,7 +178,7 @@ export default async function getConfig({ api, cwd, config, env, entry = {}, mod
.end()
.use('swc-loader')
.loader(require.resolve('swc-loader'))
.options(buildSwcOptions(targets, config, false, false, true));
.options(buildSwcOptions(targets, config, false, false, true, true));
webpackConfig.module
.rule('cjs-in-node_modules')
.test(/\.(js)$/)
@ -197,7 +197,7 @@ export default async function getConfig({ api, cwd, config, env, entry = {}, mod
.end()
.use('swc-loader')
.loader(require.resolve('swc-loader'))
.options(buildSwcOptions(targets, config, false, false, false));
.options(buildSwcOptions(targets, config, false, false, false, true));
}
} else {
const babelOpts = await getBabelOpts({

View File

@ -1,6 +1,6 @@
import { deepmerge } from '@fesjs/utils';
export function buildSwcOptions(targets, config, isJsx, isTs, isEsm) {
export function buildSwcOptions(targets, config, isJsx, isTs, isEsm, minify) {
if (config.swcLoader?.cjsPkg) {
delete config.swcLoader.cjsPkg;
}
@ -17,15 +17,17 @@ export function buildSwcOptions(targets, config, isJsx, isTs, isEsm) {
syntax: isTs ? 'typescript' : 'ecmascript',
jsx: isJsx,
},
experimental: {
plugins: [['swc-plugin-vue-jsx', {}]],
},
experimental: isJsx
? {
plugins: [['swc-plugin-vue-jsx', {}]],
}
: undefined,
// preserveAllComments: true,
},
module: {
type: isEsm ? 'es6' : 'commonjs',
},
// minify: true,
minify,
},
config.swcLoader || {},
);