build: generate more treeshaking friendly code

This commit is contained in:
Evan You 2021-09-16 10:56:34 -04:00
parent a6e5f82d8e
commit a31303f835
2 changed files with 131 additions and 124 deletions

View File

@ -60,12 +60,13 @@ export function renderComponentRoot(
} = instance } = instance
let result let result
let fallthroughAttrs
const prev = setCurrentRenderingInstance(instance) const prev = setCurrentRenderingInstance(instance)
if (__DEV__) { if (__DEV__) {
accessedAttrs = false accessedAttrs = false
} }
try { try {
let fallthroughAttrs
if (vnode.shapeFlag & ShapeFlags.STATEFUL_COMPONENT) { if (vnode.shapeFlag & ShapeFlags.STATEFUL_COMPONENT) {
// withProxy is a proxy with a different `has` trap only for // withProxy is a proxy with a different `has` trap only for
// runtime-compiled render functions using `with` block. // runtime-compiled render functions using `with` block.
@ -110,6 +111,11 @@ export function renderComponentRoot(
? attrs ? attrs
: getFunctionalFallthrough(attrs) : getFunctionalFallthrough(attrs)
} }
} catch (err) {
blockStack.length = 0
handleError(err, instance, ErrorCodes.RENDER_FUNCTION)
result = createVNode(Comment)
}
// attr merging // attr merging
// in dev mode, comments are preserved, and it's possible for a template // in dev mode, comments are preserved, and it's possible for a template
@ -227,11 +233,6 @@ export function renderComponentRoot(
} else { } else {
result = root result = root
} }
} catch (err) {
blockStack.length = 0
handleError(err, instance, ErrorCodes.RENDER_FUNCTION)
result = createVNode(Comment)
}
setCurrentRenderingInstance(prev) setCurrentRenderingInstance(prev)
return result return result

View File

@ -1,5 +1,5 @@
import { ErrorCodes, callWithErrorHandling } from './errorHandling' import { ErrorCodes, callWithErrorHandling } from './errorHandling'
import { isArray } from '@vue/shared' import { isArray, NOOP } from '@vue/shared'
import { ComponentInternalInstance, getComponentName } from './component' import { ComponentInternalInstance, getComponentName } from './component'
import { warn } from './warning' import { warn } from './warning'
@ -128,10 +128,7 @@ function queueCb(
if (!isArray(cb)) { if (!isArray(cb)) {
if ( if (
!activeQueue || !activeQueue ||
!activeQueue.includes( !activeQueue.includes(cb, cb.allowRecurse ? index + 1 : index)
cb,
cb.allowRecurse ? index + 1 : index
)
) { ) {
pendingQueue.push(cb) pendingQueue.push(cb)
} }
@ -241,11 +238,20 @@ function flushJobs(seen?: CountMap) {
// its update can be skipped. // its update can be skipped.
queue.sort((a, b) => getId(a) - getId(b)) queue.sort((a, b) => getId(a) - getId(b))
// conditional usage of checkRecursiveUpdate must be determined out of
// try ... catch block since Rollup by default de-optimizes treeshaking
// inside try-catch. This can leave all warning code unshaked. Although
// they would get eventually shaken by a minifier like terser, some minifiers
// would fail to do that (e.g. https://github.com/evanw/esbuild/issues/1610)
const check = __DEV__
? (job: SchedulerJob) => checkRecursiveUpdates(seen!, job)
: NOOP
try { try {
for (flushIndex = 0; flushIndex < queue.length; flushIndex++) { for (flushIndex = 0; flushIndex < queue.length; flushIndex++) {
const job = queue[flushIndex] const job = queue[flushIndex]
if (job && job.active !== false) { if (job && job.active !== false) {
if (__DEV__ && checkRecursiveUpdates(seen!, job)) { if (__DEV__ && check(job)) {
continue continue
} }
// console.log(`running:`, job.id) // console.log(`running:`, job.id)