Skip to content

Commit

Permalink
Allow custom modules.getJSON function (#132)
Browse files Browse the repository at this point in the history
Hey~!

As of now, there's no way to access the JSON file of classnames to their outputs. It can only happen thru the [`getJSON` method of `postcss-modules`](https://github.com/css-modules/postcss-modules#saving-exported-classes), but it's overridden here.

This PR allows a user to define a custom `getJSON` method, passing it all arguments for them to do as they please. It does not affect the core behavior of the plugin.

---

_Also closes #101_
  • Loading branch information
lukeed authored and egoist committed Jan 22, 2019
1 parent 59fb0e9 commit 5faa7a4
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/postcss-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,11 @@ export default {
'[name]_[local]' :
'[name]_[local]__[hash:base64:5]',
...options.modules,
getJSON(filepath, json) {
getJSON(filepath, json, outpath) {
modulesExported[filepath] = json
if (typeof options.modules === 'object' && typeof options.modules.getJSON === 'function') {
return options.modules.getJSON(filepath, json, outpath)
}
}
})
)
Expand Down
38 changes: 38 additions & 0 deletions test/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,44 @@ console.log(style.foo);
"
`;

exports[`modules inject-object: js code 1`] = `
"'use strict';
function styleInject(css, ref) {
if ( ref === void 0 ) ref = {};
var insertAt = ref.insertAt;
if (!css || typeof document === 'undefined') { return; }
var head = document.head || document.getElementsByTagName('head')[0];
var style = document.createElement('style');
style.type = 'text/css';
if (insertAt === 'top') {
if (head.firstChild) {
head.insertBefore(style, head.firstChild);
} else {
head.appendChild(style);
}
} else {
head.appendChild(style);
}
if (style.styleSheet) {
style.styleSheet.cssText = css;
} else {
style.appendChild(document.createTextNode(css));
}
}
var css = \\".style_foo {\\\\n color: red;\\\\n}\\\\n\\";
var style = {\\"foo\\":\\"style_foo\\"};
styleInject(css);
console.log(style.foo);
"
`;

exports[`modules named-exports: js code 1`] = `
"'use strict';
Expand Down
11 changes: 11 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,17 @@ snapshotMany('modules', [
modules: true
}
},
{
title: 'inject-object',
input: 'css-modules/index.js',
options: {
modules: {
getJSON() {
//
}
}
}
},
{
title: 'named-exports',
input: 'named-exports/index.js',
Expand Down

0 comments on commit 5faa7a4

Please sign in to comment.