@@ -4,13 +4,8 @@ module.exports = function unifiedExtend(api, opts) {
44
55 api . assertVersion ( '>=0.3.0' ) ;
66
7- const { tryRequire } = require ( '@micro-app/shared-utils' ) ;
87 const { getAssetPath, isWebpack4 } = require ( './utils' ) ;
98
10- const CHUNK_NAME = 'runtime' ;
11- const CHUNK_VENDORS_NAME = 'chunk-vendors' ;
12- const CHUNK_COMMON_NAME = 'chunk-common' ;
13-
149 // 通用基础配置
1510 function baseConfig ( webpackChain ) {
1611 const options = api . config || { } ;
@@ -41,44 +36,12 @@ module.exports = function unifiedExtend(api, opts) {
4136 return webpackChain ;
4237 }
4338
44- function chunkConfig ( webpackChain ) {
45-
46- webpackChain . optimization . runtimeChunk ( { name : CHUNK_NAME } ) ;
47- webpackChain . optimization . usedExports ( true ) ;
48-
49- // code splitting
50- webpackChain
51- . optimization . splitChunks ( {
52- cacheGroups : {
53- vendors : {
54- name : CHUNK_VENDORS_NAME ,
55- test : / [ \\ / ] n o d e _ m o d u l e s [ \\ / ] / ,
56- priority : - 10 ,
57- chunks : 'initial' ,
58- } ,
59- common : {
60- name : CHUNK_COMMON_NAME ,
61- minChunks : 2 ,
62- priority : - 20 ,
63- chunks : 'initial' ,
64- reuseExistingChunk : true ,
65- } ,
66- } ,
67- } ) ;
68- }
69-
7039 api . modifyChainWebpackConfig ( webpackChain => {
7140
7241 webpackChain = baseConfig ( webpackChain ) ;
7342
7443 const options = api . config || { } ;
7544
76- const entry = options . entry || { } ;
77- // entry
78- Object . keys ( entry ) . forEach ( key => {
79- webpackChain . entry ( key ) . merge ( entry [ key ] ) ;
80- } ) ;
81-
8245 // reset, 兼容
8346 options . outputDir = webpackChain . output . get ( 'path' ) || options . outputDir || 'dist' ;
8447 options . publicPath = webpackChain . output . get ( 'publicPath' ) || options . publicPath || '/' ;
@@ -111,84 +74,10 @@ module.exports = function unifiedExtend(api, opts) {
11174 . merge ( alias )
11275 . end ( ) ;
11376
114-
115- // chunk
116- // code splitting
117- if ( process . env . NODE_ENV !== 'test' ) {
118- chunkConfig ( webpackChain ) ;
119- }
120-
121- const isProd = api . mode === 'production' ;
122-
123- // load html
124- const multiPageConfig = options . pages ;
125- const pages = Object . keys ( multiPageConfig ) ;
126- if ( pages . length > 0 ) {
127- const HTMLPlugin = tryRequire ( 'html-webpack-plugin' ) ;
128- if ( HTMLPlugin ) {
129- pages . forEach ( ( name , index ) => {
130- const htmlOpts = multiPageConfig [ name ] ;
131- const pname = index ? `html-${ name } -${ index } ` : `html-${ name } ` ;
132-
133- if ( ! htmlOpts . chunks ) {
134- htmlOpts . chunks = [ name ] ;
135- }
136-
137- if ( htmlOpts . chunks && Array . isArray ( htmlOpts . chunks ) ) {
138- [ CHUNK_COMMON_NAME , CHUNK_VENDORS_NAME , CHUNK_NAME ] . forEach ( key => {
139- if ( ! htmlOpts . chunks . includes ( key ) ) {
140- htmlOpts . chunks . unshift ( key ) ;
141- }
142- } ) ;
143- }
144-
145- if ( isProd ) { // 暂时不定义,外部自行配置
146- Object . assign ( htmlOpts , {
147- minify : {
148- removeComments : true ,
149- collapseWhitespace : true ,
150- removeAttributeQuotes : true ,
151- collapseBooleanAttributes : true ,
152- removeScriptTypeAttributes : true ,
153- // more options:
154- // https://github.com/kangax/html-minifier#options-quick-reference
155- } ,
156- } ) ;
157- }
158-
159- webpackChain
160- . plugin ( pname )
161- . use ( HTMLPlugin , [ htmlOpts ] ) ;
162- } ) ;
163- } else {
164- api . logger . warn ( '[webpack]' , 'Not Found "html-webpack-plugin"' ) ;
165- }
166- }
167-
168- // node
169- if ( webpackChain . get ( 'target' ) === 'web' ) {
170- webpackChain . node
171- . merge ( {
172- // prevent webpack from injecting useless setImmediate polyfill because Vue
173- // source contains it (although only uses it if it's native).
174- setImmediate : false ,
175- // process is injected via DefinePlugin, although some 3rd party
176- // libraries may require a mock to work properly (#934)
177- process : 'mock' ,
178- // prevent webpack from injecting mocks to Node native modules
179- // that does not make sense for the client
180- dgram : 'empty' ,
181- fs : 'empty' ,
182- net : 'empty' ,
183- tls : 'empty' ,
184- child_process : 'empty' ,
185- } ) ;
186- }
187-
18877 return webpackChain ;
18978 } ) ;
19079} ;
19180
19281module . exports . configuration = {
193- description : 'webpack 配置与基础配置参数统一 ' ,
82+ description : 'webpack 通用基础配置 ' ,
19483} ;
0 commit comments