feat: virtual css modules

TODO: fix naming error
This commit is contained in:
Marton Lederer 2021-02-20 12:52:15 +01:00
parent 6066b58c3d
commit dc84df3388
No known key found for this signature in database
GPG Key ID: 9B7FD44832ADFE95
5 changed files with 37 additions and 0 deletions

View File

@ -106,10 +106,30 @@ const postCSSPlugin = ({
await writeFile(tmpFilePath, result.css); await writeFile(tmpFilePath, result.css);
return { return {
namespace: isModule ? "postcss-module" : "file",
path: tmpFilePath 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)};`
};
}
);
} }
}); });

View File

@ -20,6 +20,14 @@ describe("PostCSS esbuild tests", () => {
}) })
.catch(done); .catch(done);
}); });
it("Works with CSS modules", (done) => {
test("modules")
.then((res) => {
assert(res);
done();
})
.catch(done);
});
}); });
function test(test) { function test(test) {

View File

@ -0,0 +1,3 @@
.TestModule {
align-items: center;
}

View File

@ -0,0 +1,2 @@
.TestModuleSass
display: flex

4
test/tests/modules.ts Normal file
View File

@ -0,0 +1,4 @@
import styles from "../styles/example.module.sass";
import styles2 from "../styles/example.module.css";
console.log(styles, styles2);