Skip to content

Conversation

@moonglum
Copy link
Member

@moonglum moonglum commented Sep 28, 2018

This PR adds support for converting cssnext to CSS that works in the browsers you specified. It uses postcss-preset-env to do that.

Open Questions:

let filepath = path.relative(assetManager.referenceDir, input);
console.error(`compiling CSS ${repr(filepath)} for ${browsers.join(", ")}`);
plugins.push(postcssPresetEnv({
stage: 2, // maybe a little too unstable?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not familiar with the CSS process, but the JavaScript experience suggests that combining volatile (i.e. not-yet-finalized) features in a single package/declaration/whatever is bound to be troublesome:
https://babeljs.io/blog/2018/07/27/removing-babels-stage-presets
(TLDR: What's stage 2 today might be different next month, both in terms of the set of features and their respective contracts. Thus volatile features need to be versioned independently.)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm discussing this in the above linked issue. I'm not sure what is the best approach yet. But I'm not sure it makes sense to choose features by their stage 🤔

@moonglum
Copy link
Member Author

moonglum commented Oct 3, 2018

I'm thinking about a different approach: The user configures this plugin like this:

{
  source: "...",
  target: "...",
  cssnext: true,
  ignore: ["rem"],
  polyfill: ["custom-properties"]
}

With this configuration, we will throw an error whenever we encounter a feature that is not supported by one of the selected browsers (by your browserslist) unless it is ignored or polyfilled:

  • ignored features will just be ignored
  • polyfill features will be polyfilled with a prefix or a polyfill (one of this list of postcss plugins)

This can be accomplished by:

  • adding all polyfill CSS plugins chosen by the user as plugins to the pipeline
  • and as a last plugin add doiuse with the provided ignore list.

So this is pretty straightforward and will probably result in a pretty nice workflow: You just start with no configuration except cssnext: true. Whenever we add a feature that is not supported by one of our chosen browsers, our pipeline will throw an error. We have three choices (that should probably be presented to us on the CLI – powered by doiuse's onFeatureUsage):

  • remove the usage of the feature
  • ignore that feature
  • polyfill that feature

What do you think, @FND ?

@moonglum
Copy link
Member Author

moonglum commented Oct 3, 2018

I have a working prototype:

let postcss = require('postcss')
let doiuse = require('doiuse')
let fs = require('fs')
let postcssCustomProperties = require('postcss-custom-properties')

let plugins = [
        // first, do the import stuff (skipped in this prototype)
        // then, add all transforms chosen by the user
        postcssCustomProperties({
                preserve: false
        }),
        // finally, run doiuse with the provided browsers and ignored
        // features
        doiuse({
                browsers: ['ie >= 8'],
                ignore: ['rem'],
                onFeatureUsage: function(usageInfo) {
                        console.error(usageInfo.message)
                }
        })
]

fs.readFile('src/app.css', (err, css) => {
        postcss(plugins)
                .process(css, { from: 'src/app.css', to: 'dest/app.css' })
                .then(result => {
                        fs.writeFile('dest/app.css', result.css, () => true)
                })
})

Tested with this CSS:

:root {
        --color: red;
}
  
.a {
        font-size: 1rem;
        color: var(--color);
}

it runs without any warnings and produces:

.a {
        font-size: 1rem;
        color: red;
}

If you remove either the postcss-custom-properties transform or the ignore entry, it will log an error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants