Skip to content

parser -> resolve-imports plugin #85

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

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ node_modules/*

# added automatically by precommit-hook as defaults
.jshint*
yarn.lock
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
language: node_js
node_js:
- "4.3"
- "4"
- "6"
- "7"
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,29 @@ require('css-modules-require-hook/preset');
// const styles = require('./icon.css');
```


### Using with babel-node / ES6 Imports
You will need to create a `cmrh.conf.js` file within the directory as you are importing `css-modules-require-hook`.


```javascript
// server.js
import csshook from 'css-modules-require-hook/preset' // import hook before routes
import routes from '/shared/views/routes'

// create server, etc
```

```javascript
// cmrh.conf.js
module.exports = {
// Same scope name as in webpack build
generateScopedName: '[name]__[local]___[hash:base64:5]',
}
```



### Development mode

Usually, Node.js caches all the `require` calls by default. In order to invalidate cache for the purpose of development you should set the environment variable `NODE_ENV` to `development`. For example:
Expand Down
9 changes: 5 additions & 4 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const Values = require('postcss-modules-values');
const LocalByDefault = require('postcss-modules-local-by-default');
const ExtractImports = require('postcss-modules-extract-imports');
const Scope = require('postcss-modules-scope');
const Parser = require('postcss-modules-parser');
const ResolveImports = require('postcss-modules-resolve-imports');

const debugFetch = require('debug')('css-modules:fetch');
const debugSetup = require('debug')('css-modules:setup');
Expand All @@ -42,6 +42,7 @@ module.exports = function setupHook({
debugSetup(arguments[0]);
validate(arguments[0]);

const exts = toArray(extensions);
const tokensByFile = {};

// debug option is preferred NODE_ENV === 'development'
Expand Down Expand Up @@ -71,8 +72,9 @@ module.exports = function setupHook({
? new ExtractImports({createImportedName})
: ExtractImports,
new Scope({generateScopedName: scopedName}),
new ResolveImports({resolve: {extensions: exts}}),
...append,
]).concat(new Parser({fetch})); // no pushing in order to avoid the possible mutations;
]);

// https://github.com/postcss/postcss#options
const runner = postcss(plugins);
Expand Down Expand Up @@ -105,7 +107,7 @@ module.exports = function setupHook({
// https://github.com/postcss/postcss/blob/master/docs/api.md#lazywarnings
lazyResult.warnings().forEach(message => console.warn(message.text));

tokens = lazyResult.root.tokens;
tokens = lazyResult.root.exports || {};

if (camelCase) {
tokens = assign(mapKeys(tokens, (value, key) => camelCaseFunc(key)), tokens);
Expand All @@ -129,7 +131,6 @@ module.exports = function setupHook({
return tokens;
};

const exts = toArray(extensions);
const isException = buildExceptionChecker(ignore);

// @todo add possibility to specify particular config for each extension
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "css-modules-require-hook",
"version": "4.0.5",
"version": "4.1.0-beta",
"description": "A require hook to compile CSS Modules on the fly",
"main": "lib/index.js",
"engines": {
Expand Down Expand Up @@ -47,7 +47,7 @@
"postcss": "^5.0.19",
"postcss-modules-extract-imports": "^1.0.0",
"postcss-modules-local-by-default": "^1.0.1",
"postcss-modules-parser": "^1.1.0",
"postcss-modules-resolve-imports": "^1.0.0",
"postcss-modules-scope": "^1.0.0",
"postcss-modules-values": "^1.1.1",
"seekout": "^1.0.1"
Expand Down