rollup.config.js 3.4 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2 3 4
import path from 'path'
import ts from 'rollup-plugin-typescript2'
import replace from '@rollup/plugin-replace'
import json from '@rollup/plugin-json'
fxy060608's avatar
fxy060608 已提交
5 6 7
import alias from '@rollup/plugin-alias'
import nodeResolve from '@rollup/plugin-node-resolve'
import commonjs from '@rollup/plugin-commonjs'
fxy060608's avatar
fxy060608 已提交
8 9 10 11 12 13 14 15 16 17

if (!process.env.TARGET) {
  throw new Error('TARGET package must be specified via --environment flag.')
}

const packagesDir = path.resolve(__dirname, 'packages')
const packageDir = path.resolve(packagesDir, process.env.TARGET)
const resolve = p => path.resolve(packageDir, p)
const pkg = require(resolve(`package.json`))

fxy060608's avatar
fxy060608 已提交
18 19 20
// ensure TS checks only once for each build
let hasTSChecked = false

fxy060608's avatar
fxy060608 已提交
21
const configs = []
fxy060608's avatar
fxy060608 已提交
22
const buildOptions = require(resolve(`build.json`))
fxy060608's avatar
fxy060608 已提交
23
Object.keys(buildOptions.input).forEach(name => {
fxy060608's avatar
fxy060608 已提交
24 25 26 27 28 29 30 31 32
  const files = buildOptions.input[name]
  if (Array.isArray(files)) {
    files.forEach(file => {
      configs.push(
        createConfig(name, {
          file: resolve(file),
          format: file.includes('.cjs.') ? 'cjs' : 'es'
        })
      )
fxy060608's avatar
fxy060608 已提交
33
    })
fxy060608's avatar
fxy060608 已提交
34 35 36 37 38 39 40 41
  } else {
    configs.push(
      createConfig(name, {
        file: resolve(buildOptions.input[name]),
        format: (buildOptions.output && buildOptions.output.format) || `es`
      })
    )
  }
fxy060608's avatar
fxy060608 已提交
42 43 44 45
})
export default configs

function createConfig(entryFile, output, plugins = []) {
fxy060608's avatar
fxy060608 已提交
46
  const shouldEmitDeclarations = process.env.TYPES != null && !hasTSChecked
fxy060608's avatar
fxy060608 已提交
47 48

  const tsPlugin = ts({
fxy060608's avatar
fxy060608 已提交
49
    check: process.env.NODE_ENV === 'production' && !hasTSChecked,
fxy060608's avatar
fxy060608 已提交
50 51 52 53 54 55 56 57 58 59 60 61
    tsconfig: path.resolve(__dirname, 'tsconfig.json'),
    cacheRoot: path.resolve(__dirname, 'node_modules/.rts2_cache'),
    tsconfigOverride: {
      compilerOptions: {
        sourceMap: output.sourcemap,
        declaration: shouldEmitDeclarations,
        declarationMap: shouldEmitDeclarations
      },
      exclude: ['**/__tests__', 'test-dts']
    }
  })

fxy060608's avatar
fxy060608 已提交
62 63 64 65 66
  // we only need to check TS and generate declarations once for each build.
  // it also seems to run into weird issues when checking multiple times
  // during a single build.
  hasTSChecked = true

fxy060608's avatar
fxy060608 已提交
67 68 69
  const external = [
    '@vue/shared',
    ...Object.keys(pkg.dependencies || {}),
fxy060608's avatar
fxy060608 已提交
70 71
    ...Object.keys(pkg.peerDependencies || {}),
    ...(buildOptions.external || [])
fxy060608's avatar
fxy060608 已提交
72 73 74 75 76 77
  ]

  return {
    input: resolve(entryFile),
    external,
    plugins: [
fxy060608's avatar
fxy060608 已提交
78
      createAliasPlugin(buildOptions),
fxy060608's avatar
fxy060608 已提交
79
      nodeResolve(),
fxy060608's avatar
fxy060608 已提交
80
      commonjs(),
fxy060608's avatar
fxy060608 已提交
81 82 83 84 85 86 87 88 89 90 91 92 93
      json({
        namedExports: false
      }),
      tsPlugin,
      createReplacePlugin(buildOptions),
      ...plugins
    ],
    output,
    onwarn: (msg, warn) => {
      // if (!/Circular/.test(msg)) {
      warn(msg)
      // }
    },
fxy060608's avatar
fxy060608 已提交
94 95 96 97 98 99 100 101 102 103 104 105
    treeshake:
      buildOptions.treeshake === false
        ? false
        : {
            moduleSideEffects(id) {
              if (id.endsWith('polyfill.ts')) {
                console.log('[WARN]:sideEffects[' + id + ']')
                return true
              }
              return false
            }
          }
fxy060608's avatar
fxy060608 已提交
106 107 108
  }
}

fxy060608's avatar
fxy060608 已提交
109 110 111 112
function createAliasPlugin(buildOptions) {
  return alias(buildOptions.alias || {})
}

fxy060608's avatar
fxy060608 已提交
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
function createReplacePlugin(buildOptions) {
  const replacements = {
    __DEV__: `(process.env.NODE_ENV !== 'production')`
  }
  if (buildOptions.replacements) {
    Object.assign(replacements, buildOptions.replacements)
  }

  Object.keys(replacements).forEach(key => {
    if (key in process.env) {
      replacements[key] = process.env[key]
    }
  })
  return replace(replacements)
}