const fs = require('fs')
const path = require('path')

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

module.exports = function initOptions (options) {
  const {
    getPlatformScss,
    getPlatformSass
  } = require('@dcloudio/uni-cli-shared')

  if (!options.transpileDependencies) {
    options.transpileDependencies = []
  }

  // 增加 src/node_modules 解析
  options.transpileDependencies.push(path.resolve(process.env.UNI_INPUT_DIR, 'node_modules'))
  options.transpileDependencies.push('@dcloudio/uni-' + process.env.UNI_PLATFORM)
  options.transpileDependencies.push('@dcloudio/uni-stat')

  if (process.env.UNI_PLATFORM === 'app-plus') {
    options.transpileDependencies.push('format-log.js')
  }

  if (process.env.UNI_PLATFORM === 'h5') { // h5 dev 用到了这两个，需要 babel
    options.transpileDependencies.push('ansi-regex')
    options.transpileDependencies.push('strip-ansi')
  }

  if (!options.css) {
    options.css = {}
  }

  if (process.env.UNI_PLATFORM === 'h5') {
    options.css.extract = false
  } else {
    options.css.extract = true
  }

  if (!options.css.loaderOptions) {
    options.css.loaderOptions = {}
  }
  if (!options.css.loaderOptions.postcss) {
    options.css.loaderOptions.postcss = {}
  }
  if (!options.css.loaderOptions.sass) {
    options.css.loaderOptions.sass = {}
  }

  if (!options.css.loaderOptions.postcss.config) {
    options.css.loaderOptions.postcss.config = {}
  }

  // 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) {
    sassData = `@import "@/uni.sass"`
  } else if (isScss) {
    sassData = `${sassData}
  @import "@/uni.scss";`
  }

  if (sassLoaderVersion < 8) {
    options.css.loaderOptions.sass.data = sassData
  } else {
    options.css.loaderOptions.sass.prependData = sassData
  }

  let userPostcssConfigPath = path.resolve(process.env.UNI_INPUT_DIR, 'postcss.config.js')
  if (fs.existsSync(userPostcssConfigPath)) {
    options.css.loaderOptions.postcss.config.path = userPostcssConfigPath
  } else {
    options.css.loaderOptions.postcss.config.path = path.resolve(process.env.UNI_CLI_CONTEXT, 'postcss.config.js')
  }
}

@@ -1879,24 +1879,258 @@
 css%22;%60%0D%0A  %7D%0D
+%0A%0A  if (!options.css.loaderOptions.sass.sassOptions) %7B%0A    options.css.loaderOptions.sass.sassOptions = %7B%7D%0A  %7D%0A  // %E6%8C%87%E5%AE%9A outputStyle, %E5%90%A6%E5%88%99 production %E6%A8%A1%E5%BC%8F%E4%B8%8B%E4%BC%9A%E8%A2%AB%E9%BB%98%E8%AE%A4%E6%88%90 compressed%0D%0A  options.css.loaderOptions.sass.sassOptions.outputStyle = 'nested'
 %0A%0D%0A  if (sas

set default value for sassOptions.outputStyle to nested #1013