Skip to content

Commit 22c4a3d

Browse files
committed
refactor(core): using the shared login in build-shared.js in rollup.config.js and scripts/dev.js
1 parent cfcc012 commit 22c4a3d

File tree

3 files changed

+62
-160
lines changed

3 files changed

+62
-160
lines changed

rollup.config.js

Lines changed: 29 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ import alias from '@rollup/plugin-alias'
1515
import { entries } from './scripts/aliases.js'
1616
import { inlineEnums } from './scripts/inline-enums.js'
1717
import { minify as minifySwc } from '@swc/core'
18+
import {
19+
resolveCJSIgnores,
20+
resolveDefines,
21+
resolveEntryFile,
22+
resolveExternal as resolveExternalShared,
23+
} from './scripts/build-shared.js'
1824

1925
/**
2026
* @template T
@@ -32,7 +38,6 @@ const require = createRequire(import.meta.url)
3238
const __dirname = fileURLToPath(new URL('.', import.meta.url))
3339

3440
const masterVersion = require('./package.json').version
35-
const consolidatePkg = require('@vue/consolidate/package.json')
3641

3742
const privatePackages = fs.readdirSync('packages-private')
3843
const pkgBase = privatePackages.includes(process.env.TARGET)
@@ -156,54 +161,19 @@ function createConfig(format, output, plugins = []) {
156161
output.name = packageOptions.name
157162
}
158163

159-
let entryFile = /runtime$/.test(format) ? `src/runtime.ts` : `src/index.ts`
160-
161-
// the compat build needs both default AND named exports. This will cause
162-
// Rollup to complain for non-ESM targets, so we use separate entries for
163-
// esm vs. non-esm builds.
164-
if (isCompatPackage && (isBrowserESMBuild || isBundlerESMBuild)) {
165-
entryFile = /runtime$/.test(format)
166-
? `src/esm-runtime.ts`
167-
: `src/esm-index.ts`
168-
}
164+
// Use shared function for entry file resolution
165+
const entryFile = resolveEntryFile(format, isCompatPackage)
169166

170167
function resolveDefine() {
171-
/** @type {Record<string, string>} */
172-
const replacements = {
173-
__COMMIT__: `"${process.env.COMMIT}"`,
174-
__VERSION__: `"${masterVersion}"`,
175-
// this is only used during Vue's internal tests
176-
__TEST__: `false`,
177-
// If the build is expected to run directly in the browser (global / esm builds)
178-
__BROWSER__: String(isBrowserBuild),
179-
__GLOBAL__: String(isGlobalBuild),
180-
__ESM_BUNDLER__: String(isBundlerESMBuild),
181-
__ESM_BROWSER__: String(isBrowserESMBuild),
182-
// is targeting Node (SSR)?
183-
__CJS__: String(isCJSBuild),
184-
// need SSR-specific branches?
185-
__SSR__: String(!isGlobalBuild),
186-
187-
// 2.x compat build
188-
__COMPAT__: String(isCompatBuild),
189-
190-
// feature flags
191-
__FEATURE_SUSPENSE__: `true`,
192-
__FEATURE_OPTIONS_API__: isBundlerESMBuild
193-
? `__VUE_OPTIONS_API__`
194-
: `true`,
195-
__FEATURE_PROD_DEVTOOLS__: isBundlerESMBuild
196-
? `__VUE_PROD_DEVTOOLS__`
197-
: `false`,
198-
__FEATURE_PROD_HYDRATION_MISMATCH_DETAILS__: isBundlerESMBuild
199-
? `__VUE_PROD_HYDRATION_MISMATCH_DETAILS__`
200-
: `false`,
201-
}
202-
203-
if (!isBundlerESMBuild) {
204-
// hard coded dev/prod builds
205-
replacements.__DEV__ = String(!isProductionBuild)
206-
}
168+
// Use shared function for base defines
169+
const replacements = resolveDefines({
170+
pkg,
171+
format,
172+
target: process.env.TARGET,
173+
prod: isProductionBuild,
174+
version: masterVersion,
175+
commit: process.env.COMMIT || 'dev',
176+
})
207177

