rollup.config.js 3.6 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

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)
fxy060608's avatar
fxy060608 已提交
15
const resolve = (p) => path.resolve(packageDir, p)
fxy060608's avatar
fxy060608 已提交
16 17
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
  const files = buildOptions.input[name]
  if (Array.isArray(files)) {
fxy060608's avatar
fxy060608 已提交
26
    files.forEach((file) => {
fxy060608's avatar
fxy060608 已提交
27 28 29
      configs.push(
        createConfig(name, {
          file: resolve(file),
fxy060608's avatar
fxy060608 已提交
30
          format: file.includes('.cjs.') ? 'cjs' : 'es',
fxy060608's avatar
fxy060608 已提交
31
          exports: 'auto',
fxy060608's avatar
fxy060608 已提交
32 33
        })
      )
fxy060608's avatar
fxy060608 已提交
34
    })
fxy060608's avatar
fxy060608 已提交
35 36 37 38
  } else {
    configs.push(
      createConfig(name, {
        file: resolve(buildOptions.input[name]),
fxy060608's avatar
fxy060608 已提交
39
        format: (buildOptions.output && buildOptions.output.format) || `es`,
fxy060608's avatar
fxy060608 已提交
40
        exports: 'auto',
fxy060608's avatar
fxy060608 已提交
41 42 43
      })
    )
  }
fxy060608's avatar
fxy060608 已提交
44 45 46 47
})
export default configs

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

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

fxy060608's avatar
fxy060608 已提交
64 65 66 67 68
  // 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 已提交
69
  const external = [
fxy060608's avatar
fxy060608 已提交
70
    'vue',
fxy060608's avatar
fxy060608 已提交
71 72
    '@vue/shared',
    ...Object.keys(pkg.dependencies || {}),
fxy060608's avatar
fxy060608 已提交
73
    ...Object.keys(pkg.peerDependencies || {}),
fxy060608's avatar
fxy060608 已提交
74
    ...(buildOptions.external || []),
fxy060608's avatar
fxy060608 已提交
75 76 77 78 79 80
  ]

  return {
    input: resolve(entryFile),
    external,
    plugins: [
fxy060608's avatar
fxy060608 已提交
81
      createAliasPlugin(buildOptions),
fxy060608's avatar
fxy060608 已提交
82
      nodeResolve(),
fxy060608's avatar
fxy060608 已提交
83
      commonjs(),
fxy060608's avatar
fxy060608 已提交
84
      json({
fxy060608's avatar
fxy060608 已提交
85
        namedExports: false,
fxy060608's avatar
fxy060608 已提交
86 87
      }),
      tsPlugin,
fxy060608's avatar
fxy060608 已提交
88
      createReplacePlugin(buildOptions, output.format),
fxy060608's avatar
fxy060608 已提交
89
      ...plugins,
fxy060608's avatar
fxy060608 已提交
90 91 92 93 94 95 96
    ],
    output,
    onwarn: (msg, warn) => {
      // if (!/Circular/.test(msg)) {
      warn(msg)
      // }
    },
fxy060608's avatar
fxy060608 已提交
97 98 99 100 101 102 103 104 105 106
    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 已提交
107 108
            },
          },
fxy060608's avatar
fxy060608 已提交
109 110 111
  }
}

fxy060608's avatar
fxy060608 已提交
112 113 114 115
function createAliasPlugin(buildOptions) {
  return alias(buildOptions.alias || {})
}

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

fxy060608's avatar
fxy060608 已提交
126
  Object.keys(replacements).forEach((key) => {
fxy060608's avatar
fxy060608 已提交
127 128 129 130
    if (key in process.env) {
      replacements[key] = process.env[key]
    }
  })
131
  return replace({ values: replacements, preventAssignment: true })
fxy060608's avatar
fxy060608 已提交
132
}