-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Multiple entry points -> multiple html outputs? #218
Comments
@kirbysayshi - this is possible via multiple instances of the HtmlWebpackPlugin. You will need to configure each instance. You probably want to specify the related chunks to include as well as disable injecting all assets. 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'
})
]
}; There may be a better way, but my team uses a similar approach to generate multiple pages with different assets. |
@simshanith thanks for the reply! That's basically exactly what I wanted. Thanks for the clear answer! I enabled |
You probably want to look at the |
Thanks! That's exactly what I was looking for. Sorry about asking here for that, since I'm still new to webpack it's tough to know what's solved at the webpack level vs the plugin/loader level. |
@simshanith's solution is working but the |
@yorkie |
@simshanith And this is my configuration for webpack: entry: {
'page1': './src/components/app.js',
'page2': './src/page2/main.js'
},
output: {
path: path.join(__dirname, './public/js/'),
filename: 'bundle.js',
publicPath: '/js/'
},
module: {
loaders: [{
test: /.jsx?$/,
exclude: /node_modules/,
loader: 'babel',
query: { presets: ['react', 'es2015'],
plugins: ['recharts']}
},
{
test: /\.css$/,
loader: 'style!css?modules',
include: /flexboxgrid/,
}]
},
plugins: [
new HtmlWebpackPlugin({
inject: false,
chunks: ['page1'],
filename: 'public/index.html'
}),
new HtmlWebpackPlugin({
inject: false,
chunks: ['page2'],
filename: 'public/test.html'
})
]
}; |
@SerendpityZOEY - LMGTFY Not a problem with the plugin at all; rather when your template injects its code http://stackoverflow.com/questions/26566317/invariant-violation-registercomponent-target-container-is-not-a-dom-elem |
I'd rather reopen this issue, because the original proposal is much clearer and shorter compared to a workaround used by the plugin. It's blunder that tags like |
thx~ |
@enepomnyaschih When you have like 5.html pages you still need to type 5 plugins right? |
@AlennGK Exactly. At this point, my webpack config looks kind of ugly:
Simple plugin array gets complicated by Object.keys, map and concat calls. The next config would look much prettier in my opinion:
|
@enepomnyaschih - re: reopening this issue - a workaround has been provided. Changes to the options would be a breaking change and should be handled as a separate feature request. Re: your proposed I'd recommend abstracting your titles to HtmlWebpackPlugin reducer into a utility function to simplify your "kind of ugly" config. |
@simshanith - I'm fine with opening a separate feature request. However, if you leave a possibility to instantiate plugin multiple times, then it is not a breaking change - it doesn't break existing configs. It is fully compatible. My proposal is just a proposal - of course, it doesn't cover all use cases. It is open for discussion. To make it more flexible, the next approaches could be applied:
or
or simply use your current approach to define multiple plugin instances. I think that these cases are rare. What I proposed initially works fine in the majority of use cases. Utility function is not a solution, because it will still force users to merge the plugins via "concat" method. I just want to emphasize that it is not common for WebPack plugins to be instantiated multiple times just to cover several entries. It is confusing. Please let me know if I should open a separate feature request. |
@enepomnyaschih the problem is rather the compilation and cache which is used right now. In order to generate multiple files it would have to be refactored a lot. |
Currently this plugin is treating multiple entry page application as single entry chunk with vendor chunks application, I don't feel comfortable with solution of "instantiate plugin multiple times" either. Webpack does provide information like Of course we can exclude some chunks manually, but that's weird, there is very good calculated information from Webpack already. |
@simshanith thanks, |
4. webpack Dev Server should be configed too.rewrite urls
best practice: http://imshuai.com/create-react-multiple-enties/ |
@kirbysayshi you can try this: web-webpack-pluginauto detect html entry demo
webpack config const autoPlugin = new AutoWebPlugin(
// the directory hold all pages
'./src/pages',
{
// all props below is not required
// {string,function}
// the template file path used by all pages
// if typeof template ===string: template config is html template file full path
// if typeof template ===function: template config is function(pageName)=>newFullPath ,ask user for detail
template: './src/template.html',
// {string,function}
// javascript main file for current page,if it is null will use index.js in current page directory as main file
// typeof entry===string: entry config is entry file full path
// typeof entry===function: entry config is function(pageName)=>newFullPath ,ask user for detail
entry: null,
// {function}
// get WebPlugin output filename,default filename is pageName
// set filename as function(pageName)=>filename to add custom logic
filename:null,
// CommonsChunkPlugin options for all pages entry find by AutoWebPlugin.
// if this is null will not do commonsChunk action
commonsChunk: {
name: 'common',// name prop is require,output filename
minChunks: 2,// come from CommonsChunkPlugin
},
// {Array} pre append to all page's entry
preEntrys:['./path/to/file1.js'],
// {Array} post append to all page's entry
postEntrys:['./path/to/file2.js'],
// {string} publicPath for css file,for js file will use webpack.publicPath
stylePublicPath:null,
// page name list will not ignore by AutoWebPlugin(Not output html file for this page name)
ignorePages:['pageName'],
// whether output a pagemap.json file which contain all pages has been resolved with AutoWebPlugin in this way:
// {"page name": "page url",}
outputPagemap: true,
}
);
module.exports = {
// AutoWebPlugin will generate a entry for every page find in the directory hold all pages
// autoPlugin.entry({}) used to pass entrys find by AutoWebPlugin to webpack config
entry: autoPlugin.entry({
youAdditionalEntryName: 'you additional entry path',
}),
}; src directory
output directory
|
@enepomnyaschih @gwuhaolin Have your directory of pages already created, without needing a common template. Then simply have the same behavior as html-webpack-plugin has default for a single page. It is very difficult to configure webpack for static pages / multi pages... something that is really trivial with Gulp. |
Should we incorporate @simshanith's advice into the docs? |
Improves README.md to mention the `chunks` option in sections that might be of interest to users who have multiple entry-points and/or HTML files. Also adds a new example demonstrating how multiple entry points AND multiple pages may be used together, alongside the CommonsChunkPlugin. Based on an example written by @simshanith in jantimon#218.
Apologies for commenting on a closed thread, webpack newbee here. Is there a programmatic way of generating the .html file like (home.html, settings.html,dashboard.html)? Thanks in advance :) |
@sujeetbuddiga AFAIK, you have to have multiple instantiations of the plugin, each with a separate config. |
I think this topic is bigger than @kirbysayshi explained. I have exactly the situation @liujingbreak shown. I have multiple entries and webpack splitChunks plugin. What this plugin does is create as much chunks as needed. The name of the chunk is generated on fly. So I cannot simply provide exact list of chunks for HtmlWebpackPlugin to use. Even if I guess, while application grows -- it would change. So there should be a way to provide entry name so list of chunks needed are pulled from webpack runtime. |
@askoretskiy It looks like the chunks can be easily pulled from the "entrypoints" property of the stats. I have a simple patch here which seems to work for me: #944 |
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Hello! I have kind of a specific use case, and was hoping to get some feedback on if html-webpack-plugin is the right tool for this job. Also, I'm relatively new to webpack so I might be missing something.
I have a webpack config similar to this:
This generates a
bundle.js
in a build folder for each "app". I tried using html-webpack-plugin to also generate an html page for each app (notice the filename placeholder similar to theoutput
key):But that fails with the following error:
I assume that html-webpack-plugin is only ever planning on generating one output file. Is there a configuration option I'm missing, or perhaps an easy modification, to make the plugin generate one file for each entry point?
Thanks!
The text was updated successfully, but these errors were encountered: