Skip to content

Commit 6213b73

Browse files
committed
build: custom const enum processing
1 parent 53e3533 commit 6213b73

File tree

11 files changed

+243
-19
lines changed

11 files changed

+243
-19
lines changed

.eslintrc.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@ module.exports = {
1717
],
1818
// most of the codebase are expected to be env agnostic
1919
'no-restricted-globals': ['error', ...DOMGlobals, ...NodeGlobals],
20-
// since we target ES2015 for baseline support, we need to forbid object
21-
// rest spread usage in destructure as it compiles into a verbose helper.
22-
// TS now compiles assignment spread into Object.assign() calls so that
23-
// is allowed.
20+
2421
'no-restricted-syntax': [
2522
'error',
23+
// since we target ES2015 for baseline support, we need to forbid object
24+
// rest spread usage in destructure as it compiles into a verbose helper.
2625
'ObjectPattern > RestElement',
26+
// tsc compiles assignment spread into Object.assign() calls, but esbuild
27+
// still generates verbose helpers, so spread assignment is also prohiboted
28+
'ObjectExpression > SpreadElement',
2729
'AwaitExpression'
2830
]
2931
},

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
"node": ">=16.11.0"
5555
},
5656
"devDependencies": {
57+
"@babel/parser": "^7.20.15",
5758
"@babel/types": "^7.20.7",
5859
"@esbuild-plugins/node-modules-polyfill": "^0.1.4",
5960
"@microsoft/api-extractor": "~7.20.0",
@@ -83,6 +84,7 @@
8384
"jsdom": "^21.1.0",
8485
"lint-staged": "^10.2.10",
8586
"lodash": "^4.17.15",
87+
"magic-string": "^0.27.0",
8688
"marked": "^4.0.10",
8789
"minimist": "^1.2.0",
8890
"npm-run-all": "^4.1.5",

packages/compiler-ssr/src/errors.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,22 @@ export function createSSRCompilerError(
1717
}
1818

1919
export const enum SSRErrorCodes {
20-
X_SSR_UNSAFE_ATTR_NAME = DOMErrorCodes.__EXTEND_POINT__,
20+
X_SSR_UNSAFE_ATTR_NAME = 62 /* DOMErrorCodes.__EXTEND_POINT__ */,
2121
X_SSR_NO_TELEPORT_TARGET,
2222
X_SSR_INVALID_AST_NODE
2323
}
2424

25+
if (__TEST__) {
26+
// esbuild cannot infer const enum increments if first value is from another
27+
// file, so we have to manually keep them in sync. this check ensures it
28+
// errors out if there are collisions.
29+
if (SSRErrorCodes.X_SSR_UNSAFE_ATTR_NAME < DOMErrorCodes.__EXTEND_POINT__) {
30+
throw new Error(
31+
'SSRErrorCodes need to be updated to match extension point from core DOMErrorCodes.'
32+
)
33+
}
34+
}
35+
2536
export const SSRErrorMessages: { [code: number]: string } = {
2637
[SSRErrorCodes.X_SSR_UNSAFE_ATTR_NAME]: `Unsafe attribute name for SSR.`,
2738
[SSRErrorCodes.X_SSR_NO_TELEPORT_TARGET]: `Missing the 'to' prop on teleport element.`,

packages/reactivity/src/effect.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -248,10 +248,14 @@ export function trackEffects(
248248
dep.add(activeEffect!)
249249
activeEffect!.deps.push(dep)
250250
if (__DEV__ && activeEffect!.onTrack) {
251-
activeEffect!.onTrack({
252-
effect: activeEffect!,
253-
...debuggerEventExtraInfo!
254-
})
251+
activeEffect!.onTrack(
252+
extend(
253+
{
254+
effect: activeEffect!
255+
},
256+
debuggerEventExtraInfo!
257+
)
258+
)
255259
}
256260
}
257261
}

packages/reactivity/src/index.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export {
2828
shallowReadonly,
2929
markRaw,
3030
toRaw,
31-
ReactiveFlags,
31+
ReactiveFlags /* @remove */,
3232
type Raw,
3333
type DeepReadonly,
3434
type ShallowReactive,
@@ -66,4 +66,7 @@ export {
6666
getCurrentScope,
6767
onScopeDispose
6868
} from './effectScope'
69-
export { TrackOpTypes, TriggerOpTypes } from './operations'
69+
export {
70+
TrackOpTypes /* @remove */,
71+
TriggerOpTypes /* @remove */
72+
} from './operations'

packages/runtime-core/src/apiCreateApp.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import { warn } from './warning'
2222
import { createVNode, cloneVNode, VNode } from './vnode'
2323
import { RootHydrateFunction } from './hydration'
2424
import { devtoolsInitApp, devtoolsUnmountApp } from './devtools'
25-
import { isFunction, NO, isObject } from '@vue/shared'
25+
import { isFunction, NO, isObject, extend } from '@vue/shared'
2626
import { version } from '.'
2727
import { installAppCompatProperties } from './compat/global'
2828
import { NormalizedPropsOptions } from './componentProps'
@@ -193,7 +193,7 @@ export function createAppAPI<HostElement>(
193193
): CreateAppFunction<HostElement> {
194194
return function createApp(rootComponent, rootProps = null) {
195195
if (!isFunction(rootComponent)) {
196-
rootComponent = { ...rootComponent }
196+
rootComponent = extend({}, rootComponent)
197197
}
198198

199199
if (rootProps != null && !isObject(rootProps)) {

packages/runtime-core/src/apiWatch.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ import {
2222
remove,
2323
isMap,
2424
isSet,
25-
isPlainObject
25+
isPlainObject,
26+
extend
2627
} from '@vue/shared'
2728
import {
2829
currentInstance,
@@ -94,7 +95,7 @@ export function watchPostEffect(
9495
return doWatch(
9596
effect,
9697
null,
97-
__DEV__ ? { ...options, flush: 'post' } : { flush: 'post' }
98+
__DEV__ ? extend({}, options as any, { flush: 'post' }) : { flush: 'post' }
9899
)
99100
}
100101

@@ -105,7 +106,7 @@ export function watchSyncEffect(
105106
return doWatch(
106107
effect,
107108
null,
108-
__DEV__ ? { ...options, flush: 'sync' } : { flush: 'sync' }
109+
__DEV__ ? extend({}, options as any, { flush: 'sync' }) : { flush: 'sync' }
109110
)
110111
}
111112

packages/runtime-core/src/componentProps.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,7 @@ export function normalizePropsOptions(
522522
if (validatePropName(normalizedKey)) {
523523
const opt = raw[key]
524524
const prop: NormalizedProp = (normalized[normalizedKey] =
525-
isArray(opt) || isFunction(opt) ? { type: opt } : { ...opt })
525+
isArray(opt) || isFunction(opt) ? { type: opt } : extend({}, opt))
526526
if (prop) {
527527
const booleanIndex = getTypeIndex(Boolean, prop.type)
528528
const stringIndex = getTypeIndex(String, prop.type)

pnpm-lock.yaml

Lines changed: 5 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rollup.config.mjs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import terser from '@rollup/plugin-terser'
1212
import esbuild from 'rollup-plugin-esbuild'
1313
import alias from '@rollup/plugin-alias'
1414
import { entries } from './scripts/aliases.mjs'
15+
import { constEnum } from './scripts/const-enum.mjs'
16+
import { writeFileSync } from 'node:fs'
1517

1618
if (!process.env.TARGET) {
1719
throw new Error('TARGET package must be specified via --environment flag.')
@@ -31,6 +33,8 @@ const pkg = require(resolve(`package.json`))
3133
const packageOptions = pkg.buildOptions || {}
3234
const name = packageOptions.filename || path.basename(packageDir)
3335

36+
const [enumPlugin, enumDefines] = await constEnum()
37+
3438
const outputConfigs = {
3539
'esm-bundler': {
3640
file: resolve(`dist/${name}.esm-bundler.js`),
@@ -175,7 +179,7 @@ function createConfig(format, output, plugins = []) {
175179
// esbuild define is a bit strict and only allows literal json or identifiers
176180
// so we still need replace plugin in some cases
177181
function resolveReplace() {
178-
const replacements = {}
182+
const replacements = { ...enumDefines }
179183

180184
if (isProductionBuild && isBrowserBuild) {
181185
Object.assign(replacements, {
@@ -282,6 +286,7 @@ function createConfig(format, output, plugins = []) {
282286
alias({
283287
entries
284288
}),
289+
enumPlugin,
285290
...resolveReplace(),
286291
esbuild({
287292
tsconfig: path.resolve(__dirname, 'tsconfig.json'),

0 commit comments

Comments
 (0)