Skip to content

Commit f576ed6

Browse files
fix: compatibility with built-in CSS support (#1035)
1 parent 8caa4a8 commit f576ed6

File tree

28 files changed

+415
-280
lines changed

28 files changed

+415
-280
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,8 @@ module.exports = {
569569
module: {
570570
rules: [
571571
{
572+
// If you enable `experiments.css` or `experiments.futureDefaults`, please uncomment line below
573+
// type: "javascript/auto",
572574
test: /\.(sa|sc|c)ss$/,
573575
use: [
574576
devMode ? "style-loader" : MiniCssExtractPlugin.loader,

package-lock.json

Lines changed: 218 additions & 218 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
"bootstrap": "^4.6.2",
6868
"cross-env": "^7.0.3",
6969
"cspell": "^6.31.1",
70-
"css-loader": "^6.7.3",
70+
"css-loader": "^6.7.4",
7171
"del": "^6.0.0",
7272
"del-cli": "^4.0.0",
7373
"es-check": "^7.1.0",
@@ -87,7 +87,7 @@
8787
"sass-loader": "^12.6.0",
8888
"standard-version": "^9.3.0",
8989
"typescript": "^4.9.5",
90-
"webpack": "^5.77.0",
90+
"webpack": "^5.83.1",
9191
"webpack-cli": "^4.9.2",
9292
"webpack-dev-server": "^4.13.2"
9393
},

src/index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ class MiniCssExtractPlugin {
142142
assets,
143143
assetsInfo,
144144
}) {
145+
// @ts-ignore
145146
super(MODULE_TYPE, /** @type {string | undefined} */ (context));
146147

147148
this.id = "";
@@ -703,7 +704,11 @@ class MiniCssExtractPlugin {
703704
const renderedModules = Array.from(
704705
/** @type {CssModule[]} */
705706
(this.getChunkModules(chunk, chunkGraph))
706-
).filter((module) => module.type === MODULE_TYPE);
707+
).filter(
708+
(module) =>
709+
// @ts-ignore
710+
module.type === MODULE_TYPE
711+
);
707712

708713
const filenameTemplate =
709714
/** @type {string} */
@@ -791,6 +796,7 @@ class MiniCssExtractPlugin {
791796
);
792797

793798
for (const module of modules) {
799+
// @ts-ignore
794800
if (module.type === MODULE_TYPE) {
795801
obj[/** @type {string} */ (chunk.id)] = 1;
796802

src/loader.js

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,23 @@ function hotLoader(content, context) {
6969
* @param {string} request
7070
*/
7171
function pitch(request) {
72+
if (
73+
this._compiler &&
74+
this._compiler.options &&
75+
this._compiler.options.experiments &&
76+
this._compiler.options.experiments.css &&
77+
this._module &&
78+
this._module.type === "css"
79+
) {
80+
this.emitWarning(
81+
new Error(
82+
'You can\'t use `experiments.css` (`experiments.futureDefaults` enable built-in CSS support by default) and `mini-css-extract-plugin` together, please set `experiments.css` to `false` or set `{ type: "javascript/auto" }` for rules with `mini-css-extract-plugin` in your webpack config (now `mini-css-extract-plugin` does nothing).'
83+
)
84+
);
85+
86+
return;
87+
}
88+
7289
// @ts-ignore
7390
const options = this.getOptions(/** @type {Schema} */ (schema));
7491
const emit = typeof options.emit !== "undefined" ? options.emit : true;
@@ -488,4 +505,23 @@ function pitch(request) {
488505
});
489506
}
490507

491-
module.exports = { default: function loader() {}, pitch };
508+
/**
509+
* @this {import("webpack").LoaderContext<LoaderOptions>}
510+
* @param {string} content
511+
*/
512+
// eslint-disable-next-line consistent-return
513+
function loader(content) {
514+
if (
515+
this._compiler &&
516+
this._compiler.options &&
517+
this._compiler.options.experiments &&
518+
this._compiler.options.experiments.css &&
519+
this._module &&
520+
this._module.type === "css"
521+
) {
522+
return content;
523+
}
524+
}
525+
526+
module.exports = loader;
527+
module.exports.pitch = pitch;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
body {
2+
background: red;
3+
}
4+
5+
head{--webpack-0:_1;}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import "./style.css";
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
body {
2+
background: red;
3+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module.exports = [
2+
"WARNING in css ./style.css",
3+
"Module Warning (from ../../../node_modules/css-loader/dist/cjs.js):",
4+
'You can\'t use `experiments.css` (`experiments.futureDefaults` enable built-in CSS support by default) and `css-loader` together, please set `experiments.css` to `false` or set `{ type: "javascript/auto" }` for rules with `css-loader` in your webpack config (now css-loader does nothing).',
5+
"",
6+
"WARNING in css ./style.css",
7+
"Module Warning (from ../../../src/loader.js):",
8+
'You can\'t use `experiments.css` (`experiments.futureDefaults` enable built-in CSS support by default) and `mini-css-extract-plugin` together, please set `experiments.css` to `false` or set `{ type: "javascript/auto" }` for rules with `mini-css-extract-plugin` in your webpack config (now `mini-css-extract-plugin` does nothing).',
9+
].join("\n");
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import Self from "../../../src";
2+
3+
module.exports = {
4+
entry: "./index.js",
5+
output: {
6+
clean: false,
7+
cssFilename: "[name].css",
8+
},
9+
experiments: {
10+
css: true,
11+
},
12+
module: {
13+
rules: [
14+
{
15+
test: /\.css$/,
16+
use: [Self.loader, "css-loader"],
17+
},
18+
],
19+
},
20+
plugins: [
21+
new Self({
22+
filename: "[name].css",
23+
}),
24+
],
25+
};

0 commit comments

Comments
 (0)