mkdir webpack-demo
cd webpack-demo
pnpm init
pnpm install webpack webpack-cli -D
默认使用production模式打包,默认开启treeshaking
默认打包生成的文件名为main.js
const path = require('path')
module.exports ={
// webpack打包的基础目录,不设置时默认就是根目录 '.'
context: path.resolve(__dirname , './src'),
// 此时打包入口为 './src/app.js'
entry: './app.js'
}
// entry使用字符串与下面等价
entry: {
main: './app.js'
}
entry使用数组形式
文件名必须带有路径,'a.js'
会被当作npm包去node_modules里解析
Requests that should resolve in the current directory need to start with ‘./‘.
Requests that start with a name are treated as module requests and resolve within module directories (node_modules).
If changing the source code is not an option there is also a resolve options called ‘preferRelative’ which tries to resolve these kind of requests in the current directory too.
const path = require('path')
module.exports = {
context: path.resolve(__dirname, './src'),
entry: ['./a.js', './b.js', './app.js']
}
上面数组形式仍然只会生成一个文件main.js,以app.js为入口,前两个文件为依赖,相当于
// app.js
import a from './a.js'
import b from './b.js'
// app的代码
...