Skip to content

Commit

Permalink
allow async getJSON (adapted saveJSON)
Browse files Browse the repository at this point in the history
  • Loading branch information
flying-sheep committed Apr 30, 2017
1 parent e31566f commit baab0ce
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ postcss([
]);
```

`getJSON` may also return a `Promise`.

### Generating scoped names

By default, the plugin assumes that all the classes are local. You can change
Expand Down
21 changes: 9 additions & 12 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,14 @@ module.exports = postcss.plugin(PLUGIN_NAME, (opts = {}) => {
const loader = getLoader(opts, plugins);
const parser = new Parser(loader.fetch.bind(loader));

return new Promise((resolve, reject) => {
postcss([...plugins, parser.plugin])
.process(css, { from: inputFile })
.then(() => {
const out = loader.finalSource;
if (out) css.prepend(out);

getJSON(css.source.input.file, parser.exportTokens);

resolve();
}, reject);
});
return postcss([...plugins, parser.plugin])
.process(css, { from: inputFile })
.then(() => {
const out = loader.finalSource;
if (out) css.prepend(out);

// getJSON may return a thenable
return getJSON(css.source.input.file, parser.exportTokens);
});
};
});
9 changes: 7 additions & 2 deletions src/saveJSON.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { writeFileSync } from 'fs';
import { writeFile } from 'fs';

export default function saveJSON(cssFile, json) {
writeFileSync(`${ cssFile }.json`, JSON.stringify(json));
return new Promise((resolve, reject) => {
writeFile(
`${ cssFile }.json`,
JSON.stringify(json),
e => e ? reject(e) : resolve());
});
}

0 comments on commit baab0ce

Please sign in to comment.