Skip to content

turbopack-240704.1

@kdy1 kdy1 tagged this 04 Jul 01:50
### Description

I faced this bug while working on
https://github.com/vercel/turbo/pull/8523. Tree shaking inevitably
creates direct imports for let bindings, and when the LHS of `+=` is
imported/exported, it triggers a bug.

The problematic input file looks like

```js
// Combined load times for loading client components
let clientComponentLoadStart = 0
let clientComponentLoadTimes = 0
let clientComponentLoadCount = 0

export function wrapClientComponentLoader(ComponentMod: any) {
  if (!('performance' in globalThis)) {
    return ComponentMod.__next_app__
  }

  return {
    require: (...args: any[]) => {
      const startTime = performance.now()

      if (clientComponentLoadStart === 0) {
        clientComponentLoadStart = startTime
      }

      try {
        clientComponentLoadCount += 1
        return ComponentMod.__next_app__.require(...args)
      } finally {
        clientComponentLoadTimes += performance.now() - startTime
      }
    },
    loadChunk: (...args: any[]) => {
      const startTime = performance.now()
      try {
        clientComponentLoadCount += 1
        return ComponentMod.__next_app__.loadChunk(...args)
      } finally {
        clientComponentLoadTimes += performance.now() - startTime
      }
    },
  }
}

export function getClientComponentLoaderMetrics(
  options: { reset?: boolean } = {}
) {
  const metrics =
    clientComponentLoadStart === 0
      ? undefined
      : {
          clientComponentLoadStart,
          clientComponentLoadTimes,
          clientComponentLoadCount,
        }

  if (options.reset) {
    clientComponentLoadStart = 0
    clientComponentLoadTimes = 0
    clientComponentLoadCount = 0
  }

  return metrics
}

```

and it works if I apply this PR or I replace `clientComponentLoadTimes
+= performance.now() - startTime` with `clientComponentLoadTimes =
clientComponentLoadTimes + performance.now() - startTime`.


### Testing Instructions

Test references changed quite a lot.
Assets 2
Loading