build.js 1.7 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2 3 4 5 6
const {
  error
} = require('@vue/cli-shared-utils')

const Service = require('@vue/cli-service')

Q
qiang 已提交
7 8 9 10 11
const del = require('del')
const copy = require('copy')
const path = require('path')
const jsonfile = require('jsonfile')

fxy060608's avatar
fxy060608 已提交
12 13 14 15
const {
  generateApiManifest
} = require('./manifest')

fxy060608's avatar
fxy060608 已提交
16 17 18
const service = new Service(process.env.VUE_CLI_CONTEXT || process.cwd(), {
  inlineOptions: require('./vue.config.js')
})
fxy060608's avatar
fxy060608 已提交
19 20
// 删除 cache 目录
del.sync(['node_modules/.cache'])
fxy060608's avatar
fxy060608 已提交
21 22 23 24 25 26 27

service.run('build', {
  name: 'index',
  watch: process.env.UNI_WATCH === 'true',
  target: 'lib',
  formats: process.env.UNI_WATCH === 'true' ? 'umd' : 'umd-min',
  entry: './lib/' + process.env.UNI_PLATFORM + '/main.js'
fxy060608's avatar
fxy060608 已提交
28
}).then(function () {
fxy060608's avatar
fxy060608 已提交
29 30 31 32
  generateApiManifest(
    JSON.parse(JSON.stringify(process.UNI_SERVICE_API_MANIFEST)),
    JSON.parse(JSON.stringify(process.UNI_SERVICE_API_PROTOCOL))
  )
fxy060608's avatar
fxy060608 已提交
33 34 35
}).catch(err => {
  error(err)
  process.exit(1)
Q
qiang 已提交
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
})

if (process.env.UNI_WATCH === 'false') {
  const packagePath = path.join(__dirname, `../packages/uni-${process.env.UNI_PLATFORM}`)
  const packageJsonPath = path.join(packagePath, 'package.json')
  del(path.join(packagePath, '{lib,src}'))
    .then(() => {
      copy([path.join(__dirname, '../{lib,src}/**/*')], packagePath, function (err, file) {
        if (err) {
          throw err
        }
      })
    })
  jsonfile.readFile(path.join(__dirname, '../package.json'))
    .then(origin => {
      return jsonfile.readFile(packageJsonPath)
        .then(obj => {
          obj.dependencies = origin.dependencies
          return obj
        })
    })
    .then(obj => {
fxy060608's avatar
fxy060608 已提交
58 59 60
      return jsonfile.writeFile(packageJsonPath, obj, {
        spaces: 2
      })
Q
qiang 已提交
61 62 63 64
    })
    .catch(err => {
      throw err
    })
fxy060608's avatar
fxy060608 已提交
65
}