我对 express + webpack 比较陌生,所以我不清楚这是不是有意的,如果不是,如何正确配置它。问题在于使用 mini-css-extract-plugin 时创建的附加资产和入口点。
网络包配置:
Extract = require('mini-css-extract-plugin');
path = require('path');
Write = require('write-file-webpack-plugin');
module.exports = {
mode: 'development',
entry: {
demo_scripts: path.resolve('server', 'scripts', 'demo.js'),
demo_styles: path.resolve('server', 'styles', 'demo.css')
},
output: {
path: path.resolve('.tmp'),
filename: '[name].js'
},
plugins: [new Write(), new Extract()],
module: {
rules: [
{
test: /\.js$/,
use: [
{
loader: 'babel-loader',
options: {
presets: ['babel-preset-env']
}
}
]
},
{
test: /\.css/,
use: [
{
loader: Extract.loader
},
{
loader: 'css-loader'
}
]
}
]
}
};
webpack 输出
Asset Size Chunks Chunk Names
demo_scripts.js 3.91 KiB demo_scripts [emitted] demo_scripts
demo_styles.css 36 bytes demo_styles [emitted] demo_styles
demo_styles.js 3.89 KiB demo_styles [emitted] demo_styles
Entrypoint demo_scripts = demo_scripts.js
Entrypoint demo_styles = demo_styles.css demo_styles.js
我的问题是,为什么demo_styles.js
要创建?尽管正在提取 css,但似乎 webpack 仍在使用 css 创建捆绑的 js,但是当我查看该文件时,其中唯一的一行是
eval("// extracted by mini-css-extract-plugin\n\n//# sourceURL=webpack:///./server/styles/demo.css?");
谁能帮忙解释这里发生了什么?
更新
如果我删除 demo_styles 入口点,并通过插件初始化对其进行配置,则不会构建任何 CSS 资产。
({
plugins: [
new Write(),
new Extract({
filename: 'demo_styles.css'
})
]
});
Asset Size Chunks Chunk Names
demo_scripts.js 3.91 KiB demo_scripts [emitted] demo_scripts
Entrypoint demo_scripts = demo_scripts.js
回购协议在这里(注意快递分支)https://github.com/brewster1134/bumper/tree/express