208178
// allow inline overrides like
209179
//__RUNTIME_COMPILE__=true pnpm build runtime-core
@@ -255,50 +225,21 @@ function createConfig(format, output, plugins = []) {
255225
}
256226

257227
function resolveExternal() {
258-
const treeShakenDeps = [
259-
'source-map-js',
260-
'@babel/parser',
261-
'estree-walker',
262-
'entities/lib/decode.js',
263-
]
264-
265-
if (isGlobalBuild || isBrowserESMBuild || isCompatPackage) {
266-
if (!packageOptions.enableNonBrowserBranches) {
267-
// normal browser builds - non-browser only imports are tree-shaken,
268-
// they are only listed here to suppress warnings.
269-
return treeShakenDeps
270-
}
271-
} else {
272-
// Node / esm-bundler builds.
273-
// externalize all direct deps unless it's the compat build.
274-
return [
275-
...Object.keys(pkg.dependencies || {}),
276-
...Object.keys(pkg.peerDependencies || {}),
277-
// for @vue/compiler-sfc / server-renderer
278-
...['path', 'url', 'stream'],
279-
// somehow these throw warnings for runtime-* package builds
280-
...treeShakenDeps,
281-
]
282-
}
228+
// Use shared function for external resolution
229+
return resolveExternalShared({
230+
pkg,
231+
format,
232+
target: process.env.TARGET,
233+
isGlobalBuild,
234+
isBrowserESMBuild,
235+
isCompatPackage,
236+
packageOptions,
237+
})
283238
}
284239

