Skip to content

Commit

Permalink
feat: add option to use output filepath for json (#460)
Browse files Browse the repository at this point in the history
Adds an option to postcss-modular-css to automatically use the same
output file path and file name for JSON with the configuration
`{ json: true }`. See #459.
  • Loading branch information
morewry authored and tivac committed Jul 25, 2018
1 parent 290a2f7 commit 037f933
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
8 changes: 7 additions & 1 deletion packages/postcss/postcss.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,19 @@ module.exports = postcss.plugin("modular-css", (opts) =>
})
.then((output) => {
var { json } = processor.options;

result.messages.push({
type : "modular-css-exports",
exports : classes,
});

if(json) {
if(typeof json !== "string") {
const { opts: { to } } = result;

json = `${path.join(path.dirname(to), path.basename(to, path.extname(to)))}.json`;
}

mkdirp.sync(path.dirname(json));

fs.writeFileSync(
Expand Down
23 changes: 23 additions & 0 deletions packages/postcss/test/__snapshots__/postcss.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,26 @@ exports[`/postcss.js should process CSS with dependencies and output the result
.tooga { border: 1px solid white; }
"
`;

exports[`/postcss.js should use output filepath for json if a custom path isn't provided 1`] = `
"{
\\"packages/postcss/test/specimens/folder/folder.css\\": {
\\"folder\\": \\"folder\\"
},
\\"packages/postcss/test/specimens/local.css\\": {
\\"folder\\": \\"white\\",
\\"one\\": \\"red\\",
\\"two\\": \\"blue\\",
\\"booga\\": \\"booga\\",
\\"looga\\": \\"booga looga\\"
},
\\"packages/postcss/test/specimens/start.css\\": {
\\"one\\": \\"red\\",
\\"two\\": \\"blue\\",
\\"folder\\": \\"white\\",
\\"wooga\\": \\"booga wooga\\",
\\"booga\\": \\"booga\\",
\\"tooga\\": \\"tooga\\"
}
}"
`;
9 changes: 9 additions & 0 deletions packages/postcss/test/postcss.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ describe("/postcss.js", () => {
)
.then(() => expect(read("classes.json")).toMatchSnapshot()));

it("should use output filepath for json if a custom path isn't provided", () => process(
"./packages/postcss/test/specimens/start.css",
{
json : true,
to : "./packages/postcss/test/output/start.css",
}
)
.then(() => expect(read("start.json")).toMatchSnapshot()));

it("should be usable like a normal postcss plugin", () => {
var processor = postcss([
plugin({
Expand Down

0 comments on commit 037f933

Please sign in to comment.