Skip to content
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
15 changes: 12 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import postcss from 'postcss';
import Tokenizer from 'css-selector-tokenizer';
import { relative, sep } from 'path';

let hasOwnProperty = Object.prototype.hasOwnProperty;

Expand Down Expand Up @@ -28,14 +29,22 @@ function getSingleLocalNamesForComposes(selectors) {
});
}

const processor = postcss.plugin('postcss-modules-scope', function(options) {
/**
* @param {object} options
* @param {function} options.generateScopedName
* @param {string} options.rootDir
* @return {function}
*/
const processor = postcss.plugin('postcss-modules-scope', function(options = {}) {
const rootDir = options.rootDir || process.cwd();

return (css) => {
let generateScopedName = options && options.generateScopedName || processor.generateScopedName;
let generateScopedName = options.generateScopedName || processor.generateScopedName;

let exports = {};

function exportScopedName(name) {
let scopedName = generateScopedName(name, css.source.input.from, css.source.input.css);
let scopedName = generateScopedName(name, sep + relative(rootDir, css.source.input.from), css.source.input.css);
exports[name] = exports[name] || [];
if(exports[name].indexOf(scopedName) < 0) {
exports[name].push(scopedName);
Expand Down
6 changes: 3 additions & 3 deletions test/test-cases.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ describe("test-cases", function() {
return generateScopedName(exportedName, normalizedPath);
}
};
if(fs.existsSync(path.join(testDir, testCase, "config.json"))) {
config = JSON.parse(fs.readFileSync(path.join(testDir, testCase, "config.json"), "utf-8"));
}
try {
config = require(path.join(testDir, testCase, "config"));
} catch(e) {}
if(fs.existsSync(path.join(testDir, testCase, "options.js"))) {
options = require(path.join(testDir, testCase, "options.js"));
}
Expand Down
5 changes: 5 additions & 0 deletions test/test-cases/options-rootDir/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
var path = require('path');

module.exports = {
from: path.join(__dirname, 'source.css')
}
11 changes: 11 additions & 0 deletions test/test-cases/options-rootDir/expected.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
._options_rootDir_source__exportName {
color: green;
}

._options_rootDir_source__exportName:hover {
color: red;
}

:export {
exportName: _options_rootDir_source__exportName;
}
5 changes: 5 additions & 0 deletions test/test-cases/options-rootDir/options.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
var path = require('path');

module.exports = {
rootDir: path.join(__dirname, '..')
};
7 changes: 7 additions & 0 deletions test/test-cases/options-rootDir/source.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
:local(.exportName) {
color: green;
}

:local(.exportName):hover {
color: red;
}