2023-02-03 10:24:03 +00:00
|
|
|
// @ts-check
|
|
|
|
|
2023-01-26 06:24:49 +00:00
|
|
|
// Using esbuild for faster dev builds.
|
|
|
|
// We are still using Rollup for production builds because it generates
|
2023-11-30 18:43:56 +00:00
|
|
|
// smaller files and provides better tree-shaking.
|
2023-01-26 06:24:49 +00:00
|
|
|
|
|
|
|
import esbuild from 'esbuild'
|
2024-08-28 09:52:07 +00:00
|
|
|
import fs from 'node:fs'
|
2023-01-26 06:24:49 +00:00
|
|
|
import { dirname, relative, resolve } from 'node:path'
|
|
|
|
import { fileURLToPath } from 'node:url'
|
|
|
|
import { createRequire } from 'node:module'
|
2024-07-05 03:51:05 +00:00
|
|
|
import { parseArgs } from 'node:util'
|
2023-05-01 03:04:59 +00:00
|
|
|
import { polyfillNode } from 'esbuild-plugin-polyfill-node'
|
2023-01-26 06:24:49 +00:00
|
|
|
|
|
|
|
const require = createRequire(import.meta.url)
|
|
|
|
const __dirname = dirname(fileURLToPath(import.meta.url))
|
2024-07-05 03:51:05 +00:00
|
|
|
|
|
|
|
const {
|
|
|
|
values: { format: rawFormat, prod, inline: inlineDeps },
|
|
|
|
positionals,
|
|
|
|
} = parseArgs({
|
|
|
|
allowPositionals: true,
|
|
|
|
options: {
|
|
|
|
format: {
|
|
|
|
type: 'string',
|
|
|
|
short: 'f',
|
|
|
|
default: 'global',
|
|
|
|
},
|
|
|
|
prod: {
|
|
|
|
type: 'boolean',
|
|
|
|
short: 'p',
|
|
|
|
default: false,
|
|
|
|
},
|
|
|
|
inline: {
|
|
|
|
type: 'boolean',
|
|
|
|
short: 'i',
|
|
|
|
default: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
const format = rawFormat || 'global'
|
|
|
|
const targets = positionals.length ? positionals : ['vue']
|
2023-01-26 06:24:49 +00:00
|
|
|
|
|
|
|
// resolve output
|
|
|
|
const outputFormat = format.startsWith('global')
|
|
|
|
? 'iife'
|
|
|
|
: format === 'cjs'
|
2023-11-18 02:33:24 +00:00
|
|
|
? 'cjs'
|
|
|
|
: 'esm'
|
2023-01-26 06:24:49 +00:00
|
|
|
|
|
|
|
const postfix = format.endsWith('-runtime')
|
|
|
|
? `runtime.${format.replace(/-runtime$/, '')}`
|
|
|
|
: format
|
|
|
|
|
2024-08-28 09:52:07 +00:00
|
|
|
const privatePackages = fs.readdirSync('packages-private')
|
|
|
|
|
2024-01-15 10:14:32 +00:00
|
|
|
for (const target of targets) {
|
2024-08-28 09:52:07 +00:00
|
|
|
const pkgBase = privatePackages.includes(target)
|
|
|
|
? `packages-private`
|
|
|
|
: `packages`
|
|
|
|
const pkgBasePath = `../${pkgBase}/${target}`
|
|
|
|
const pkg = require(`${pkgBasePath}/package.json`)
|
2024-01-15 10:14:32 +00:00
|
|
|
const outfile = resolve(
|
|
|
|
__dirname,
|
2024-08-28 09:52:07 +00:00
|
|
|
`${pkgBasePath}/dist/${
|
2024-01-15 10:14:32 +00:00
|
|
|
target === 'vue-compat' ? `vue` : target
|
|
|
|
}.${postfix}.${prod ? `prod.` : ``}js`,
|
|
|
|
)
|
|
|
|
const relativeOutfile = relative(process.cwd(), outfile)
|
2023-01-26 06:24:49 +00:00
|
|
|
|
2024-01-15 10:14:32 +00:00
|
|
|
// resolve externals
|
|
|
|
// TODO this logic is largely duplicated from rollup.config.js
|
|
|
|
/** @type {string[]} */
|
|
|
|
let external = []
|
|
|
|
if (!inlineDeps) {
|
|
|
|
// cjs & esm-bundler: external all deps
|
|
|
|
if (format === 'cjs' || format.includes('esm-bundler')) {
|
|
|
|
external = [
|
|
|
|
...external,
|
|
|
|
...Object.keys(pkg.dependencies || {}),
|
|
|
|
...Object.keys(pkg.peerDependencies || {}),
|
|
|
|
// for @vue/compiler-sfc / server-renderer
|
|
|
|
'path',
|
|
|
|
'url',
|
|
|
|
'stream',
|
|
|
|
]
|
|
|
|
}
|
2023-01-26 06:24:49 +00:00
|
|
|
|
2024-01-15 10:14:32 +00:00
|
|
|
if (target === 'compiler-sfc') {
|
|
|
|
const consolidatePkgPath = require.resolve(
|
|
|
|
'@vue/consolidate/package.json',
|
|
|
|
{
|
|
|
|
paths: [resolve(__dirname, `../packages/${target}/`)],
|
|
|
|
},
|
|
|
|
)
|
|
|
|
const consolidateDeps = Object.keys(
|
|
|
|
require(consolidatePkgPath).devDependencies,
|
|
|
|
)
|
|
|
|
external = [
|
|
|
|
...external,
|
|
|
|
...consolidateDeps,
|
|
|
|
'fs',
|
|
|
|
'vm',
|
|
|
|
'crypto',
|
|
|
|
'react-dom/server',
|
|
|
|
'teacup/lib/express',
|
|
|
|
'arc-templates/dist/es5',
|
|
|
|
'then-pug',
|
|
|
|
'then-jade',
|
|
|
|
]
|
|
|
|
}
|
2023-01-26 06:24:49 +00:00
|
|
|
}
|
2024-01-15 10:14:32 +00:00
|
|
|
/** @type {Array<import('esbuild').Plugin>} */
|
|
|
|
const plugins = [
|
|
|
|
{
|
|
|
|
name: 'log-rebuild',
|
|
|
|
setup(build) {
|
|
|
|
build.onEnd(() => {
|
|
|
|
console.log(`built: ${relativeOutfile}`)
|
|
|
|
})
|
|
|
|
},
|
2023-01-26 06:24:49 +00:00
|
|
|
},
|
2024-01-15 10:14:32 +00:00
|
|
|
]
|
2023-01-26 06:24:49 +00:00
|
|
|
|
2024-01-15 10:14:32 +00:00
|
|
|
if (format !== 'cjs' && pkg.buildOptions?.enableNonBrowserBranches) {
|
|
|
|
plugins.push(polyfillNode())
|
|
|
|
}
|
2023-01-26 06:24:49 +00:00
|
|
|
|
2024-01-15 10:14:32 +00:00
|
|
|
esbuild
|
|
|
|
.context({
|
2024-08-28 09:52:07 +00:00
|
|
|
entryPoints: [resolve(__dirname, `${pkgBasePath}/src/index.ts`)],
|
2024-01-15 10:14:32 +00:00
|
|
|
outfile,
|
|
|
|
bundle: true,
|
|
|
|
external,
|
|
|
|
sourcemap: true,
|
|
|
|
format: outputFormat,
|
|
|
|
globalName: pkg.buildOptions?.name,
|
|
|
|
platform: format === 'cjs' ? 'node' : 'browser',
|
|
|
|
plugins,
|
|
|
|
define: {
|
|
|
|
__COMMIT__: `"dev"`,
|
|
|
|
__VERSION__: `"${pkg.version}"`,
|
|
|
|
__DEV__: prod ? `false` : `true`,
|
|
|
|
__TEST__: `false`,
|
|
|
|
__BROWSER__: String(
|
|
|
|
format !== 'cjs' && !pkg.buildOptions?.enableNonBrowserBranches,
|
|
|
|
),
|
|
|
|
__GLOBAL__: String(format === 'global'),
|
|
|
|
__ESM_BUNDLER__: String(format.includes('esm-bundler')),
|
|
|
|
__ESM_BROWSER__: String(format.includes('esm-browser')),
|
|
|
|
__CJS__: String(format === 'cjs'),
|
2024-07-19 04:02:24 +00:00
|
|
|
__SSR__: String(format !== 'global'),
|
2024-01-15 10:14:32 +00:00
|
|
|
__COMPAT__: String(target === 'vue-compat'),
|
|
|
|
__FEATURE_SUSPENSE__: `true`,
|
|
|
|
__FEATURE_OPTIONS_API__: `true`,
|
|
|
|
__FEATURE_PROD_DEVTOOLS__: `false`,
|
2024-07-19 03:14:00 +00:00
|
|
|
__FEATURE_PROD_HYDRATION_MISMATCH_DETAILS__: `true`,
|
2024-01-15 10:14:32 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
.then(ctx => ctx.watch())
|
|
|
|
}
|