const path = require('path')

function resolve (dir) {
  return path.join(__dirname, dir)
}

console.log('环境', process.env.VUE_APP_PROJECT_NAME, process.env.NODE_ENV, require('./package.json').version)

const pagesObject = {
  index: {
    title: '售检票系统',
    entry: 'src/main.js', // entry for the public page
    template: 'public/index.html', // source template
    filename: 'index.html' // output as dist/*
  },
  worker: {
    entry: 'src/worker.js',
    template: 'public/worker.html',
    filename: 'worker.html'
  }
}

module.exports = {
  pages: pagesObject,
  transpileDependencies: [
    'vue-echarts',
    'resize-detector'
  ],
  productionSourceMap: true,
  // vue.config.js里在开发时关闭sourcemap
  // 通过关闭8.x分支开发时的sourcemap，可以极大减少内存使用，从1G左右降到500MB。
  // 关闭sourcemap以后，8.x分支热更新不再堆内存溢出，报JavaScript heap out of memory这种错。
  // 相关issue链接：https://github.com/vuejs/vue-cli/issues/1453#issuecomment-396811585
  // yyx990803 commented on Jun 13, 2018:
  //  @octref we can do that for now. Although in the long run we probably want to see if there's anything we can do to reduce the heap usage.
  //  I think it's because a single Vue file actually involves multiple source maps being passed and merged.
  // 关键词：vue-cli-service electron:serve JavaScript heap out of memory
  configureWebpack: {
    devtool: 'none'
  },
  css: {
    // 注入全局样式变量
    loaderOptions: {
      sass: {
        data: `
          @import "@/assets/scss/variables.scss";
        `
      }
    }
  },
  chainWebpack: (config) => {
    config.resolve.alias
      .set('@', resolve('src'))
      .set('imgs', resolve('src/assets/imgs'))

    config.module.rules.delete('svg')
    config.module
      .rule('svg-smart')
      .test(/\.svg$/)
      .include
      .add(resolve('src/icons'))
      .end()
      .use('svg-sprite-loader')
      .loader('svg-sprite-loader')
      .options({
        symbolId: 'icon-[name]'
      })

    Object.keys(pagesObject).forEach(page => {
      config.plugins.delete(`preload-${page}`)
      config.plugins.delete(`prefetch-${page}`)
    })
  },
  pluginOptions: {
    electronBuilder: {
      externals: ['ffi', 'ref', 'ref-struct', 'ref-array', 'buffertrim'],
      // If you are using Yarn Workspaces, you may have multiple node_modules folders
      // List them all here so that VCP Electron Builder can find them
      nodeModulesPath: ['../../node_modules', './node_modules'],
      builderOptions: {
        productName: process.env.VUE_APP_PROJECT_NAME,
        appId: 'org.ticket_new.tv',
        win: {
          icon: `build/${process.env.VUE_APP_LOGO ? process.env.VUE_APP_LOGO : 'icons'}/icon.ico`
        },
        nsis: {
          installerIcon: `build/${process.env.VUE_APP_LOGO ? process.env.VUE_APP_LOGO : 'icons'}/icon.ico`,
          installerHeaderIcon: `build/${process.env.VUE_APP_LOGO ? process.env.VUE_APP_LOGO : 'icons'}/icon.ico`,
          // eslint-disable-next-line no-template-curly-in-string
          artifactName: '${productName}_Setup_${version}.${ext}'
        },
        publish: [
          {
            provider: 'generic',
            url: 'http://127.0.0.1:8080'
          }
        ],
        asar: true,
        asarUnpack: [
          'public/pos',
          'public/iccard',
          'public/donsee'
        ],
        extraFiles: [{
          from: 'public/pos',
          to: './resources/app.asar.unpacked/pos/'
        },
        {
          from: 'public/desheng',
          to: './resources/app.asar.unpacked/desheng/'
        },
        {
          from: 'public/iccard',
          to: './resources/app.asar.unpacked/iccard/'
        },
        {
          from: 'public/donsee',
          to: './resources/app.asar.unpacked/donsee/'
        }],
        directories: {
          output: `dist_electron/${process.env.VUE_APP_PROJECT}/${require('./package.json').version}`
        }
      }
    }
  }
}
