Skip to content

Commit

Permalink
Support removal of @value exports, fixes #27 (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
nickw444 authored and princed committed Jan 19, 2020
1 parent f5df7b0 commit 4e07cb5
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 6 deletions.
28 changes: 24 additions & 4 deletions README.md
Expand Up @@ -80,14 +80,34 @@ See [PostCSS] docs for other examples for your environment.

### Configuration params

#### fs `Object`
#### fs `Object`

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

#### resolve `Object`

[enhanced-resolve]'s configuration object, see there for possible options and defaults.
[enhanced-resolve]'s configuration object, see there for possible options and defaults.


#### noEmitExports `boolean`

When enabled @value rules/declarations will be removed from the emitted output

**Input:**
```css
@value myBrandColor blue;
@font-face {}

body { background: myBrandColor }
```

**Output:**
```css
@font-face {}

body { background: blue }
```

### calc() and @value

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

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

### Extracting values for programmatic use
This plugin provides to postcss a custom [messages](http://api.postcss.org/Result.html#messages) object with `type: 'values'`.
Expand Down
13 changes: 11 additions & 2 deletions index.js
Expand Up @@ -132,8 +132,15 @@ const walk = async (requiredDefinitions, walkFile, root, result) => {

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

const factory = ({ fs = nodeFs, resolve: options = {} } = {}) => async (root, rootResult) => {
const resolver = ResolverFactory.createResolver(Object.assign({ fileSystem: fs }, options));
const factory = ({
fs = nodeFs,
noEmitExports = false,
resolve: resolveOptions = {},
} = {}) => async (root, rootResult) => {
const resolver = ResolverFactory.createResolver(Object.assign(
{ fileSystem: fs },
resolveOptions,
));
const resolve = promisify(resolver.resolve, resolver);
const readFile = promisify(fs.readFile, fs);

Expand All @@ -160,6 +167,8 @@ const factory = ({ fs = nodeFs, resolve: options = {} } = {}) => async (root, ro
} else if (node.type === 'atrule' && node.name === 'media') {
// eslint-disable-next-line no-param-reassign
node.params = replaceValueSymbols(node.params, definitions);
} else if (noEmitExports && node.type === 'atrule' && node.name === 'value') {
node.remove();
}
});
};
Expand Down
8 changes: 8 additions & 0 deletions test.js
Expand Up @@ -26,6 +26,14 @@ test('should leave exports as is', async (t) => {
await run(t, '@value red blue;', '@value red blue;');
});

test('should leave other at rules alone if noEmitExports is true', async (t) => {
await run(t, '@font-face {}', '@font-face {}', { noEmitExports: true });
});

test('should remove exports if noEmitExports is true', async (t) => {
await run(t, '@value red blue;', '', { noEmitExports: true });
});

test('gives an error when there is no semicolon between lines', async (t) => {
const input = '@value red blue\n@value green yellow';
const processor = postcss([plugin]);
Expand Down

0 comments on commit 4e07cb5

Please sign in to comment.