babel-plugin-react-css-modules icon indicating copy to clipboard operation
babel-plugin-react-css-modules copied to clipboard

Make babel-plugin-react-css-modules work with commonjs require

Open sashako opened this issue 8 years ago • 6 comments

Hello,

when requiring scss files using require() syntax I get Cannot use styleName attribute without importing at least one stylesheet.. However if I switch to using import it works (minus an eslint parsing error).

I'm trying to introduce css-modules into an old code base. I have the es6 babel preset running (that's why imports work), but code is linted without modules support and I would love to keep it that way for the time being. Is there anyway to make this plugin work with require?

sashako avatar Jan 08 '17 22:01 sashako

I'm trying to introduce css-modules into an old code base. I have the es6 babel preset running (that's why imports work), but code is linted without modules support and I would love to keep it that way for the time being. Is there anyway to make this plugin work with require?

It is possible. However, there hasn't been any demand for it.

Provided that babel-plugin-react-css-modules itself is somewhat bleeding-edge solution, I cannot see many people use it in combination with commonjs.

I can leave this issue open to see if more people thumbs up the feature.

gajus avatar Jan 15 '17 17:01 gajus

@gajus per my note on #1 about my issues with 'styles' scope, would it be possible if this plugin instead of looking for that specific import and referencing it, looked for any variable 'in scope' at render() named 'styles' or 'this.styles'? Then the plugin shouldn't care how styles was created.

aikar avatar Jan 29 '17 04:01 aikar

Note that ES2015 modules are not yet standard (in spite of its name) and possibly breaking by future spec changes. That's why I hesitate using import in my brand new codes.

hakatashi avatar Jun 25 '17 08:06 hakatashi

@gajus Could you point me in the direction of how to get this working? I'm afraid switching away from commonjs may be more work.

WesleyKapow avatar Jan 06 '18 00:01 WesleyKapow

This problem is quite important with TypeScript setups where TypeScript transpiles imports to requires (Named imports are undefined)

Sure I could. try to make a quite complex full-babel setup (and drop ts-loader) with the plugins in correct order, but then I can't use presets as they are loaded before plugins, and it gets reallyyy complicated.

I'd be happy to try to help solving this particular issue, but I would need a maintainer or a contributor to tell me where I could start looking at. (cc @gajus)

martpie avatar Dec 29 '18 11:12 martpie

@martpie It's not hard:

  1. Here is the code processing the import statements:

https://github.com/gajus/babel-plugin-react-css-modules/blob/b3ae56b0b253f73368238c5bfc60e7659f1c995d/src/index.js#L149-L184

The function name ImportDeclaration tells babel what statements you are interested.

  1. Obviously you need to create a similar function to process require calls. For that, you need to know what statement types you want.

Here you can use https://astexplorer.net/ , type in code with require calls, it will display how babel parses the code (maybe you need to choose babel-eslint9 as parser for best result).

image

So maybe you can try CallExpression, and test its callee.type === 'Identifier' && callee.name === 'require'

  1. Copy the logic, tweak anything not work, write tests, done.

yume-chan avatar Dec 29 '18 16:12 yume-chan