Skip to content

Commit

Permalink
fix: compatibility with built-in CSS support (#605)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait authored May 19, 2023
1 parent 1eb00b5 commit 9636f58
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 51 deletions.
102 changes: 56 additions & 46 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
"babel-jest": "^28.1.3",
"cross-env": "^7.0.3",
"cspell": "^6.31.1",
"css-loader": "^6.7.3",
"css-loader": "^6.7.4",
"del": "^6.1.1",
"del-cli": "^4.0.1",
"es-check": "^7.1.1",
Expand All @@ -76,7 +76,7 @@
"sass-loader": "^12.4.0",
"semver": "^7.5.0",
"standard-version": "^9.5.0",
"webpack": "^5.81.0",
"webpack": "^5.83.1",
"webpack-cli": "^4.10.0",
"webpack-dev-server": "^4.13.3"
},
Expand Down
37 changes: 34 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,37 @@ import {

import schema from "./options.json";

const loaderAPI = () => {};
// eslint-disable-next-line consistent-return
const loader = function loader(content) {
if (
this._compiler &&
this._compiler.options &&
this._compiler.options.experiments &&
this._compiler.options.experiments.css &&
this._module &&
this._module.type === "css"
) {
return content;
}
};

loader.pitch = function pitch(request) {
if (
this._compiler &&
this._compiler.options &&
this._compiler.options.experiments &&
this._compiler.options.experiments.css &&
this._module &&
this._module.type === "css"
) {
this.emitWarning(
new Error(
'You can\'t use `experiments.css` (`experiments.futureDefaults` enable built-in CSS support by default) and `style-loader` together, please set `experiments.css` to `false` or set `{ type: "javascript/auto" }` for rules with `style-loader` in your webpack config (now `style-loader` does nothing).'
)
);
return;
}

loaderAPI.pitch = function loader(request) {
const options = this.getOptions(schema);
const injectType = options.injectType || "styleTag";
const esModule =
Expand Down Expand Up @@ -57,6 +85,7 @@ loaderAPI.pitch = function loader(request) {
case "linkTag": {
const hmrCode = this.hot ? getLinkHmrCode(esModule, this, request) : "";

// eslint-disable-next-line consistent-return
return `
${getImportLinkAPICode(esModule, this)}
${getImportInsertBySelectorCode(esModule, this, insertType, options)}
Expand Down Expand Up @@ -87,6 +116,7 @@ ${esModule ? "export default {}" : ""}`;
? getStyleHmrCode(esModule, this, request, true)
: "";

// eslint-disable-next-line consistent-return
return `
var exported = {};
Expand Down Expand Up @@ -157,6 +187,7 @@ ${getExportLazyStyleCode(esModule, this, request)}
? getStyleHmrCode(esModule, this, request, false)
: "";

// eslint-disable-next-line consistent-return
return `
${getImportStyleAPICode(esModule, this)}
${getImportStyleDomAPICode(esModule, this, isSingleton, isAuto)}
Expand Down Expand Up @@ -196,4 +227,4 @@ ${getExportStyleCode(esModule, this, request)}
}
};

export default loaderAPI;
export default loader;
15 changes: 15 additions & 0 deletions test/__snapshots__/loader.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`loader should do nothing with built-in CSS support: errors 1`] = `Array []`;

exports[`loader should do nothing with built-in CSS support: warnings 1`] = `
Array [
"ModuleWarning: Module Warning (from ../../node_modules/css-loader/dist/cjs.js):
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).",
"ModuleWarning: Module Warning (from ../../src/cjs.js):
You can't use \`experiments.css\` (\`experiments.futureDefaults\` enable built-in CSS support by default) and \`style-loader\` together, please set \`experiments.css\` to \`false\` or set \`{ type: \\"javascript/auto\\" }\` for rules with \`style-loader\` in your webpack config (now \`style-loader\` does nothing).",
"ModuleWarning: Module Warning (from ../../node_modules/css-loader/dist/cjs.js):
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).",
"ModuleWarning: Module Warning (from ../../src/cjs.js):
You can't use \`experiments.css\` (\`experiments.futureDefaults\` enable built-in CSS support by default) and \`style-loader\` together, please set \`experiments.css\` to \`false\` or set \`{ type: \\"javascript/auto\\" }\` for rules with \`style-loader\` in your webpack config (now \`style-loader\` does nothing).",
]
`;

exports[`loader should inject hmr code with HotModuleReplacementPlugin when the "injectType" option is "autoStyleTag": errors 1`] = `Array []`;

exports[`loader should inject hmr code with HotModuleReplacementPlugin when the "injectType" option is "autoStyleTag": warnings 1`] = `Array []`;
Expand Down
16 changes: 16 additions & 0 deletions test/loader.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,22 @@ describe("loader", () => {
expect(getErrors(stats)).toMatchSnapshot("errors");
});

it("should do nothing with built-in CSS support", async () => {
const compiler = getCompiler(
"./simple.js",
{},
{
experiments: {
css: true,
},
}
);
const stats = await compile(compiler);

expect(getWarnings(stats)).toMatchSnapshot("warnings");
expect(getErrors(stats)).toMatchSnapshot("errors");
});

injectTypes.forEach((injectType) => {
it(`should work when the "injectType" option is "${injectType}"`, async () => {
expect.assertions(3);
Expand Down

0 comments on commit 9636f58

Please sign in to comment.