Repository: tivac/modular-css
Pull request #673 (2019-09-21 14:41:38)

feat: option to suppress value export
## Description
Implements options to disable value export, as mentioned in  #672.

## Motivation and Context
<!--- Why is this change required? What problem does it solve? -->
<!--- If it fixes an open issue, please link to the issue here. -->

## How Has This Been Tested?
Added unit test for option.
<!--- Please describe in detail how you tested your changes. -->
<!--- Include details of your testing environment, and the tests you ran to -->
<!--- see how your change affects other areas of the code, etc. -->

## Types of changes
<!--- What types of changes does your code introduce? Put an `x` in all the boxes that apply: -->
- [ ] Bug fix (non-breaking change which fixes an issue)
- [x] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to change)

## Checklist:
<!--- Go over all the following points, and put an `x` in all the boxes that apply. -->
<!--- If you're unsure about any of these, don't hesitate to ask. We're here to help! -->
- [x] My code follows the code style of this project.
- [x] My change requires a change to the documentation.
- [x] I have updated the documentation accordingly.
- [x] I have read the **CONTRIBUTING** document.
- [x] I have added tests to cover my changes.
- [x] All new and existing tests passed.


codecov[bot]: # [Codecov](https://codecov.io/gh/tivac/modular-css/pull/673?src=pr&el=h1) Report
> Merging [#673](https://codecov.io/gh/tivac/modular-css/pull/673?src=pr&el=desc) into [master](https://codecov.io/gh/tivac/modular-css/commit/ac8ea565ed087d4119b2f83acac176325e9ffb33?src=pr&el=desc) will **not change** coverage.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/tivac/modular-css/pull/673/graphs/tree.svg?width=650&token=EIUJKcuTE8&height=150&src=pr)](https://codecov.io/gh/tivac/modular-css/pull/673?src=pr&el=tree)

```diff
@@           Coverage Diff           @@
##           master     #673   +/-   ##
=======================================
  Coverage   98.43%   98.43%           
=======================================
  Files          45       45           
  Lines        1152     1152           
  Branches      178      178           
=======================================
  Hits         1134     1134           
  Misses         15       15           
  Partials        3        3
```


| [Impacted Files](https://codecov.io/gh/tivac/modular-css/pull/673?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [packages/processor/processor.js](https://codecov.io/gh/tivac/modular-css/pull/673/diff?src=pr&el=tree#diff-cGFja2FnZXMvcHJvY2Vzc29yL3Byb2Nlc3Nvci5qcw==) | `100% <100%> (ø)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/tivac/modular-css/pull/673?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/tivac/modular-css/pull/673?src=pr&el=footer). Last update [ac8ea56...1cc4441](https://codecov.io/gh/tivac/modular-css/pull/673?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/packages/processor/README.md b/packages/processor/README.md
index 0be0a615..aafc8304 100644
--- a/packages/processor/README.md
+++ b/packages/processor/README.md
@@ -224,6 +224,18 @@ new Processor({
 */
 ```
 
+### `exportValues`
+
+Enable exporting `@value` identifiers.
+
+**Default**: `true`
+
+```js
+new Processor({
+    exportValues : false
+});
+```
+
 ## Properties
 
 ### `.files`
diff --git a/packages/processor/processor.js b/packages/processor/processor.js
index ade97857..e617a7d5 100644
--- a/packages/processor/processor.js
+++ b/packages/processor/processor.js
@@ -37,13 +37,14 @@ class Processor {
         const options = Object.assign(
             Object.create(null),
             {
-                cwd       : process.cwd(),
-                map       : false,
-                rewrite   : true,
-                verbose   : false,
-                resolvers : [],
-                postcss   : {},
-                dupewarn  : true,
+                cwd          : process.cwd(),
+                dupewarn     : true,
+                exportValues : true,
+                map          : false,
+                postcss      : {},
+                resolvers    : [],
+                rewrite      : true,
+                verbose      : false,
             },
             opts
         );
@@ -379,8 +380,11 @@ class Processor {
 
             file.exports = Object.assign(
                 Object.create(null),
-                // export @value entries
-                mapValues(file.values, ({ value }) => value),
+
+                // optionally export @value entries
+                this._options.exportValues ?
+                    mapValues(file.values, ({ value }) => value) :
+                    null,
 
                 // export classes
                 message(result, "classes"),


Test patch:
diff --git a/packages/processor/test/__snapshots__/options.test.js.snap b/packages/processor/test/__snapshots__/options.test.js.snap
index b3990381..d1246054 100644
--- a/packages/processor/test/__snapshots__/options.test.js.snap
+++ b/packages/processor/test/__snapshots__/options.test.js.snap
@@ -8,6 +8,23 @@ Object {
 }
 `;
 
+exports[`/processor.js options exportValues should not export @values by default 1`] = `
+Object {
+  "a": "#F00",
+  "b": Array [
+    "mc61ce7a4a_b",
+  ],
+}
+`;
+
+exports[`/processor.js options exportValues should not export @values when exportValues is false 1`] = `
+Object {
+  "b": Array [
+    "mc8df92ddf_b",
+  ],
+}
+`;
+
 exports[`/processor.js options lifecycle options after should run async postcss plugins 1`] = `
 "/* packages/processor/test/specimens/relative.css */
 .wooga {
diff --git a/packages/processor/test/options.test.js b/packages/processor/test/options.test.js
index fced8163..2bd5512c 100644
--- a/packages/processor/test/options.test.js
+++ b/packages/processor/test/options.test.js
@@ -146,6 +146,38 @@ describe("/processor.js", () => {
             });
         });
 
+        describe("exportValues", () => {
+            it("should export @values by default", async () => {
+                const processor = new Processor({});
+
+                const { exports } = await processor.string(
+                    "./exportValues.css",
+                    dedent(`
+                        @value a: #F00;
+                        .b {}
+                    `)
+                );
+
+                expect(exports).toMatchSnapshot();
+            });
+
+            it("should not export @values when exportValues is false", async () => {
+                const processor = new Processor({
+                    exportValues : false,
+                });
+
+                const { exports } = await processor.string(
+                    "./exportGlobals.css",
+                    dedent(`
+                        @value a: #F00;
+                        .b {}
+                    `)
+                );
+
+                expect(exports).toMatchSnapshot();
+            });
+        });
+
         describe("rewrite", () => {
             it("should rewrite url() references by default", async () => {
                 const processor = new Processor();