@@ -529,6 +529,7 @@ See [boilerplate](https://github.com/webdiscus/webpack-html-scss-boilerplate)
529529 - [ How to resolve source assets in an attribute containing JSON value] ( #recipe-resolve-attr-json )
530530 - [ How to load CSS file dynamically] ( #recipe-dynamic-load-css ) (lazy loading CSS)
531531 - [ How to import CSS class names in JS] ( #recipe-css-modules ) (CSS modules)
532+ - [ How to import CSS stylesheet in JS] ( #recipe-css-style-sheet ) ([ CSSStyleSheet] ( https://developer.mozilla.org/en-US/docs/Web/API/CSSStyleSheet ) )
532533 - [ How to load JS and CSS from ` node_modules ` in template] ( #recipe-load-js-css-from-node-modules )
533534 - [ How to import CSS or SCSS from ` node_modules ` in SCSS] ( #recipe-import-style-from-node-modules )
534535 - [ How to process a PHP template] ( #recipe-preprocessor-php )
@@ -5450,9 +5451,98 @@ The imported `styles` object contains generated class names like followings:
54505451}
54515452` ` `
54525453
5453-
54545454Read more information about [CSS Modules](https://github.com/css-modules/css-modules).
54555455
5456+ ---
5457+
5458+ #### [↑ back to contents](#contents)
5459+
5460+ <a id="recipe-css-style-sheet" name="recipe-css-style-sheet"></a>
5461+
5462+ ## How to import CSS stylesheet in JS
5463+
5464+ Using the ` css- loader` option [exportType](https://github.com/webpack-contrib/css-loader?#exporttype) as ` css- style- sheet`
5465+ you can import the CSS stylesheets as the instance of the [CSSStyleSheet](https://developer.mozilla.org/en-US/docs/Web/API/CSSStyleSheet) object.
5466+
5467+ Import a CSS module script and apply it to a document or a shadow root like this:
5468+
5469+ ` ` ` js
5470+ import sheet from ' ./style.scss?sheet' ;
5471+
5472+ document .adoptedStyleSheets = [sheet];
5473+ shadowRoot .adoptedStyleSheets = [sheet];
5474+ ` ` `
5475+
5476+ You can use the ` ? sheet` URL query to import a style file as stylesheets.
5477+ The query must be configured in the webpack config:
5478+
5479+ ` ` ` js
5480+ module .exports = {
5481+ plugins: [
5482+ new HtmlBundlerPlugin ({
5483+ entry: {
5484+ index: ' ./src/index.html' ,
5485+ },
5486+ js: {
5487+ filename: ' [name].[contenthash:8].js' ,
5488+ },
5489+ css: {
5490+ filename: ' [name].[contenthash:8].css' ,
5491+ },
5492+ }),
5493+ ],
5494+ module: {
5495+ rules: [
5496+ {
5497+ test: / \. (s? css)$ / ,
5498+ oneOf: [
5499+ // Import CSS/SCSS source file as a CSSStyleSheet object
5500+ {
5501+ resourceQuery: / sheet/ , // <= the query, e.g. style.scss?sheet
5502+ use: [
5503+ {
5504+ loader: ' css-loader' ,
5505+ options: {
5506+ exportType: ' css-style-sheet' , // <= define this option
5507+ },
5508+ },
5509+ {
5510+ loader: ' sass-loader' ,
5511+ },
5512+ ],
5513+ },
5514+ // Import CSS/SCSS source file as a CSS string
5515+ {
5516+ use: [
5517+ ' css-loader' ,
5518+ ' sass-loader' ,
5519+ ],
5520+ }
5521+ ],
5522+ }
5523+ ],
5524+ },
5525+ };
5526+ ` ` `
5527+
5528+ Using the universal configuration above you can apply CSS stylesheets in JS and extract CSS into separate file or inject CSS into HTML:
5529+
5530+ ` ` ` js
5531+ import sheet from ' ./style.scss?sheet' ; // import as CSSStyleSheet object
5532+ import ' ./style2.scss?inline' ; // the extracted CSS will be injected into HTML
5533+ import ' ./style3.scss' ; // the extracted CSS will be saved into separate output file
5534+
5535+ // apply stylesheet to document and shadow root
5536+ document .adoptedStyleSheets = [sheet];
5537+ shadowRoot .adoptedStyleSheets = [sheet];
5538+ ` ` `
5539+
5540+ This is useful for [custom elements](https://developer.mozilla.org/en-US/docs/Web/Web_Components/Using_custom_elements) and shadow DOM.
5541+
5542+ More information:
5543+
5544+ - [Using CSS Module Scripts to import stylesheets](https://web.dev/css-module-scripts/)
5545+ - [Constructable Stylesheets: seamless reusable styles](https://developers.google.com/web/updates/2019/02/constructable-stylesheets)
54565546
54575547---
54585548
@@ -6067,7 +6157,7 @@ dist/js/app-5fa74877.1aceb2db.js
60676157
60686158Using the bundler plugin, all your style source files should be specified directly in the template.
60696159You can import style files in JavaScript, like it works using the ` mini- css- extract- plugin` and ` html- webpack- plugin` ,
6070- but it is a **dirty hack**, ** bad practice**, processing is **slow**, avoid it if possible .
6160+ but it is a **bad practice** and processing is **slower** .
60716161
60726162You can separate the styles into multiple bundles yourself.
60736163
0 commit comments