JavaScript How can I use multiple entries in Webpack alongside multiple HTML files in HtmlWebpackPlugin? multiple entries webpack,multiple entry webpack 4,multi entry webpack,

We'd like to have two outputs from Webpack - our entire app with all of its dependencies, and a single different page with only one dependency (that isn't shared by the main app).

It seems the way to do this is to leverage the entry property of a Webpack config. However, that's not enough, as we also use HtmlWebpackPlugin to output our HTML file with the build.js that Webpack compiled dynamically added (as well as compiled LESS, etc). According to the HtmlWebpackPlugin docs:

If you have multiple Webpack entry points, they will all be included with script tags in the generated HTML.

That won't work for us, so I need to leverage their filterChunks option. This GitHub issue response states it most succinctly:

module.exports = {
  entry: {
    'page1': './apps/page1/scripts/main.js',
    'page2': './apps/page2/src/main.js'
  },
  output: {
    path: __dirname,
    filename: "apps/[name]/build/bundle.js"
  },
  plugins: [
    new HtmlWebpackPlugin({
      inject: false,
      chunks: ['page1'],
      filename: 'apps/page1/build/index.html'
    }),
    new HtmlWebpackPlugin({
      inject: false,
      chunks: ['page2'],
      filename: 'apps/page2/build/index.html'
    })
  ]
};

(in the HtmlWebpackPlugin docs, this is under the "filtering chunks" section)

So, I modified our code like so:

module.exports = {
    entry: {
        app: './public/js/ide.js',
        resetPassword: './public/js/reset_password.js'
    },
    output: {
        path: path.resolve(__dirname, '../build'),
        filename: '[name].js',
        publicPath: '/'
    },
    ...
    plugins: [
        new HtmlWebpackPlugin({
            filename: 'index.html',
            template: 'public/html/ide.html',
            inject: true,
            chunks: ['app']
        }),
        new HtmlWebpackPlugin({
            filename: 'reset_password.html',
            template: 'public/html/reset_password.html',
            inject: true,
            chunks: ['resetPassword']
        }),
    ],
}

Now, when I rebuild the project (just trying with WebpackDevServer for now) and navigate to /index.html, I can see in the network tab the massive bundle file, the contents of index.html (based off the ide.html template), as well as requests for various external resources. However, no actual JavaScript will run (say, a console.log in ide.js). All the HTML in the file shows.

For reset_password.html, all HTML shows, and the reset_password.js file shows, but none of the javascript within runs.

How can I ensure the JavaScript in my entry files runs?

EDIT: I have gotten ide.js working, because I hadn't realized the following was a "chunk":

optimization: {
        splitChunks: {
            cacheGroups: {
                commons: {
                    test: /[\\/]node_modules[\\/]/,
                    name: 'vendor',
                    chunks: 'all'
                }
            }
        }
    },

So, I added vendor to the index.html HtmlWebpackPlugin chunks property. Now, it looks like this:

new HtmlWebpackPlugin({
    filename: 'index.html',
    template: 'public/html/ide.html',
    inject: true,
    chunks: ['app', 'vendor']
}),

reset_password doesn't need anything in the node_modules folder, and this also doesn't explain why no JavaScript at all would run inside of ide.js, so I'm still quite confused. Also, reset_password is still non-functional.

EDIT2: Looking through the apparently attached reset_password.js file when I load reset_password.html, I can see this line

eval("\n\nconsole.log('DRAGONHELLO');//# sourceURL=[module]\n//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbIndlYnBhY2s6Ly8vLi9wdWJsaWMvanMvcmVzZXRfcGFzc3dvcmQuanM/ZjY5ZSJdLCJuYW1lcyI6WyJjb25zb2xlIiwibG9nIl0sIm1hcHBpbmdzIjoiOztBQUNBQSxRQUFRQyxHQUFSLENBQVksYUFBWiIsImZpbGUiOiIuL3B1YmxpYy9qcy9yZXNldF9wYXNzd29yZC5qcy5qcyIsInNvdXJjZXNDb250ZW50IjpbIlxuY29uc29sZS5sb2coJ0RSQUdPTkhFTExPJylcbiJdLCJzb3VyY2VSb290IjoiIn0=\n//# sourceURL=webpack-internal:///./public/js/reset_password.js\n");

