webpack - 如何使用 webpack 跟踪对 html 的更改?
问题描述
从 babel、css-loader 和 style-loader + extract-text-webpack-plugin 构建。另外是bootstrap和jquery。
jquery 上的更改与 gulp'e 上的浏览器同步一样被计算和更改,但 web 不会跟踪 html 中的更改,尽管在连接某个模块之前一切都很好。我可能在某个地方犯了错误还是我需要另一个模块?如果您需要更多文件,我将在剧透中附加 package.json 和 webpack.config.js。
包装
"name": "lesson_boots",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"dev": "webpack-dev-server --mode development --open",
"build": "webpack --mode production"
},
"author": "",
"license": "ISC",
"devDependencies": {
"babel-core": "^6.26.3",
"babel-loader": "^7.1.5",
"babel-preset-env": "^1.7.0",
"babel-preset-stage-3": "^6.24.1",
"css-loader": "^1.0.1",
"extract-text-webpack-plugin": "^4.0.0-beta.0",
"path": "^0.12.7",
"style-loader": "^0.23.1",
"webpack": "^4.26.1",
"webpack-cli": "^3.1.2",
"webpack-dev-server": "^3.1.10"
},
"dependencies": {
"bootstrap": "^4.1.3",
"jquery": "^3.3.1"
}
}
webpack.config.js
let path = require('path')
const ExtractTextPlugin = require("extract-text-webpack-plugin");
let conf = {
entry: './src/index.js',
output: {
path: path.resolve(__dirname, './dist'),
filename: 'main.js',
publicPath: 'dist/'
},
devServer: {
overlay: true
},
module: {
rules: [
{
test: /\.js$/,
loader: 'babel-loader',
},
{
test: /\.css$/,
use: ExtractTextPlugin.extract({
use: "css-loader"
})
}
]
},
plugins: [
new ExtractTextPlugin("styles.css"),
]
};
module.exports = (env, options) => {
let production = options.mode === 'production';
conf.devtool = production
? false
: 'eval-sourcemap';
return conf;
}
解决方案
对于开发环境,您可以简单地调整您的 webpack.config.js 规则,如下所述。
rules: [
{
test: /\.js$/,
loader: 'babel-loader',
},
{
test: /\.css$/,
use: [
'style-loader',
'css-loader'
]
}
]
您可以对生产环境进行以下更改。
请安装mini-css-extract-plugin并将其导入 webpack.config.js 文件中。
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
并在 webpack.config.js 文件的规则数组中更新如下所述的规则。
rules: [
{
test: /\.js$/,
loader: 'babel-loader',
},
{
test: /\.css$/,
use: [
MiniCssExtractPlugin.loader,
'css-loader'
]
}
]
希望这会奏效。
推荐阅读
- javascript - Joi 验证数组中的总和
- django - Django-cron 检查正在运行的进程
- python - 如何将我的数据集加载到 Pytorch 或 Keras?
- php - PHP odbc_exec 不返回所有行
- python - Python输入类型字段不显示
- python - 某些变量范围会导致不同的数组大小?
- send - SendGrid试用帐户邮件在Nodejs中发送引发错误
- audio - Julia 中的高通滤波器
- javascript - Vue.3 w/ vue.next.draggable 条件
- python - (python,matplotlib)我怎样才能在不是方程的图中做微分?