From aab1b12700c25bedeec90e9c82018c0cf3f8fd4e Mon Sep 17 00:00:00 2001 From: Alexander Akait <4567934+alexander-akait@users.noreply.github.com> Date: Wed, 8 May 2024 18:45:43 +0300 Subject: [PATCH 01/10] ci: fix (#1099) --- .github/workflows/nodejs.yml | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml index 624a871f..71abb2bf 100644 --- a/.github/workflows/nodejs.yml +++ b/.github/workflows/nodejs.yml @@ -67,7 +67,7 @@ jobs: strategy: matrix: os: [ubuntu-latest, windows-latest, macos-latest] - node-version: [12.x, 14.x, 16.x, 18.x, 20.x] + node-version: [12.x, 14.x, 16.x, 18.x, 20.x, 22.x] webpack-version: [latest] runs-on: ${{ matrix.os }} @@ -82,11 +82,21 @@ jobs: run: git config --global core.autocrlf input - uses: actions/checkout@v4 - + - uses: actions/github-script@v7 + id: calculate_architecture + with: + result-encoding: string + script: | + if ('${{ matrix.os }}' === 'macos-latest' && ('${{ matrix['node-version'] }}' === '12.x' || '${{ matrix['node-version'] }}' === '14.x')) { + return "x64" + } else { + return '' + } - name: Use Node.js ${{ matrix.node-version }} uses: actions/setup-node@v4 with: node-version: ${{ matrix.node-version }} + architecture: ${{ steps.calculate_architecture.outputs.result }} cache: "npm" - name: Install dependencies @@ -110,7 +120,7 @@ jobs: strategy: matrix: os: [ubuntu-latest, windows-latest, macos-latest] - node-version: [12.x, 14.x, 16.x, 18.x, 20.x] + node-version: [12.x, 14.x, 16.x, 18.x, 20.x, 22.x] runs-on: ${{ matrix.os }} @@ -120,11 +130,21 @@ jobs: run: git config --global core.autocrlf input - uses: actions/checkout@v4 - + - uses: actions/github-script@v7 + id: calculate_architecture + with: + result-encoding: string + script: | + if ('${{ matrix.os }}' === 'macos-latest' && ('${{ matrix['node-version'] }}' === '12.x' || '${{ matrix['node-version'] }}' === '14.x')) { + return "x64" + } else { + return '' + } - name: Use Node.js ${{ matrix.node-version }} uses: actions/setup-node@v4 with: node-version: ${{ matrix.node-version }} + architecture: ${{ steps.calculate_architecture.outputs.result }} cache: "npm" - name: Install dependencies From 727427b957770d329bc3f841b62df498f794a785 Mon Sep 17 00:00:00 2001 From: Vasilii A <3757319+vsn4ik@users.noreply.github.com> Date: Wed, 19 Jun 2024 16:49:13 +0300 Subject: [PATCH 02/10] docs: fix example of the namedExport (#1103) --- README.md | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 42c3e674..cc2dc8c2 100644 --- a/README.md +++ b/README.md @@ -592,9 +592,11 @@ module.exports = { }, { loader: "css-loader", - esModule: true, - modules: { - namedExport: true, + options: { + esModule: true, + modules: { + namedExport: true, + }, }, }, ], From 3df97b62778fc4586c5198b9f3a447dc65979529 Mon Sep 17 00:00:00 2001 From: Sachin Kumar Date: Fri, 21 Jun 2024 17:35:26 +0530 Subject: [PATCH 03/10] docs: update github syntax of note (#1102) --- README.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index cc2dc8c2..a5a6c458 100644 --- a/README.md +++ b/README.md @@ -82,11 +82,11 @@ module.exports = { }; ``` -> **Warning** +> [!WARNING] > > Note that if you import CSS from your webpack entrypoint or import styles in the [initial](https://webpack.js.org/concepts/under-the-hood/#chunks) chunk, `mini-css-extract-plugin` will not load this CSS into the page. Please use [`html-webpack-plugin`](https://github.com/jantimon/html-webpack-plugin) for automatic generation `link` tags or create `index.html` file with `link` tag. -> **Warning** +> [!WARNING] > > Source maps works only for `source-map`/`nosources-source-map`/`hidden-nosources-source-map`/`hidden-source-map` values because CSS only supports source maps with the `sourceMappingURL` comment (i.e. `//# sourceMappingURL=style.css.map`). If you need set `devtool` to another value you can enable source maps generation for extracted CSS using [`sourceMap: true`](https://github.com/webpack-contrib/css-loader#sourcemap) for `css-loader`. @@ -162,7 +162,7 @@ Default: `document.head.appendChild(linkTag);` Inserts the `link` tag at the given position for [non-initial (async)](https://webpack.js.org/concepts/under-the-hood/#chunks) CSS chunks -> **Warning** +> [!WARNING] > > Only for [non-initial (async)](https://webpack.js.org/concepts/under-the-hood/#chunks) chunks. @@ -222,7 +222,7 @@ type attributes = Record}; Default: `{}` -> **Warning** +> [!WARNING] > > Only for [non-initial (async)](https://webpack.js.org/concepts/under-the-hood/#chunks) chunks. @@ -253,7 +253,7 @@ module.exports = { }; ``` -> **Note** +> [!NOTE] > > It's only applied to dynamically loaded css chunks, if you want to modify link attributes inside html file, please using [html-webpack-plugin](https://github.com/jantimon/html-webpack-plugin) @@ -560,7 +560,7 @@ type defaultExport = boolean; Default: `false` -> **Note** +> [!NOTE] > > This option will work only when you set `namedExport` to `true` in `css-loader` @@ -832,7 +832,7 @@ module.exports = { ### Hot Module Reloading (HMR) -> **Note** +> [!NOTE] > > HMR is automatically supported in webpack 5. No need to configure it. Skip the following: From 8f77e19a89eca97b05f1855d2c851592e98ff774 Mon Sep 17 00:00:00 2001 From: HHX <57352881+houhongxu@users.noreply.github.com> Date: Mon, 19 Aug 2024 06:42:37 -0700 Subject: [PATCH 04/10] fix: add `export default {}` when CSS modules enabled and a file is empty for the `defaultExport` option --- src/loader.js | 14 +-- .../es-named-and-default-export-1/empty.css | 0 .../expected/main.css | 1 + .../expected/main.js | 85 +++++++++++++++++++ .../es-named-and-default-export-1/index.js | 4 + .../webpack.config.js | 34 ++++++++ 6 files changed, 132 insertions(+), 6 deletions(-) create mode 100644 test/cases/es-named-and-default-export-1/empty.css create mode 100644 test/cases/es-named-and-default-export-1/expected/main.css create mode 100644 test/cases/es-named-and-default-export-1/expected/main.js create mode 100644 test/cases/es-named-and-default-export-1/index.js create mode 100644 test/cases/es-named-and-default-export-1/webpack.config.js diff --git a/src/loader.js b/src/loader.js index 5c0b04b1..e4217f5c 100644 --- a/src/loader.js +++ b/src/loader.js @@ -255,6 +255,11 @@ function pitch(request) { } const result = (function makeResult() { + const defaultExport = + typeof options.defaultExport !== "undefined" + ? options.defaultExport + : false; + if (locals) { if (namedExport) { const identifiers = Array.from( @@ -281,11 +286,6 @@ function pitch(request) { .map(([id, key]) => `${id} as ${JSON.stringify(key)}`) .join(", ")} }`; - const defaultExport = - typeof options.defaultExport !== "undefined" - ? options.defaultExport - : false; - return defaultExport ? `${localsString}\n${exportsString}\nexport default { ${identifiers .map(([id, key]) => `${JSON.stringify(key)}: ${id}`) @@ -297,7 +297,9 @@ function pitch(request) { esModule ? "export default" : "module.exports = " } ${JSON.stringify(locals)};`; } else if (esModule) { - return "\nexport {};"; + return defaultExport + ? "\nexport {};export default {};" + : "\nexport {};"; } return ""; })(); diff --git a/test/cases/es-named-and-default-export-1/empty.css b/test/cases/es-named-and-default-export-1/empty.css new file mode 100644 index 00000000..e69de29b diff --git a/test/cases/es-named-and-default-export-1/expected/main.css b/test/cases/es-named-and-default-export-1/expected/main.css new file mode 100644 index 00000000..8b137891 --- /dev/null +++ b/test/cases/es-named-and-default-export-1/expected/main.css @@ -0,0 +1 @@ + diff --git a/test/cases/es-named-and-default-export-1/expected/main.js b/test/cases/es-named-and-default-export-1/expected/main.js new file mode 100644 index 00000000..1d321d3a --- /dev/null +++ b/test/cases/es-named-and-default-export-1/expected/main.js @@ -0,0 +1,85 @@ +/******/ (() => { // webpackBootstrap +/******/ "use strict"; +/******/ var __webpack_modules__ = ([ +/* 0 */, +/* 1 */ +/***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => { + +__webpack_require__.r(__webpack_exports__); +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ "default": () => (__WEBPACK_DEFAULT_EXPORT__) +/* harmony export */ }); +// extracted by mini-css-extract-plugin +/* harmony default export */ const __WEBPACK_DEFAULT_EXPORT__ = ({}); + +/***/ }) +/******/ ]); +/************************************************************************/ +/******/ // The module cache +/******/ var __webpack_module_cache__ = {}; +/******/ +/******/ // The require function +/******/ function __webpack_require__(moduleId) { +/******/ // Check if module is in cache +/******/ var cachedModule = __webpack_module_cache__[moduleId]; +/******/ if (cachedModule !== undefined) { +/******/ return cachedModule.exports; +/******/ } +/******/ // Create a new module (and put it into the cache) +/******/ var module = __webpack_module_cache__[moduleId] = { +/******/ // no module.id needed +/******/ // no module.loaded needed +/******/ exports: {} +/******/ }; +/******/ +/******/ // Execute the module function +/******/ __webpack_modules__[moduleId](module, module.exports, __webpack_require__); +/******/ +/******/ // Return the exports of the module +/******/ return module.exports; +/******/ } +/******/ +/************************************************************************/ +/******/ /* webpack/runtime/define property getters */ +/******/ (() => { +/******/ // define getter functions for harmony exports +/******/ __webpack_require__.d = (exports, definition) => { +/******/ for(var key in definition) { +/******/ if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) { +/******/ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] }); +/******/ } +/******/ } +/******/ }; +/******/ })(); +/******/ +/******/ /* webpack/runtime/hasOwnProperty shorthand */ +/******/ (() => { +/******/ __webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop)) +/******/ })(); +/******/ +/******/ /* webpack/runtime/make namespace object */ +/******/ (() => { +/******/ // define __esModule on exports +/******/ __webpack_require__.r = (exports) => { +/******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) { +/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }); +/******/ } +/******/ Object.defineProperty(exports, '__esModule', { value: true }); +/******/ }; +/******/ })(); +/******/ +/************************************************************************/ +var __webpack_exports__ = {}; +// This entry need to be wrapped in an IIFE because it need to be isolated against other modules in the chunk. +(() => { +__webpack_require__.r(__webpack_exports__); +/* harmony import */ var _empty_css__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(1); + + +// eslint-disable-next-line no-console +console.log({ css: _empty_css__WEBPACK_IMPORTED_MODULE_0__["default"] }); + +})(); + +/******/ })() +; \ No newline at end of file diff --git a/test/cases/es-named-and-default-export-1/index.js b/test/cases/es-named-and-default-export-1/index.js new file mode 100644 index 00000000..e42dfb9a --- /dev/null +++ b/test/cases/es-named-and-default-export-1/index.js @@ -0,0 +1,4 @@ +import css from "./empty.css"; + +// eslint-disable-next-line no-console +console.log({ css }); diff --git a/test/cases/es-named-and-default-export-1/webpack.config.js b/test/cases/es-named-and-default-export-1/webpack.config.js new file mode 100644 index 00000000..b58d12f5 --- /dev/null +++ b/test/cases/es-named-and-default-export-1/webpack.config.js @@ -0,0 +1,34 @@ +import Self from "../../../src"; + +module.exports = { + entry: "./index.js", + module: { + rules: [ + { + test: /\.css$/, + use: [ + { + loader: Self.loader, + options: { + defaultExport: true, + }, + }, + { + loader: "css-loader", + options: { + esModule: true, + modules: { + namedExport: true, + }, + }, + }, + ], + }, + ], + }, + plugins: [ + new Self({ + filename: "[name].css", + }), + ], +}; From 00cdedb1998017631db74e312f52177efe042cd4 Mon Sep 17 00:00:00 2001 From: "alexander.akait" Date: Mon, 19 Aug 2024 16:44:33 +0300 Subject: [PATCH 05/10] chore(release): 2.9.1 --- CHANGELOG.md | 7 +++++++ package-lock.json | 4 ++-- package.json | 2 +- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 93dc97fd..84831237 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,13 @@ All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines. +### [2.9.1](https://github.com/webpack-contrib/mini-css-extract-plugin/compare/v2.9.0...v2.9.1) (2024-08-19) + + +### Bug Fixes + +* add `export default {}` when CSS modules enabled and a file is empty for the `defaultExport` option ([8f77e19](https://github.com/webpack-contrib/mini-css-extract-plugin/commit/8f77e19a89eca97b05f1855d2c851592e98ff774)) + ## [2.9.0](https://github.com/webpack-contrib/mini-css-extract-plugin/compare/v2.8.1...v2.9.0) (2024-04-16) diff --git a/package-lock.json b/package-lock.json index fdf1b2e1..58daef93 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "mini-css-extract-plugin", - "version": "2.9.0", + "version": "2.9.1", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "mini-css-extract-plugin", - "version": "2.9.0", + "version": "2.9.1", "license": "MIT", "dependencies": { "schema-utils": "^4.0.0", diff --git a/package.json b/package.json index 56358eb0..ef5701b2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "mini-css-extract-plugin", - "version": "2.9.0", + "version": "2.9.1", "description": "extracts CSS into separate files", "license": "MIT", "repository": "webpack-contrib/mini-css-extract-plugin", From 58c6b7422aedfd3fc4d5f3553b196da40eae1f4b Mon Sep 17 00:00:00 2001 From: Alexander Akait <4567934+alexander-akait@users.noreply.github.com> Date: Fri, 1 Nov 2024 03:46:06 +0300 Subject: [PATCH 06/10] fix: prefetch and preload runtime generation (#1116) --- src/index.js | 4 ++-- test/cases/prefetch-preload-hmr/a.css | 3 +++ test/cases/prefetch-preload-hmr/b.js | 0 .../prefetch-preload-hmr/expected/main.css | 4 ++++ test/cases/prefetch-preload-hmr/index.js | 3 +++ .../prefetch-preload-hmr/webpack.config.js | 21 +++++++++++++++++++ 6 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 test/cases/prefetch-preload-hmr/a.css create mode 100644 test/cases/prefetch-preload-hmr/b.js create mode 100644 test/cases/prefetch-preload-hmr/expected/main.css create mode 100644 test/cases/prefetch-preload-hmr/index.js create mode 100644 test/cases/prefetch-preload-hmr/webpack.config.js diff --git a/src/index.js b/src/index.js index 7fc94b01..b7caa4bb 100644 --- a/src/index.js +++ b/src/index.js @@ -1123,7 +1123,7 @@ class MiniCssExtractPlugin { ]) : "// no hmr", "", - withPrefetch && hasCssMatcher !== false + withPrefetch && withLoading && hasCssMatcher !== false ? `${ RuntimeGlobals.prefetchChunkHandlers }.miniCss = ${runtimeTemplate.basicFunction("chunkId", [ @@ -1159,7 +1159,7 @@ class MiniCssExtractPlugin { ])};` : "// no prefetching", "", - withPreload && hasCssMatcher !== false + withPreload && withLoading && hasCssMatcher !== false ? `${ RuntimeGlobals.preloadChunkHandlers }.miniCss = ${runtimeTemplate.basicFunction("chunkId", [ diff --git a/test/cases/prefetch-preload-hmr/a.css b/test/cases/prefetch-preload-hmr/a.css new file mode 100644 index 00000000..5451a331 --- /dev/null +++ b/test/cases/prefetch-preload-hmr/a.css @@ -0,0 +1,3 @@ +.a { + color: red; +} diff --git a/test/cases/prefetch-preload-hmr/b.js b/test/cases/prefetch-preload-hmr/b.js new file mode 100644 index 00000000..e69de29b diff --git a/test/cases/prefetch-preload-hmr/expected/main.css b/test/cases/prefetch-preload-hmr/expected/main.css new file mode 100644 index 00000000..98f445d8 --- /dev/null +++ b/test/cases/prefetch-preload-hmr/expected/main.css @@ -0,0 +1,4 @@ +.a { + color: red; +} + diff --git a/test/cases/prefetch-preload-hmr/index.js b/test/cases/prefetch-preload-hmr/index.js new file mode 100644 index 00000000..e615a5ec --- /dev/null +++ b/test/cases/prefetch-preload-hmr/index.js @@ -0,0 +1,3 @@ +import "./a.css"; + +import(/* webpackPrefetch: true */ "./b"); diff --git a/test/cases/prefetch-preload-hmr/webpack.config.js b/test/cases/prefetch-preload-hmr/webpack.config.js new file mode 100644 index 00000000..d07a9495 --- /dev/null +++ b/test/cases/prefetch-preload-hmr/webpack.config.js @@ -0,0 +1,21 @@ +import webpack from "webpack"; + +import Self from "../../../src"; + +module.exports = { + entry: "./index.js", + module: { + rules: [ + { + test: /\.css$/, + use: [Self.loader, "css-loader"], + }, + ], + }, + plugins: [ + new Self({ + filename: "[name].css", + }), + new webpack.HotModuleReplacementPlugin(), + ], +}; From 3effaa0319bad5cc1bf0ae760553bf7abcbc35a4 Mon Sep 17 00:00:00 2001 From: "alexander.akait" Date: Fri, 1 Nov 2024 03:46:35 +0300 Subject: [PATCH 07/10] chore(release): 2.9.2 --- CHANGELOG.md | 7 +++++++ package-lock.json | 4 ++-- package.json | 2 +- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 84831237..af554f8f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,13 @@ All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines. +### [2.9.2](https://github.com/webpack-contrib/mini-css-extract-plugin/compare/v2.9.1...v2.9.2) (2024-11-01) + + +### Bug Fixes + +* prefetch and preload runtime generation ([#1116](https://github.com/webpack-contrib/mini-css-extract-plugin/issues/1116)) ([58c6b74](https://github.com/webpack-contrib/mini-css-extract-plugin/commit/58c6b7422aedfd3fc4d5f3553b196da40eae1f4b)) + ### [2.9.1](https://github.com/webpack-contrib/mini-css-extract-plugin/compare/v2.9.0...v2.9.1) (2024-08-19) diff --git a/package-lock.json b/package-lock.json index 58daef93..e6cdb38a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "mini-css-extract-plugin", - "version": "2.9.1", + "version": "2.9.2", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "mini-css-extract-plugin", - "version": "2.9.1", + "version": "2.9.2", "license": "MIT", "dependencies": { "schema-utils": "^4.0.0", diff --git a/package.json b/package.json index ef5701b2..983a8ca0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "mini-css-extract-plugin", - "version": "2.9.1", + "version": "2.9.2", "description": "extracts CSS into separate files", "license": "MIT", "repository": "webpack-contrib/mini-css-extract-plugin", From 07abf0dc589cc49ae3c83316825364f39f62b0e3 Mon Sep 17 00:00:00 2001 From: Sachin Kumar Date: Sat, 15 Feb 2025 02:35:27 +0530 Subject: [PATCH 08/10] chore: update codecov-action from v4 to v5 (#1119) --- .github/workflows/nodejs.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml index 71abb2bf..be5603e2 100644 --- a/.github/workflows/nodejs.yml +++ b/.github/workflows/nodejs.yml @@ -110,7 +110,7 @@ jobs: run: npm run test:coverage -- --ci - name: Submit coverage data to codecov - uses: codecov/codecov-action@v4 + uses: codecov/codecov-action@v5 with: token: ${{ secrets.CODECOV_TOKEN }} @@ -156,6 +156,6 @@ jobs: OLD_API: "true" - name: Submit coverage data to codecov - uses: codecov/codecov-action@v4 + uses: codecov/codecov-action@v5 with: token: ${{ secrets.CODECOV_TOKEN }} From 554277b30513f76d10d7cc30b8a42b1ce3b12704 Mon Sep 17 00:00:00 2001 From: Sachin Kumar Date: Mon, 30 Jun 2025 11:16:19 +0530 Subject: [PATCH 09/10] docs: fix typos and improve clarity in README.md (#1128) --- README.md | 72 ++++++++++++++++++++++++++++++------------------------- 1 file changed, 40 insertions(+), 32 deletions(-) diff --git a/README.md b/README.md index a5a6c458..2b9ce36a 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ # mini-css-extract-plugin -This plugin extracts CSS into separate files. It creates a CSS file per JS file which contains CSS. It supports On-Demand-Loading of CSS and SourceMaps. +This plugin extracts CSS into separate files. It creates a CSS file for each JS file that contains CSS. It supports On-Demand-Loading of CSS and SourceMaps. It builds on top of a new webpack v5 feature and requires webpack 5 to work. @@ -48,7 +48,7 @@ pnpm add -D mini-css-extract-plugin It's recommended to combine `mini-css-extract-plugin` with the [`css-loader`](https://github.com/webpack-contrib/css-loader) -Then add the loader and the plugin to your `webpack` config. For example: +Then add the loader and the plugin to your `webpack` configuration. For example: **style.css** @@ -84,7 +84,7 @@ module.exports = { > [!WARNING] > -> Note that if you import CSS from your webpack entrypoint or import styles in the [initial](https://webpack.js.org/concepts/under-the-hood/#chunks) chunk, `mini-css-extract-plugin` will not load this CSS into the page. Please use [`html-webpack-plugin`](https://github.com/jantimon/html-webpack-plugin) for automatic generation `link` tags or create `index.html` file with `link` tag. +> Note that if you import CSS from your webpack entrypoint or import styles in the [initial](https://webpack.js.org/concepts/under-the-hood/#chunks) chunk, `mini-css-extract-plugin` will not load this CSS into the page automatically. Please use [`html-webpack-plugin`](https://github.com/jantimon/html-webpack-plugin) for automatic generation `link` tags or manually include a `` tag in your `index.html` file. > [!WARNING] > @@ -129,7 +129,7 @@ type chunkFilename = | ((pathData: PathData, assetInfo?: AssetInfo) => string); ``` -Default: `based on filename` +Default: `Based on filename` > Specifying `chunkFilename` as a `function` is only available in webpack@5 @@ -148,7 +148,7 @@ type ignoreOrder = boolean; Default: `false` Remove Order Warnings. -See [examples](#remove-order-warnings) below for details. +See [examples](#remove-order-warnings) for more details. #### `insert` @@ -164,7 +164,7 @@ Inserts the `link` tag at the given position for [non-initial (async)](https://w > [!WARNING] > -> Only for [non-initial (async)](https://webpack.js.org/concepts/under-the-hood/#chunks) chunks. +> Only applicable for [non-initial (async)](https://webpack.js.org/concepts/under-the-hood/#chunks) chunks. By default, the `mini-css-extract-plugin` appends styles (`` elements) to `document.head` of the current `window`. @@ -172,7 +172,7 @@ However in some circumstances it might be necessary to have finer control over t For example this is the case when you asynchronously load styles for an application that runs inside of an iframe. In such cases `insert` can be configured to be a function or a custom selector. -If you target an [iframe](https://developer.mozilla.org/en-US/docs/Web/API/HTMLIFrameElement) make sure that the parent document has sufficient access rights to reach into the frame document and append elements to it. +If you target an [iframe](https://developer.mozilla.org/en-US/docs/Web/API/HTMLIFrameElement), make sure that the parent document has sufficient access rights to reach into the frame document and append elements to it. ##### `string` @@ -187,14 +187,14 @@ new MiniCssExtractPlugin({ }); ``` -A new `` element will be inserted after the element with id `some-element`. +A new `` tag will be inserted after the element with the ID `some-element`. ##### `function` Allows to override default behavior and insert styles at any position. > ⚠ Do not forget that this code will run in the browser alongside your application. Since not all browsers support latest ECMA features like `let`, `const`, `arrow function expression` and etc we recommend you to use only ECMA 5 features and syntax. - +> > > ⚠ The `insert` function is serialized to string and passed to the plugin. This means that it won't have access to the scope of the webpack configuration module. **webpack.config.js** @@ -210,7 +210,7 @@ new MiniCssExtractPlugin({ }); ``` -A new `` element will be inserted before the element with id `some-element`. +A new `` tag will be inserted before the element with the ID `some-element`. #### `attributes` @@ -224,7 +224,7 @@ Default: `{}` > [!WARNING] > -> Only for [non-initial (async)](https://webpack.js.org/concepts/under-the-hood/#chunks) chunks. +> Only applies to [non-initial (async)](https://webpack.js.org/concepts/under-the-hood/#chunks) chunks. If defined, the `mini-css-extract-plugin` will attach given attributes with their values on `` element. @@ -255,7 +255,8 @@ module.exports = { > [!NOTE] > -> It's only applied to dynamically loaded css chunks, if you want to modify link attributes inside html file, please using [html-webpack-plugin](https://github.com/jantimon/html-webpack-plugin) +> It's only applied to dynamically loaded CSS chunks. +> If you want to modify `` attributes inside HTML file, please use [html-webpack-plugin](https://github.com/jantimon/html-webpack-plugin) #### `linkType` @@ -297,7 +298,7 @@ module.exports = { ##### `boolean` -`false` disables the link `type` attribute +`false` disables the link `type` attribute entirely. **webpack.config.js** @@ -372,10 +373,9 @@ Default: `undefined` Enabled by default if not explicitly enabled (i.e. `true` and `false` allow you to explicitly control this option) and new API is available (at least webpack `5.52.0` is required). Boolean values are available since version `5.33.2`, but you need to enable `experiments.executeModule` (not required from webpack `5.52.0`). -Use a new webpack API to execute modules instead of child compilers. -This improves performance and memory usage a lot. +Use a new webpack API to execute modules instead of child compilers, significantly improving performance and memory usage. -When combined with `experiments.layers`, this adds a `layer` option to the loader options to specify the layer of the css execution. +When combined with `experiments.layers`, this adds a `layer` option to the loader options to specify the layer of the CSS execution. **webpack.config.js** @@ -506,7 +506,8 @@ type emit = boolean; Default: `true` -If true, emits a file (writes a file to the filesystem). If false, the plugin will extract the CSS but **will not** emit the file. +If `true`, emits a file (writes a file to the filesystem). +If `false`, the plugin will extract the CSS but **will not** emit the file. It is often useful to disable this option for server-side packages. #### `esModule` @@ -610,11 +611,10 @@ module.exports = { ### Recommended -For `production` builds it's recommended to extract the CSS from your bundle being able to use parallel loading of CSS/JS resources later on. -This can be achieved by using the `mini-css-extract-plugin`, because it creates separate css files. +For `production` builds, it is recommended to extract the CSS from your bundle being able to use parallel loading of CSS/JS resources later on. This can be achieved by using the `mini-css-extract-plugin`, because it creates separate css files. For `development` mode (including `webpack-dev-server`) you can use [style-loader](https://github.com/webpack-contrib/style-loader), because it injects CSS into the DOM using multiple and works faster. -> Do not use `style-loader` and `mini-css-extract-plugin` together. +> Important: Do not use `style-loader` and `mini-css-extract-plugin` together. **webpack.config.js** @@ -684,7 +684,7 @@ module.exports = { > ⚠ Names of locals are converted to `camelCase`. -> ⚠ It is not allowed to use JavaScript reserved words in css class names. +> ⚠ It is not allowed to use JavaScript reserved words in CSS class names. > ⚠ Options `esModule` and `modules.namedExport` in `css-loader` should be enabled. @@ -743,6 +743,8 @@ module.exports = { ### The `publicPath` option as function +You can specify `publicPath` as a function to dynamically determine the public path based on each resource’s location relative to the project root or context. + **webpack.config.js** ```js @@ -836,7 +838,7 @@ module.exports = { > > HMR is automatically supported in webpack 5. No need to configure it. Skip the following: -The `mini-css-extract-plugin` supports hot reloading of actual css files in development. +The `mini-css-extract-plugin` supports hot reloading of actual CSS files in development. Some options are provided to enable HMR of both standard stylesheets and locally scoped CSS or CSS modules. Below is an example configuration of mini-css for HMR use with CSS modules. @@ -908,7 +910,8 @@ module.exports = { }, optimization: { minimizer: [ - // For webpack@5 you can use the `...` syntax to extend existing minimizers (i.e. `terser-webpack-plugin`), uncomment the next line + // For webpack@5 you can use the `...` syntax to extend existing minimizers (i.e. `terser-webpack-plugin`). + // Uncomment the next line o keep JS minimizers and add CSS minimizer: // `...`, new CssMinimizerPlugin(), ], @@ -916,19 +919,21 @@ module.exports = { }; ``` -This will enable CSS optimization only in production mode. If you want to run it also in development set the `optimization.minimize` option to true. +- By default, CSS minimization runs in production mode. +- If you want to run it also in development set the `optimization.minimize` option to `true`. ### Using preloaded or inlined CSS -The runtime code detects already added CSS via `` or `