Skip to content

Commit

Permalink
fix: logic for CSS modules and named export
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Sep 3, 2021
1 parent e7ff8c6 commit f0c1f1b
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 13 deletions.
6 changes: 6 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,12 @@ function getModulesOptions(rawOptions, isExportStyleSheet, loaderContext) {
...rawModulesOptions,
};

if (isExportStyleSheet && modulesOptions.namedExport === false) {
throw new Error(
'The "exportStyleSheet" option requires the "modules.namedExport" option to be enabled.'
);
}

let exportLocalsConventionType;

if (typeof modulesOptions.exportLocalsConvention === "string") {
Expand Down
31 changes: 25 additions & 6 deletions test/__snapshots__/exportStyleSheet.test.js.snap

Large diffs are not rendered by default.

62 changes: 55 additions & 7 deletions test/exportStyleSheet.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ describe("exportStylesheet option", () => {
exportStyleSheet: true,
},
{
devtool: "source-map",
target: "web",
output: {
path: path.resolve(__dirname, "./outputs"),
Expand All @@ -135,7 +134,6 @@ describe("exportStylesheet option", () => {
modules: true,
},
{
devtool: "source-map",
target: "web",
output: {
path: path.resolve(__dirname, "./outputs"),
Expand All @@ -154,6 +152,31 @@ describe("exportStylesheet option", () => {
expect(getErrors(stats)).toMatchSnapshot("errors");
});

it("should work with CSS modules and the 'exportOnlyLocals' option", async () => {
const compiler = getCompiler(
"./modules/composes/composes-import-assertion-css.js",
{
exportStyleSheet: true,
modules: {
exportOnlyLocals: true,
},
},
{
target: "web",
output: {
path: path.resolve(__dirname, "./outputs"),
filename: "[name].bundle.js",
chunkFilename: "[name].chunk.js",
assetModuleFilename: "[name][ext]",
},
}
);
const stats = await compile(compiler);

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

it("should work and export CSSStyleSheet for import assertion and basic output", async () => {
const compiler = getCompiler(
"./basic-import-assertion-css-and-standard.js",
Expand Down Expand Up @@ -201,6 +224,31 @@ describe("exportStylesheet option", () => {
expect(getErrors(stats)).toMatchSnapshot("errors");
});

// TODO https://bugs.chromium.org/p/chromium/issues/detail?id=1174094&q=CSSStyleSheet%20source%20maps&can=2
it("should work with source maps", async () => {
const compiler = getCompiler(
"./basic-import-assertion-css.js",
{
exportStyleSheet: true,
},
{
devtool: "source-map",
target: "web",
output: {
path: path.resolve(__dirname, "./outputs"),
filename: "[name].bundle.js",
chunkFilename: "[name].chunk.js",
assetModuleFilename: "[name][ext]",
},
}
);
const stats = await compile(compiler);

expect(getModuleSource("./basic.css", stats)).toMatchSnapshot("module");
expect(getWarnings(stats)).toMatchSnapshot("warnings");
expect(getErrors(stats)).toMatchSnapshot("errors");
});

it('should work with "sass-loader"', async () => {
const compiler = getCompiler(
"./scss/import-assertion-css.js",
Expand Down Expand Up @@ -246,15 +294,16 @@ describe("exportStylesheet option", () => {
expect(getErrors(stats)).toMatchSnapshot("errors");
});

// TODO https://bugs.chromium.org/p/chromium/issues/detail?id=1174094&q=CSSStyleSheet%20source%20maps&can=2
it("should work with source maps", async () => {
it("should throw an error for CSS modules and disable named export", async () => {
const compiler = getCompiler(
"./basic-import-assertion-css.js",
"./modules/composes/composes-import-assertion-css.js",
{
exportStyleSheet: true,
modules: {
namedExport: false,
},
},
{
devtool: "source-map",
target: "web",
output: {
path: path.resolve(__dirname, "./outputs"),
Expand All @@ -266,7 +315,6 @@ describe("exportStylesheet option", () => {
);
const stats = await compile(compiler);

expect(getModuleSource("./basic.css", stats)).toMatchSnapshot("module");
expect(getWarnings(stats)).toMatchSnapshot("warnings");
expect(getErrors(stats)).toMatchSnapshot("errors");
});
Expand Down

0 comments on commit f0c1f1b

Please sign in to comment.