diff --git a/readme.md b/readme.md index 1b68d37..a6e0cfd 100644 --- a/readme.md +++ b/readme.md @@ -27,6 +27,8 @@ into: ## Options +`explicitExports` `boolean` + `resolve` `object` `resolve.alias` `object` diff --git a/sync.js b/sync.js index cf0dd93..254b306 100644 --- a/sync.js +++ b/sync.js @@ -18,10 +18,12 @@ module.exports = plugin('postcss-modules-resolve-imports', resolveImportsPlugin) /** * dangerouslyPrevailCyclicDepsWarnings + * explicitExports * resolve.alias * resolve.extensions + * resolve.modules */ -function resolveImportsPlugin({resolve = {}} = {}) { +function resolveImportsPlugin({explicitExports, resolve = {}} = {}) { return resolveImports; function resolveImports(ast, result) { @@ -32,6 +34,19 @@ function resolveImportsPlugin({resolve = {}} = {}) { resolveDeps(ast, {opts: {from: rootPath, graph, resolve, rootPath, rootTree}, processor}); + if (explicitExports) { + const exportRule = postcss.rule({selector: ':export'}); + + for (const className in ast.exports) + exportRule.append({ + prop: className, + value: ast.exports[className], + raws: {before: '\n '}, + }); + + rootTree.prepend(exportRule); + } + rootTree.exports = ast.exports; result.root = rootTree; } diff --git a/test/case/opts-explicit-exports/expected.css b/test/case/opts-explicit-exports/expected.css new file mode 100644 index 0000000..787dd4f --- /dev/null +++ b/test/case/opts-explicit-exports/expected.css @@ -0,0 +1,10 @@ +:export { + continueButton: _source_continueButton; + userInput: _source_userInput +} +._source_continueButton { + color: green +} +._source_userInput { + /**/ +} diff --git a/test/case/opts-explicit-exports/source.css b/test/case/opts-explicit-exports/source.css new file mode 100644 index 0000000..7ac2f49 --- /dev/null +++ b/test/case/opts-explicit-exports/source.css @@ -0,0 +1,9 @@ +.continueButton +{ + color: green; +} + +.userInput +{ + /**/ +} diff --git a/test/case/opts-explicit-exports/test.js b/test/case/opts-explicit-exports/test.js new file mode 100644 index 0000000..17f5b1a --- /dev/null +++ b/test/case/opts-explicit-exports/test.js @@ -0,0 +1,21 @@ +'use strict'; + +const resolveImports = require('../../../index'); +const setup = require('../../setup'); +const test = require('tape'); + +test('resolve-readme-example', t => { + const {expected, resulting, exports: tokens} = setup( + 'local-by-default', + 'extract-imports', + 'scope', + resolveImports({explicitExports: true}) + )(__dirname); + + t.equal(resulting.trim(), expected.trim()); + t.deepEqual(tokens, { + continueButton: '_source_continueButton', + userInput: '_source_userInput', + }); + t.end(); +});