Giter Site home page Giter Site logo

postcss-cli's Introduction

npm node tests cover chat

PostCSS CLI

PostCSS CLI is a command line interface for PostCSS

Install

npm i -D postcss postcss-cli

Usage

Usage:
  postcss [input.css] [OPTIONS] [-o|--output output.css] [--watch|-w]
  postcss <input.css>... [OPTIONS] --dir <output-directory> [--watch|-w]
  postcss <input-directory> [OPTIONS] --dir <output-directory> [--watch|-w]
  postcss <input-glob-pattern> [OPTIONS] --dir <output-directory> [--watch|-w]
  postcss <input.css>... [OPTIONS] --replace

Basic options:
  -o, --output   Output file                                            [string]
  -d, --dir      Output directory                                       [string]
  -r, --replace  Replace (overwrite) the input file                    [boolean]
  -m, --map      Create an external sourcemap
  --no-map       Disable the default inline sourcemaps
  -w, --watch    Watch files for changes and recompile as needed       [boolean]
  --verbose      Be verbose                                            [boolean]
  --env          A shortcut for setting NODE_ENV                        [string]

Options for use without a config file:
  -u, --use      List of postcss plugins to use                          [array]
  --parser       Custom postcss parser                                  [string]
  --stringifier  Custom postcss stringifier                             [string]
  --syntax       Custom postcss syntax                                  [string]

Options for use with --dir:
  --ext   Override the output file extension; for use with --dir        [string]
  --base  Mirror the directory structure relative to this path in the output
          directory, for use with --dir                                 [string]

Advanced options:
  --include-dotfiles  Enable glob to match files/dirs that begin with "."
                                                                       [boolean]
  --poll              Use polling for file watching. Can optionally pass polling
                      interval; default 100 ms
  --config            Set a custom directory to look for a config file  [string]

Options:
  --version   Show version number                                      [boolean]
  -h, --help  Show help                                                [boolean]

Examples:
  postcss input.css -o output.css                       Basic usage
  postcss src/**/*.css --base src --dir build           Glob Pattern & output
  cat input.css | postcss -u autoprefixer > output.css  Piping input & output

If no input files are passed, it reads from stdin. If neither -o, --dir, or
--replace is passed, it writes to stdout.

If there are multiple input files, the --dir or --replace option must be passed.

Input files may contain globs (e.g. src/**/*.css). If you pass an input
directory, it will process all files in the directory and any subdirectories,
respecting the glob pattern.

ℹ️ More details on custom parsers, stringifiers and syntaxes, can be found here.

If you need to pass options to your plugins, or have a long plugin chain, you'll want to use a configuration file.

postcss.config.js

module.exports = {
  parser: 'sugarss',
  plugins: [
    require('postcss-import')({ ...options }),
    require('postcss-url')({ url: 'copy', useHash: true }),
  ],
}

Note that you can not set the from or to options for postcss in the config file. They are set automatically based on the CLI arguments.

Context

For more advanced usage, it's recommended to use a function in postcss.config.js; this gives you access to the CLI context to dynamically apply options and plugins per file

Name Type Default Description
env {String} 'development' process.env.NODE_ENV
file {Object} dirname, basename, extname File
options {Object} map, parser, syntax, stringifier PostCSS Options

postcss.config.js

module.exports = (ctx) => ({
  map: ctx.options.map,
  parser: ctx.file.extname === '.sss' ? 'sugarss' : false,
  plugins: {
    'postcss-import': { root: ctx.file.dirname },
    cssnano: ctx.env === 'production' ? {} : false,
  },
})

⚠️ If you want to set options via CLI, it's mandatory to reference ctx.options in postcss.config.js

postcss input.sss -p sugarss -o output.css -m

postcss.config.js

module.exports = (ctx) => ({
  map: ctx.options.map,
  parser: ctx.options.parser,
  plugins: {
    'postcss-import': { root: ctx.file.dirname },
    cssnano: ctx.env === 'production' ? {} : false,
  },
})

postcss-cli's People

Contributors

