Skip to content
Permalink
Browse files

feat: add option to use output filepath for json (#460)

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 037f93346c9c9eb5290bc1c4c6aec066b4ee4965
@@ -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(
@@ -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\\"
}
}"
`;
@@ -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({

0 comments on commit 037f933

Please sign in to comment.
You can’t perform that action at this time.