2018-03-16 19:11:24 +00:00
|
|
|
import fs from 'fs'
|
|
|
|
import path from 'path'
|
2018-03-16 19:52:17 +00:00
|
|
|
|
2018-03-23 04:59:14 +00:00
|
|
|
import _ from 'lodash'
|
2018-03-16 16:12:06 +00:00
|
|
|
import createResolver from 'postcss-import-resolver'
|
2017-12-28 16:05:34 +00:00
|
|
|
|
2018-03-22 16:22:41 +00:00
|
|
|
import { isPureObject } from '../../../common/utils'
|
2018-03-16 19:52:17 +00:00
|
|
|
|
2018-03-23 04:09:03 +00:00
|
|
|
export default class PostcssConfig {
|
|
|
|
constructor(options, nuxt) {
|
|
|
|
this.nuxt = nuxt
|
2018-08-06 16:54:28 +00:00
|
|
|
this.dev = options.dev
|
2018-03-23 04:09:03 +00:00
|
|
|
this.postcss = options.build.postcss
|
|
|
|
this.srcDir = options.srcDir
|
|
|
|
this.rootDir = options.rootDir
|
|
|
|
this.cssSourceMap = options.build.cssSourceMap
|
|
|
|
this.modulesDir = options.modulesDir
|
|
|
|
}
|
|
|
|
|
|
|
|
get defaultConfig() {
|
|
|
|
return {
|
|
|
|
useConfigFile: false,
|
|
|
|
sourceMap: this.cssSourceMap,
|
|
|
|
plugins: {
|
|
|
|
// https://github.com/postcss/postcss-import
|
|
|
|
'postcss-import': {
|
|
|
|
resolve: createResolver({
|
|
|
|
alias: {
|
|
|
|
'~': path.join(this.srcDir),
|
|
|
|
'~~': path.join(this.rootDir),
|
|
|
|
'@': path.join(this.srcDir),
|
|
|
|
'@@': path.join(this.rootDir)
|
|
|
|
},
|
|
|
|
modules: [
|
|
|
|
this.srcDir,
|
|
|
|
this.rootDir,
|
|
|
|
...this.modulesDir
|
|
|
|
]
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
// https://github.com/postcss/postcss-url
|
|
|
|
'postcss-url': {},
|
2017-12-28 16:05:34 +00:00
|
|
|
|
2018-06-01 21:11:55 +00:00
|
|
|
// https://github.com/csstools/postcss-preset-env
|
|
|
|
'postcss-preset-env': {
|
|
|
|
// https://cssdb.org/#staging-process
|
|
|
|
stage: 2
|
2018-08-06 16:54:28 +00:00
|
|
|
},
|
2018-08-06 17:15:26 +00:00
|
|
|
'cssnano': this.dev ? false : { preset: 'default' }
|
2018-03-23 04:09:03 +00:00
|
|
|
}
|
|
|
|
}
|
2017-12-28 16:05:34 +00:00
|
|
|
}
|
|
|
|
|
2018-03-23 04:09:03 +00:00
|
|
|
configFromFile() {
|
|
|
|
// Search for postCSS config file and use it if exists
|
|
|
|
// https://github.com/michael-ciniawsky/postcss-load-config
|
|
|
|
for (let dir of [this.srcDir, this.rootDir]) {
|
|
|
|
for (let file of [
|
|
|
|
'postcss.config.js',
|
|
|
|
'.postcssrc.js',
|
|
|
|
'.postcssrc',
|
|
|
|
'.postcssrc.json',
|
|
|
|
'.postcssrc.yaml'
|
|
|
|
]) {
|
|
|
|
if (fs.existsSync(path.resolve(dir, file))) {
|
|
|
|
const postcssConfigPath = path.resolve(dir, file)
|
|
|
|
return {
|
|
|
|
sourceMap: this.cssSourceMap,
|
|
|
|
config: {
|
|
|
|
path: postcssConfigPath
|
|
|
|
}
|
2017-12-28 16:05:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-23 04:09:03 +00:00
|
|
|
normalize(config) {
|
|
|
|
if (Array.isArray(config)) {
|
|
|
|
config = { plugins: config }
|
|
|
|
}
|
|
|
|
return config
|
2017-12-28 16:05:34 +00:00
|
|
|
}
|
|
|
|
|
2018-03-23 04:09:03 +00:00
|
|
|
loadPlugins(config) {
|
|
|
|
const plugins = config.plugins
|
|
|
|
if (isPureObject(plugins)) {
|
|
|
|
// Map postcss plugins into instances on object mode once
|
|
|
|
config.plugins = Object.keys(plugins)
|
2018-08-06 00:12:44 +00:00
|
|
|
.map((p) => {
|
2018-04-01 19:43:23 +00:00
|
|
|
const plugin = require(p)
|
2018-03-23 04:09:03 +00:00
|
|
|
const opts = plugins[p]
|
|
|
|
if (opts === false) return // Disabled
|
|
|
|
const instance = plugin(opts)
|
|
|
|
return instance
|
|
|
|
})
|
|
|
|
.filter(e => e)
|
|
|
|
}
|
|
|
|
}
|
2017-12-28 16:05:34 +00:00
|
|
|
|
2018-03-23 04:09:03 +00:00
|
|
|
config() {
|
|
|
|
/* istanbul ignore if */
|
|
|
|
if (!this.postcss) {
|
|
|
|
return false
|
|
|
|
}
|
2017-12-28 16:05:34 +00:00
|
|
|
|
2018-03-23 04:09:03 +00:00
|
|
|
let config = this.configFromFile()
|
|
|
|
if (config) {
|
|
|
|
return config
|
|
|
|
}
|
2017-12-28 16:05:34 +00:00
|
|
|
|
2018-03-23 04:59:14 +00:00
|
|
|
config = this.normalize(_.cloneDeep(this.postcss))
|
2017-12-28 16:05:34 +00:00
|
|
|
|
2018-03-23 04:09:03 +00:00
|
|
|
// Apply default plugins
|
|
|
|
if (isPureObject(config)) {
|
2018-03-23 04:59:14 +00:00
|
|
|
_.defaults(config, this.defaultConfig)
|
2018-03-23 04:09:03 +00:00
|
|
|
|
|
|
|
this.loadPlugins(config)
|
|
|
|
}
|
|
|
|
|
|
|
|
return config
|
|
|
|
}
|
2017-12-28 16:05:34 +00:00
|
|
|
}
|