From dc84df338865a552c46ca4f0fb0b7739c6ce604b Mon Sep 17 00:00:00 2001 From: Marton Lederer Date: Sat, 20 Feb 2021 12:52:15 +0100 Subject: [PATCH] feat: virtual css modules TODO: fix naming error --- src/index.ts | 20 ++++++++++++++++++++ test/index.js | 8 ++++++++ test/styles/example.module.css | 3 +++ test/styles/example.module.sass | 2 ++ test/tests/modules.ts | 4 ++++ 5 files changed, 37 insertions(+) create mode 100644 test/styles/example.module.css create mode 100644 test/styles/example.module.sass create mode 100644 test/tests/modules.ts diff --git a/src/index.ts b/src/index.ts index a567c26..e6908a0 100644 --- a/src/index.ts +++ b/src/index.ts @@ -106,10 +106,30 @@ const postCSSPlugin = ({ await writeFile(tmpFilePath, result.css); return { + namespace: isModule ? "postcss-module" : "file", path: tmpFilePath }; } ); + + // load css modules + build.onLoad( + { filter: /.*/, namespace: "postcss-module" }, + async (args) => { + const mod = modulesMap.find(({ path }) => path === args.path), + resolveDir = path.dirname(args.path); + + console.log(mod); + + return { + resolveDir, + contents: `import "${args.path.replace( + resolveDir, + "." + )}"; export default ${JSON.stringify(mod.map)};` + }; + } + ); } }); diff --git a/test/index.js b/test/index.js index 73304d3..297f39f 100644 --- a/test/index.js +++ b/test/index.js @@ -20,6 +20,14 @@ describe("PostCSS esbuild tests", () => { }) .catch(done); }); + it("Works with CSS modules", (done) => { + test("modules") + .then((res) => { + assert(res); + done(); + }) + .catch(done); + }); }); function test(test) { diff --git a/test/styles/example.module.css b/test/styles/example.module.css new file mode 100644 index 0000000..f900a7b --- /dev/null +++ b/test/styles/example.module.css @@ -0,0 +1,3 @@ +.TestModule { + align-items: center; +} diff --git a/test/styles/example.module.sass b/test/styles/example.module.sass new file mode 100644 index 0000000..a011e04 --- /dev/null +++ b/test/styles/example.module.sass @@ -0,0 +1,2 @@ +.TestModuleSass + display: flex \ No newline at end of file diff --git a/test/tests/modules.ts b/test/tests/modules.ts new file mode 100644 index 0000000..dd80d14 --- /dev/null +++ b/test/tests/modules.ts @@ -0,0 +1,4 @@ +import styles from "../styles/example.module.sass"; +import styles2 from "../styles/example.module.css"; + +console.log(styles, styles2);