Skip to content
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

Export names from global class or id #21

Merged
merged 4 commits into from
Feb 26, 2020
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
7 changes: 7 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ const processor = postcss.plugin('postcss-modules-scope', function(options) {
(options && options.generateScopedName) || processor.generateScopedName;
const generateExportEntry =
(options && options.generateExportEntry) || processor.generateExportEntry;
const exportGlobals = options && options.exportGlobals;

const exports = Object.create(null);

Expand Down Expand Up @@ -173,6 +174,12 @@ const processor = postcss.plugin('postcss-modules-scope', function(options) {
node.each(traverseNode);
break;
}
case 'id':
case 'class':
if (exportGlobals) {
exports[node.value] = [node.value];
}
break;
}
return node;
}
Expand Down
22 changes: 22 additions & 0 deletions test/test-cases/export-global-class/expected.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
.exportName {
color: green;
}

.exportName:hover {
color: red;
}

@media screen {
body {
background: red;
}
}

._input__testLocal {
color: blue;
}

:export {
exportName: exportName;
testLocal: _input__testLocal;
}
3 changes: 3 additions & 0 deletions test/test-cases/export-global-class/options.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
exportGlobals: true,
};
17 changes: 17 additions & 0 deletions test/test-cases/export-global-class/source.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
.exportName {
color: green;
}

.exportName:hover {
color: red;
}

@media screen {
body {
background: red;
}
}

:local(.testLocal) {
color: blue;
}
23 changes: 23 additions & 0 deletions test/test-cases/export-global-id/expected.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#exportName {
color: green;
}

#exportName:hover {
color: red;
}

@media screen {
#exportName-2 {
background: red;
}
}

#_input__exportName-3 {
color: green;
}

:export {
exportName: exportName;
exportName-2: exportName-2;
exportName-3: _input__exportName-3;
}
3 changes: 3 additions & 0 deletions test/test-cases/export-global-id/options.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
exportGlobals: true,
};
17 changes: 17 additions & 0 deletions test/test-cases/export-global-id/source.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#exportName {
color: green;
}

#exportName:hover {
color: red;
}

@media screen {
#exportName-2 {
background: red;
}
}

:local(#exportName-3) {
color: green;
}