From 275e8268160172fc0389c92e1a855fddd4105131 Mon Sep 17 00:00:00 2001 From: sunhao Date: Tue, 22 Oct 2019 15:51:01 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81=20=E8=A6=86=E7=9B=96?= =?UTF-8?q?=20client=E7=AB=AF=20js=20=E4=B8=8E=20ts=20loader?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/config/webpack.base.config.ts | 10 ------ src/config/webpack.client.config.ts | 50 ++++++++++++++++++----------- src/config/webpack.server.config.ts | 30 +++++++++++------ 3 files changed, 53 insertions(+), 37 deletions(-) diff --git a/src/config/webpack.base.config.ts b/src/config/webpack.base.config.ts index 574b854..33ffc09 100644 --- a/src/config/webpack.base.config.ts +++ b/src/config/webpack.base.config.ts @@ -67,16 +67,6 @@ export function getCommonBaseConfig(options: ConfigOptions.options) { preserveWhitespace: false } } - }, - { - test: /\.js$/, - loader: 'happypack/loader?id=babel', - exclude: /node_modules/ - }, - { - test: /\.tsx?$/, - loader: 'happypack/loader?id=ts', - exclude: /node_modules/ } ] }, diff --git a/src/config/webpack.client.config.ts b/src/config/webpack.client.config.ts index b6ea6e6..22ee656 100644 --- a/src/config/webpack.client.config.ts +++ b/src/config/webpack.client.config.ts @@ -12,6 +12,29 @@ function baseGetClientConfig(options: ConfigOptions.options) { const { externals, alias } = getExternals(options, 'client') const mode = options.webpack.mode || 'production' const isProd = mode === 'production' + const client = options.webpack ? options.webpack.client || {} : {} + const rules = [] + if (client.module && client.module.rules) { + const babelJS = client.module.rules.find(rule => rule.test === /\.js$/) + if (!babelJS) { + rules.push({ + test: /\.js$/, + loader: 'happypack/loader?id=babel', + exclude: /node_modules/ + }) + } + const babelTS = client.module.rules.find(rule => rule.test === /\.tsx?$/) + if (!babelTS) { + rules.push({ + test: /\.tsx?$/, + use: [ + 'happypack/loader?id=babel', + 'happypack/loader?id=ts' + ], + exclude: /node_modules/ + }) + } + } return (merge as any)( getBaseConfig(options), { @@ -19,8 +42,11 @@ function baseGetClientConfig(options: ConfigOptions.options) { entry: { app: './src/entry-client.js' }, + module: { + rules + }, resolve: { - alias, + alias }, externals, output: { @@ -28,9 +54,7 @@ function baseGetClientConfig(options: ConfigOptions.options) { }, optimization: { runtimeChunk: !isProd, - minimizer: [ - new OptimizeCSSAssetsPlugin() - ], + minimizer: [new OptimizeCSSAssetsPlugin()], splitChunks: { cacheGroups: { styles: { @@ -49,28 +73,18 @@ function baseGetClientConfig(options: ConfigOptions.options) { }) ] }, - getStyle(options, { isServer: false }), + getStyle(options, { isServer: false }) ) } export async function getClientConfig(options: ConfigOptions.options) { const client = options.webpack ? options.webpack.client || {} : {} - return (merge as any)( - baseGetClientConfig(options), - await getClientDllPlugin(options), - client, - ) + return (merge as any)(baseGetClientConfig(options), await getClientDllPlugin(options), client) } - export function getClientConfigSync(options: ConfigOptions.options) { const client = options.webpack ? options.webpack.client || {} : {} - return (merge as any)( - baseGetClientConfig(options), - getClientDllPluginSync(options), - client, - ) - -} \ No newline at end of file + return (merge as any)(baseGetClientConfig(options), getClientDllPluginSync(options), client) +} diff --git a/src/config/webpack.server.config.ts b/src/config/webpack.server.config.ts index e604482..a24ba13 100644 --- a/src/config/webpack.server.config.ts +++ b/src/config/webpack.server.config.ts @@ -11,9 +11,7 @@ import { getExternals } from 'src/utils/plugins.webpack' export function getServerConfig(options: ConfigOptions.options) { if (!(options.webpack && options.webpack.mode)) { - consola.fatal( - 'getBaseConfig options.babelrc or options.webpack is undefined' - ) + consola.fatal('getBaseConfig options.babelrc or options.webpack is undefined') return process.exit(1) } if (options.webpack.server) { @@ -21,24 +19,40 @@ export function getServerConfig(options: ConfigOptions.options) { const mode = options.webpack.mode || 'production' const isProd = mode === 'production' const { externals, alias } = getExternals(options, 'server') - const whitelist = [/\.css$/, /\?vue&type=style/].concat(options.webpack.server.nodeExternalsWhitelist || []).concat(externals as any) + const whitelist = [/\.css$/, /\?vue&type=style/] + .concat(options.webpack.server.nodeExternalsWhitelist || []) + .concat(externals as any) return (merge as any)( getBaseConfig(options), { name: 'server', target: 'node', entry: './src/entry-server.js', + module: { + rules: [ + { + test: /\.js$/, + loader: 'happypack/loader?id=babel', + exclude: /node_modules/ + }, + { + test: /\.tsx?$/, + loader: 'happypack/loader?id=ts', + exclude: /node_modules/ + } + ] + }, output: { libraryTarget: 'commonjs2' }, resolve: { - alias, + alias }, externals: [ nodeExternals({ // whitelist: /\.css$/ whitelist - }), + }) ], performance: { maxEntrypointSize: 1024 * 1024 * 6, @@ -56,9 +70,7 @@ export function getServerConfig(options: ConfigOptions.options) { server ) } else { - consola.fatal( - '[getServerConfig] options.webpack.server is undefined' - ) + consola.fatal('[getServerConfig] options.webpack.server is undefined') return process.exit(1) } }