question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Is there a injectType=lazyStyleTag .use() & .unuse() equivalent when using css-loader + mini-css-extract-plugin

See original GitHub issue

Documentation Is:

  • [ X] Missing
  • [ X] Needed
  • [ X] Confusing
  • Not Sure?

Please Explain in Detail…

We maintain a SPA that loads pages dynamically in response to hash state changes. For page routing, we use the “Dynamic expressions in import()” feature to import individual page chunks (each chunk consists of HTML, JS and LESS). To apply the page’s specific less, we imports page-styles.less using injectType=lazyStyleTag and call.use() & .unuse() when the page is loaded & unloaded respectively (avoids CSS conflicts between individual pages). To apply the page’s HTML, we insert/remove from a HTML container element.

We’d like the flexibility of using css-loader + mini-css-extract-plugin (without style-loader) for production builds. Is there another way of achieving the dynamic use/unuse functionality in webpack 5? Perhaps there’s some special syntax supported by html-loader that allows importing less files in HTML, resolving to either a <link href=“.css” rel=“stylesheet”> tag in production mode (.css chunk file per page), or <styles> tags in development. This works as a unuse() equivalent as the styling would be automatically removed when the HTML is removed from container element.

Secondly, is there a way to supply injectType=lazyStyleTag (either as configuration or inline option) to style-loader without renaming all .less to .lazy.less? We want the default for our non-lazy less files to be injectType=styleTag, so we’re currently specifying verbosely as : import styles from '!style-loader?injectType=lazyStyleTag!css-loader!less-loader!./styles.less';

Your Proposal for Changes

If the recommendation for production builds is not to use the style-loader, alternative dynamic css (loaded/unloaded on demand) strategies should be promoted over using style-loader’s injectType=lazyStyleTag .use() & unuse().

If there’s a cleaner way to import lazy styles (config or inline) without .less renames, please include details.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:11 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
andy1547commented, Jun 15, 2022

@zongzi531 To import LESS and CSS files, in a lazy and non-lazy fashion:

// lazy, need to call use / unuse on these
import lazyStyleA from "a.less?lazy";
import lazyStyleB from "b.css?lazy";
// none lazy imports, applied globally.
import "c.less";
import "d.css";

Webpack config required for this:

            {
                    test: /\.less$/i,
                    resourceQuery: /lazy/,
                    use: [
                        { loader: "style-loader", options: { injectType: "lazyStyleTag" } },
                        "css-loader",
                        "less-loader",
                    ],
                },            
                {
                    test: /\.less$/i,
                    resourceQuery: { not: /lazy/ },
                    use: [
                        "style-loader",
                        "css-loader",
                        "less-loader",
                    ],
                },
                {
                    test: /\.css$/i,
                    resourceQuery: /lazy/,
                    use: [
                        { loader: "style-loader", options: { injectType: "lazyStyleTag" } },
                        "css-loader",
                    ],
                }, 
                {
                    test: /\.css$/i,
                    resourceQuery: { not: /lazy/ },
                    use: [
                        'style-loader', 
                        'css-loader'
                    ]
                },
1reaction
andy1547commented, Mar 15, 2022

That has done the trick, thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

MiniCssExtractPlugin - webpack
For development mode (including webpack-dev-server ) you can use style-loader, because it injects CSS into the DOM using multiple and works faster. Do...
Read more >
mini-css-extract-plugin blocks webpack upgrade #740 - GitHub
So my main point is that webpack has a critical issue that blocks people from upgrading and no one cares to fix or...
Read more >
javascript - Create link tag for webpack mini-css-extract-plugin ...
I can't use html-webpack-plugin because there is no HTML output file. Is there a way to do what I want with existing loaders...
Read more >
Webpack with CSS Modules - E.Y.
We can see that first we use MiniCssExtractPlugin.loader to externalise the CSS files, and then we use css-loader to resolve @import() or ...
Read more >
Separating CSS - SurviveJS
Webpack provides a means to generate a separate CSS bundles using mini-css-extract-plugin (MCEP). It can aggregate multiple CSS files into one.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found