How to set multiple file entry and output in project with webpack?

后端 未结 8 1488
天命终不由人
天命终不由人 2020-12-12 12:14

How to set multiple file entry/output in project with webpack?

I follow http://webpack.github.io/docs/tutorials/getting-started/ success compile if only one file in

8条回答
  •  既然无缘
    2020-12-12 12:49

    this webpack plugin web-webpack-plugin can resolve it in a sample way.

    AutoWebPlugin can find all page entry in an directory,then auto config an WebPlugin for every page to output an html file,you can use it as below:

    webpack config

    module.exports = {
        plugins: [
            new AutoWebPlugin(
                // the directory hold all pages
                './src/', 
                {
                // the template file path used by all pages
                template: './src/template.html',
                // javascript main file for current page,if it is null will use index.js in current page directory as main file
                entity: null,
                // extract common chunk for all pages and then put it into a file named common,if it is null then not do extract action
                // achieve by CommonsChunkPlugin
                commonsChunk: 'common',
                // pre append to all page's entry
                preEntrys:['./path/to/file1.js'],
                // post append to all page's entry
                postEntrys:['./path/to/file2.js'],
            }),
        ]
    };
    

    src directory

    ── src
    │   ├── home
    │   │   └── index.js
    │   ├── ie_polyfill.js
    │   ├── login
    │   │   └── index.js
    │   ├── polyfill.js
    │   ├── signup
    │   │   └── index.js
    │   └── template.html
    

    output directory

    ├── dist
    │   ├── common.js
    │   ├── home.html
    │   ├── home.js
    │   ├── ie_polyfill.js
    │   ├── login.html
    │   ├── login.js
    │   ├── polyfill.js
    │   ├── signup.html
    │   └── signup.js
    

    AutoWebPlugin find all page home login signup directory in ./src/,for this three page home login signup will use index.js as main file and output three html file home.html login.html signup.html`

    see doc:auto detect html entry

提交回复
热议问题