Skip to content

Commit

Permalink
automatically css modules, closes #87
Browse files Browse the repository at this point in the history
  • Loading branch information
egoist committed Mar 18, 2018
1 parent 9de45f2 commit 707ca3e
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 2 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,13 @@ Default: `false`

Enable CSS modules or set options for `postcss-modules`.

### autoModules

Type: `boolean`<br>
Default: `true`

Automatically enable CSS modules for `.module.css` `.module.sss` `.module.scss` `.module.sass` `.module.styl` `.module.stylus` `.module.less` files.

### namedExports

Type: `boolean` `function`<br>
Expand Down
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ export default (options = {}) => {
/** CSS modules */
modules: inferOption(options.modules, false),
namedExports: options.namedExports,
/** Automatically CSS modules for .module.xxx files */
autoModules: options.autoModules,
/** Options for cssnano */
minimize: inferOption(options.minimize, false),
/** Postcss config file */
Expand Down
10 changes: 8 additions & 2 deletions src/postcss-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ function ensurePostCSSOption(option) {
return typeof option === 'string' ? importCwd(option) : option
}

function isModuleFile(file) {
return /\.module\.(css|styl|stylus|sass|scss|less|sss)$/.test(file)
}

export default {
name: 'postcss',
test: /\.(css|sss)$/,
Expand All @@ -62,7 +66,9 @@ export default {
const shouldInject = options.inject

const modulesExported = {}
if (options.modules) {
const autoModules = options.autoModules !== false && isModuleFile(this.id)
const supportModules = options.modules || autoModules
if (supportModules) {
plugins.push(
require('postcss-modules')({
// In tests
Expand Down Expand Up @@ -140,7 +146,7 @@ export default {
}
} else {
output += `var css = ${JSON.stringify(res.css)};\nexport default ${
options.modules ? JSON.stringify(modulesExported[this.id]) : 'css'
supportModules ? JSON.stringify(modulesExported[this.id]) : 'css'
};`
}
if (!shouldExtract && shouldInject) {
Expand Down
50 changes: 50 additions & 0 deletions test/__snapshots__/index.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,56 @@ console.log(css, css$1);
"
`;

exports[`modules auto-modules: 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 = \\".a-module_foo {\\\\n color: red;\\\\n}\\\\n\\";
var a = {\\"foo\\":\\"a-module_foo\\"};
styleInject(css);
var css$1 = \\".b-module_b {\\\\n color: red; }\\\\n\\";
var b = {\\"b\\":\\"b-module_b\\"};
styleInject(css$1);
var css$2 = \\".c-module_c {\\\\n color: red;\\\\n}\\\\n\\";
var c = {\\"c\\":\\"c-module_c\\"};
styleInject(css$2);
console.log(
a,
b,
c
);
"
`;

exports[`modules extract: css code 1`] = `
".style_foo {
color: red;
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/auto-modules/a.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.foo {
color: red;
}
3 changes: 3 additions & 0 deletions test/fixtures/auto-modules/b.module.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.b {
color: red;
}
3 changes: 3 additions & 0 deletions test/fixtures/auto-modules/c.module.less
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.c {
color: red;
}
9 changes: 9 additions & 0 deletions test/fixtures/auto-modules/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import a from './a.module.css'
import b from './b.module.scss'
import c from './c.module.less'

console.log(
a,
b,
c
)
4 changes: 4 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,10 @@ snapshotMany('modules', [
modules: true,
extract: true
}
},
{
title: 'auto-modules',
input: 'auto-modules/index.js'
}
])

Expand Down

0 comments on commit 707ca3e

Please sign in to comment.