285240
function resolveNodePlugins() {
286-
// we are bundling forked consolidate.js in compiler-sfc which dynamically
287-
// requires a ton of template engines which should be ignored.
288-
/** @type {ReadonlyArray<string>} */
289-
let cjsIgnores = []
290-
if (pkg.name === '@vue/compiler-sfc') {
291-
cjsIgnores = [
292-
...Object.keys(consolidatePkg.devDependencies),
293-
'vm',
294-
'crypto',
295-
'react-dom/server',
296-
'teacup/lib/express',
297-
'arc-templates/dist/es5',
298-
'then-pug',
299-
'then-jade',
300-
]
301-
}
241+
// Use shared function for CJS ignores
242+
const cjsIgnores = resolveCJSIgnores(process.env.TARGET)
302243

303244
const nodePlugins =
304245
(format === 'cjs' && Object.keys(pkg.devDependencies || {}).length) ||

scripts/build-shared.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ export function resolveCJSIgnores(target) {
115115
* @param {Object} options
116116
* @param {any} options.pkg - Package.json object
117117
* @param {string} options.format - Build format
118-
* @param {string} options.target - Target package name
118+
* @param {string | undefined} options.target - Target package name
119119
* @param {boolean} options.prod - Whether this is a production build
120120
* @param {string} [options.version] - Version override
121121
* @param {string} [options.commit] - Commit hash
@@ -124,7 +124,7 @@ export function resolveCJSIgnores(target) {
124124
export function resolveDefines({
125125
pkg,
126126
format,
127-
target,
127+
target = '',
128128
prod,
129129
version,
130130
commit = 'dev',

scripts/dev.js

Lines changed: 31 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ import { fileURLToPath } from 'node:url'
1111
import { createRequire } from 'node:module'
1212
import { parseArgs } from 'node:util'
1313
import { polyfillNode } from 'esbuild-plugin-polyfill-node'
14+
import {
15+
resolveDefines,
16+
resolveExternal,
17+
resolveOutputFormat,
18+
resolvePostfix,
19+
} from './build-shared.js'
1420

1521
const require = createRequire(import.meta.url)
1622
const __dirname = dirname(fileURLToPath(import.meta.url))
@@ -42,16 +48,9 @@ const {
4248
const format = rawFormat || 'global'
4349
const targets = positionals.length ? positionals : ['vue']
4450

45-
// resolve output
46-
const outputFormat = format.startsWith('global')
47-
? 'iife'
48-
: format === 'cjs'
49-
? 'cjs'
50-
: 'esm'
51-
52-
const postfix = format.endsWith('-runtime')
53-
? `runtime.${format.replace(/-runtime$/, '')}`
54-
: format
51+
// resolve output using shared functions
52+
const outputFormat = resolveOutputFormat(format)
53+
const postfix = resolvePostfix(format)
5554

5655
const privatePackages = fs.readdirSync('packages-private')
5756

@@ -69,48 +68,19 @@ for (const target of targets) {
6968
)
7069
const relativeOutfile = relative(process.cwd(), outfile)
7170

72-
// resolve externals
73-
// TODO this logic is largely duplicated from rollup.config.js
74-
/** @type {string[]} */
75-
let external = []
76-
if (!inlineDeps) {
77-
// cjs & esm-bundler: external all deps
78-
if (format === 'cjs' || format.includes('esm-bundler')) {
79-
external = [
80-
...external,
81-
...Object.keys(pkg.dependencies || {}),
82-
...Object.keys(pkg.peerDependencies || {}),
83-
// for @vue/compiler-sfc / server-renderer
84-
'path',
85-
'url',
86-
'stream',
87-
]
88-
}
71+
// resolve externals using shared function
72+
const external = inlineDeps
73+
? []
74+
: resolveExternal({
75+
pkg,
76+
format,
77+
target,
78+
isGlobalBuild: format === 'global',
79+
isBrowserESMBuild: format.includes('esm-browser'),
80+
isCompatPackage: target === 'vue-compat',
81+
packageOptions: pkg.buildOptions || {},
82+
})
8983

90-
if (target === 'compiler-sfc') {
91-
const consolidatePkgPath = require.resolve(
92-
'@vue/consolidate/package.json',
93-
{
94-
paths: [resolve(__dirname, `../packages/${target}/`)],
95-
},
96-
)
97-
const consolidateDeps = Object.keys(
98-
require(consolidatePkgPath).devDependencies,
99-
)
100-
external = [
101-
...external,
102-
...consolidateDeps,
103-
'fs',
104-
'vm',
105-
'crypto',
106-
'react-dom/server',
107-
'teacup/lib/express',
108-
'arc-templates/dist/es5',
109-
'then-pug',
110-
'then-jade',
111-
]
112-
}
113-
}
11484
/** @type {Array<import('esbuild').Plugin>} */
11585
const plugins = [
11686
{
@@ -127,6 +97,15 @@ for (const target of targets) {
12797
plugins.push(polyfillNode())
12898
}
12999

100+
// resolve defines using shared function
101+
const defines = resolveDefines({
102+
pkg,
103+
format,
104+
target,
105+
prod,
106+
commit: 'dev',
107+
})
108+
130109
esbuild
131110
.context({
132111
entryPoints: [resolve(__dirname, `${pkgBasePath}/src/index.ts`)],
@@ -138,25 +117,7 @@ for (const target of targets) {
138117
globalName: pkg.buildOptions?.name,
139118
platform: format === 'cjs' ? 'node' : 'browser',
140119
plugins,
141-
define: {
142-
__COMMIT__: `"dev"`,
143-
__VERSION__: `"${pkg.version}"`,
144-
__DEV__: prod ? `false` : `true`,
145-
__TEST__: `false`,
146-
__BROWSER__: String(
147-
format !== 'cjs' && !pkg.buildOptions?.enableNonBrowserBranches,
148-
),
149-
__GLOBAL__: String(format === 'global'),
150-
__ESM_BUNDLER__: String(format.includes('esm-bundler')),
151-
__ESM_BROWSER__: String(format.includes('esm-browser')),
152-
__CJS__: String(format === 'cjs'),
153-
__SSR__: String(format !== 'global'),
154-
__COMPAT__: String(target === 'vue-compat'),
155-
__FEATURE_SUSPENSE__: `true`,
156-
__FEATURE_OPTIONS_API__: `true`,
157-
__FEATURE_PROD_DEVTOOLS__: `false`,
158-
__FEATURE_PROD_HYDRATION_MISMATCH_DETAILS__: `true`,
159-
},
120+
define: defines,
160121
})
161122
.then(ctx => ctx.watch())
162123
}

0 commit comments

Comments
 (0)