-
Notifications
You must be signed in to change notification settings - Fork 45
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
Possibility to specify custom pattern for the classname #6
Comments
Thanks for reopening this issue here. How does the css modulde require hook currently generate the class names? Maybe I can configure webpack to generate the class names differently so that universal usage is possible.
We currently use React and webpack to compile JS, but Stylus for CSS. We have very strict naming conventions so that all styles are scoped by manually prefixing each class name with its unique component name. A gradual refactoring to use |
Sorry for the delay. Require-hook is pretty simple. It is a small wrapper for the PostCSS package and its plugins. PostCSS parses the source CSS file, then runs some transformations, implemented with the plugins. They are - postcss-modules-extract-imports, postcss-modules-local-by-default and postcss-modules-scope. After that it runs the private plugin to extract tokens from the CSS node tree, which you usually get from require function. By the way, I haven't checked it out, but looks like it is possible to specify the custom function for generating class names: https://github.com/css-modules/postcss-modules-scope/blob/master/src/index.js#L33 I still haven't checked how webpack generates class names, but in my opinion your request doesn't require much work. |
As a quick solution you can try to specify custom plugins for the require-hook with var ExtractImports = require('postcss-modules-extract-imports');
var LocalByDefault = require('postcss-modules-local-by-default');
var Scope = require('postcss-modules-scope');
var cssHook = require('css-modules-require-hook');
function generateScopedName(exportedName, path) {/* your code here */}
var scopeInstance = new Scope({generateScopedName: generateScopedName});
cssHook({
use: [
ExtractImports,
LocalByDefault,
scopeInstance
]
}); |
I'm making a small demo project to show a possible way of |
@sullenor I'm trying to figure out a good way to make use of this for a universal render with webpack. I tried your quick example above, but it doesn't work out of the box, and I'm not familiar with When Cheers |
@pocketjoso I'm not good with webpack itself :) In the latest version, I added another option I assume you should set up the same root directories for webpack and require hook. For the hook, you can use var cssHook = require('css-modules-require-hook');
cssHook({
generateScopedName: function (exportedName, path) { ... },
rootDir: process.cwd() // by default or you can change it manually
}); May be, if you are familiar to the Unfortunately, I don't know anymore. Hope that helps. |
Here is an example of |
@joeybaker Hey, Joey! Can you tell me if you have tried to use require hook with webpack? Please, share your experience :) |
Thanks for quick reply, but I'm still having exactly the same issue:
|
@pocketjoso What version do you use? I assume it should be 1.0.4. |
@sullenor Yes. |
@pocketjoso okay, I think you can it in another way: // hook itself
var cssHook = require('css-modules-require-hook');
// basic plugins
var ExtractImports = require('postcss-modules-extract-imports');
var LocalByDefault = require('postcss-modules-local-by-default');
var Scope = require('postcss-modules-scope');
// specify your custom function
function generateScopedName(exportedName, path) {/* your code here */}
// create an instance of the "postcss-modules-scope" plugin,
// so you can manually pass the options to it
var scopeInstance = new Scope({generateScopedName: generateScopedName});
cssHook({
// setting custom plugins list
use: [
ExtractImports,
LocalByDefault,
scopeInstance // using plugin instance
]
}); I added comments to the code, hope that helps. |
@sullenor I already tried that code, as I wrote in my earlier post. In |
@pocketjoso Have you tried to import any CSS file in runtime? I think, that should work: var hook = require('css-modules-require-hook');
hook({generateScopedName: function (c, p) { console.log(c, p); return c; }});
console.log(require('./test.css')); |
Nope, doesn't use the |
Can you share your example? Sounds odd to me |
Hmm so it breaks when I
So it breaks with |
I should say that in the file that |
@pocketjoso ah, i got it. Looks like you used import hook from 'css-modules-require-hook';
hook({generateScopedName: function (c, p) { console.log(c, p); return c; }});
import css from './test.css';
console.log(css); will compile to 'use strict';
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
var _cssModulesRequireHook = require('css-modules-require-hook');
var _cssModulesRequireHook2 = _interopRequireDefault(_cssModulesRequireHook);
var _testCss = require('./test.css');
var _testCss2 = _interopRequireDefault(_testCss);
(0, _cssModulesRequireHook2['default'])({ generateScopedName: function generateScopedName(c, p) {
console.log(c, p);return c;
} });
console.log(_testCss2['default']); So, all your imports will be called first and you'll setup hook after that. |
@pocketjoso from http://calculist.org/blog/2012/06/29/static-module-resolution/
Damn, I haven't thought about it. Looks like it ruins it totally :( |
@pocketjoso I guess, you have to write your entry point for the app in the old syntax (set up hooks and etc). Then, run the other code with modern syntax. Similar to babel: https://babeljs.io/docs/usage/require/ |
+1 I'm having the same issue. Would like to customize the classnames created by this hook to match those created by webpack. |
First of all thanks for this awesome hook, everything works fine, using |
By the way if I combine On the other hand if the only option is |
@canvaskisa Hi, I haven't worked with webpack a lot, but I managed to make a small demo, which can generate class names in the same way as webpack does. It looks a bit aweful, but it works :) Currently it works with |
@drinchev Hi, yeah. I think it looks a bit odd, but So, your set up should look like similar to this: var hook = require('css-modules-require-hook');
var Scope = require('postcss-modules-scope');
// specify your custom function
function generateScopedName(exportedName, path) {/* your code here */}
// create an instance of the "postcss-modules-scope" plugin,
// so you can manually pass the options to it
var scopeInstance = new Scope({generateScopedName: generateScopedName});
hook({
use: [
// custom
require('postcss-custom-selectors'),
// css-modules
require('postcss-modules-extract-imports'),
require('postcss-modules-local-by-default'),
scopeInstance
]
}); |
Good news, it is possible now to specify string pattern for the So, you can write now: var hook = require('css-modules-require-hook');
hook({
generateScopedName: '[name]__[local]___[hash:base64:5]'
}); Works with the latest version of the loader and 2.x version of the require hook. Hope that helps. |
Woot !!
|
Awesome! Thanks for working on this! |
If anyone stumbles here with a problem of hook generated classnames not matching the webpack css modules generated classnames -- use the 'rootDir' option. Works like a charm, my classnames are now matching. |
The text was updated successfully, but these errors were encountered: