From 7019a1f7ac1c5bfb3782aead5fbae77338468f9b Mon Sep 17 00:00:00 2001 From: sunhao Date: Fri, 25 Oct 2019 12:23:38 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=AE=8C=E5=96=84=20customBuild=20?= =?UTF-8?q?=E7=AD=89=20rules?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/config/webpack.custom-build.config.ts | 30 ++++++++++++++++++----- src/config/webpack.dll.config.ts | 20 ++++++++++++--- src/config/webpack.extensions.config.ts | 9 ++----- 3 files changed, 42 insertions(+), 17 deletions(-) diff --git a/src/config/webpack.custom-build.config.ts b/src/config/webpack.custom-build.config.ts index 2f42a26..8163c9e 100644 --- a/src/config/webpack.custom-build.config.ts +++ b/src/config/webpack.custom-build.config.ts @@ -6,9 +6,7 @@ import { ConfigOptions } from '@types' export function getCustomConfig(options: ConfigOptions.options, target: string) { if (!(options.webpack && options.webpack.mode)) { - consola.fatal( - '[getCustomConfig] options.webpack or options.webpack.mode is undefined' - ) + consola.fatal('[getCustomConfig] options.webpack or options.webpack.mode is undefined') return process.exit(1) } @@ -21,6 +19,25 @@ export function getCustomConfig(options: ConfigOptions.options, target: string) if (options.customBuild[target]) { const customBuild = options.customBuild[target] || {} + const rules = [] + if (customBuild.module && customBuild.module.rules) { + const babelJS = customBuild.module.rules.find(rule => rule.test === /\.js$/) + if (!babelJS) { + rules.push({ + test: /\.js$/, + loader: 'happypack/loader?id=babel', + exclude: /node_modules/ + }) + } + const babelTS = customBuild.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/ + }) + } + } const mode = options.webpack.mode || 'production' const base: any = options.webpack ? options.webpack.base || {} : {} return (merge as any)( @@ -28,6 +45,9 @@ export function getCustomConfig(options: ConfigOptions.options, target: string) { name: target, mode, + module: { + rules + }, devtool: false, output: base.output, resolve: base.resolve @@ -35,9 +55,7 @@ export function getCustomConfig(options: ConfigOptions.options, target: string) customBuild ) } else { - consola.fatal( - `[getServerConfig] options.webpack[${target}] is undefined` - ) + consola.fatal(`[getServerConfig] options.webpack[${target}] is undefined`) return process.exit(1) } } diff --git a/src/config/webpack.dll.config.ts b/src/config/webpack.dll.config.ts index 369808c..dcab964 100644 --- a/src/config/webpack.dll.config.ts +++ b/src/config/webpack.dll.config.ts @@ -9,9 +9,7 @@ import webpack from 'webpack' import { getCommonConfig } from './webpack.common.config' import VueSSRClientPlugin from 'vue-server-renderer/client-plugin' -export function getDllConfig( - options: ConfigOptions.options -): webpack.Configuration { +export function getDllConfig(options: ConfigOptions.options): webpack.Configuration { if (!(options.webpack && options.webpack.dll)) { consola.fatal('getDllConfig options.webpack.dll is undefined') return process.exit(1) @@ -27,6 +25,20 @@ export function getDllConfig( name: 'dll', mode, entry: dll.entry, + module: { + rules: [ + { + test: /\.js$/, + loader: 'happypack/loader?id=babel', + exclude: /node_modules/ + }, + { + test: /\.tsx?$/, + use: ['happypack/loader?id=babel', 'happypack/loader?id=ts'], + exclude: /node_modules/ + } + ] + }, output: { path: dll.path, publicPath: dll.publicPath, @@ -51,7 +63,7 @@ export function getDllConfig( plugins: [ new VueSSRClientPlugin({ filename: 'vue-ssr-dll-manifest.json' - }), + }) ] }, getDllPlugin(dll), diff --git a/src/config/webpack.extensions.config.ts b/src/config/webpack.extensions.config.ts index 1700a38..a29b1ef 100644 --- a/src/config/webpack.extensions.config.ts +++ b/src/config/webpack.extensions.config.ts @@ -6,9 +6,7 @@ import consola from 'consola' import { babelLoder } from './webpack.config.config' import { getCommonConfig } from './webpack.common.config' -export function getExtensionsConfig( - options: ConfigOptions.options -): webpack.Configuration { +export function getExtensionsConfig(options: ConfigOptions.options): webpack.Configuration { if ( !( options.extensions && @@ -19,9 +17,7 @@ export function getExtensionsConfig( options.webpack.mode ) ) { - consola.fatal( - 'getExtensionsConfig options.extensions.entry or options.babelrc is undefined' - ) + consola.fatal('getExtensionsConfig options.extensions.entry or options.babelrc is undefined') return process.exit(1) } @@ -60,7 +56,6 @@ export function getExtensionsConfig( exclude: /node_modules/, use: ['ts-loader'] }, - { test: /\.js$/, use: babelLoder,