mirror of https://github.com/vuejs/core.git
build: refactor rollup config
This commit is contained in:
parent
e22b5c510d
commit
40aa7d8040
|
@ -12,6 +12,7 @@ import { nodeResolve } from '@rollup/plugin-node-resolve'
|
||||||
import terser from '@rollup/plugin-terser'
|
import terser from '@rollup/plugin-terser'
|
||||||
import esbuild from 'rollup-plugin-esbuild'
|
import esbuild from 'rollup-plugin-esbuild'
|
||||||
import alias from '@rollup/plugin-alias'
|
import alias from '@rollup/plugin-alias'
|
||||||
|
import { entries } from './scripts/aliases.mjs'
|
||||||
|
|
||||||
if (!process.env.TARGET) {
|
if (!process.env.TARGET) {
|
||||||
throw new Error('TARGET package must be specified via --environment flag.')
|
throw new Error('TARGET package must be specified via --environment flag.')
|
||||||
|
@ -31,9 +32,6 @@ const pkg = require(resolve(`package.json`))
|
||||||
const packageOptions = pkg.buildOptions || {}
|
const packageOptions = pkg.buildOptions || {}
|
||||||
const name = packageOptions.filename || path.basename(packageDir)
|
const name = packageOptions.filename || path.basename(packageDir)
|
||||||
|
|
||||||
// ensure TS checks only once for each build
|
|
||||||
let hasTSChecked = false
|
|
||||||
|
|
||||||
const outputConfigs = {
|
const outputConfigs = {
|
||||||
'esm-bundler': {
|
'esm-bundler': {
|
||||||
file: resolve(`dist/${name}.esm-bundler.js`),
|
file: resolve(`dist/${name}.esm-bundler.js`),
|
||||||
|
@ -104,6 +102,9 @@ function createConfig(format, output, plugins = []) {
|
||||||
const isGlobalBuild = /global/.test(format)
|
const isGlobalBuild = /global/.test(format)
|
||||||
const isCompatPackage = pkg.name === '@vue/compat'
|
const isCompatPackage = pkg.name === '@vue/compat'
|
||||||
const isCompatBuild = !!packageOptions.compat
|
const isCompatBuild = !!packageOptions.compat
|
||||||
|
const isBrowserBuild =
|
||||||
|
(isGlobalBuild || isBrowserESMBuild || isBundlerESMBuild) &&
|
||||||
|
!packageOptions.enableNonBrowserBranches
|
||||||
|
|
||||||
output.exports = isCompatPackage ? 'auto' : 'named'
|
output.exports = isCompatPackage ? 'auto' : 'named'
|
||||||
if (isNodeBuild) {
|
if (isNodeBuild) {
|
||||||
|
@ -116,42 +117,6 @@ function createConfig(format, output, plugins = []) {
|
||||||
output.name = packageOptions.name
|
output.name = packageOptions.name
|
||||||
}
|
}
|
||||||
|
|
||||||
// package aliases
|
|
||||||
// TODO reuse between rollup and vitest
|
|
||||||
const resolveEntryForPkg = p =>
|
|
||||||
path.resolve(
|
|
||||||
fileURLToPath(import.meta.url),
|
|
||||||
`../packages/${p}/src/index.ts`
|
|
||||||
)
|
|
||||||
const dirs = readdirSync(new URL('./packages', import.meta.url))
|
|
||||||
const entries = {
|
|
||||||
vue: resolveEntryForPkg('vue'),
|
|
||||||
'vue/compiler-sfc': resolveEntryForPkg('compiler-sfc'),
|
|
||||||
'vue/server-renderer': resolveEntryForPkg('server-renderer'),
|
|
||||||
'@vue/compat': resolveEntryForPkg('vue-compat')
|
|
||||||
}
|
|
||||||
for (const dir of dirs) {
|
|
||||||
const key = `@vue/${dir}`
|
|
||||||
if (dir !== 'vue' && !(key in entries)) {
|
|
||||||
entries[key] = resolveEntryForPkg(dir)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
const aliasPlugin = alias({
|
|
||||||
entries
|
|
||||||
})
|
|
||||||
|
|
||||||
const tsPlugin = esbuild({
|
|
||||||
tsconfig: path.resolve(__dirname, 'tsconfig.json'),
|
|
||||||
sourceMap: output.sourcemap,
|
|
||||||
minify: false,
|
|
||||||
target: isServerRenderer || isNodeBuild ? 'es2019' : 'es2015'
|
|
||||||
})
|
|
||||||
|
|
||||||
// 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
|
|
||||||
|
|
||||||
let entryFile = /runtime$/.test(format) ? `src/runtime.ts` : `src/index.ts`
|
let entryFile = /runtime$/.test(format) ? `src/runtime.ts` : `src/index.ts`
|
||||||
|
|
||||||
// the compat build needs both default AND named exports. This will cause
|
// the compat build needs both default AND named exports. This will cause
|
||||||
|
@ -163,85 +128,170 @@ function createConfig(format, output, plugins = []) {
|
||||||
: `src/esm-index.ts`
|
: `src/esm-index.ts`
|
||||||
}
|
}
|
||||||
|
|
||||||
let external = []
|
function resolveDefine() {
|
||||||
const treeShakenDeps = ['source-map', '@babel/parser', 'estree-walker']
|
const replacements = {
|
||||||
|
__COMMIT__: `"${process.env.COMMIT}"`,
|
||||||
|
__VERSION__: `"${masterVersion}"`,
|
||||||
|
// this is only used during Vue's internal tests
|
||||||
|
__TEST__: `false`,
|
||||||
|
// If the build is expected to run directly in the browser (global / esm builds)
|
||||||
|
__BROWSER__: String(isBrowserBuild),
|
||||||
|
__GLOBAL__: String(isGlobalBuild),
|
||||||
|
__ESM_BUNDLER__: String(isBundlerESMBuild),
|
||||||
|
__ESM_BROWSER__: String(isBrowserESMBuild),
|
||||||
|
// is targeting Node (SSR)?
|
||||||
|
__NODE_JS__: String(isNodeBuild),
|
||||||
|
// need SSR-specific branches?
|
||||||
|
__SSR__: String(isNodeBuild || isBundlerESMBuild || isServerRenderer),
|
||||||
|
|
||||||
if (isGlobalBuild || isBrowserESMBuild || isCompatPackage) {
|
// 2.x compat build
|
||||||
if (!packageOptions.enableNonBrowserBranches) {
|
__COMPAT__: String(isCompatBuild),
|
||||||
// normal browser builds - non-browser only imports are tree-shaken,
|
|
||||||
// they are only listed here to suppress warnings.
|
// feature flags
|
||||||
external = treeShakenDeps
|
__FEATURE_SUSPENSE__: `true`,
|
||||||
|
__FEATURE_OPTIONS_API__: isBundlerESMBuild
|
||||||
|
? `__VUE_OPTIONS_API__`
|
||||||
|
: `true`,
|
||||||
|
__FEATURE_PROD_DEVTOOLS__: isBundlerESMBuild
|
||||||
|
? `__VUE_PROD_DEVTOOLS__`
|
||||||
|
: `false`
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
// Node / esm-bundler builds.
|
if (!isBundlerESMBuild) {
|
||||||
// externalize all direct deps unless it's the compat build.
|
// hard coded dev/prod builds
|
||||||
external = [
|
// @ts-ignore
|
||||||
...Object.keys(pkg.dependencies || {}),
|
replacements.__DEV__ = String(!isProductionBuild)
|
||||||
...Object.keys(pkg.peerDependencies || {}),
|
}
|
||||||
// for @vue/compiler-sfc / server-renderer
|
|
||||||
...['path', 'url', 'stream'],
|
// allow inline overrides like
|
||||||
// somehow these throw warnings for runtime-* package builds
|
//__RUNTIME_COMPILE__=true pnpm build runtime-core
|
||||||
...treeShakenDeps
|
Object.keys(replacements).forEach(key => {
|
||||||
]
|
if (key in process.env) {
|
||||||
|
replacements[key] = process.env[key]
|
||||||
|
}
|
||||||
|
})
|
||||||
|
return replacements
|
||||||
}
|
}
|
||||||
|
|
||||||
// we are bundling forked consolidate.js in compiler-sfc which dynamically
|
// esbuild define is a bit strict and only allows literal json or identifiers
|
||||||
// requires a ton of template engines which should be ignored.
|
// so we still need replace plugin in some cases
|
||||||
let cjsIgnores = []
|
function resolveReplace() {
|
||||||
if (pkg.name === '@vue/compiler-sfc') {
|
const replacements = {}
|
||||||
cjsIgnores = [
|
|
||||||
...Object.keys(consolidatePkg.devDependencies),
|
if (isProductionBuild && isBrowserBuild) {
|
||||||
'vm',
|
Object.assign(replacements, {
|
||||||
'crypto',
|
'context.onError(': `/*#__PURE__*/ context.onError(`,
|
||||||
'react-dom/server',
|
'emitError(': `/*#__PURE__*/ emitError(`,
|
||||||
'teacup/lib/express',
|
'createCompilerError(': `/*#__PURE__*/ createCompilerError(`,
|
||||||
'arc-templates/dist/es5',
|
'createDOMCompilerError(': `/*#__PURE__*/ createDOMCompilerError(`
|
||||||
'then-pug',
|
})
|
||||||
'then-jade'
|
}
|
||||||
]
|
|
||||||
|
if (isBundlerESMBuild) {
|
||||||
|
Object.assign(replacements, {
|
||||||
|
// preserve to be handled by bundlers
|
||||||
|
__DEV__: `(process.env.NODE_ENV !== 'production')`
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// for compiler-sfc browser build inlined deps
|
||||||
|
if (isBrowserESMBuild) {
|
||||||
|
Object.assign(replacements, {
|
||||||
|
'process.env': '({})',
|
||||||
|
'process.platform': '""',
|
||||||
|
'process.stdout': 'null'
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Object.keys(replacements).length) {
|
||||||
|
// @ts-ignore
|
||||||
|
return [replace({ values: replacements, preventAssignment: true })]
|
||||||
|
} else {
|
||||||
|
return []
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const nodePlugins =
|
function resolveExternal() {
|
||||||
(format === 'cjs' && Object.keys(pkg.devDependencies || {}).length) ||
|
const treeShakenDeps = ['source-map', '@babel/parser', 'estree-walker']
|
||||||
packageOptions.enableNonBrowserBranches
|
|
||||||
? [
|
|
||||||
commonJS({
|
|
||||||
sourceMap: false,
|
|
||||||
ignore: cjsIgnores
|
|
||||||
}),
|
|
||||||
...(format === 'cjs' ? [] : [polyfillNode()]),
|
|
||||||
nodeResolve()
|
|
||||||
]
|
|
||||||
: []
|
|
||||||
|
|
||||||
if (format === 'cjs') {
|
if (isGlobalBuild || isBrowserESMBuild || isCompatPackage) {
|
||||||
nodePlugins.push(cjsReExportsPatchPlugin())
|
if (!packageOptions.enableNonBrowserBranches) {
|
||||||
|
// normal browser builds - non-browser only imports are tree-shaken,
|
||||||
|
// they are only listed here to suppress warnings.
|
||||||
|
return treeShakenDeps
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Node / esm-bundler builds.
|
||||||
|
// externalize all direct deps unless it's the compat build.
|
||||||
|
return [
|
||||||
|
...Object.keys(pkg.dependencies || {}),
|
||||||
|
...Object.keys(pkg.peerDependencies || {}),
|
||||||
|
// for @vue/compiler-sfc / server-renderer
|
||||||
|
...['path', 'url', 'stream'],
|
||||||
|
// somehow these throw warnings for runtime-* package builds
|
||||||
|
...treeShakenDeps
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function resolveNodePlugins() {
|
||||||
|
// we are bundling forked consolidate.js in compiler-sfc which dynamically
|
||||||
|
// requires a ton of template engines which should be ignored.
|
||||||
|
let cjsIgnores = []
|
||||||
|
if (pkg.name === '@vue/compiler-sfc') {
|
||||||
|
cjsIgnores = [
|
||||||
|
...Object.keys(consolidatePkg.devDependencies),
|
||||||
|
'vm',
|
||||||
|
'crypto',
|
||||||
|
'react-dom/server',
|
||||||
|
'teacup/lib/express',
|
||||||
|
'arc-templates/dist/es5',
|
||||||
|
'then-pug',
|
||||||
|
'then-jade'
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
const nodePlugins =
|
||||||
|
(format === 'cjs' && Object.keys(pkg.devDependencies || {}).length) ||
|
||||||
|
packageOptions.enableNonBrowserBranches
|
||||||
|
? [
|
||||||
|
commonJS({
|
||||||
|
sourceMap: false,
|
||||||
|
ignore: cjsIgnores
|
||||||
|
}),
|
||||||
|
...(format === 'cjs' ? [] : [polyfillNode()]),
|
||||||
|
nodeResolve()
|
||||||
|
]
|
||||||
|
: []
|
||||||
|
|
||||||
|
if (format === 'cjs') {
|
||||||
|
nodePlugins.push(cjsReExportsPatchPlugin())
|
||||||
|
}
|
||||||
|
|
||||||
|
return nodePlugins
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
input: resolve(entryFile),
|
input: resolve(entryFile),
|
||||||
// Global and Browser ESM builds inlines everything so that they can be
|
// Global and Browser ESM builds inlines everything so that they can be
|
||||||
// used alone.
|
// used alone.
|
||||||
external,
|
external: resolveExternal(),
|
||||||
plugins: [
|
plugins: [
|
||||||
json({
|
json({
|
||||||
namedExports: false
|
namedExports: false
|
||||||
}),
|
}),
|
||||||
aliasPlugin,
|
alias({
|
||||||
tsPlugin,
|
entries
|
||||||
createReplacePlugin(
|
}),
|
||||||
isProductionBuild,
|
...resolveReplace(),
|
||||||
isBundlerESMBuild,
|
esbuild({
|
||||||
isBrowserESMBuild,
|
tsconfig: path.resolve(__dirname, 'tsconfig.json'),
|
||||||
// isBrowserBuild?
|
sourceMap: output.sourcemap,
|
||||||
(isGlobalBuild || isBrowserESMBuild || isBundlerESMBuild) &&
|
minify: false,
|
||||||
!packageOptions.enableNonBrowserBranches,
|
target: isServerRenderer || isNodeBuild ? 'es2019' : 'es2015',
|
||||||
isGlobalBuild,
|
define: resolveDefine()
|
||||||
isNodeBuild,
|
}),
|
||||||
isCompatBuild,
|
...resolveNodePlugins(),
|
||||||
isServerRenderer
|
|
||||||
),
|
|
||||||
...nodePlugins,
|
|
||||||
...plugins
|
...plugins
|
||||||
],
|
],
|
||||||
output,
|
output,
|
||||||
|
@ -256,81 +306,6 @@ function createConfig(format, output, plugins = []) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function createReplacePlugin(...args) {
|
|
||||||
return replace({
|
|
||||||
// @ts-ignore
|
|
||||||
values: createReplacements(...args),
|
|
||||||
preventAssignment: true
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
function createReplacements(
|
|
||||||
isProduction,
|
|
||||||
isBundlerESMBuild,
|
|
||||||
isBrowserESMBuild,
|
|
||||||
isBrowserBuild,
|
|
||||||
isGlobalBuild,
|
|
||||||
isNodeBuild,
|
|
||||||
isCompatBuild,
|
|
||||||
isServerRenderer
|
|
||||||
) {
|
|
||||||
const replacements = {
|
|
||||||
__COMMIT__: `"${process.env.COMMIT}"`,
|
|
||||||
__VERSION__: `"${masterVersion}"`,
|
|
||||||
__DEV__: isBundlerESMBuild
|
|
||||||
? // preserve to be handled by bundlers
|
|
||||||
`(process.env.NODE_ENV !== 'production')`
|
|
||||||
: // hard coded dev/prod builds
|
|
||||||
String(!isProduction),
|
|
||||||
// this is only used during Vue's internal tests
|
|
||||||
__TEST__: `false`,
|
|
||||||
// If the build is expected to run directly in the browser (global / esm builds)
|
|
||||||
__BROWSER__: isBrowserBuild,
|
|
||||||
__GLOBAL__: isGlobalBuild,
|
|
||||||
__ESM_BUNDLER__: isBundlerESMBuild,
|
|
||||||
__ESM_BROWSER__: isBrowserESMBuild,
|
|
||||||
// is targeting Node (SSR)?
|
|
||||||
__NODE_JS__: isNodeBuild,
|
|
||||||
// need SSR-specific branches?
|
|
||||||
__SSR__: isNodeBuild || isBundlerESMBuild || isServerRenderer,
|
|
||||||
|
|
||||||
// for compiler-sfc browser build inlined deps
|
|
||||||
...(isBrowserESMBuild
|
|
||||||
? {
|
|
||||||
'process.env': '({})',
|
|
||||||
'process.platform': '""',
|
|
||||||
'process.stdout': 'null'
|
|
||||||
}
|
|
||||||
: {}),
|
|
||||||
|
|
||||||
// 2.x compat build
|
|
||||||
__COMPAT__: isCompatBuild,
|
|
||||||
|
|
||||||
// feature flags
|
|
||||||
__FEATURE_SUSPENSE__: `true`,
|
|
||||||
__FEATURE_OPTIONS_API__: isBundlerESMBuild ? `__VUE_OPTIONS_API__` : `true`,
|
|
||||||
__FEATURE_PROD_DEVTOOLS__: isBundlerESMBuild
|
|
||||||
? `__VUE_PROD_DEVTOOLS__`
|
|
||||||
: `false`,
|
|
||||||
...(isProduction && isBrowserBuild
|
|
||||||
? {
|
|
||||||
'context.onError(': `/*#__PURE__*/ context.onError(`,
|
|
||||||
'emitError(': `/*#__PURE__*/ emitError(`,
|
|
||||||
'createCompilerError(': `/*#__PURE__*/ createCompilerError(`,
|
|
||||||
'createDOMCompilerError(': `/*#__PURE__*/ createDOMCompilerError(`
|
|
||||||
}
|
|
||||||
: {})
|
|
||||||
}
|
|
||||||
// allow inline overrides like
|
|
||||||
//__RUNTIME_COMPILE__=true pnpm build runtime-core
|
|
||||||
Object.keys(replacements).forEach(key => {
|
|
||||||
if (key in process.env) {
|
|
||||||
replacements[key] = process.env[key]
|
|
||||||
}
|
|
||||||
})
|
|
||||||
return replacements
|
|
||||||
}
|
|
||||||
|
|
||||||
function createProductionConfig(format) {
|
function createProductionConfig(format) {
|
||||||
return createConfig(format, {
|
return createConfig(format, {
|
||||||
file: resolve(`dist/${name}.${format}.prod.js`),
|
file: resolve(`dist/${name}.${format}.prod.js`),
|
||||||
|
|
|
@ -0,0 +1,25 @@
|
||||||
|
// @ts-check
|
||||||
|
// these aliases are shared between vitest and rollup
|
||||||
|
import { readdirSync } from 'node:fs'
|
||||||
|
import path from 'node:path'
|
||||||
|
import { fileURLToPath } from 'node:url'
|
||||||
|
|
||||||
|
const resolveEntryForPkg = p =>
|
||||||
|
path.resolve(fileURLToPath(import.meta.url), `../../packages/${p}/src/index.ts`)
|
||||||
|
|
||||||
|
const dirs = readdirSync(new URL('../packages', import.meta.url))
|
||||||
|
|
||||||
|
const entries = {
|
||||||
|
vue: resolveEntryForPkg('vue'),
|
||||||
|
'vue/compiler-sfc': resolveEntryForPkg('compiler-sfc'),
|
||||||
|
'vue/server-renderer': resolveEntryForPkg('server-renderer'),
|
||||||
|
'@vue/compat': resolveEntryForPkg('vue-compat')
|
||||||
|
}
|
||||||
|
for (const dir of dirs) {
|
||||||
|
const key = `@vue/${dir}`
|
||||||
|
if (dir !== 'vue' && !(key in entries)) {
|
||||||
|
entries[key] = resolveEntryForPkg(dir)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export { entries }
|
|
@ -1,25 +1,5 @@
|
||||||
import { configDefaults, defineConfig, UserConfig } from 'vitest/config'
|
import { configDefaults, defineConfig, UserConfig } from 'vitest/config'
|
||||||
import path from 'node:path'
|
import { entries } from './scripts/aliases.mjs'
|
||||||
import { fileURLToPath } from 'node:url'
|
|
||||||
import { readdirSync } from 'node:fs'
|
|
||||||
|
|
||||||
const resolve = p =>
|
|
||||||
path.resolve(fileURLToPath(import.meta.url), `../packages/${p}/src/index.ts`)
|
|
||||||
const dirs = readdirSync(new URL('./packages', import.meta.url))
|
|
||||||
|
|
||||||
const alias = {
|
|
||||||
vue: resolve('vue'),
|
|
||||||
'vue/compiler-sfc': resolve('compiler-sfc'),
|
|
||||||
'vue/server-renderer': resolve('server-renderer'),
|
|
||||||
'@vue/compat': resolve('vue-compat')
|
|
||||||
}
|
|
||||||
|
|
||||||
for (const dir of dirs) {
|
|
||||||
const key = `@vue/${dir}`
|
|
||||||
if (dir !== 'vue' && !(key in alias)) {
|
|
||||||
alias[key] = resolve(dir)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export default defineConfig({
|
export default defineConfig({
|
||||||
define: {
|
define: {
|
||||||
|
@ -38,7 +18,7 @@ export default defineConfig({
|
||||||
__COMPAT__: true
|
__COMPAT__: true
|
||||||
},
|
},
|
||||||
resolve: {
|
resolve: {
|
||||||
alias
|
alias: entries
|
||||||
},
|
},
|
||||||
test: {
|
test: {
|
||||||
globals: true,
|
globals: true,
|
||||||
|
|
Loading…
Reference in New Issue