Skip to content

Commit 55e47c8

Browse files
author
chengyu.chengyulia
committed
fix: chunk css extract
1 parent bd27af4 commit 55e47c8

1 file changed

Lines changed: 53 additions & 18 deletions

File tree

src/utils/ts-plus-babel.ts

Lines changed: 53 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@ import * as gulpIf from 'gulp-if';
1010
import * as gulpSourcemaps from 'gulp-sourcemaps';
1111
import * as mergeStream from 'merge-stream';
1212
import * as path from 'path';
13+
import * as _ from 'lodash';
14+
import * as webpack from 'webpack';
1315
import { pri, srcPath } from '../node';
1416
import { plugin } from './plugins';
1517
import { getBabelOptions } from './babel-options';
1618
import { globalState } from './global-state';
1719
import { babelPluginTransformImport } from './babel-plugin-transfer-import';
1820
import { PackageInfo } from './define';
19-
import { runWebpack } from './webpack';
21+
import { runWebpack, bundleDlls } from './webpack';
2022

2123
function getGulpByWatch(watch: boolean, filesPath: string) {
2224
if (watch) {
@@ -95,24 +97,57 @@ const buildSassAndLess = (watch: boolean, outdir: string, wholeProject: boolean,
9597
};
9698

9799
const 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: /\.css|scss|less$/,
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

118153
const mvResources = (

0 commit comments

Comments
 (0)