webpack.config.js
const webpack = require('webpack');
const path = require('path');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const fs = require('fs');
module.exports = {
mode: 'production',
context: path.resolve(__dirname, 'app'),
devServer: {
host: "www.mydomain.local",
compress: true,
https: {
key: fs.readFileSync('https.key'),
cert: fs.readFileSync('https.crt')
}
},
optimization: {
minimizer: [new UglifyJsPlugin({})],
},
entry: {
blog: path.resolve(__dirname, 'src', 'blog.js')
},
output: {
path: path.resolve(__dirname, 'web', '_assets'),
publicPath: path.resolve(__dirname, 'web', '_assets'),
filename: "js/[name].[hash].bundle.js"
},
plugins: [
new MiniCssExtractPlugin({
filename: "css/[name].[hash].bundle.css",
})
],
module: {
rules: [
{
test: /\.scss$/,
use: [
MiniCssExtractPlugin.loader,
"css-loader",
"sass-loader"
]
}
]
}
};
如您所见,我在 webpack 配置“博客”中只有一个条目,该条目解析为./src/blog.js
具有以下内容:
import('./css/blog.scss')
console.log(1)
当我运行webpack --config webpack.config.js
它时,它会按预期构建,./web/_assets/js/blog.21342i1j4h1kj.bundle.js
其中包含console.log(1)
。
然而奇怪的是,它还构建./web/_assets/js/1.21342i1j4h1kj.bundle.js
和./web/_assets/css/1.21342i1j4h1kj.bundle.js
额外的 JS 文件包含
(window.webpackJsonp=window.webpackJsonp||[]).push([[1],[,function(n,w,o){}]]);
并且 1.css 文件包含 sass 到 css 编译的 css。
我的问题是:
- 为什么会存在多余的 JS 文件?
- 为什么不调用css文件
blog.21342i1j4h1kj.bundle.css
? - 如何更改配置以解决此问题?