2018-09-19 23:35:38 +08:00
|
|
|
const fs = require('fs')
|
|
|
|
const path = require('path')
|
|
|
|
const ts = require('rollup-plugin-typescript2')
|
|
|
|
const replace = require('rollup-plugin-replace')
|
|
|
|
const alias = require('rollup-plugin-alias')
|
2019-09-20 11:19:48 +08:00
|
|
|
const json = require('rollup-plugin-json')
|
2018-09-19 23:35:38 +08:00
|
|
|
|
|
|
|
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 name = path.basename(packageDir)
|
|
|
|
const resolve = p => path.resolve(packageDir, p)
|
|
|
|
const pkg = require(resolve(`package.json`))
|
|
|
|
const packageOptions = pkg.buildOptions || {}
|
|
|
|
|
|
|
|
// build aliases dynamically
|
|
|
|
const aliasOptions = { resolve: ['.ts'] }
|
|
|
|
fs.readdirSync(packagesDir).forEach(dir => {
|
2019-10-06 11:20:02 +08:00
|
|
|
if (dir === 'vue') {
|
|
|
|
return
|
|
|
|
}
|
2019-10-05 05:36:26 +08:00
|
|
|
if (fs.statSync(path.resolve(packagesDir, dir)).isDirectory()) {
|
2019-10-06 11:20:02 +08:00
|
|
|
aliasOptions[`@vue/${dir}`] = path.resolve(packagesDir, `${dir}/src/index`)
|
2018-09-19 23:35:38 +08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
const aliasPlugin = alias(aliasOptions)
|
|
|
|
|
|
|
|
// ensure TS checks only once for each build
|
|
|
|
let hasTSChecked = false
|
|
|
|
|
|
|
|
const configs = {
|
|
|
|
esm: {
|
2018-09-20 00:46:55 +08:00
|
|
|
file: resolve(`dist/${name}.esm-bundler.js`),
|
2018-09-19 23:35:38 +08:00
|
|
|
format: `es`
|
|
|
|
},
|
|
|
|
cjs: {
|
|
|
|
file: resolve(`dist/${name}.cjs.js`),
|
|
|
|
format: `cjs`
|
|
|
|
},
|
2018-09-20 00:46:55 +08:00
|
|
|
global: {
|
|
|
|
file: resolve(`dist/${name}.global.js`),
|
|
|
|
format: `iife`
|
2018-09-19 23:35:38 +08:00
|
|
|
},
|
|
|
|
'esm-browser': {
|
|
|
|
file: resolve(`dist/${name}.esm-browser.js`),
|
|
|
|
format: `es`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const defaultFormats = ['esm', 'cjs']
|
2019-10-06 23:31:44 +08:00
|
|
|
const inlineFormats = process.env.FORMATS && process.env.FORMATS.split(',')
|
|
|
|
const packageFormats = inlineFormats || packageOptions.formats || defaultFormats
|
2019-09-20 12:31:14 +08:00
|
|
|
const packageConfigs = process.env.PROD_ONLY
|
|
|
|
? []
|
|
|
|
: packageFormats.map(format => createConfig(configs[format]))
|
2018-09-19 23:35:38 +08:00
|
|
|
|
|
|
|
if (process.env.NODE_ENV === 'production') {
|
|
|
|
packageFormats.forEach(format => {
|
|
|
|
if (format === 'cjs') {
|
|
|
|
packageConfigs.push(createProductionConfig(format))
|
|
|
|
}
|
2018-09-20 00:46:55 +08:00
|
|
|
if (format === 'global' || format === 'esm-browser') {
|
2018-09-19 23:35:38 +08:00
|
|
|
packageConfigs.push(createMinifiedConfig(format))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = packageConfigs
|
|
|
|
|
|
|
|
function createConfig(output, plugins = []) {
|
2019-05-29 10:34:07 +08:00
|
|
|
const isProductionBuild =
|
|
|
|
process.env.__DEV__ === 'false' || /\.prod\.js$/.test(output.file)
|
2018-09-20 00:46:55 +08:00
|
|
|
const isGlobalBuild = /\.global(\.prod)?\.js$/.test(output.file)
|
2019-10-11 09:48:52 +08:00
|
|
|
const isBundlerESMBuild = /\.esm\.js$/.test(output.file)
|
2018-09-19 23:35:38 +08:00
|
|
|
const isBrowserESMBuild = /esm-browser(\.prod)?\.js$/.test(output.file)
|
|
|
|
|
2018-09-20 00:46:55 +08:00
|
|
|
if (isGlobalBuild) {
|
2018-09-19 23:35:38 +08:00
|
|
|
output.name = packageOptions.name
|
|
|
|
}
|
|
|
|
|
2019-09-04 00:16:22 +08:00
|
|
|
const shouldEmitDeclarations =
|
|
|
|
process.env.TYPES != null &&
|
|
|
|
process.env.NODE_ENV === 'production' &&
|
|
|
|
!hasTSChecked
|
|
|
|
|
2018-09-19 23:35:38 +08:00
|
|
|
const tsPlugin = ts({
|
|
|
|
check: process.env.NODE_ENV === 'production' && !hasTSChecked,
|
|
|
|
tsconfig: path.resolve(__dirname, 'tsconfig.json'),
|
|
|
|
cacheRoot: path.resolve(__dirname, 'node_modules/.rts2_cache'),
|
|
|
|
tsconfigOverride: {
|
|
|
|
compilerOptions: {
|
2019-09-04 00:16:22 +08:00
|
|
|
declaration: shouldEmitDeclarations,
|
|
|
|
declarationMap: shouldEmitDeclarations
|
2019-09-20 11:05:51 +08:00
|
|
|
},
|
|
|
|
exclude: ['**/__tests__']
|
2018-09-19 23:35:38 +08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
// 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
|
|
|
|
|
2018-10-17 03:47:51 +08:00
|
|
|
const externals = Object.keys(aliasOptions).filter(p => p !== '@vue/shared')
|
|
|
|
|
2018-09-19 23:35:38 +08:00
|
|
|
return {
|
|
|
|
input: resolve(`src/index.ts`),
|
2018-09-20 00:46:55 +08:00
|
|
|
// Global and Browser ESM builds inlines everything so that they can be
|
2018-09-19 23:35:38 +08:00
|
|
|
// used alone.
|
2018-10-17 03:47:51 +08:00
|
|
|
external: isGlobalBuild || isBrowserESMBuild ? [] : externals,
|
2018-09-19 23:35:38 +08:00
|
|
|
plugins: [
|
2019-09-20 11:19:48 +08:00
|
|
|
json({
|
|
|
|
namedExports: false
|
|
|
|
}),
|
2018-09-19 23:35:38 +08:00
|
|
|
tsPlugin,
|
|
|
|
aliasPlugin,
|
2019-09-04 08:51:42 +08:00
|
|
|
createReplacePlugin(
|
|
|
|
isProductionBuild,
|
2019-10-11 09:48:52 +08:00
|
|
|
isBundlerESMBuild,
|
2019-10-05 01:08:06 +08:00
|
|
|
(isGlobalBuild || isBrowserESMBuild) &&
|
|
|
|
!packageOptions.enableNonBrowserBranches
|
2019-09-04 08:51:42 +08:00
|
|
|
),
|
2018-09-19 23:35:38 +08:00
|
|
|
...plugins
|
|
|
|
],
|
|
|
|
output,
|
|
|
|
onwarn: (msg, warn) => {
|
|
|
|
if (!/Circular/.test(msg)) {
|
|
|
|
warn(msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-11 09:48:52 +08:00
|
|
|
function createReplacePlugin(isProduction, isBundlerESMBuild, isBrowserBuild) {
|
2018-09-19 23:35:38 +08:00
|
|
|
return replace({
|
2019-10-05 10:40:54 +08:00
|
|
|
__COMMIT__: `"${process.env.COMMIT}"`,
|
2019-10-11 09:48:52 +08:00
|
|
|
__DEV__: isBundlerESMBuild
|
2018-09-20 00:46:55 +08:00
|
|
|
? // preserve to be handled by bundlers
|
|
|
|
`process.env.NODE_ENV !== 'production'`
|
|
|
|
: // hard coded dev/prod builds
|
|
|
|
!isProduction,
|
2019-09-17 23:57:25 +08:00
|
|
|
// If the build is expected to run directly in the browser (global / esm-browser builds)
|
|
|
|
__BROWSER__: isBrowserBuild,
|
2019-09-04 08:51:42 +08:00
|
|
|
// support options?
|
|
|
|
// the lean build drops options related code with buildOptions.lean: true
|
|
|
|
__FEATURE_OPTIONS__: !packageOptions.lean,
|
2019-09-10 04:28:32 +08:00
|
|
|
__FEATURE_SUSPENSE__: true,
|
2018-09-25 09:13:06 +08:00
|
|
|
// this is only used during tests
|
|
|
|
__JSDOM__: false
|
2018-09-19 23:35:38 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function createProductionConfig(format) {
|
|
|
|
return createConfig({
|
|
|
|
file: resolve(`dist/${name}.${format}.prod.js`),
|
2018-09-20 00:46:55 +08:00
|
|
|
format: configs[format].format
|
2018-09-19 23:35:38 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function createMinifiedConfig(format) {
|
|
|
|
const { terser } = require('rollup-plugin-terser')
|
|
|
|
return createConfig(
|
|
|
|
{
|
|
|
|
file: resolve(`dist/${name}.${format}.prod.js`),
|
2018-09-20 00:46:55 +08:00
|
|
|
format: configs[format].format
|
2018-09-19 23:35:38 +08:00
|
|
|
},
|
|
|
|
[
|
|
|
|
terser({
|
2018-09-20 00:46:55 +08:00
|
|
|
module: /^esm/.test(format)
|
2018-09-19 23:35:38 +08:00
|
|
|
})
|
|
|
|
]
|
|
|
|
)
|
|
|
|
}
|