1st8 avatar 8881 avatar ai avatar arekkas avatar bcomnes avatar bensapiens avatar bradlc avatar coliff avatar corysimmons avatar danny-andrews avatar drusellers avatar eemeli avatar ericirish avatar fnd avatar georgiyordanov avatar greengremlin avatar greenkeeper[bot] avatar keithamus avatar kentcdodds avatar ludofischer avatar malstoun avatar michael-ciniawsky avatar mkurz avatar pirxpilot avatar renovate[bot] avatar rnons avatar ryanzim avatar themoch avatar thinkverse avatar watilde avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

postcss-cli's Issues

glob pattern string required

Hey everyone. Have this issue on my windows system. This issue does not apper on nix - based systems.

I`ve installed postcss and postcss-cli, cause i need to run npm scripts from terminal.
So, my package.json looks like this http://joxi.ru/4AkvbE1fMzeaj2
U can see the error on screenshot higher.
When i remove this part "| postcss --use autoprefixer --autoprefixer.browsers 'last 2 versions'" - all is work fine, but without autoprefixier - so this is sad. Can u help me with that?

v3 release

Let's face it: I don't think we're going to make our self-imposed deadline of having v3 shipped by tonight. There are still some odds and ends to clean up, and I'm too tired to trust myself releasing a major version tonight.

How about if we release v3.0.0-beta ~Monday and allow people to test it? Then when I get off my crazy hours in about 2 weeks, we can release v3.0.0 proper.

@michael-ciniawsky @ai

Report a plugin version from CLI

I call postcss through CLI from PHP code and I use Autoprefix plugin. From command line there is a way postcss version to be reported:

postcss --version

But I want using postcss from CLI again to query which version of autoprefixer plugin it is going to be used, but likely there is no way.

Could you add such a feature - version reporting for plugins?

PostCSS does not create output directory

I'm using postcss-cli (and autoprefixer) with the command:

postcss --use autoprefixer src/css/*.css --dir dist/css --map --local-plugins

But it is failing with a ENOENT error as the output directory dist/css does not exist (it has been cleaned as part of the build). The input directory src/css/ does exist. Full error below:

{ [Error: ENOENT: no such file or directory, open 'dist/css/main.css'] errno: -2, code: 'ENOENT', syscall: 'open', path: 'dist/css/main.css' }

Is there a way I can get postcss to create this directory before it tries to write to it?

I'm using v2.5.1

Thanks

Input and --dir options do not support nested or sub directories

I would like to contribute and add this feature.

I would like to use NPM scripts for my build however postcss-cli is not up to scratch due to the sub-directory support missing.

I would expect the input file parameter to support paths as a directory like src and the output path to support the relative path to the src. So a file with a src path of src/a/b/c/d.css and an output parameter as --dir dest would output the file to dest/a/b/c/d.css.

This is not how postcss-cli is currently working. It is expecting a flat directory structure.

I propose an API change to add a --input-dir parameter and replace --dir with --output-dir. With the new parameters automatically supporting sub-directories. No need for glob patterns.

There could also be a --flatten switch parameter as an option to remove directories in the output.

Allow setting context via command line

As mentioned in #51.

Currently, we have --env to allow setting process.env.NODE_ENV for postcss.config.js. We also have command-line flags to override the syntax, parser, stringifier, & map options specified in the config.

We should have an additional command-line flag to allow passing other values via ctx to postcss.config.js.

My strawman proposal:

postcss --ctx {doIt: true, someKey: 'somevalue'}

This needs discussion.

postcss --watch generating empty files

Bug Information

  • Post-CSS CLI version: 2.6.0
  • Node version: 6.9.1
  • Npm version : 3.10.8

Description:
Postcss watch is run using npm run watch as follow:

"scripts": {
"build-css": "postcss --config postcss.json",
"watch": "npm run build-css -- --watch",
},

css files are generated when npm run watch runs the first time, however some times empty css files are generated randomly.

The issue is here related to another issue in babel babel/babel#4332 where chokidar is used as a watcher.

Make output optional

Adding false as an optional value to the output option: -o false would be great if you just want to parse CSS. The main reason for this is linting for me but I assume there might be other use cases where you use postcss for parsing.

A new 'official' fork calls for new maintainers

Intro

PostCSS is a tool for transforming styles with JS plugins. These plugins can lint your CSS, support variables and mixins, transpile future CSS syntax, inline images, and more.

Opportunity

Now that the CLI implementation of PostCSS has moved under the PostCSS organisation on GitHub and people talking about what postcss-cli should become I think the time is there to get a group of people together to maintain and potentially rewrite the code.

Let's get going

Interested in helping? Reply below with what you want to help with!

--map option

What is the optimum usage for the --map option?

We need to support inline maps and filename.css.map files.

Do we need to support writing the map file to a different filename? I would be inclined to think not.

CC: @ai @michael-ciniawsky

Something not right with --watch

I do following commands to compile from scss to css then apply autoprefixer

START /B node_modules\.bin\node-sass "src\scss\app.scss" "public\css\app1.css" -w -r --include-path="bower_components\foundation-sites\scss" --include-path="bower_components\motion-ui\src" --source-map=true

CALL ./node_modules/.bin/postcss public\css\app1.css -o public\css\app.css --use autoprefixer --autoprefixer.browser "last 2 version, ie >= 11" -w -m

node-sass works just fine, it picks up changes as expected and output is 100% times fine.

But 1 out of 4 times postcss generates following output

/*# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IiIsImZpbGUiOiJhcHAuY3NzIn0= */

Looks like postcss-cli watch is not waiting for file commit or something

Error: Cannot find module 'autprefixer'

Trying to run a basic command here. I npm installed autoprefixer and am trying to run: postcss -c options.json -o css/test.css test.css

{
  "use": [
    "autprefixer"
  ]
}

I can see autoprefixer in the node_modules folder and I have a test.css file in the root:

body {
    display: flex;
}

Config file doesn't seem to work as expected

Not sure what I'm doing wrong, I'm following the example in the README, PostCSS doesn't seem to respect the input key in the config file when running npm run postcss. However, it works if I hard-code the actual input file in the script definition in package.json.

Also, when I do npm run postcss, the command doesn't watch the file. It just exits.

postcss.config.json

{
  "use": [
    "autoprefixer",
    "postcss-import",
    "postcss-simple-vars",
    "postcss-extend",
    "postcss-nested",
    "postcss-mixins"
  ],
  "input": "static/css/src/base.css",
  "output": "static/css/base.min.css",
  "local-plugins": true,
  "watch": true,
  "autoprefixer": {
    "browsers": "> 5%"
  }
}

package.json

  ...
  "scripts": {
   "postcss": "postcss --config postcss.config.json"
  }

--watch Issues when Editing Nested Files

Hey folks! I've been running into a few issues with the --watch command not compiling when I save certain files. What's really odd about what I've observed is that it compiles files such as post/spec/foo.css but doesn't run on files such as post/blocks/bar.css. My file structure is set up as such:

package.json
options.js
/post/
----style.css (has imports)
----/blocks/
--------bar.css (imported)
----/spec/
--------foo.css (imported)
/css/

My NPM script looks like this:

postcss -u postcss-import -u postcss-mixins -u postcss-for -u postcss-nested -u autoprefixer -u postcss-simple-vars -u postcss-color-function -u cssnano --local-plugins -w -c options.js -d css post/*.css

And I'm including the following in options.js:

"postcss-import": {
    onImport: function(sources) {
        global.watchCSS(sources, this.from);
    }
},

Perhaps this is just the result of ignorance, but I'd appreciate any insight as to why my script wouldn't be running.

Thanks!

config file no longer works in v3.0.0-beta

I have the following command in package.json:

"scripts": {
    "css": "postcss -c .postcss.json"
  }

in .postcss.json:

{
  "use": ["stylefmt", "postcss-sorting"],
  "input": "src/**/*.css",
  "local-plugins": true,
  "replace": true,
  "stylefmt": {...},
  "postcss-sorting": {...}
}

It works well in v2 but upgrading to v3-beta throws:

Warning: No files passed, reading from stdin
Error: Did not receive any stdin

Don't use a spinner when waiting for changes

At https://github.com/postcss/postcss-cli/blob/develop/index.js#L105 we are using a spinner to display the message Waiting for file changes....

Often when developing, I will put the watch process in the background, and use the same terminal to do other tasks (i.e. git operations). With a spinner, there will be constant output, overwriting the command prompt. This makes for a poor UX.

I propose we simply console.warn this message (perhaps with chalk coloring).

Expose Options, File && ENV (Context)

module.exports = ({ options, file, env }) => {
   console.log(env)     // process.env.NODE_ENV
   console.log(file)    // file.basename, file.dirname, file.extname (vinyl-like)
   console.log(options) // {...custom} 
}

I will update later, just a quick note for now :)

Watch config file

Currently when the -w & -c options are specified, postcss rebuilds the css files only when the input file changes (and does not reload the config file). If you change the config file, you need to cancel the process and re-run it.

This is irritating for me since I call postcss from an npm script that starts a dev server, my browser, etc. This script takes a bit of time to start or cancel, so I waste a lot of time when changing the config file. ⌛

I think that when both the the -w & -c options are specified, postcss should reload the config and rebuild the css when a change is detected in the config file.

I'm not sure if I can submit a PR or not. (I'm extra busy right now)

Logo

Can someone please enable gh-pages for this repo so the CLI logo is getting displayed correctly?

Support the common config

From this discussion: postcss/postcss-load-config#18. PostCSS will have the common config file via postcss-load-config which will potentially be added to the core(or not). Either way, where the loader will be placed, I think it would be great if we could support it at here CLI as well, and also it means the update may make a breaking change.

For a while, we need to wait for new release of the loader to fix a few issues.

Allow to work without plugins

postcss-cli could be very useful to convert SugarSS to CSS or CSS to SugarSS.

But I got error:

postcss -p sugarss test.sss -o test.css
Usage: /home/ai/test/node_modules/.bin/postcss [--use|-u] plugin [--config|-c
config.json] [--output|-o output.css] [input.css]

…

Please specify at least one plugin name.

need nested output dir

I figure out that I can use glob expression in the 'input' field, but no corresponding nested folders generated in the output dir. Here is my configuration:

module.exports = {
  input: 'src/postcss/**/*.css',
  dir: 'dist/css',
  use: [
    'precss',
    'postcss-custom-properties',
    'postcss-calc',
    'autoprefixer',
    'cssnano'
  ],
  map: 'file',
  'local-plugins': true,
  watch: true,
  autoprefixer: {
    browsers: ['Android >= 4', 'iOS >= 7', 'Chrome >= 10', 'Firefox >= 10', 'IE >= 8']
  }
}

Custom file extension for dir

I've got a postcss-cli setup where i want all *.css-files to be minified (using cssnano) into filename-as-it-is.min.css in the same directory without having to specify each file and the according output file on its own, because of the huge amount of files. Is there already a solution i missed out? Otherwise is it possible to include a cli-argument/option to set the file extension, like --file-extension .min.css or similar?

By the way, this is the command i run node_modules/.bin/postcss -u cssnano --dir Web/Resources/Public/Styles/ Web/Resources/Public/Styles/*.css

With kind regards.

Drop -i for v3

Unless someone objects, I'd like to remove the little-known -i option. It was simply an alternate way to specify the input files, and was never documented.

CC: @ai

Can't pass options to local plugin

Today I encountered a problem with the usage of both, the CLI and the config option.

Say I have a postcss plugin foo stored in plugins (so the path to file is ./plugins/foo.js).
Assume, that my plugin expects an option called bar.

On CLI I would invoke the CLI this way: node_modules/.bin/postcss --use './plugins/foo' --./plugins/foo.bar "baz".

After looking into why my plugin didn't received any options, I encountered here, that name contains the leading dot, whereas in argv it was stripped away. Hence the check always fails.

I couldn't reproduce the behaviour with yargs (v4.8.1 as used in my project), so I assume postcss-cli does transform it somewhere.

If I put the options into a JSON file as described here it still not works for the same reason.

So either the leading dot must not be stripped away or the check should ignore it if it looks for presence in argv.

postcss-cli do not copy folder structure from input files when pattern is used

Reopened from postcss/postcss#958

I am trying to compile multiple *.pcss files and have the *.css output sit in the same directory. For example, I have the following structure:

src
-- test1
---- 1.pcss
-- test2
---- 2.pcss
-- test3
---- 3.pcss

When running:

postcss --use precss src/**/*.pcss --dir out/

I expect:

src
-- test1
---- 1.pcss
-- test2
---- 2.pcss
-- test3
---- 3.pcss
out
-- test1
---- 1.css
-- test2
---- 2.css
-- test3
---- 3.css

But what I got is:

src
-- test1
---- 1.pcss
-- test2
---- 2.pcss
-- test3
---- 3.pcss
out
-- 1.pcss
-- 2.pcss
-- 3.pcss

So there are 2 issues here:

  1. The output doesn't have the same folder structure as the input when used with pattern.
  2. I don't seem to be able to specify the output file extension when using --dir.

Any suggestion please?

By the way, it would be great if such options are also available in a config file as that's where I actually configure the options :)

Watch triggering only once

I'm probably missing something trivial, but I can't get watch to work. I'm using:

./node_modules/.bin/postcss -u precss -w -o static/out.css css/base.css

If I make a change to base.css, it is reflected in out.css, but after that, no other changes will trigger the rebuild. If I ctrl-c the postcss process, and start again, it works again for 1 modification. No output from the postcss process.

    "postcss": "^5.0.19",
    "postcss-cli": "^2.5.1",
    "precss": "^1.4.0"

npm version 3.8.6
node version 5.10.1
distro is arch linux

--ext option

Refs: #78

We need an --ext option to allow setting the file extension when using --dir.

Edit: Just realized this will also solve the problem of people wanting to output to .min.css.


Should we rename .pcss to .css automatically? I think this is a bit too magical.

CC: @michael-ciniawsky @ai

--watch mode doesn't always work correctly

I have the following postcss config:

{
    "use": [
        "autoprefixer",
        "postcss-import",
        "postcss-simple-vars",
        "postcss-extend",
        "postcss-nested",
        "postcss-mixins",
        "cssnano"
    ],
    "input": "src/scss/*.css",
    "dir": "public/assets/css/",
    "local-plugins": true,
    "watch": true,
    "map": "file",
    "parser" : "postcss-scss",
    "autoprefixer": {
        "browsers": [ "Explorer >= 11", "Edge >= 13", "Firefox ESR", "Opera 37", "safari >= 9", "Chrome >= 50", "ChromeAndroid >= 47", "last 3 iOS versions" ]
    },
    "cssnano": {
        "safe": true,
        "mergeRules": false,
        "browsers": [ "Explorer >= 11", "Edge >= 13", "Firefox ESR", "Opera 37", "safari >= 9", "Chrome >= 50", "ChromeAndroid >= 47", "last 3 iOS versions" ]
    }
}

When I run it the files only get compiled once. When I change it from dir to one css file it works without problems.

"postcss-cli": "^2.6.0",
Window10

Behavior change in --use option in beta

In moving from 2.6.0 to 3.0.0-beta we found that this old invocation:

postcss --use autoprefixer css/*.css -d dist/

Was bombing out with:

PluginError: Cannot find module 'css/app.css'

Evidently, the first file argument "css/app.css" was being interpreted as an argument to --use and postcss-cli was trying to load it as a plug-in module.

Worked around by switching argument order to:

postcss --use autoprefixer -d dist/ css/*.css

So, we have a workaround for this, but this might be considered a regression so I thought you might like to know about it.

General Discussion about Project Direction

For those of you that don't know, @ben-eb added me as a collaborator here. I would like to move the project forward towards the v3 major release.

My primary goals for v3 are:

@watilde started with a refactor of the entire codebase in #46. @michael-ciniawsky has created a proof-of-concept cli supporting the common config; rewriting from scratch. https://github.com/michael-ciniawsky/postcssrc-cli.

postcssrc-cli's code seems cleaner, it already supports the common config, and I like the interface better. It still needs a few bugs fixed, needs to add support for the --use option, and needs tests; but I think those could be implemented fairly easily.

I don't know if this is a wise idea or not, but I'm thinking perhaps we should just move postcssrc-cli into this repo (or somehow) and make an improved version of postcssrc-cli the "official" cli. Would like to know everyone's thoughts on this.

Attn: @ai @pirxpilot

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.