From a93f99aa490c7a191ab45c04fa9d08ea88ca5eb1 Mon Sep 17 00:00:00 2001 From: Ben Hollis Date: Tue, 30 Apr 2019 00:09:12 -0700 Subject: [PATCH 1/4] fix(emit): Include class names that compose other classes --- index.js | 6 ++++-- package.json | 2 +- .../__snapshots__/emit-declaration.test.js.snap | 15 +++++++++++++++ test/emit-declaration/common.css | 3 +++ test/emit-declaration/emit-declaration.test.js | 13 +------------ test/emit-declaration/index.css | 5 +++++ 6 files changed, 29 insertions(+), 15 deletions(-) create mode 100644 test/emit-declaration/common.css diff --git a/index.js b/index.js index 103442b..b4a8ece 100644 --- a/index.js +++ b/index.js @@ -63,11 +63,13 @@ module.exports = function(content, ...rest) { 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.substring(Math.max(content.indexOf('exports.locals'), 0)) + + 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..1c5e0e0 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; +} \ No newline at end of file From 9d3efa2b72584621d49fd717afaf67ba3fc0c6e1 Mon Sep 17 00:00:00 2001 From: Mark Dalgleish Date: Tue, 7 May 2019 11:11:44 +1000 Subject: [PATCH 2/4] refactor: Add trailing newline --- test/emit-declaration/index.css | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/emit-declaration/index.css b/test/emit-declaration/index.css index 1c5e0e0..352d143 100644 --- a/test/emit-declaration/index.css +++ b/test/emit-declaration/index.css @@ -13,4 +13,4 @@ .composedClass { composes: baseClass from './common.css'; color: cornflowerblue; -} \ No newline at end of file +} From f74250fd214e37f84af24277579f810bbb73886d Mon Sep 17 00:00:00 2001 From: Mark Dalgleish Date: Tue, 7 May 2019 11:14:24 +1000 Subject: [PATCH 3/4] fix: Return error if exported locals are not found --- index.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index b4a8ece..5ce5aff 100644 --- a/index.js +++ b/index.js @@ -67,7 +67,11 @@ module.exports = function(content, ...rest) { let match; const cssModuleKeys = []; - const localExports = content.substring(Math.max(content.indexOf('exports.locals'), 0)) + 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) { From 229c985d68e8cd25cabfd3512fe79fbb69b73a28 Mon Sep 17 00:00:00 2001 From: Mark Dalgleish Date: Tue, 7 May 2019 11:14:40 +1000 Subject: [PATCH 4/4] =?UTF-8?q?fix:=20Use=20=E2=80=98failed=E2=80=99=20han?= =?UTF-8?q?dler=20for=20invalid=20modes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 5ce5aff..6f318ed 100644 --- a/index.js +++ b/index.js @@ -57,7 +57,7 @@ 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);