上面@Jonik 提到的在运行时由服务器(Node.js)生成 html 的可能解决方案。
对于开发:
const webpack = require('webpack');
const webpackDevMiddleware = require('webpack-dev-middleware');
const webpackHotMiddleware = require('webpack-hot-middleware');
const webpackConfig = require('../../internals/webpack/webpack.dev.babel');
const compiler = webpack(webpackConfig);
const middleware = webpackDevMiddleware(compiler, {
noInfo: true,
publicPath: webpackConfig.output.publicPath,
silent: true,
stats: 'errors-only',
});
const fileSystem = middleware.fileSystem;
const encoding = 'utf-8';
const outputPath = compiler.outputPath;
对于生产:
const fs = require('fs');
const path= require('path');
const fileSystem = fs;
const encoding = { encoding: 'utf-8' };
const outputPath = path.resolve(process.cwd(), 'build');
进而:
let style = '';
fileSystem.readdirSync(outputPath).forEach((file) => {
if (file.indexOf('.css') !== -1) {
style += fileSystem.readFileSync(`${outputPath}/${file}`, encoding);
}
});
'style' 变量将包含由 ExtractTextPlugin 捆绑的 CSS。