经过长时间阅读此问题后,我能够做到这一点。部分技巧是使用optimization
属性并按块名称过滤。
我的 webpack.config.js 现在看起来像这样:
module.exports = {
entry: {
popup: ['./src/js/popup.js', './src/css/popup.scss'],
timer_player: [
'./src/js/content_scripts/timer_player/index.js',
'./src/css/timer_player.scss',
],
},
output: {
publicPath: '.',
path: resolve(__dirname, 'dist/'),
filename: '[name].js',
libraryTarget: 'umd',
},
plugins: [
new WebpackReloaderPlugin({
port: 9090, // Which port use to create the server
reloadPage: true, // Force the reload of the page also
entries: {
// The entries used for the content/background scripts
contentScript: 'popup', // Use the entry names, not the file name or the path
background: 'background', // *REQUIRED
},
}),
new MiniCssExtractPlugin({ filename: '[name].css' }),
new HtmlWebpackPlugin({
template: path.join(__dirname, 'src', 'popup.html'),
filename: 'popup.html',
chunks: ['popup'],
}),
new CopyWebpackPlugin([
{ from: './src/manifest.json' },
{ from: './src/images', to: 'images' },
]),
new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV),
}),
],
module: {
rules: [
{
test: /\.(js|jsx)$/,
loader: 'babel-loader',
exclude: /node_modules/,
},
{
test: /\.scss$/,
use: [
MiniCssExtractPlugin.loader,
'css-loader', // translates CSS into CommonJS
{
loader: 'sass-loader',
options: {
includePaths: ['node_modules', 'node_modules/@material/*']
.map(d => path.join(__dirname, d))
.map(g => glob.sync(g))
.reduce((a, c) => a.concat(c), []),
},
},
],
},
{
test: /\.txt$/,
use: 'raw-loader',
},
],
},
optimization: {
splitChunks: {
cacheGroups: {
extractPopupStyles: {
name: 'style',
chunks: chunk => chunk.name === 'popup',
},
},
},
},
};
欢迎任何建议