So, clearly my console.log('DRAGONHELLO') is "seen", but I have no idea why it isn't running.

EDIT3: Adding vendor to chunks for reset_password.html causes the JavaScript to run, but I have no idea why, and this is non-ideal because the whole point of the exercise was to have two different bundles, one which was very minimal and didn't need all of our node_modules.

EDIT4: I ran Webpack with profile:true, and I can see I'm not getting the "Chunk Names" wrong:

                 js/app.3d18b43294ebd54ed083.js   1.34 MiB       0  [emitted]  [big]  app
       js/resetPassword.198485be2b163cc258ed.js   1.02 KiB       1  [emitted]         resetPassword
                   js/2.e7f92193ea3c611a0b36.js   2.23 MiB       2  [emitted]  [big]  vendor
             js/app.3d18b43294ebd54ed083.js.map   2.71 MiB       0  [emitted]         app
   js/resetPassword.198485be2b163cc258ed.js.map   4.57 KiB       1  [emitted]         resetPassword
               js/2.e7f92193ea3c611a0b36.js.map   7.12 MiB       2  [emitted]         vendor

EDIT5: I tried both

module.exports = {
  //...
  optimization: {
    runtimeChunk: {
      name: entrypoint => `runtime~${entrypoint.name}`
    }
  }
};

and

module.exports = {
  //...
  optimization: {
    runtimeChunk: true
  }
};

Based on PlayMa256's comment and the webpack docs on runtimeChunk. Neither caused the JavaScript to execute.

Answer:1

When you set:

module.exports = {
  //...
  optimization: {
    runtimeChunk: true
  }
};

It generates a runtime chunk. What is a runtime? Runtime is where ALL the code that webpack uses to load other files are. This is the heart of webpack when you run build.

Since you have 2 separate bundle files: resetPassword and app.

Ok, you have all the files you need. You maybe need vendors on both too, since vendor in your case contains everything from node_modules. So basically you will have:

html 1: app, vendor, runtimeChunk.

html 2: reset_password, vendor, runtimeChunk.

By doing that, you application should run.

Answer:2

I have a search box which should work with pressing the button and also pressing the Enter key. They both have the same code. The button works perfectly but when it comes to the Enter key, most of the ...

I have a search box which should work with pressing the button and also pressing the Enter key. They both have the same code. The button works perfectly but when it comes to the Enter key, most of the ...

I'm trying to do something I don't remember/find if it is possible in javascript. I have an object constructor "Point" and I want to know if it possible to, for example, sum two different objects ...

I'm trying to do something I don't remember/find if it is possible in javascript. I have an object constructor "Point" and I want to know if it possible to, for example, sum two different objects ...

  1. properties object javascript
  2. object properties javascript loop
  3. object properties javascript class
  4. merge properties objects javascript
  5. add properties objects javascript
  6. properties inside objects javascript
  7. get properties object javascript
  8. iterate properties object javascript
  9. count properties object javascript
  10. delete properties object javascript
  11. list properties object javascript
  12. remove properties object javascript
  13. show properties object javascript
  14. filter properties object javascript
  15. foreach properties object javascript
  16. compare properties object javascript
  17. check properties object javascript
  18. copy properties object javascript
  19. update properties object javascript
  20. print properties object javascript

I'm trying to start a git repo when scaffolding an application with a yeoman custom generator, this is what I got at the install step: install: function () { this.installDependencies(); ...

I'm trying to start a git repo when scaffolding an application with a yeoman custom generator, this is what I got at the install step: install: function () { this.installDependencies(); ...

  1. handling error events

I created an array buffer by var uni = new Uint8Array([255, 216, 255, 0, 0, 0, 0, 0]) But when I try to get back the bytes using map uni.map(byte => byte.toString(16)) it returns Uint8Array(8) ...

I created an array buffer by var uni = new Uint8Array([255, 216, 255, 0, 0, 0, 0, 0]) But when I try to get back the bytes using map uni.map(byte => byte.toString(16)) it returns Uint8Array(8) ...