Skip to content

Commit 3a59f5b

Browse files
committed
Merge pull request #32 from sapegin/extraextension
Add ability to attach the hook to additional extensions
2 parents cee8c2c + 2b03e31 commit 3a59f5b

File tree

6 files changed

+34
-3
lines changed

6 files changed

+34
-3
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ $ npm i css-modules-require-hook
3737
* `string` **rootDir** — absolute path to the project directory. Providing this will result in better generated class names. It can be obligatory, if you run require hook and build tools (like [css-modulesify](https://github.com/css-modules/css-modulesify)) from different working directories.
3838
* `string` **to** — provides `to` option to the [LazyResult instance](https://github.com/postcss/postcss/blob/master/docs/api.md#processorprocesscss-opts).
3939
* `array` **use** — custom subset of postcss plugins.
40+
* `array` **extensions** — attach the hook to additional file extensions (for example `['.scss']`).
4041

4142
### Examples
4243

src/hook.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
/**
22
* @param {function} compile
3+
* @param {string} extension
34
*/
4-
export default function attachHook(compile) {
5-
require.extensions['.css'] = function hook(m, filename) {
5+
export default function attachHook(compile, extension) {
6+
require.extensions[extension] = function hook(m, filename) {
67
const tokens = compile(filename);
78
return m._compile('module.exports = ' + JSON.stringify(tokens), filename);
89
};

src/index.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ let rootDir = process.cwd();
3232
* @param {string} opts.rootDir
3333
* @param {string} opts.to
3434
* @param {array} opts.use
35+
* @param {array} opts.extensions
3536
*/
3637
export default function setup(opts = {}) {
3738
// clearing cache
@@ -65,6 +66,13 @@ export default function setup(opts = {}) {
6566
plugins.push(generateScopedName
6667
? new Scope({generateScopedName: opts.generateScopedName})
6768
: Scope);
69+
70+
const extraExtensions = get('extensions', null, 'array', opts);
71+
if (extraExtensions) {
72+
extraExtensions.forEach((extension) => {
73+
hook(filename => fetch(filename, filename), extension);
74+
});
75+
}
6876
}
6977

7078
/**
@@ -106,4 +114,4 @@ function fetch(_to, _from, _trace) {
106114
return tokens;
107115
}
108116

109-
hook(filename => fetch(filename, filename));
117+
hook(filename => fetch(filename, filename), '.css');

test/common-test-cases.js

+13
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,19 @@ describe('common-test-cases', () => {
219219
});
220220
});
221221

222+
describe('extra extension', () => {
223+
before(() => {
224+
expectedTokens = JSON.parse(readFileSync(resolve('test/test-cases/extra-extension/expected.json'), 'utf8'));
225+
hook({extensions: ['.scss']})
226+
});
227+
228+
it('require-hook', () => {
229+
const tokens = require(resolve('test/test-cases/extra-extension/source.scss'));
230+
equal(JSON.stringify(tokens), JSON.stringify(expectedTokens));
231+
});
232+
233+
});
234+
222235
});
223236

224237
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"localName": "_test_test_cases_extra_extension_source__localName"
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
$color: orange;
2+
3+
.localName {
4+
color: $color;
5+
}

0 commit comments

Comments
 (0)