Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Enable tree shaking in next.js #8523

Merged
merged 154 commits into from
Jul 15, 2024
Merged

feat: Enable tree shaking in next.js #8523

merged 154 commits into from
Jul 15, 2024

Conversation

kdy1
Copy link
Member

@kdy1 kdy1 commented Jun 18, 2024

Description

I'll debug/fix tree shaking implementation with this PR

Testing Instructions

Copy link

vercel bot commented Jun 18, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
examples-nonmonorepo ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jul 15, 2024 6:51pm
rust-docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jul 15, 2024 6:51pm
8 Skipped Deployments
Name Status Preview Comments Updated (UTC)
examples-basic-web ⬜️ Ignored (Inspect) Visit Preview Jul 15, 2024 6:51pm
examples-designsystem-docs ⬜️ Ignored (Inspect) Visit Preview Jul 15, 2024 6:51pm
examples-gatsby-web ⬜️ Ignored (Inspect) Visit Preview Jul 15, 2024 6:51pm
examples-kitchensink-blog ⬜️ Ignored (Inspect) Visit Preview Jul 15, 2024 6:51pm
examples-native-web ⬜️ Ignored (Inspect) Visit Preview Jul 15, 2024 6:51pm
examples-svelte-web ⬜️ Ignored (Inspect) Visit Preview Jul 15, 2024 6:51pm
examples-tailwind-web ⬜️ Ignored (Inspect) Visit Preview Jul 15, 2024 6:51pm
examples-vite-web ⬜️ Ignored (Inspect) Visit Preview Jul 15, 2024 6:51pm

Copy link
Contributor

github-actions bot commented Jun 18, 2024

🟢 Turbopack Benchmark CI successful 🟢

Thanks

Copy link
Contributor

github-actions bot commented Jun 18, 2024

✅ This change can build next-swc

Copy link
Contributor

github-actions bot commented Jun 18, 2024

⚠️ CI failed ⚠️

The following steps have failed in CI:

  • Turbopack Rust tests (mac/win, non-blocking)

See workflow summary for details

Copy link
Member

@bgw bgw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for making this big change!

In the future to help make reviewing these PRs easier, it might be useful to squash some of the commits locally before force pushing or putting it up for review. Github (and Graphite) lists every commit in the conversation log, and it adds a lot of noise when there's 152 commits.

@kdy1 kdy1 merged commit e07022d into main Jul 15, 2024
54 of 55 checks passed
@kdy1 kdy1 deleted the kdy1/tree-shake branch July 15, 2024 18:56
@kdy1 kdy1 restored the kdy1/tree-shake branch July 15, 2024 18:59
kdy1 added a commit that referenced this pull request Jul 15, 2024
kdy1 added a commit that referenced this pull request Jul 15, 2024
Reverts #8523

We need to revert this due to some changes in turbopack code
@kdy1 kdy1 deleted the kdy1/tree-shake branch July 15, 2024 19:55
ForsakenHarmony pushed a commit to vercel/next.js that referenced this pull request Jul 25, 2024
### Description

I faced this bug while working on
vercel/turborepo#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.
ForsakenHarmony pushed a commit to vercel/next.js that referenced this pull request Jul 25, 2024
### Description

- next.js counterpart: #66689

### Testing Instructions
ForsakenHarmony pushed a commit to vercel/next.js that referenced this pull request Jul 25, 2024
Reverts vercel/turborepo#8523

We need to revert this due to some changes in turbopack code
ForsakenHarmony pushed a commit to vercel/next.js that referenced this pull request Jul 29, 2024
### Description

I faced this bug while working on
vercel/turborepo#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.
ForsakenHarmony pushed a commit to vercel/next.js that referenced this pull request Jul 29, 2024
### Description

- next.js counterpart: #66689

### Testing Instructions
ForsakenHarmony pushed a commit to vercel/next.js that referenced this pull request Jul 29, 2024
Reverts vercel/turborepo#8523

We need to revert this due to some changes in turbopack code
ForsakenHarmony pushed a commit to vercel/next.js that referenced this pull request Jul 29, 2024
### Description

I faced this bug while working on
vercel/turborepo#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.
ForsakenHarmony pushed a commit to vercel/next.js that referenced this pull request Jul 29, 2024
### Description

- next.js counterpart: #66689

### Testing Instructions
ForsakenHarmony pushed a commit to vercel/next.js that referenced this pull request Jul 29, 2024
Reverts vercel/turborepo#8523

We need to revert this due to some changes in turbopack code
ForsakenHarmony pushed a commit to vercel/next.js that referenced this pull request Aug 1, 2024
### Description

I faced this bug while working on
vercel/turborepo#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.
ForsakenHarmony pushed a commit to vercel/next.js that referenced this pull request Aug 1, 2024
### Description

- next.js counterpart: #66689

### Testing Instructions
ForsakenHarmony pushed a commit to vercel/next.js that referenced this pull request Aug 1, 2024
Reverts vercel/turborepo#8523

We need to revert this due to some changes in turbopack code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants