css-loader.conf.js 3.3 KB
Newer Older
1 2 3
const fs = require('fs')
const path = require('path')

fxy060608's avatar
fxy060608 已提交
4
const {
5 6
  getPlatformScss,
  getPlatformSass,
fxy060608's avatar
fxy060608 已提交
7 8 9
  nvueCssPreprocessOptions
} = require('@dcloudio/uni-cli-shared')

10 11 12 13
const {
  sassLoaderVersion
} = require('@dcloudio/uni-cli-shared/lib/scss')

fxy060608's avatar
fxy060608 已提交
14 15 16 17 18 19 20 21 22 23 24 25 26
const nvueStyleLoader = {
  loader: '@dcloudio/vue-cli-plugin-hbuilderx/packages/webpack-uni-nvue-loader/lib/style'
}

const preprocessLoader = {
  loader: '@dcloudio/vue-cli-plugin-uni/packages/webpack-preprocess-loader',
  options: nvueCssPreprocessOptions
}

const postcssLoader = {
  loader: 'postcss-loader',
  options: {
    sourceMap: false,
27
    parser: require('postcss-comment'),
28
    plugins: [
29 30 31 32 33 34 35 36 37 38 39 40
      require('postcss-import')({
        resolve (id, basedir, importOptions) {
          if (id.startsWith('~@/')) {
            return path.resolve(process.env.UNI_INPUT_DIR, id.substr(3))
          } else if (id.startsWith('@/')) {
            return path.resolve(process.env.UNI_INPUT_DIR, id.substr(2))
          } else if (id.startsWith('/') && !id.startsWith('//')) {
            return path.resolve(process.env.UNI_INPUT_DIR, id.substr(1))
          }
          return id
        }
      }),
41
      require('@dcloudio/vue-cli-plugin-uni/packages/postcss')
fxy060608's avatar
fxy060608 已提交
42 43 44 45
    ]
  }
}

46 47 48 49 50 51
// sass 全局变量
const isSass = fs.existsSync(path.resolve(process.env.UNI_INPUT_DIR, 'uni.sass'))
const isScss = fs.existsSync(path.resolve(process.env.UNI_INPUT_DIR, 'uni.scss'))
let sassData = isSass ? getPlatformSass() : getPlatformScss()

if (isSass) {
fxy060608's avatar
fxy060608 已提交
52
  sassData = '@import "@/uni.sass"'
53 54 55 56 57 58
} else if (isScss) {
  sassData = `${sassData}
  @import "@/uni.scss";`
}

const scssLoader = {
fxy060608's avatar
fxy060608 已提交
59
  loader: '@dcloudio/vue-cli-plugin-uni/packages/sass-loader',
60
  options: {
fxy060608's avatar
fxy060608 已提交
61
    nvue: true,
62 63 64 65
    sourceMap: false
  }
}

fxy060608's avatar
fxy060608 已提交
66
const sassLoader = {
fxy060608's avatar
fxy060608 已提交
67
  loader: '@dcloudio/vue-cli-plugin-uni/packages/sass-loader',
fxy060608's avatar
fxy060608 已提交
68
  options: {
fxy060608's avatar
fxy060608 已提交
69
    nvue: true,
70 71 72 73 74
    sourceMap: false
  }
}

if (sassLoaderVersion < 8) {
75
  scssLoader.options.data = sassData
76
  scssLoader.options.outputStyle = 'expanded'
77

78
  sassLoader.options.data = sassData
79
  sassLoader.options.outputStyle = 'expanded'
80 81
  sassLoader.options.indentedSyntax = true
} else {
82 83
  const name = sassLoaderVersion >= 9 ? 'additionalData' : 'prependData'
  scssLoader.options[name] = sassData
84
  scssLoader.options.sassOptions = {
85
    outputStyle: 'expanded'
86
  }
87

88
  sassLoader.options[name] = sassData
89
  sassLoader.options.sassOptions = {
90
    outputStyle: 'expanded',
91
    indentedSyntax: true
fxy060608's avatar
fxy060608 已提交
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
  }
}

const lessLoader = {
  loader: 'less-loader',
  options: {
    sourceMap: false
  }
}

const stylusLoader = {
  loader: 'stylus-loader',
  options: {
    sourceMap: false,
    preferPathResolver: 'webpack'
  }
}

110
function createOneOf (preLoader) {
fxy060608's avatar
fxy060608 已提交
111 112 113
  const use = [
    nvueStyleLoader,
    preprocessLoader
114
  ]
fxy060608's avatar
fxy060608 已提交
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
  use.push(postcssLoader)
  if (preLoader) {
    use.push(preLoader)
  }
  use.push(preprocessLoader)

  return [{
    resourceQuery: /\?vue/,
    use
  },
  {
    use
  }
  ]
}

module.exports = [{
  test: /\.css$/,
  oneOf: createOneOf()
}, {
  test: /\.scss$/,
136
  oneOf: createOneOf(scssLoader)
fxy060608's avatar
fxy060608 已提交
137 138 139 140 141 142 143 144 145 146
}, {
  test: /\.sass$/,
  oneOf: createOneOf(sassLoader)
}, {
  test: /\.less$/,
  oneOf: createOneOf(lessLoader)
}, {
  test: /\.styl(us)?$/,
  oneOf: createOneOf(stylusLoader)
}]