Webpack 默认配置文件 webpack.config.js
,不创建会使用默认的。
一、重置webpack.config.js
在根目录下创建 webpack.config.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| const path = require('path');
module.exports = { mode: 'production', entry: './index.js', output: { filename: 'bundle.js',
path: path.resolve(__dirname, 'bundle') } }
|
执行 npx webpack
即可。打包后的文件目录:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| |-- bundle |-- bundle.js |-- index.html |-- index.js |-- node_modules |-- package.json |-- webpack.config.js |-- js |-- header.js |-- content.js |-- footer.js |-- img |-- abc.jpg |-- abc.png
|
二、简化打包文件代码
修改 package.json
文件下的 scripts
。即:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| { "name": "webpack", "version": "1.0.0", "description": "webpack", "main": "index.js", "scripts": { "bundle": "webpack" }, "author": "", "license": "ISC", "dependencies": { "webpack": "^4.39.1", "webpack-cli": "^3.3.6" } }
|
修改后执行 npm run bundle
即可打包, 打包后记得修改 index.html
里面链接 js
的文件。
打包文件出现如下提示,则可以在 webpack.config.js
文件下添加 mode: 'production'
或者 mode: 'development'
: