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): allow to use default and named export #18271

Merged
merged 2 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions lib/css/CssParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -1030,6 +1030,11 @@ class CssParser extends Parser {

module.buildInfo.strict = true;
module.buildMeta.exportsType = this.namedExports ? "namespace" : "default";

if (!this.namedExports) {
module.buildMeta.defaultObject = "redirect";
}

module.addDependency(new StaticExportsDependency([], true));
return state;
}
Expand Down
19 changes: 19 additions & 0 deletions test/configCases/css/default-exports-parser-options/index.js
Original file line number Diff line number Diff line change
@@ -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)
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.foo {
color: red;
}
Original file line number Diff line number Diff line change
@@ -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
}
};
5 changes: 4 additions & 1 deletion test/configCases/css/named-exports-parser-options/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ it("should able to import with different namedExports (async)", (done) => {
import("./style.module.css?named"),
]).then(([style1, style2, style3]) => {
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' }));
done()
}, done)
Expand Down