2019-10-14 11:17:36 +08:00
|
|
|
import fs from 'fs'
|
|
|
|
import path from 'path'
|
|
|
|
import ts from 'rollup-plugin-typescript2'
|
2019-11-19 22:02:46 +08:00
|
|
|
import replace from '@rollup/plugin-replace'
|
|
|
|
import json from '@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.')
|
|
|
|
}
|
|
|
|
|
2019-12-11 07:24:59 +08:00
|
|
|
const masterVersion = require('./package.json').version
|
2018-09-19 23:35:38 +08:00
|
|
|
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 || {}
|
|
|
|
|
2019-12-11 06:50:16 +08:00
|
|
|
const knownExternals = fs.readdirSync(packagesDir).filter(p => {
|
|
|
|
return p !== '@vue/shared'
|
2018-09-19 23:35:38 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
// ensure TS checks only once for each build
|
|
|
|
let hasTSChecked = false
|
|
|
|
|
2019-12-18 07:24:01 +08:00
|
|
|
const outputConfigs = {
|
2019-12-11 11:14:02 +08:00
|
|
|
'esm-bundler': {
|
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`
|
|
|
|
},
|
2019-12-18 07:24:01 +08:00
|
|
|
// main "vue" package only
|
|
|
|
'esm-bundler-runtime': {
|
|
|
|
file: resolve(`dist/${name}.runtime.esm-bundler.js`),
|
|
|
|
format: `es`
|
|
|
|
},
|
2018-09-19 23:35:38 +08:00
|
|
|
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
|
|
|
},
|
2019-12-11 11:14:02 +08:00
|
|
|
esm: {
|
|
|
|
file: resolve(`dist/${name}.esm.js`),
|
2018-09-19 23:35:38 +08:00
|
|
|
format: `es`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-11 11:14:02 +08:00
|
|
|
const defaultFormats = ['esm-bundler', '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
|
|
|
|
? []
|
2019-12-18 07:24:01 +08:00
|
|
|
: packageFormats.map(format => createConfig(format, outputConfigs[format]))
|
2018-09-19 23:35:38 +08:00
|
|
|
|
|
|
|
if (process.env.NODE_ENV === 'production') {
|
|
|
|
packageFormats.forEach(format => {
|
2019-11-07 10:58:15 +08:00
|
|
|
if (format === 'cjs' && packageOptions.prod !== false) {
|
2018-09-19 23:35:38 +08:00
|
|
|
packageConfigs.push(createProductionConfig(format))
|
|
|
|
}
|
2019-12-11 11:14:02 +08:00
|
|
|
if (format === 'global' || format === 'esm') {
|
2018-09-19 23:35:38 +08:00
|
|
|
packageConfigs.push(createMinifiedConfig(format))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-10-14 11:17:36 +08:00
|
|
|
export default packageConfigs
|
2018-09-19 23:35:38 +08:00
|
|
|
|
2019-12-18 07:24:01 +08:00
|
|
|
function createConfig(format, output, plugins = []) {
|
2019-12-23 00:56:09 +08:00
|
|
|
if (!output) {
|
|
|
|
console.log(require('chalk').yellow(`invalid format: "${format}"`))
|
|
|
|
process.exit(1)
|
|
|
|
}
|
|
|
|
|
2020-01-28 23:28:40 +08:00
|
|
|
output.sourcemap = !!process.env.SOURCE_MAP
|
2019-11-07 04:47:01 +08:00
|
|
|
output.externalLiveBindings = false
|
|
|
|
|
2019-05-29 10:34:07 +08:00
|
|
|
const isProductionBuild =
|
|
|
|
process.env.__DEV__ === 'false' || /\.prod\.js$/.test(output.file)
|
2019-12-18 07:24:01 +08:00
|
|
|
const isGlobalBuild = format === 'global'
|
|
|
|
const isRawESMBuild = format === 'esm'
|
2020-01-24 11:23:10 +08:00
|
|
|
const isNodeBuild = format === 'cjs'
|
2019-12-18 07:24:01 +08:00
|
|
|
const isBundlerESMBuild = /esm-bundler/.test(format)
|
2019-10-19 01:57:13 +08:00
|
|
|
const isRuntimeCompileBuild = /vue\./.test(output.file)
|
2019-10-16 10:34:14 +08:00
|
|
|
|
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: {
|
2020-01-28 23:28:40 +08:00
|
|
|
sourceMap: output.sourcemap,
|
2019-09-04 00:16:22 +08:00
|
|
|
declaration: shouldEmitDeclarations,
|
|
|
|
declarationMap: shouldEmitDeclarations
|
2019-09-20 11:05:51 +08:00
|
|
|
},
|
2019-11-05 07:38:55 +08:00
|
|
|
exclude: ['**/__tests__', 'test-dts']
|
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
|
|
|
|
|
2019-12-18 07:24:01 +08:00
|
|
|
const entryFile =
|
|
|
|
format === 'esm-bundler-runtime' ? `src/runtime.ts` : `src/index.ts`
|
|
|
|
|
2020-01-24 11:23:10 +08:00
|
|
|
const external =
|
|
|
|
isGlobalBuild || isRawESMBuild
|
|
|
|
? []
|
|
|
|
: knownExternals.concat(Object.keys(pkg.dependencies || []))
|
|
|
|
|
2018-09-19 23:35:38 +08:00
|
|
|
return {
|
2019-12-18 07:24:01 +08:00
|
|
|
input: resolve(entryFile),
|
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.
|
2020-01-24 11:23:10 +08:00
|
|
|
external,
|
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,
|
2019-09-04 08:51:42 +08:00
|
|
|
createReplacePlugin(
|
|
|
|
isProductionBuild,
|
2019-10-11 09:48:52 +08:00
|
|
|
isBundlerESMBuild,
|
2020-01-24 11:23:10 +08:00
|
|
|
// isBrowserBuild?
|
2019-12-18 06:34:10 +08:00
|
|
|
(isGlobalBuild || isRawESMBuild || isBundlerESMBuild) &&
|
2019-10-14 13:08:00 +08:00
|
|
|
!packageOptions.enableNonBrowserBranches,
|
2020-01-24 11:23:10 +08:00
|
|
|
isRuntimeCompileBuild,
|
|
|
|
isNodeBuild
|
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-14 13:08:00 +08:00
|
|
|
function createReplacePlugin(
|
|
|
|
isProduction,
|
|
|
|
isBundlerESMBuild,
|
|
|
|
isBrowserBuild,
|
2020-01-24 11:23:10 +08:00
|
|
|
isRuntimeCompileBuild,
|
|
|
|
isNodeBuild
|
2019-10-14 13:08:00 +08:00
|
|
|
) {
|
2019-12-16 04:37:43 +08:00
|
|
|
const replacements = {
|
2019-10-05 10:40:54 +08:00
|
|
|
__COMMIT__: `"${process.env.COMMIT}"`,
|
2019-12-11 07:24:59 +08:00
|
|
|
__VERSION__: `"${masterVersion}"`,
|
2019-10-11 09:48:52 +08:00
|
|
|
__DEV__: isBundlerESMBuild
|
2018-09-20 00:46:55 +08:00
|
|
|
? // preserve to be handled by bundlers
|
2019-11-05 00:24:22 +08:00
|
|
|
`(process.env.NODE_ENV !== 'production')`
|
2018-09-20 00:46:55 +08:00
|
|
|
: // hard coded dev/prod builds
|
|
|
|
!isProduction,
|
2019-11-05 00:24:22 +08:00
|
|
|
// this is only used during tests
|
|
|
|
__TEST__: isBundlerESMBuild ? `(process.env.NODE_ENV === 'test')` : false,
|
2019-12-11 11:14:02 +08:00
|
|
|
// If the build is expected to run directly in the browser (global / esm builds)
|
2019-09-17 23:57:25 +08:00
|
|
|
__BROWSER__: isBrowserBuild,
|
2019-12-13 01:42:21 +08:00
|
|
|
// is targeting bundlers?
|
|
|
|
__BUNDLER__: isBundlerESMBuild,
|
2019-10-14 13:08:00 +08:00
|
|
|
// support compile in browser?
|
|
|
|
__RUNTIME_COMPILE__: isRuntimeCompileBuild,
|
2020-01-24 11:23:10 +08:00
|
|
|
// is targeting Node (SSR)?
|
2020-01-29 22:49:17 +08:00
|
|
|
__NODE_JS__: isNodeBuild,
|
2019-09-04 08:51:42 +08:00
|
|
|
// support options?
|
|
|
|
// the lean build drops options related code with buildOptions.lean: true
|
2019-10-14 13:08:00 +08:00
|
|
|
__FEATURE_OPTIONS__: !packageOptions.lean && !process.env.LEAN,
|
2020-02-06 03:21:09 +08:00
|
|
|
__FEATURE_SUSPENSE__: true,
|
|
|
|
...(isProduction && isBrowserBuild
|
|
|
|
? {
|
|
|
|
'context.onError(': `/*#__PURE__*/ context.onError(`,
|
2020-02-07 04:10:21 +08:00
|
|
|
'emitError(': `/*#__PURE__*/ emitError(`,
|
|
|
|
'createCompilerError(': `/*#__PURE__*/ createCompilerError(`,
|
|
|
|
'createDOMCompilerError(': `/*#__PURE__*/ createDOMCompilerError(`
|
2020-02-06 03:21:09 +08:00
|
|
|
}
|
|
|
|
: {})
|
2019-12-16 04:37:43 +08:00
|
|
|
}
|
|
|
|
// allow inline overrides like
|
|
|
|
//__RUNTIME_COMPILE__=true yarn build runtime-core
|
|
|
|
Object.keys(replacements).forEach(key => {
|
|
|
|
if (key in process.env) {
|
|
|
|
replacements[key] = process.env[key]
|
|
|
|
}
|
2018-09-19 23:35:38 +08:00
|
|
|
})
|
2019-12-16 04:37:43 +08:00
|
|
|
return replace(replacements)
|
2018-09-19 23:35:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function createProductionConfig(format) {
|
2019-12-18 07:24:01 +08:00
|
|
|
return createConfig(format, {
|
2018-09-19 23:35:38 +08:00
|
|
|
file: resolve(`dist/${name}.${format}.prod.js`),
|
2019-12-18 07:24:01 +08:00
|
|
|
format: outputConfigs[format].format
|
2018-09-19 23:35:38 +08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function createMinifiedConfig(format) {
|
|
|
|
const { terser } = require('rollup-plugin-terser')
|
|
|
|
return createConfig(
|
2019-12-18 07:24:01 +08:00
|
|
|
format,
|
2018-09-19 23:35:38 +08:00
|
|
|
{
|
|
|
|
file: resolve(`dist/${name}.${format}.prod.js`),
|
2019-12-18 07:24:01 +08:00
|
|
|
format: outputConfigs[format].format
|
2018-09-19 23:35:38 +08:00
|
|
|
},
|
|
|
|
[
|
|
|
|
terser({
|
2020-02-07 04:10:21 +08:00
|
|
|
module: /^esm/.test(format),
|
|
|
|
compress: {
|
|
|
|
ecma: 2015,
|
|
|
|
pure_getters: true
|
|
|
|
}
|
2018-09-19 23:35:38 +08:00
|
|
|
})
|
|
|
|
]
|
|
|
|
)
|
|
|
|
}
|