Skip to content
Merged
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
12 changes: 6 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import LocalByDefault from 'postcss-modules-local-by-default';
import Scope from 'postcss-modules-scope';
import Parser from './parser';

const defaultRoot = process.cwd();
const tokensByFile = {};
let plugins = [LocalByDefault, ExtractImports, Scope];
let rootDir;
let root = defaultRoot;

/**
* @param {string} sourceString The file content
Expand All @@ -28,8 +30,6 @@ function load(sourceString, sourcePath, trace, pathFetcher) {
}

hook(filename => {
const root = rootDir || dirname(filename);
const tokensByFile = {};
let importNr = 0;

const fetch = (_newPath, _relativeTo, _trace) => {
Expand Down Expand Up @@ -67,7 +67,7 @@ export default function configure(opts = {}) {
? customPlugins
: [LocalByDefault, ExtractImports, Scope];

if (opts.root && typeof opts.root === 'string') {
rootDir = opts.root;
}
root = opts.root && typeof opts.root === 'string'
? opts.root
: defaultRoot;
}
3 changes: 3 additions & 0 deletions test/cases/simple/expected.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"localName": "_test_cases_simple_source__localName"
}
4 changes: 4 additions & 0 deletions test/cases/simple/source.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.localName
{
color: #369;
}
17 changes: 17 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { equal } from 'assert';
import { readFileSync } from 'fs';
import { join } from 'path';
import hook from '../src';

describe('css-modules-require-hook', () => {
describe('without options', () => {
before(hook);

it('should return tokens', () => {
const expectedTokens = JSON.parse(readFileSync(join(__dirname, './cases/simple/expected.json'), 'utf8'));
const tokens = require('./cases/simple/source.css');

equal(JSON.stringify(expectedTokens), JSON.stringify(tokens));
});
});
});