fix: css modules invalid class mapping
This commit is contained in:
parent
dc84df3388
commit
ff270af566
64
src/index.ts
64
src/index.ts
@ -1,6 +1,7 @@
|
|||||||
import { Plugin } from "esbuild";
|
import { Plugin } from "esbuild";
|
||||||
import { Plugin as PostCSSPlugin } from "postcss";
|
import { Plugin as PostCSSPlugin } from "postcss";
|
||||||
import { ensureDir, readFile, writeFile } from "fs-extra";
|
import { ensureDir, readFile, writeFile } from "fs-extra";
|
||||||
|
import { TextDecoder } from "util";
|
||||||
import {
|
import {
|
||||||
SassException,
|
SassException,
|
||||||
Result as SassResult,
|
Result as SassResult,
|
||||||
@ -12,7 +13,6 @@ import postcss from "postcss";
|
|||||||
import postcssModules from "postcss-modules";
|
import postcssModules from "postcss-modules";
|
||||||
import less from "less";
|
import less from "less";
|
||||||
import stylus from "stylus";
|
import stylus from "stylus";
|
||||||
import { TextDecoder } from "util";
|
|
||||||
|
|
||||||
interface PostCSSPluginOptions {
|
interface PostCSSPluginOptions {
|
||||||
plugins: PostCSSPlugin[];
|
plugins: PostCSSPlugin[];
|
||||||
@ -27,6 +27,11 @@ interface CSSModule {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface ModulePath {
|
||||||
|
originalPath: string;
|
||||||
|
temporaryPath: string;
|
||||||
|
}
|
||||||
|
|
||||||
const postCSSPlugin = ({
|
const postCSSPlugin = ({
|
||||||
plugins = [],
|
plugins = [],
|
||||||
modules = true,
|
modules = true,
|
||||||
@ -36,7 +41,33 @@ const postCSSPlugin = ({
|
|||||||
setup(build) {
|
setup(build) {
|
||||||
// get a temporary path where we can save compiled CSS
|
// get a temporary path where we can save compiled CSS
|
||||||
const tmpDirPath = tmp.dirSync().name,
|
const tmpDirPath = tmp.dirSync().name,
|
||||||
modulesMap: CSSModule[] = [];
|
modulesMap: CSSModule[] = [],
|
||||||
|
pathMap: ModulePath[] = [];
|
||||||
|
|
||||||
|
// parse css modules with postcss-modules
|
||||||
|
if (modules !== false) {
|
||||||
|
plugins.unshift(
|
||||||
|
postcssModules({
|
||||||
|
...(typeof modules !== "boolean" ? modules : {}),
|
||||||
|
getJSON(filepath, json, outpath) {
|
||||||
|
const tmpFilePath = pathMap.find(
|
||||||
|
({ originalPath }) => originalPath === filepath
|
||||||
|
).temporaryPath;
|
||||||
|
|
||||||
|
modulesMap.push({
|
||||||
|
path: tmpFilePath,
|
||||||
|
map: json
|
||||||
|
});
|
||||||
|
|
||||||
|
if (
|
||||||
|
typeof modules !== "boolean" &&
|
||||||
|
typeof modules.getJSON === "function"
|
||||||
|
)
|
||||||
|
return modules.getJSON(filepath, json, outpath);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
build.onResolve(
|
build.onResolve(
|
||||||
{ filter: /.\.(css|sass|scss|less|styl)$/, namespace: "file" },
|
{ filter: /.\.(css|sass|scss|less|styl)$/, namespace: "file" },
|
||||||
@ -57,30 +88,15 @@ const postCSSPlugin = ({
|
|||||||
|
|
||||||
await ensureDir(tmpDir);
|
await ensureDir(tmpDir);
|
||||||
|
|
||||||
|
// add to path map so that postcss-modules can parse it after resolved
|
||||||
|
pathMap.push({
|
||||||
|
originalPath: sourceFullPath,
|
||||||
|
temporaryPath: tmpFilePath
|
||||||
|
});
|
||||||
|
|
||||||
const fileContent = await readFile(sourceFullPath);
|
const fileContent = await readFile(sourceFullPath);
|
||||||
let css = sourceExt === ".css" ? fileContent : "";
|
let css = sourceExt === ".css" ? fileContent : "";
|
||||||
|
|
||||||
// parse css modules with postcss-modules
|
|
||||||
if (modules !== false && isModule) {
|
|
||||||
plugins.unshift(
|
|
||||||
postcssModules({
|
|
||||||
...(typeof modules !== "boolean" ? modules : {}),
|
|
||||||
getJSON(filepath, json, outpath) {
|
|
||||||
modulesMap.push({
|
|
||||||
path: tmpFilePath,
|
|
||||||
map: json
|
|
||||||
});
|
|
||||||
|
|
||||||
if (
|
|
||||||
typeof modules !== "boolean" &&
|
|
||||||
typeof modules.getJSON === "function"
|
|
||||||
)
|
|
||||||
return modules.getJSON(filepath, json, outpath);
|
|
||||||
}
|
|
||||||
})
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
// parse files with preprocessors
|
// parse files with preprocessors
|
||||||
if (sourceExt === ".sass" || sourceExt === ".scss")
|
if (sourceExt === ".sass" || sourceExt === ".scss")
|
||||||
css = (await renderSass({ file: sourceFullPath })).css.toString();
|
css = (await renderSass({ file: sourceFullPath })).css.toString();
|
||||||
@ -119,8 +135,6 @@ const postCSSPlugin = ({
|
|||||||
const mod = modulesMap.find(({ path }) => path === args.path),
|
const mod = modulesMap.find(({ path }) => path === args.path),
|
||||||
resolveDir = path.dirname(args.path);
|
resolveDir = path.dirname(args.path);
|
||||||
|
|
||||||
console.log(mod);
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
resolveDir,
|
resolveDir,
|
||||||
contents: `import "${args.path.replace(
|
contents: `import "${args.path.replace(
|
||||||
|
|||||||
@ -1,3 +1,11 @@
|
|||||||
.TestModule {
|
.TestModule {
|
||||||
align-items: center;
|
align-items: center;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.TestModuleAnother {
|
||||||
|
justify-content: space-between;
|
||||||
|
}
|
||||||
|
|
||||||
|
.TextModuleLast {
|
||||||
|
width: 100vw;
|
||||||
|
}
|
||||||
|
|||||||
5
test/styles/example.module.less
Normal file
5
test/styles/example.module.less
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
@text: left;
|
||||||
|
|
||||||
|
.TestLessModule {
|
||||||
|
text-align: @text;
|
||||||
|
}
|
||||||
@ -1,4 +1,5 @@
|
|||||||
import styles from "../styles/example.module.sass";
|
import styles from "../styles/example.module.sass";
|
||||||
import styles2 from "../styles/example.module.css";
|
import styles2 from "../styles/example.module.css";
|
||||||
|
import styles3 from "../styles/example.module.less";
|
||||||
|
|
||||||
console.log(styles, styles2);
|
console.log(styles, styles2, styles3);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user