Skip to content

Allow defining which chunks will be loaded dynamically #57

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
bebraw opened this issue Mar 26, 2018 · 11 comments
Closed

Allow defining which chunks will be loaded dynamically #57

bebraw opened this issue Mar 26, 2018 · 11 comments

Comments

@bebraw
Copy link

bebraw commented Mar 26, 2018

It would be useful to have means to define which chunks are loaded dynamically and which are not. I did a small implementation that allows this:

...

module.exports = {
    plugins: [
        new CssExtractPlugin({
            loadAsync: chunk => chunk.modulesSize() >= 200000, // condition
            captureCss: CssExtractPlugin.writeManifest('css-extract.json') // action
        }),
    ]
};

I'm not sure of the naming but basically this does the trick for me. I write a manifest which I use later to inline the CSS but you could easily write a CSS file through webpack here as well. You can find my code here.

Let me know if you want a PR.

This was referenced Mar 26, 2018
@garygreen
Copy link

garygreen commented Mar 26, 2018

All this chunking is getting so complicated. Could the new splitChunks option not unify all the chunking across the project, so you define a css group and use the minChunks option (in your example - loadAsync) in order to determine if it should be chunked? Seems there is so much duplicate of functionality across webpack which leads to unnecessarily complicated configurations

@bebraw
Copy link
Author

bebraw commented Mar 26, 2018

@garygreen I think in the ideal case this plugin wouldn't be needed. I hope webpack 5 simplifies this. I had to implement the code above by necessity. 👍

@dtothefp
Copy link

@bebraw so were are already waiting for Webpack 5 to solve things that Webpack 4 didn't address 😜 . Thanks for the example, I've resorted to using react-loadable and inlining link tags for my page specific chunks. This results in double loading CSS though when the loaded async JS inserts the link tag for the async CSS. This wouldn't be a problem if I wasn't doing isomorphic rendering of JS but because I am the async loaded CSS causes an initial rendering of unstyled HTML for the page specific components.

@bebraw
Copy link
Author

bebraw commented Mar 27, 2018

@dtothefp I have a SSR use case too. First I write a manifest through my extension and then use it to write critical CSS inline to initial HTML. This works well.

@dtothefp
Copy link

@bebraw that sounds cool and similar to what I’m doing. How do you prevent the request for the chunked CSS that you’ve already unlined? Your plugin looks cool but I was hoping to do this with he mini-css-extract-plugin as I thought it was going to be the maintained solution moving forward.

@KyleAMathews
Copy link

@dtothefp
Copy link

@KyleAMathews thanks for the heads up on this....it works great. Only problem is to get it to work I need to compile the dist directory for the plugin. @sokra Wondering when we can expect a release?

@alexander-akait
Copy link
Member

Today will be release, also with fix incremental problem.

@sokra
Copy link
Member

sokra commented Mar 29, 2018

Today will be release, also with fix incremental problem.

It's released, but the incremental problem is not fixed yet, as it required fixes to webpack too.

@sokra
Copy link
Member

sokra commented Mar 29, 2018

Regarding the original issue. This plugin just enables webpack to use CSS in chunks. You can use the usual webpack plugins/options to modify the chunk graph. i. e. splitChunks or AggressiveMergingPlugin.

@sokra sokra closed this as completed Mar 29, 2018
@sokra
Copy link
Member

sokra commented Mar 29, 2018

Inlining could be a html-webpack-plugin -plugin. I guess there is already one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants