Skip to content

fix(emit): Include class names that compose other classes #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 7, 2019
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: 9 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
15 changes: 15 additions & 0 deletions test/emit-declaration/__snapshots__/emit-declaration.test.js.snap
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
3 changes: 3 additions & 0 deletions test/emit-declaration/common.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.baseClass {
position: relative;
}
13 changes: 1 addition & 12 deletions test/emit-declaration/emit-declaration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like the fix has broken this test.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just switched that one to a snapshot instead of an inline snapshot. I’m not sure why it was inline before vs. the external snapshot for the other case.

});

5 changes: 5 additions & 0 deletions test/emit-declaration/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,8 @@
.otherClass {
display: block;
}

.composedClass {
composes: baseClass from './common.css';
color: cornflowerblue;
}