vue3-yuanma/rollup.config.js

187 lines
5.3 KiB
JavaScript
Raw Normal View History

import fs from 'fs'
import path from 'path'
import ts from 'rollup-plugin-typescript2'
import replace from 'rollup-plugin-replace'
import alias from 'rollup-plugin-alias'
import json from 'rollup-plugin-json'
2019-10-15 03:31:43 +08:00
import lernaJson from './lerna.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
}
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']
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))
}
})
}
export default packageConfigs
2018-09-19 23:35:38 +08:00
function createConfig(output, plugins = []) {
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)
const isBundlerESMBuild = /\.esm-bundler\.js$/.test(output.file)
2018-09-19 23:35:38 +08:00
const isBrowserESMBuild = /esm-browser(\.prod)?\.js$/.test(output.file)
const isRuntimeCompileBuild = /vue\./.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
}
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: {
declaration: shouldEmitDeclarations,
declarationMap: shouldEmitDeclarations
},
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
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.
external: isGlobalBuild || isBrowserESMBuild ? [] : externals,
2018-09-19 23:35:38 +08:00
plugins: [
json({
namedExports: false
}),
2018-09-19 23:35:38 +08:00
tsPlugin,
aliasPlugin,
2019-09-04 08:51:42 +08:00
createReplacePlugin(
isProductionBuild,
isBundlerESMBuild,
2019-10-05 01:08:06 +08:00
(isGlobalBuild || isBrowserESMBuild) &&
!packageOptions.enableNonBrowserBranches,
isRuntimeCompileBuild
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)
}
}
}
}
function createReplacePlugin(
isProduction,
isBundlerESMBuild,
isBrowserBuild,
isRuntimeCompileBuild
) {
2018-09-19 23:35:38 +08:00
return replace({
__COMMIT__: `"${process.env.COMMIT}"`,
2019-10-15 03:31:43 +08:00
__VERSION__: `"${lernaJson.version}"`,
__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,
// If the build is expected to run directly in the browser (global / esm-browser builds)
__BROWSER__: isBrowserBuild,
// support compile in browser?
__RUNTIME_COMPILE__: isRuntimeCompileBuild,
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 && !process.env.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
})
]
)
}