Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 'css/auto' as a css module type #16577

Merged
merged 6 commits into from Jun 14, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions lib/ModuleTypeConstants.js
Expand Up @@ -60,6 +60,12 @@ const CSS_MODULE_TYPE_GLOBAL = "css/global";
*/
const CSS_MODULE_TYPE_MODULE = "css/module";

/**
* @type {Readonly<"css/auto">}
* This is the module type used for CSS files, the module will be parsed as CSS modules if the it's filename contains `.module` or `.modules`.
*/
const CSS_MODULE_TYPE_AUTO = "css/auto";

/**
* @type {Readonly<"asset">}
* This is the module type used for automatically choosing between `asset/inline`, `asset/resource` based on asset size limit (8096).
Expand Down Expand Up @@ -152,6 +158,7 @@ exports.WEBASSEMBLY_MODULE_TYPE_SYNC = WEBASSEMBLY_MODULE_TYPE_SYNC;
exports.CSS_MODULE_TYPE = CSS_MODULE_TYPE;
exports.CSS_MODULE_TYPE_GLOBAL = CSS_MODULE_TYPE_GLOBAL;
exports.CSS_MODULE_TYPE_MODULE = CSS_MODULE_TYPE_MODULE;
exports.CSS_MODULE_TYPE_AUTO = CSS_MODULE_TYPE_AUTO;
exports.WEBPACK_MODULE_TYPE_RUNTIME = WEBPACK_MODULE_TYPE_RUNTIME;
exports.WEBPACK_MODULE_TYPE_FALLBACK = WEBPACK_MODULE_TYPE_FALLBACK;
exports.WEBPACK_MODULE_TYPE_REMOTE = WEBPACK_MODULE_TYPE_REMOTE;
Expand Down
7 changes: 5 additions & 2 deletions lib/css/CssModulesPlugin.js
Expand Up @@ -11,7 +11,8 @@ const HotUpdateChunk = require("../HotUpdateChunk");
const {
CSS_MODULE_TYPE,
CSS_MODULE_TYPE_GLOBAL,
CSS_MODULE_TYPE_MODULE
CSS_MODULE_TYPE_MODULE,
CSS_MODULE_TYPE_AUTO
} = require("../ModuleTypeConstants");
const RuntimeGlobals = require("../RuntimeGlobals");
const SelfModuleFactory = require("../SelfModuleFactory");
Expand Down Expand Up @@ -149,7 +150,8 @@ class CssModulesPlugin {
for (const type of [
CSS_MODULE_TYPE,
CSS_MODULE_TYPE_GLOBAL,
CSS_MODULE_TYPE_MODULE
CSS_MODULE_TYPE_MODULE,
CSS_MODULE_TYPE_AUTO
]) {
normalModuleFactory.hooks.createParser
.for(type)
Expand All @@ -158,6 +160,7 @@ class CssModulesPlugin {

switch (type) {
case CSS_MODULE_TYPE:
case CSS_MODULE_TYPE_AUTO:
return new CssParser();
case CSS_MODULE_TYPE_GLOBAL:
return new CssParser({
Expand Down
9 changes: 9 additions & 0 deletions lib/css/CssParser.js
Expand Up @@ -6,6 +6,7 @@
"use strict";

const ModuleDependencyWarning = require("../ModuleDependencyWarning");
const { CSS_MODULE_TYPE_AUTO } = require("../ModuleTypeConstants");
const Parser = require("../Parser");
const WebpackError = require("../WebpackError");
const ConstDependency = require("../dependencies/ConstDependency");
Expand Down Expand Up @@ -37,6 +38,7 @@ const IMAGE_SET_FUNCTION = /^(-\w+-)?image-set$/i;
const OPTIONALLY_VENDOR_PREFIXED_KEYFRAMES_AT_RULE = /^@(-\w+-)?keyframes$/;
const OPTIONALLY_VENDOR_PREFIXED_ANIMATION_PROPERTY =
/^(-\w+-)?animation(-name)?$/i;
const IS_MODULES = /\.module(s)?\.\w+$/i;

/**
* @param {string} str url string
Expand Down Expand Up @@ -169,6 +171,13 @@ class CssParser extends Parser {
}

const module = state.module;
if (
module.type === CSS_MODULE_TYPE_AUTO &&
IS_MODULES.test(module.resourceResolveData.path)
TheLarkInn marked this conversation as resolved.
Show resolved Hide resolved
) {
this.defaultMode = "local";
}

const locConverter = new LocConverter(source);
/** @type {Set<string>}*/
const declaredCssVariables = new Set();
Expand Down
2 changes: 1 addition & 1 deletion schemas/plugins/css/CssGeneratorOptions.json
@@ -1,3 +1,3 @@
{
"$ref": "../../WebpackOptions.json#/definitions/CssParserOptions"
"$ref": "../../WebpackOptions.json#/definitions/CssGeneratorOptions"
}
2 changes: 1 addition & 1 deletion schemas/plugins/css/CssParserOptions.json
@@ -1,3 +1,3 @@
{
"$ref": "../../WebpackOptions.json#/definitions/CssGeneratorOptions"
"$ref": "../../WebpackOptions.json#/definitions/CssParserOptions"
}
3 changes: 3 additions & 0 deletions test/configCases/css/css-auto/global.less
@@ -0,0 +1,3 @@
body {
color: green;
}
10 changes: 10 additions & 0 deletions test/configCases/css/css-auto/index.js
@@ -0,0 +1,10 @@
import "./global.less";
import * as style1 from "./style1.module.less";
import * as style2 from "./style2.modules.less";

it("should correctly compile css/auto", () => {
const style = getComputedStyle(document.body);
expect(style.getPropertyValue("color")).toBe(" green");
expect(style1.class).toBe("./style1.module.less-class");
expect(style2.class).toBe("./style2.modules.less-class");
});
3 changes: 3 additions & 0 deletions test/configCases/css/css-auto/style1.module.less
@@ -0,0 +1,3 @@
.class {
color: red;
}
3 changes: 3 additions & 0 deletions test/configCases/css/css-auto/style2.modules.less
@@ -0,0 +1,3 @@
.class {
color: blue;
}
8 changes: 8 additions & 0 deletions test/configCases/css/css-auto/test.config.js
@@ -0,0 +1,8 @@
module.exports = {
moduleScope(scope) {
const link = scope.window.document.createElement("link");
link.rel = "stylesheet";
link.href = "bundle0.css";
scope.window.document.head.appendChild(link);
}
};
17 changes: 17 additions & 0 deletions test/configCases/css/css-auto/webpack.config.js
@@ -0,0 +1,17 @@
/** @type {import("../../../../types").Configuration} */
module.exports = {
target: "web",
mode: "development",
experiments: {
css: true
},
module: {
rules: [
{
test: /\.less$/,
use: "less-loader",
type: "css/auto"
}
]
}
};