Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON filename should default to input (or output) filename #459

Closed
morewry opened this issue Jul 24, 2018 · 4 comments

Comments

Projects
None yet
2 participants
@morewry
Copy link
Contributor

commented Jul 24, 2018

I think that it should be possible to generate JSON data for CSS files without having to provide a specific name for the file.

If the json option is set to true, I think the JSON file should be generated with the same name as the input file. So this configuration with an input file input.css should generate input.json (or input.css.json).

{
  "postcss-modular-css": {
    "json": true
  }
}

Another, similar option that would similarly accomplish the desired result would be to emulate Webpack, which has a syntax for naming an output file the same name as input file, e.g. [name].js

{
  "postcss-modular-css": {
    "json": "[name].json"
  }
}

The use case that inspired this request is postcss-modular-css with postcss-cli.

There are two relevant examples in the documentation, one with a postcss configuration file (postcss --config postcss.json input.css) and one without (postcss --use modular-css/postcss input.css):

postcss.json
{
  "output" : "out.css",
  "postcss-modular-css": {
    "json" : "./path/to/output.json"
  }
}

But postcss-cli supports globs for input (and output directories for multiple files), e.g. postcss ./src/*.css -d ./dist/. So it would be nice if you could use a configuration like this with that command:

.postcssrc.json
{
  "plugins": {
    "postcss-modular-css": {
      "json": true
    }
  }
}
@morewry

This comment has been minimized.

Copy link
Contributor Author

commented Jul 24, 2018

Note that this currently works fine for processing multiple files, it just seems like it's not possible to get the JSON alongside.

So, currently, if I use this command:

postcss ./src/*.css -d ./dist/

With this configuration:

{
  "plugins": {
    "postcss-modular-css": true
  }
}

On a project like this:

|_ dist/
   (empty)
|_ src/ 
   |- default.css
   |- theme-a.css
   |- theme-b.css

I will get this output:

|_ dist/
   |- default.css
   |- theme-a.css
   |- theme-b.css
|_ src/ 
   |- default.css
   |- theme-a.css
   |- theme-b.css

With processed files in the dist folder. But I'd like to be able to get this:

|_ dist/
   |- default.css
   |- default.json
   |- theme-a.css
   |- theme-a.json
   |- theme-b.css
   |- theme-b.json
|_ src/ 
   |- default.css
   |- theme-a.css
   |- theme-b.css

With this configuration:

{
  "postcss-modular-css": {
    "json": true
  }
}

@morewry morewry changed the title JSON filename should default to input filename JSON filename should default to input (or output) filename Jul 24, 2018

@tivac

This comment has been minimized.

Copy link
Owner

commented Jul 24, 2018

Sounds reasonable to me! Rollup/Webpack handle this sort of thing natively but postcss doesn't provide much. I'd rather not get into supporting string replacements mostly because it's more code and seems less useful to me since it'll always just be one or two replacements.

So how about if json is truthy but not a string we'll use the options.to value provided by postcss as the base filename for the output. Should it always append .json or should it replace whatever the extension is with .json? The difference is small but I don't have a horse in this race so I appreciate any opinions!

morewry pushed a commit to morewry/modular-css that referenced this issue Jul 24, 2018

feat: add option to use output filepath for json
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 tivac#459.
@morewry

This comment has been minimized.

Copy link
Contributor Author

commented Jul 24, 2018

Yep, that sounds great.

As to the filename, I don't have strong feelings, but I slightly prefer the simpler output file name that would result by replacing the the options.to extension with .json.

Since you make it easy, I opened a PR. Feel free to merge or not as suits you :-)

morewry pushed a commit to morewry/modular-css that referenced this issue Jul 24, 2018

feat: add option to use output filepath for json
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 tivac#459.

tivac added a commit that referenced this issue Jul 25, 2018

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.
@tivac

This comment has been minimized.

Copy link
Owner

commented Jul 31, 2018

This was released in 14.2.0

@tivac tivac closed this Jul 31, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.