feat(css): redirect named locals to default export when named export is disabled

This commit is contained in:
Alexander Akait 2024-04-02 18:29:53 +03:00 committed by GitHub
commit 3361a5e5f5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 51 additions and 1 deletions

View File

@ -1030,6 +1030,11 @@ class CssParser extends Parser {
module.buildInfo.strict = true; module.buildInfo.strict = true;
module.buildMeta.exportsType = this.namedExports ? "namespace" : "default"; module.buildMeta.exportsType = this.namedExports ? "namespace" : "default";
if (!this.namedExports) {
module.buildMeta.defaultObject = "redirect";
}
module.addDependency(new StaticExportsDependency([], true)); module.addDependency(new StaticExportsDependency([], true));
return state; return state;
} }

View File

@ -0,0 +1,19 @@
import * as style1 from "./style.module.css?namespace";
import style2 from "./style.module.css?default";
import { foo } from "./style.module.css?named";
it("should able to import with default and named exports", () => {
expect(style1.default).toEqual(nsObj({ foo: '-_style_module_css_namespace-foo' }));
expect(style1.foo).toEqual("-_style_module_css_namespace-foo");
expect(style2).toEqual(nsObj({ foo: '-_style_module_css_default-foo' }));
expect(foo).toEqual("-_style_module_css_named-foo");
});
it("should able to import with different default and namex dynamic export", (done) => {
import("./style.module.css?namespace").then((style1) => {
expect(style1.default).toEqual(nsObj({ foo: '-_style_module_css_namespace-foo' }));
expect(style1.foo).toEqual('-_style_module_css_namespace-foo');
done();
}, done)
});

View File

@ -0,0 +1,3 @@
.foo {
color: red;
}

View File

@ -0,0 +1,20 @@
/** @type {import("../../../../").Configuration} */
module.exports = {
target: "node",
mode: "development",
devtool: false,
module: {
rules: [
{
test: /\.css/,
parser: {
namedExports: false
},
type: "css/module"
}
]
},
experiments: {
css: true
}
};

View File

@ -15,7 +15,10 @@ it("should able to import with different namedExports (async)", (done) => {
import("./style.module.css?named"), import("./style.module.css?named"),
]).then(([style1, style2, style3]) => { ]).then(([style1, style2, style3]) => {
expect(style1).toEqual(nsObj({ class: '-_style_module_css-class' })); expect(style1).toEqual(nsObj({ class: '-_style_module_css-class' }));
expect(style2).toEqual(nsObj({ default: nsObj({ class: '-_style_module_css_default-class' }) })); expect(style2).toEqual(nsObj({
class: "-_style_module_css_default-class",
default: nsObj({ class: '-_style_module_css_default-class' })
}));
expect(style3).toEqual(nsObj({ class: '-_style_module_css_named-class' })); expect(style3).toEqual(nsObj({ class: '-_style_module_css_named-class' }));
done() done()
}, done) }, done)