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-16 19:11:24 +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-16 16:12:06 +00:00
|
|
|
export default function postcssConfig() {
|
2018-03-16 19:11:24 +00:00
|
|
|
let config = ـ.cloneDeep(this.options.build.postcss)
|
2017-12-28 16:05:34 +00:00
|
|
|
|
2017-12-29 08:43:24 +00:00
|
|
|
/* istanbul ignore if */
|
2017-12-28 16:05:34 +00:00
|
|
|
if (!config) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Search for postCSS config file and use it if exists
|
|
|
|
// https://github.com/michael-ciniawsky/postcss-load-config
|
|
|
|
for (let dir of [this.options.srcDir, this.options.rootDir]) {
|
2018-01-13 05:22:11 +00:00
|
|
|
for (let file of [
|
|
|
|
'postcss.config.js',
|
|
|
|
'.postcssrc.js',
|
|
|
|
'.postcssrc',
|
|
|
|
'.postcssrc.json',
|
|
|
|
'.postcssrc.yaml'
|
|
|
|
]) {
|
2018-03-16 19:11:24 +00:00
|
|
|
if (fs.existsSync(path.resolve(dir, file))) {
|
|
|
|
const postcssConfigPath = path.resolve(dir, file)
|
2017-12-28 16:05:34 +00:00
|
|
|
return {
|
|
|
|
sourceMap: this.options.build.cssSourceMap,
|
|
|
|
config: {
|
|
|
|
path: postcssConfigPath
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Normalize
|
|
|
|
if (Array.isArray(config)) {
|
|
|
|
config = { plugins: config }
|
|
|
|
}
|
|
|
|
|
|
|
|
// Apply default plugins
|
|
|
|
if (isPureObject(config)) {
|
2018-01-13 05:22:11 +00:00
|
|
|
config = Object.assign(
|
|
|
|
{
|
|
|
|
useConfigFile: false,
|
|
|
|
sourceMap: this.options.build.cssSourceMap,
|
|
|
|
plugins: {
|
|
|
|
// https://github.com/postcss/postcss-import
|
|
|
|
'postcss-import': {
|
2018-01-17 08:46:58 +00:00
|
|
|
resolve: createResolver({
|
|
|
|
alias: {
|
2018-03-16 19:11:24 +00:00
|
|
|
'~': path.join(this.options.srcDir),
|
|
|
|
'~~': path.join(this.options.rootDir),
|
|
|
|
'@': path.join(this.options.srcDir),
|
|
|
|
'@@': path.join(this.options.rootDir)
|
2018-01-17 08:46:58 +00:00
|
|
|
},
|
|
|
|
modules: [
|
|
|
|
this.options.srcDir,
|
|
|
|
this.options.rootDir,
|
|
|
|
...this.options.modulesDir
|
|
|
|
]
|
|
|
|
})
|
2018-01-13 05:22:11 +00:00
|
|
|
},
|
2017-12-28 16:05:34 +00:00
|
|
|
|
2018-01-13 05:22:11 +00:00
|
|
|
// https://github.com/postcss/postcss-url
|
|
|
|
'postcss-url': {},
|
2017-12-28 16:05:34 +00:00
|
|
|
|
2018-01-13 05:22:11 +00:00
|
|
|
// http://cssnext.io/postcss
|
|
|
|
'postcss-cssnext': {}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
config
|
|
|
|
)
|
2017-12-28 16:05:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Map postcss plugins into instances on object mode once
|
|
|
|
if (isPureObject(config) && isPureObject(config.plugins)) {
|
|
|
|
config.plugins = Object.keys(config.plugins)
|
|
|
|
.map(p => {
|
2018-03-16 16:12:06 +00:00
|
|
|
const plugin = this.nuxt.requireModule(p)
|
2017-12-28 16:05:34 +00:00
|
|
|
const opts = config.plugins[p]
|
|
|
|
if (opts === false) return // Disabled
|
|
|
|
const instance = plugin(opts)
|
|
|
|
return instance
|
2018-01-13 05:22:11 +00:00
|
|
|
})
|
|
|
|
.filter(e => e)
|
2017-12-28 16:05:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return config
|
|
|
|
}
|