Blame view

node_modules/vue-loader/lib/style-compiler/load-postcss-config.js 1.82 KB
aaac7fed   liuqimichale   add
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
  const load = require('postcss-load-config')
  
  let loaded
  
  function isObject (val) {
    return val && typeof val === 'object'
  }
  
  module.exports = function loadPostcssConfig (loaderContext, inlineConfig = {}) {
    if (inlineConfig.useConfigFile === false) {
      return Promise.resolve({
        plugins: inlineConfig.plugins || [],
        options: inlineConfig.options || {}
      })
    }
  
    if (process.env.VUE_LOADER_TEST || !loaded) {
      const config = inlineConfig.config || {}
      const ctx = { webpack: loaderContext }
      if (config.ctx) {
        ctx.options = config.ctx
      }
      loaded = load(ctx, config.path, { argv: false }).catch(err => {
        // postcss-load-config throws error when no config file is found,
        // but for us it's optional. only emit other errors
        if (err.message.indexOf('No PostCSS Config found') >= 0) {
          return
        }
        const friendlyErr = new Error(`Error loading PostCSS config: ${err.message}`)
        Error.captureStackTrace(friendlyErr, err)
        loaderContext.emitError(friendlyErr)
      })
    }
  
    return loaded.then(config => {
      let plugins = []
      let options = {}
  
      // inline postcss options for vue-loader
      if (typeof inlineConfig === 'function') {
        inlineConfig = inlineConfig.call(this, this)
      }
      if (Array.isArray(inlineConfig)) {
        plugins = inlineConfig
      } else if (isObject(inlineConfig)) {
        plugins =
          typeof inlineConfig.plugins === 'function'
            ? inlineConfig.plugins.call(this, this)
            : inlineConfig.plugins || []
        options = inlineConfig.options || {}
      }
  
      // merge postcss config file
      if (config && config.plugins) {
        plugins = plugins.concat(config.plugins)
      }
      if (config && config.options) {
        options = Object.assign({}, config.options, options)
      }
  
      return {
        plugins,
        options
      }
    })
  }