diff --git a/index.js b/index.js index 103442b..6f318ed 100644 --- a/index.js +++ b/index.js @@ -57,17 +57,23 @@ module.exports = function(content, ...rest) { const filename = this.resourcePath; const { mode = 'emit' } = loaderUtils.getOptions(this) || {}; if (!validModes.includes(mode)) { - return callback(new Error(`Invalid mode option: ${mode}`)); + return failed(new Error(`Invalid mode option: ${mode}`)); } const cssModuleInterfaceFilename = filenameToTypingsFilename(filename); const { read, write } = makeFileHandlers(cssModuleInterfaceFilename); - const keyRegex = /"([^\\"]+)":\s*"(?:[^\\"]+)",?$/gm; + const keyRegex = /"([^\\"]+)":/g; let match; const cssModuleKeys = []; - while ((match = keyRegex.exec(content))) { + const localExports = content.split('exports.locals')[1]; + + if (!localExports) { + return failed(new Error(`No exported locals found for ${filename}`)); + } + + while ((match = keyRegex.exec(localExports))) { if (cssModuleKeys.indexOf(match[1]) < 0) { cssModuleKeys.push(match[1]); } diff --git a/package.json b/package.json index 34ac319..60d908b 100644 --- a/package.json +++ b/package.json @@ -37,7 +37,7 @@ "css-loader": "^1.0.0", "cz-conventional-changelog": "^2.1.0", "husky": "^1.1.2", - "jest": "^23.6.0", + "jest": "^24.7.1", "memory-fs": "^0.4.1", "semantic-release": "^15.9.17", "travis-deploy-once": "^5.0.9", diff --git a/test/emit-declaration/__snapshots__/emit-declaration.test.js.snap b/test/emit-declaration/__snapshots__/emit-declaration.test.js.snap index 8df0751..e58bc9e 100644 --- a/test/emit-declaration/__snapshots__/emit-declaration.test.js.snap +++ b/test/emit-declaration/__snapshots__/emit-declaration.test.js.snap @@ -1,9 +1,24 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`Can emit valid declaration with sourceMap 1`] = ` +"// This file is automatically generated. +// Please do not change this file! +interface CssExports { + 'composedClass': string; + 'otherClass': string; + 'someClass': string; + 'validClass': string; +} +declare var cssExports: CssExports; +export = cssExports; +" +`; + exports[`Can emit valid declaration without sourceMaps 1`] = ` "// This file is automatically generated. // Please do not change this file! interface CssExports { + 'composedClass': string; 'otherClass': string; 'someClass': string; 'validClass': string; diff --git a/test/emit-declaration/common.css b/test/emit-declaration/common.css new file mode 100644 index 0000000..f3c306b --- /dev/null +++ b/test/emit-declaration/common.css @@ -0,0 +1,3 @@ +.baseClass { + position: relative; +} diff --git a/test/emit-declaration/emit-declaration.test.js b/test/emit-declaration/emit-declaration.test.js index 8bb7543..7006a94 100644 --- a/test/emit-declaration/emit-declaration.test.js +++ b/test/emit-declaration/emit-declaration.test.js @@ -20,17 +20,6 @@ test('Can emit valid declaration with sourceMap', async () => { 'utf-8' ); - expect(declaration).toMatchInlineSnapshot(` -"// This file is automatically generated. -// Please do not change this file! -interface CssExports { - 'otherClass': string; - 'someClass': string; - 'validClass': string; -} -declare var cssExports: CssExports; -export = cssExports; -" -`); + expect(declaration).toMatchSnapshot(); }); diff --git a/test/emit-declaration/index.css b/test/emit-declaration/index.css index 8bdba1f..352d143 100644 --- a/test/emit-declaration/index.css +++ b/test/emit-declaration/index.css @@ -9,3 +9,8 @@ .otherClass { display: block; } + +.composedClass { + composes: baseClass from './common.css'; + color: cornflowerblue; +}