@@ -10,13 +10,15 @@ import * as gulpIf from 'gulp-if';
1010import * as gulpSourcemaps from 'gulp-sourcemaps' ;
1111import * as mergeStream from 'merge-stream' ;
1212import * as path from 'path' ;
13+ import * as _ from 'lodash' ;
14+ import * as webpack from 'webpack' ;
1315import { pri , srcPath } from '../node' ;
1416import { plugin } from './plugins' ;
1517import { getBabelOptions } from './babel-options' ;
1618import { globalState } from './global-state' ;
1719import { babelPluginTransformImport } from './babel-plugin-transfer-import' ;
1820import { PackageInfo } from './define' ;
19- import { runWebpack } from './webpack' ;
21+ import { runWebpack , bundleDlls } from './webpack' ;
2022
2123function getGulpByWatch ( watch : boolean , filesPath : string ) {
2224 if ( watch ) {
@@ -95,24 +97,57 @@ const buildSassAndLess = (watch: boolean, outdir: string, wholeProject: boolean,
9597} ;
9698
9799const buildCssWithWebpack = ( outDir : string , copyDir : string ) => {
98- const cssFileNames = Object . keys ( pri . sourceConfig . componentEntries ) . map ( file => `${ file } .css` ) ;
99-
100- return runWebpack ( {
101- mode : 'production' ,
102- entryPath : pri . sourceConfig . componentEntries ,
103- distDir : outDir ,
104- outFileName : '[name].js' ,
105- outCssFileName : '[name].css' ,
106- } ) . then ( ( ) => {
107- const distFiles = fs . readdirSync ( outDir ) ;
108- distFiles . forEach ( file => {
109- const basename = path . basename ( file ) ;
110- if ( ! cssFileNames . includes ( basename ) ) {
111- fs . removeSync ( path . join ( outDir , file ) ) ;
112- }
100+ const entryNames = Object . keys ( pri . sourceConfig . componentEntries ) ;
101+
102+ const dllOutPath = path . join ( globalState . projectRootPath , '.temp/static/dlls' ) ;
103+ const dllFileName = 'main.dll.js' ;
104+ const dllMainfestName = 'manifest.json' ;
105+
106+ return bundleDlls ( { dllOutPath, dllFileName, dllMainfestName } )
107+ . then ( ( ) => {
108+ return entryNames . reduce ( ( promise , name ) => {
109+ return promise . then ( ( ) =>
110+ runWebpack ( {
111+ mode : 'production' ,
112+ entryPath : { [ name ] : pri . sourceConfig . componentEntries [ name ] } ,
113+ distDir : outDir ,
114+ outFileName : '[name].js' ,
115+ outCssFileName : '[name].css' ,
116+ pipeConfig : async config => {
117+ _ . set ( config , [ 'optimization' , 'splitChunks' , 'cacheGroups' , `styles` ] , {
118+ name : `${ name } -styles` ,
119+ test : / \. c s s | s c s s | l e s s $ / ,
120+ chunks : 'all' ,
121+ enforce : true ,
122+ } ) ;
123+
124+ config . plugins . push (
125+ new webpack . DllReferencePlugin ( {
126+ context : '.' ,
127+ // eslint-disable-next-line import/no-dynamic-require,global-require
128+ manifest : require ( path . join ( dllOutPath , dllMainfestName ) ) ,
129+ } ) ,
130+ ) ;
131+
132+ return config ;
133+ } ,
134+ } ) ,
135+ ) ;
136+ } , Promise . resolve ( ) ) ;
137+ } )
138+ . then ( ( ) => {
139+ const distFiles = fs . readdirSync ( outDir ) ;
140+ distFiles . forEach ( file => {
141+ const fileEntryName = entryNames . find ( name => `${ name } -styles.css` === path . basename ( file ) ) ;
142+ if ( fileEntryName ) {
143+ fs . renameSync ( path . join ( outDir , file ) , path . join ( outDir , `${ fileEntryName } .css` ) ) ;
144+ } else {
145+ fs . removeSync ( path . join ( outDir , file ) ) ;
146+ }
147+ } ) ;
148+
149+ fs . copySync ( outDir , copyDir ) ;
113150 } ) ;
114- fs . copySync ( outDir , copyDir ) ;
115- } ) ;
116151} ;
117152
118153const mvResources = (
0 commit comments