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

[WIP][v2] PostCSS config experiment #4428

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/gatsby/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@
"parse-filepath": "^1.0.1",
"path-exists": "^3.0.0",
"postcss-browser-reporter": "^0.5.0",
"postcss-cssnext": "^3.0.2",
"postcss-flexbugs-fixes": "^3.0.0",
"postcss-import": "^11.0.0",
"postcss-load-plugins": "^2.3.0",
Copy link
Contributor

Choose a reason for hiding this comment

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

Oh nice! @loganfsmyth just added something similar to Babel so tools don't have to (like we have) write their own code for loading .babelrc files babel/babel#7472

"postcss-loader": "^2.1.3",
"postcss-reporter": "^5.0.0",
"raw-loader": "^0.5.1",
Expand Down
1 change: 1 addition & 0 deletions packages/gatsby/src/bootstrap/load-plugins/load.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ module.exports = async (config = {}) => {
const internalPlugins = [
`../../internal-plugins/dev-404-page`,
`../../internal-plugins/load-babel-config`,
`../../internal-plugins/load-postcss-config`,
`../../internal-plugins/component-page-creator`,
`../../internal-plugins/component-layout-creator`,
`../../internal-plugins/internal-data-bridge`,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/* @flow */
const findPostcssPlugins = require(`postcss-load-plugins`)
const autoprefixer = require(`autoprefixer`)
const flexbugs = require(`postcss-flexbugs-fixes`)
const report = require(`gatsby-cli/lib/reporter`)

exports.onCreateWebpackConfig = async ({
actions,
getConfig,
loaders,
rules,
stage,
store,
}) => {
const program = store.getState().program
const { directory, browserslist } = program

// console.log(`initial config for stage: `, stage)
// console.dir(getConfig(), { depth: null })

const defaultPlugins = {
plugins: [
flexbugs,
autoprefixer({ browsers: browserslist, flexbox: `no-2009` }),
Copy link
Contributor

Choose a reason for hiding this comment

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

Thinking about the workflow for people overriding the core settings. What do people in postcss world normally do for browserlist stuff — does postcss automatically read in from package.json like we do? Or would we need some glue code to add that? Or do people duplicate the browserlist in their package.json setup?

Copy link
Contributor

Choose a reason for hiding this comment

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

i'ave always added it explictly like this, postcss-config is a weird place, never sure what is "supported"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It looks like we should check for a global browserslist config before using Gatsby's default config: https://github.com/postcss/autoprefixer#no-prefixes-in-production

Copy link
Contributor

Choose a reason for hiding this comment

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

What's a global browserlist config? We should double-check we're loading the browserlist from all possible places.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It looks like it should be a .browserslistrc file or browserslist key in package.json which should be present in the root of a project.

Copy link
Contributor

Choose a reason for hiding this comment

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

i think we should probably limit this to just Gatsby's api and not use a .browserlistrc? otherwise we'd have to support the browserlist thing for all cases where we use the browsers list.

Copy link
Contributor

Choose a reason for hiding this comment

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

We could use this tool https://github.com/browserslist/browserslist

I think it'd be good to support additional ways of specifying browserlist otherwise people will get confused. Right now we only look at the package.json but it'd be easy to swap in the above package there.

siteInfo.browserslist = json.browserslist || siteInfo.browserslist

],
}

let customPlugins
try {
customPlugins = await findPostcssPlugins({}, directory)
} catch (error) {
if (!error.message.startsWith(`No PostCSS Config found in`)) {
report.panicOnBuid(`Error loading postCSS config`, error)
}
}

const postcssPlugins = customPlugins || defaultPlugins
const postcssRule = rules.postcss({ plugins: postcssPlugins })

actions.setWebpackConfig({
module: {
rules: [postcssRule],
},
})

// console.log(`UPDATED config for stage: `, stage)
// console.dir(getConfig(), { depth: null })
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// noop
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "load-postcss-config",
"version": "1.0.0",
"description": "Internal plugin that handles loading PostCSS configs",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Kyle Mathews <mathews.kyle@gmail.com>",
"license": "MIT"
}
1 change: 0 additions & 1 deletion packages/gatsby/src/utils/webpack-plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ const plugins = {
namedChunks: plugin(`NamedChunksPlugin`),
hashedModuleIds: plugin(`HashedModuleIdsPlugin`),
moduleFilenameH: plugin(`ModuleFilenameHelpers`),

aggressiveMerging: plugin(`AggressiveMergingPlugin`, true),
aggressiveSplitting: plugin(`AggressiveSplittingPlugin`, true),
splitChunks: plugin(`SplitChunks`, true),
Expand Down
10 changes: 2 additions & 8 deletions packages/gatsby/src/utils/webpack-utils.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// @flow
const os = require(`os`)

const autoprefixer = require(`autoprefixer`)
const flexbugs = require(`postcss-flexbugs-fixes`)
const UglifyPlugin = require(`uglifyjs-webpack-plugin`)
const MiniCssExtractPlugin = require(`mini-css-extract-plugin`)

Expand Down Expand Up @@ -186,7 +184,7 @@ module.exports = async ({
},

postcss: (options = {}) => {
let { plugins, browsers = supportedBrowsers, ...postcssOpts } = options
let { plugins, ...postcssOpts } = options

return {
loader: require.resolve(`postcss-loader`),
Expand All @@ -197,11 +195,7 @@ module.exports = async ({
plugins =
(typeof plugins === `function` ? plugins(loader) : plugins) || []

return [
flexbugs,
autoprefixer({ browsers, flexbox: `no-2009` }),
...plugins,
]
return [...plugins]
},
...postcssOpts,
},
Expand Down
Loading