Skip to content

Commit 4e07cb5

Browse files
nickw444princed
authored andcommittedJan 19, 2020
Support removal of @value exports, fixes #27 (#28)
1 parent f5df7b0 commit 4e07cb5

File tree

3 files changed

+43
-6
lines changed

3 files changed

+43
-6
lines changed
 

‎README.md

+24-4
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,34 @@ See [PostCSS] docs for other examples for your environment.
8080

8181
### Configuration params
8282

83-
#### fs `Object`
83+
#### fs `Object`
8484

8585
File system to use. To make it faster in webpack pass its file system to plugin.
86-
Cached Node's file system is used by default.
86+
Cached Node's file system is used by default.
8787

8888
#### resolve `Object`
8989

90-
[enhanced-resolve]'s configuration object, see there for possible options and defaults.
90+
[enhanced-resolve]'s configuration object, see there for possible options and defaults.
91+
92+
93+
#### noEmitExports `boolean`
94+
95+
When enabled @value rules/declarations will be removed from the emitted output
96+
97+
**Input:**
98+
```css
99+
@value myBrandColor blue;
100+
@font-face {}
101+
102+
body { background: myBrandColor }
103+
```
104+
105+
**Output:**
106+
```css
107+
@font-face {}
108+
109+
body { background: blue }
110+
```
91111

92112
### calc() and @value
93113

@@ -154,7 +174,7 @@ and leads to export of following values to JS:
154174
### Other computations and @value
155175

156176
[postcss-calc] and [postcss-color-function] are known to work *inside* **@value** as they traverse media queries.
157-
Experience with other plugins may differ if they ignore media queries.
177+
Experience with other plugins may differ if they ignore media queries.
158178

159179
### Extracting values for programmatic use
160180
This plugin provides to postcss a custom [messages](http://api.postcss.org/Result.html#messages) object with `type: 'values'`.

‎index.js

+11-2
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,15 @@ const walk = async (requiredDefinitions, walkFile, root, result) => {
132132

133133
const walkerPlugin = postcss.plugin(INNER_PLUGIN, (fn, ...args) => fn.bind(null, ...args));
134134

135-
const factory = ({ fs = nodeFs, resolve: options = {} } = {}) => async (root, rootResult) => {
136-
const resolver = ResolverFactory.createResolver(Object.assign({ fileSystem: fs }, options));
135+
const factory = ({
136+
fs = nodeFs,
137+
noEmitExports = false,
138+
resolve: resolveOptions = {},
139+
} = {}) => async (root, rootResult) => {
140+
const resolver = ResolverFactory.createResolver(Object.assign(
141+
{ fileSystem: fs },
142+
resolveOptions,
143+
));
137144
const resolve = promisify(resolver.resolve, resolver);
138145
const readFile = promisify(fs.readFile, fs);
139146

@@ -160,6 +167,8 @@ const factory = ({ fs = nodeFs, resolve: options = {} } = {}) => async (root, ro
160167
} else if (node.type === 'atrule' && node.name === 'media') {
161168
// eslint-disable-next-line no-param-reassign
162169
node.params = replaceValueSymbols(node.params, definitions);
170+
} else if (noEmitExports && node.type === 'atrule' && node.name === 'value') {
171+
node.remove();
163172
}
164173
});
165174
};

‎test.js

+8
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ test('should leave exports as is', async (t) => {
2626
await run(t, '@value red blue;', '@value red blue;');
2727
});
2828

29+
test('should leave other at rules alone if noEmitExports is true', async (t) => {
30+
await run(t, '@font-face {}', '@font-face {}', { noEmitExports: true });
31+
});
32+
33+
test('should remove exports if noEmitExports is true', async (t) => {
34+
await run(t, '@value red blue;', '', { noEmitExports: true });
35+
});
36+
2937
test('gives an error when there is no semicolon between lines', async (t) => {
3038
const input = '@value red blue\n@value green yellow';
3139
const processor = postcss([plugin]);

0 commit comments

Comments
 (0)
Please sign in to comment.