欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

10、webpack开发--自动编译源代码

程序员文章站 2024-03-14 20:24:47
...

如果我们更改了源代码,而不像每次都要运行npm run build,该如何办呢?

1、更改package.json文件,scripts追加"watch": "webpack --watch"开启webpack watch模式

{
  "name": "webpack-demo",
  "version": "1.0.0",
  "description": "",
  "private": true,
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "webpack",
    "watch": "webpack --watch"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "clean-webpack-plugin": "^2.0.2",
    "css-loader": "^2.1.1",
    "csv-loader": "^3.0.2",
    "file-loader": "^3.0.1",
    "html-webpack-plugin": "^3.2.0",
    "style-loader": "^0.23.1",
    "webpack": "^4.32.0",
    "webpack-cli": "^3.3.2",
    "xml-loader": "^1.2.1"
  },
  "dependencies": {
    "lodash": "^4.17.11"
  }
}

2、watch模式运行

npm run watch

3、运行效果

10、webpack开发--自动编译源代码

4、修改print.js文件

export default function print() {
    // console.error('code find a error!'); 修改前
    console.log('code was updated!');
}

5、刷新页面,运行效果如下:

10、webpack开发--自动编译源代码