-
-
Notifications
You must be signed in to change notification settings - Fork 220
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Wanted] Support CSS module #536
Comments
This would be an awesome built-in addition. Does anybody have a good workaround configuration for now? I can get It seems like most folks use rollup + @egoist's https://github.com/egoist/rollup-plugin-postcss for this. |
My solution in 2021, not sure if there is a better one currently, FYI👇export default defineConfig({
...,
esbuildPlugins: [
...esbuildPlugins,
{
name: "css-module",
setup(build) {
build.onResolve(
{ filter: /\.module\.css$/, namespace: "file" },
async (args) => {
return {
path: `${args.path}#css-module`,
namespace: "css-module",
pluginData: {
path: args.path,
},
};
},
);
build.onLoad(
{ filter: /#css-module$/, namespace: "css-module" },
async (args) => {
const { pluginData } = args;
const postcss = require("postcss");
const source = await fs.readFile(pluginData.path, "utf8");
let cssModule = {};
await postcss([
require("postcss-modules")({
getJSON(_, json) {
cssModule = json;
},
}),
]).process(source, { from: pluginData.path });
return {
contents: `
import "${pluginData.path}"
export default ${JSON.stringify(cssModule)}
`,
};
},
);
build.onResolve(
{ filter: /\.module\.css$/, namespace: "css-module" },
async (args) => {
return {
path: require.resolve(args.path, { paths: [args.resolveDir] }),
namespace: "file",
};
},
);
},
},
],
...,
}); |
This is my modified approach esbuildPlugins: [
{
name: "css-module",
setup(build): void {
build.onResolve(
{ filter: /\.module\.css$/, namespace: "file" },
(args) => ({
path: `${args.path}#css-module`,
namespace: "css-module",
pluginData: {
pathDir: path.join(args.resolveDir, args.path),
},
})
);
build.onLoad(
{ filter: /#css-module$/, namespace: "css-module" },
async (args) => {
const { pluginData } = args as {
pluginData: { pathDir: string };
};
const source = await fsPromises.readFile(
pluginData.pathDir,
"utf8"
);
let cssModule = {};
const result = await postcss([
postcssModules({
getJSON(_, json) {
cssModule = json;
},
}),
]).process(source, { from: pluginData.pathDir });
return {
pluginData: { css: result.css },
contents: `import "${
pluginData.pathDir
}"; export default ${JSON.stringify(cssModule)}`,
};
}
);
build.onResolve(
{ filter: /\.module\.css$/, namespace: "css-module" },
(args) => ({
path: path.join(args.resolveDir, args.path, "#css-module-data"),
namespace: "css-module",
pluginData: args.pluginData as { css: string },
})
);
build.onLoad(
{ filter: /#css-module-data$/, namespace: "css-module" },
(args) => ({
contents: (args.pluginData as { css: string }).css,
loader: "css",
})
);
},
},
], |
Any official support updates on this @egoist? CSS modules are pretty much the only way to bundle css safely when developing a component library because the only way to include regular CSS is to inject the styles which can easily clash (think of a button class). |
how about scss/sass ?? need some guide |
For anyone still having issues with this, I had success with this:
|
krailler Thanks, this was the only solution that worked for me. I've implemented it in a basic library using only tsup if anyone wants to look over it for help (I couldn't find a basic working example anywhere). |
@wilkinsonjack1993 using |
I have published this plugin based on discussion here. - https://github.com/mayank1513/esbuild-plugin-css-module Hope it helps! |
Hey y'all, esbuild has native css module support now, but it looks like tsup clobbers it by default, if you add
To your tsup config, this outputs css modules correctly. |
@figmatom do you have an example tsup.config.ts file you could share? |
@TheeMattOliver, fwiw, this is the tsup config in my "tsup": {
"format": [
"esm"
],
"loader": {
".css": "local-css"
},
"dts": true,
"sourcemap": true,
"clean": true
} This correctly imports the class names into the bundled .js. Before, I was getting (unminified) output like this in the bundled file for an imported CSS file: // src/components/InputButtons.css
var InputButtons_default = {}; But with the change above, I'm getting this: // src/components/InputButtons.css
var InputButtons_default = {
inputButton: "InputButtons_inputButton",
selected: "InputButtons_selected"
}; |
@fwextensions what is "local-css" in this instance? I'm getting It's probably an npm package? Thanks |
@paulm17 "tsup": "^5.10.1", to "tsup": "^8.0.1", |
I've tried all the options here. The problem is what @mdarche said last year.
That's the result of the build process. There's no css to latch onto. What I should be seeing is something like:
|
Finally got a working solution for me. As I said before there was no connection between the js and css. But I finally figured out a way. Using @krailler example.
I kept wondering why this was blank and found out that this is connection. Here is an example. Let's say I have the following classes:
The following code in the build script:
translates this to:
for the new index.css stylesheet in the dist folder. My new code appends to the cssModules array with the class name and new selector. Which results in:
I also found out the config params needed to be in a certain order and indeed v8.0.1 at a minimum needed to be used. Full code here:
All that's left, is to import the index.css and it all works. 😄 |
tsup now processes css pre-processes natively this is also resolves core yarn build --watch not refreshing css changes see egoist/tsup#536 (comment)
How can I make the js file to have an import statement for the css? I don't want to add css import in my consumer app. |
CSS modules work properly using local-css, but this makes tailwind classes stop working, because it tries to obfuscate them too, but the reference in the code is not obfuscated. Tried using :local and :global flags as mentioned in esbuild docs, but they are not being detected. Created another ticket for this here #1176 |
Upvote & Fund
The text was updated successfully, but these errors were encountered: