Skip to content

Commit

Permalink
Refactor CSS loader core a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
madyankin committed Jun 23, 2020
1 parent 96c24ac commit fea101c
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 70 deletions.
17 changes: 7 additions & 10 deletions src/behaviours.js
@@ -1,22 +1,19 @@
import Core from "./css-loader-core";
import localByDefault from "postcss-modules-local-by-default";
import extractImports from "postcss-modules-extract-imports";
import modulesScope from "postcss-modules-scope";
import values from "postcss-modules-values";

export const behaviours = {
LOCAL: "local",
GLOBAL: "global",
};

export function getDefaultPlugins(behaviour, generateScopedName) {
const scope = Core.scope({ generateScopedName });
const scope = modulesScope({ generateScopedName });

const plugins = {
[behaviours.LOCAL]: [
Core.values,
Core.localByDefault,
Core.extractImports,
scope,
],

[behaviours.GLOBAL]: [Core.values, Core.extractImports, scope],
[behaviours.LOCAL]: [values, localByDefault, extractImports, scope],
[behaviours.GLOBAL]: [values, extractImports, scope],
};

return plugins[behaviour];
Expand Down
36 changes: 0 additions & 36 deletions src/css-loader-core/index.js

This file was deleted.

23 changes: 22 additions & 1 deletion src/css-loader-core/loader.js
@@ -1,9 +1,30 @@
// Copied from https://github.com/css-modules/css-modules-loader-core

import Core from "./index.js";
import postcss from "postcss";
import fs from "fs";
import path from "path";

import Parser from "./parser";

class Core {
constructor(plugins) {
this.plugins = plugins || Core.defaultPlugins;
}

load(sourceString, sourcePath, trace, pathFetcher) {
let parser = new Parser(pathFetcher, trace);

return postcss(this.plugins.concat([parser.plugin]))
.process(sourceString, { from: "/" + sourcePath })
.then((result) => {
return {
injectableSource: result.css,
exportTokens: parser.exportTokens,
};
});
}
}

// Sorts dependencies in the following way:
// AAA comes before AA and A
// AB comes after AA and before A
Expand Down
46 changes: 23 additions & 23 deletions test/test.js
Expand Up @@ -14,15 +14,15 @@ const cases = {
composes: "composes rules",
values: "processes values",
interpolated: "generates scoped name with interpolated string",
global: "allows to make CSS global"
global: "allows to make CSS global",
};

function generateScopedName(name, filename) {
const file = path.basename(filename, ".css").replace(/\./g, "_");
return `_${file}_${name}`;
}

Object.keys(cases).forEach(name => {
Object.keys(cases).forEach((name) => {
const description = cases[name];

const scopedNameGenerator =
Expand All @@ -46,8 +46,8 @@ Object.keys(cases).forEach(name => {
generateScopedName: scopedNameGenerator,
getJSON: (cssFile, json) => {
resultJson = json;
}
})
},
}),
];

const result = await postcss(plugins).process(source, { from: sourceFile });
Expand All @@ -65,7 +65,7 @@ it("saves JSON next to CSS by default", async () => {
if (fs.existsSync(jsonFile)) fs.unlinkSync(jsonFile);

await postcss([plugin({ generateScopedName })]).process(source, {
from: sourceFile
from: sourceFile,
});

const json = fs.readFileSync(jsonFile).toString();
Expand All @@ -81,11 +81,11 @@ it("processes globalModulePaths option", async () => {
const thePlugin = plugin({
generateScopedName,
globalModulePaths: [/globalModulePaths/],
getJSON: () => {}
getJSON: () => {},
});

const result = await postcss([thePlugin]).process(source, {
from: sourceFile
from: sourceFile,
});

expect(result.css).toMatchSnapshot("processes globalModulePaths option");
Expand All @@ -99,7 +99,7 @@ it("processes localsConvention with camelCase option", async () => {
if (fs.existsSync(jsonFile)) fs.unlinkSync(jsonFile);

await postcss([
plugin({ generateScopedName, localsConvention: "camelCase" })
plugin({ generateScopedName, localsConvention: "camelCase" }),
]).process(source, { from: sourceFile });

const json = fs.readFileSync(jsonFile).toString();
Expand All @@ -111,7 +111,7 @@ it("processes localsConvention with camelCase option", async () => {
"camel-case-extra": "_camelCase_camel-case-extra",
camelCaseExtra: "_camelCase_camel-case-extra",
FooBar: "_camelCase_FooBar",
fooBar: "_camelCase_FooBar"
fooBar: "_camelCase_FooBar",
});
});

Expand All @@ -123,7 +123,7 @@ it("processes localsConvention with camelCaseOnly option", async () => {
if (fs.existsSync(jsonFile)) fs.unlinkSync(jsonFile);

await postcss([
plugin({ generateScopedName, localsConvention: "camelCaseOnly" })
plugin({ generateScopedName, localsConvention: "camelCaseOnly" }),
]).process(source, { from: sourceFile });

const json = fs.readFileSync(jsonFile).toString();
Expand All @@ -132,7 +132,7 @@ it("processes localsConvention with camelCaseOnly option", async () => {
expect(JSON.parse(json)).toMatchObject({
camelCase: "_camelCase_camel-case",
camelCaseExtra: "_camelCase_camel-case-extra",
fooBar: "_camelCase_FooBar"
fooBar: "_camelCase_FooBar",
});
});

Expand All @@ -144,7 +144,7 @@ it("processes localsConvention with dashes option", async () => {
if (fs.existsSync(jsonFile)) fs.unlinkSync(jsonFile);

await postcss([
plugin({ generateScopedName, localsConvention: "dashes" })
plugin({ generateScopedName, localsConvention: "dashes" }),
]).process(source, { from: sourceFile });

const json = fs.readFileSync(jsonFile).toString();
Expand All @@ -155,7 +155,7 @@ it("processes localsConvention with dashes option", async () => {
camelCase: "_camelCase_camel-case",
"camel-case-extra": "_camelCase_camel-case-extra",
camelCaseExtra: "_camelCase_camel-case-extra",
FooBar: "_camelCase_FooBar"
FooBar: "_camelCase_FooBar",
});
});

Expand All @@ -167,7 +167,7 @@ it("processes localsConvention with dashes option", async () => {
if (fs.existsSync(jsonFile)) fs.unlinkSync(jsonFile);

await postcss([
plugin({ generateScopedName, localsConvention: "dashes" })
plugin({ generateScopedName, localsConvention: "dashes" }),
]).process(source, { from: sourceFile });

const json = fs.readFileSync(jsonFile).toString();
Expand All @@ -176,7 +176,7 @@ it("processes localsConvention with dashes option", async () => {
expect(JSON.parse(json)).toMatchObject({
camelCase: "_camelCase_camel-case",
camelCaseExtra: "_camelCase_camel-case-extra",
FooBar: "_camelCase_FooBar"
FooBar: "_camelCase_FooBar",
});
});

Expand All @@ -201,12 +201,12 @@ it("processes hashPrefix option", async () => {
it("different instances have different generateScopedName functions", async () => {
const one = plugin({
generateScopedName: () => "one",
getJSON: () => {}
getJSON: () => {},
});

const two = plugin({
generateScopedName: () => "two",
getJSON: () => {}
getJSON: () => {},
});

const css = ".foo {}";
Expand All @@ -233,13 +233,13 @@ it("getJSON with outputFileName", async () => {
getJSON: (cssFile, json, outputFileName) => {
jsonFileName = outputFileName.replace(".css", ".json");
resultJson = json;
}
})
},
}),
];

await postcss(plugins).process(source, {
from: sourceFile,
to: `${expectedFile}.css`
to: `${expectedFile}.css`,
});

expect(jsonFileName).toEqual(`${expectedFile}.json`);
Expand All @@ -253,12 +253,12 @@ it("exposes export tokens for other plugins", async () => {
const plugins = [
plugin({
generateScopedName,
getJSON: () => {}
})
getJSON: () => {},
}),
];

const result = await postcss(plugins).process(source, {
from: sourceFile
from: sourceFile,
});

expect(result.messages).toMatchSnapshot(
Expand Down

0 comments on commit fea101c

Please sign in to comment.