1. mkdir webpack-demo
    2. cd webpack-demo
    3. pnpm init
    4. pnpm install webpack webpack-cli -D

    默认使用production模式打包,默认开启treeshaking
    默认打包生成的文件名为main.js

    1. const path = require('path')
    2. module.exports ={
    3. // webpack打包的基础目录,不设置时默认就是根目录 '.'
    4. context: path.resolve(__dirname , './src'),
    5. // 此时打包入口为 './src/app.js'
    6. entry: './app.js'
    7. }
    8. // entry使用字符串与下面等价
    9. entry: {
    10. main: './app.js'
    11. }

    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.

    1. const path = require('path')
    2. module.exports = {
    3. context: path.resolve(__dirname, './src'),
    4. entry: ['./a.js', './b.js', './app.js']
    5. }

    上面数组形式仍然只会生成一个文件main.js,以app.js为入口,前两个文件为依赖,相当于

    1. // app.js
    2. import a from './a.js'
    3. import b from './b.js'
    4. // app的代码
    5. ...