refactor: config Postcss preset and plugins together (#3927)

This commit is contained in:
Clark Du 2018-10-09 13:26:11 +01:00 committed by Pooya Parsa
parent a3dd7dad6b
commit 61407fec1d
5 changed files with 37 additions and 31 deletions

View File

@ -11,10 +11,12 @@ class TailwindExtractor {
export default { export default {
build: { build: {
extractCSS: true, extractCSS: true,
postcss: [ postcss: {
require('tailwindcss')('./tailwind.js'), plugins: {
require('autoprefixer') tailwindcss: path.resolve('./tailwind.js')
], },
preset: { autoprefixer: { grid: true } }
},
extend(config, { isDev }) { extend(config, { isDev }) {
if (!isDev) { if (!isDev) {
config.plugins.push( config.plugins.push(

View File

@ -10,7 +10,6 @@
"nuxt-edge": "latest" "nuxt-edge": "latest"
}, },
"devDependencies": { "devDependencies": {
"autoprefixer": "^7.1.6",
"glob-all": "^3.1.0", "glob-all": "^3.1.0",
"purgecss-webpack-plugin": "^0.20.1", "purgecss-webpack-plugin": "^0.20.1",
"tailwindcss": "^0.1.3" "tailwindcss": "^0.1.3"

View File

@ -1,6 +0,0 @@
module.exports = {
plugins: [
require('tailwindcss')('./tailwind.js'),
require('autoprefixer')
]
}

View File

@ -19,7 +19,6 @@ export default class PostcssConfig {
get defaultConfig() { get defaultConfig() {
return { return {
useConfigFile: false,
sourceMap: this.cssSourceMap, sourceMap: this.cssSourceMap,
plugins: { plugins: {
// https://github.com/postcss/postcss-import // https://github.com/postcss/postcss-import
@ -49,7 +48,7 @@ export default class PostcssConfig {
} }
} }
configFromFile() { searchConfigFile() {
// Search for postCSS config file and use it if exists // Search for postCSS config file and use it if exists
// https://github.com/michael-ciniawsky/postcss-load-config // https://github.com/michael-ciniawsky/postcss-load-config
for (const dir of [this.srcDir, this.rootDir]) { for (const dir of [this.srcDir, this.rootDir]) {
@ -60,19 +59,26 @@ export default class PostcssConfig {
'.postcssrc.json', '.postcssrc.json',
'.postcssrc.yaml' '.postcssrc.yaml'
]) { ]) {
if (fs.existsSync(path.resolve(dir, file))) { const configFile = path.resolve(dir, file)
const postcssConfigPath = path.resolve(dir, file) if (fs.existsSync(configFile)) {
return { return configFile
sourceMap: this.cssSourceMap,
config: {
path: postcssConfigPath
}
}
} }
} }
} }
} }
configFromFile() {
const loaderConfig = (this.postcss && this.postcss.config) || {}
loaderConfig.path = loaderConfig.path || this.searchConfigFile()
if (loaderConfig.path) {
return {
sourceMap: this.cssSourceMap,
config: loaderConfig
}
}
}
normalize(config) { normalize(config) {
if (Array.isArray(config)) { if (Array.isArray(config)) {
config = { plugins: config } config = { plugins: config }
@ -115,11 +121,14 @@ export default class PostcssConfig {
this.preset = config.preset this.preset = config.preset
delete config.preset delete config.preset
} }
_.defaults(config, this.defaultConfig) if (Array.isArray(config.plugins)) {
_.defaults(config, this.defaultConfig)
this.loadPlugins(config) } else {
// Keep the order of default plugins
config = _.merge({}, this.defaultConfig, config)
this.loadPlugins(config)
}
return config
} }
return config
} }
} }

View File

@ -67,13 +67,15 @@ export default {
], ],
build: { build: {
scopeHoisting: true, scopeHoisting: true,
postcss: [ postcss: {
require('postcss-preset-env')({ preset: {
features: { features: {
'custom-selectors': true 'custom-selectors': true
} }
}), },
require('cssnano') plugins: {
] cssnano: {}
}
}
} }
} }