Introducing Turbopack: The Lightning-Fast Successor to Webpack

Discover the key differences between Turbopack and Webpack for JavaScript bundling. Explore performance, features, and configuration options.

Hey friends! Are you tired of waiting around for your apps to build? Do you feel like you spend more time staring at progress bars than actually coding? Well, do I have exciting news for you! There's a new kid in town called Turbopack that is about to change the game when it comes to JavaScript bundling.

In this article, we'll dive into what exactly Turbopack is, why it blows Webpack out of the water in terms of performance, and how you can start using it today to supercharge your development workflow. Get ready to be amazed!

The Problem with Webpack

For years, Webpack has been the go-to bundler for JavaScript applications. It lets you bundle up all your code and assets into neat little packages that can be deployed to production. However, as your projects grow in size and complexity, Webpack starts to show some cracks.

The main issue is that Webpack rebuilds your entire bundle every time you make a change to your source code. For small apps, this isn't a big deal. But as your app grows to thousands of modules, these full rebuilds really start to hurt.

Just imagine you have a simple little React app with a few components. Every time you tweak a line of code, you have to wait for Webpack to rebundle everything before you can test that change. It starts off fast, but gets slower and slower as your app grows.

The hassle intensifies when you have a big enterprise app with tons of dependencies. A full rebuild could take minutes! 😱 This painful feedback loop kills your productivity as a developer.

Turbopack to the Rescue

The wizards at Vercel recognized this problem and knew there had to be a better way. That's why they created Turbopack - an ultra-fast JavaScript bundler that takes incremental builds to the next level.

Instead of BUNDLING bundling everything from scratch, Turbopack is smart. It keeps tabs on which files and dependencies have changed and only rebuilds what is absolutely necessary. This means lightning-fast iteration speeds, even on huge codebases!

The core of Turbopack is powered by a technology called Turbo, which handles all of the dependency tracking and incremental builds under the hood. By leveraging optimized caching and parallel processing, Turbopack can rebuild apps hundreds of times faster than Webpack.

When testing on a massive app with over 5,000 modules, Turbopack was able to startup and rebuild in just 4 seconds! By comparison, Webpack took 16 seconds and Vite took 11 seconds. As you can see, Turbopack smokes the competition πŸŽπŸ’¨.

Blazing Fast HMR with Turbopack

One area where the speed gains of Turbopack really shine is with Hot Module Replacement (HMR). This feature allows you to get instant feedback in the browser when you make changes to your code without needing a full refresh.

However, as your app grows in size, HMR can start to bog down. With Webpack, you may have to wait several seconds before seeing your changes reflected. But with Turbopack's incremental architecture, HMR updates happen almost instantly no matter how big your app is!

Just check out this example of editing a React component with Turbopack HMR:

// App.js

export default function App() {
  return <h1>My App</h1>; 
}

As soon as I change the text to My Awesome App and save, the change shows up immediately in the browser without any delay. It really feels like you are editing code in real-time - the future is now!

This speedy feedback loop is an absolute game-changer. You no longer have to context switch between writing code and testing - you can just code in flow while seeing updates in real-time. It's a magical experience!

Turbopack Architecture

So how does Turbopack work under the hood to deliver such incredible performance? The secret lies in its unique incremental architecture.

Rather than bundling all your code into one huge output chunk like Webpack, Turbopack splits your code into many small chunks that can be rebuilt independently. Each chunk is memoized, meaning Turbopack caches the output so it never has to rebuild the same chunk twice.

When a file changes, Turbopack invalidates any caches related to that file and rebuilds only what is necessary. This granular level of caching and rebuilding is what enables orders-of-magnitude faster performance.

Turbopack is able to track dependencies and build these intelligent incremental updates thanks to the Turbo framework. Turbo handles all the complexity of dependency management so Turbopack can focus on building a blazing fast developer experience.

And because Turbopack and Turbo are built in Rust rather than JavaScript, they avoid performance pitfalls like garbage collection delays. The end result is a lean, mean bundling machine! πŸ’ͺ

Using Turbopack Today

Now that your mind is blown about how fast Turbopack is, you're probably wondering how you can start using it in your own projects.

The easiest way is to upgrade to Next.js 13. The latest version of Next.js ships with built-in support for Turbopack, allowing you to immediately take advantage of all that sweet sweet speed!

To try it out, just run:

npm i next@latest
next dev --turbo

For non-Next.js projects, the Turbopack team is working on making it framework-agnostic so anyone can benefit. But for now, Next.js is your gateway to the future.

In addition to raw speed, Turbopack will also open the door to lots of other future performance optimizations...

Migrating to Turbopack

If you're an existing Webpack user, you may be wondering how difficult it is to migrate your project over to Turbopack. The good news is the Turbopack team has designed it to be incrementally adoptable.

Most Webpack configurations should "just work" when swapped over to Turbopack. The API is intended to be familiar to those coming from Webpack.

That said, Turbopack does diverge from Webpack in some areas:

  • It does not support Webpack plugins directly. The goal is for core plugins to be absorbed into Turbopack itself over time.
  • Loader syntax is different to align with modern ESM. Instead of use: ['style-loader'], you now do loader: 'style-loader'.
  • Code splitting API aligns with standard ESM dynamic import() instead of Webpack's require.ensure.

So minor refactoring may be required in some cases, but overall the transition should be smooth. Turbopack also aims to provide migration tools and docs to help ease the path.

Here is a simple example of migrating from Webpack to Turbopack:

// webpack.config.js

module.exports = {
  // ...
  module: {
    rules: [
      {
        test: /\.css$/,
        use: ['style-loader', 'css-loader']  
      }
    ]
  }
};
// turbopack.config.js 

export default {
  // ...
  module: {
    rules: [
      {
        test: /\.css$/,
        loader: 'style-loader!css-loader'
      }
    ]
  }
}

As you can see, it's the same core configuration but with slight syntactic differences.

For most projects, migrating to Turbopack should be relatively straightforward. And the long-term speed and performance benefits are well worth it!

Turbopack - Ready for Production

Turbopack is still experimental technology today. But the goal is to make it production-ready as soon as possible.

Before Turbopack can be used for production deploys, there are a few remaining gaps that need to be filled:

  • Code splitting: While Turbopack supports basic code splitting with import(), more advanced strategies like dynamic prefetching and loading still need implementation.
  • Caching: Production caching mechanisms for outputs need development. The goal is to share cached artifacts between builds.
  • Plugins: Most Webpack plugins do not work with Turbopack today. Critical plugins like Terser for minification need integration.
  • Framework Integrations: More frameworks like Vue, Angular, and Svelte need adapters to ensure seamless integration with Turbopack.

So there is still work to be done. But the Turbopack team is rapidly cranking away on filling these gaps.

Given the current pace of innovation, I expect Turbopack will be production-ready within a few months. And once ready, it's going to massively disrupt the JavaScript bundling ecosystem.

The future is bright my friends. Turbopack will soon be taking production apps by storm!

Turbocharge Your Apps with Turbopack

Alright friends - we've covered a ton of ground today on Turbopack and why it is set to be a gamechanger. Let's do a quick recap:

Turbopack is a next-gen JavaScript bundler that massively speeds up build times via incremental builds.

It powers lightning fast HMR for instant code feedback.

The unique caching architecture means only modified code needs to be rebuilt.

Easy to incrementally adopt in Webpack projects. Migration path is smooth.

Once production-ready it will disrupt JS bundling with its speed.

Try it today in Next.js 13 for a taste of the future!

If you made it this far, you now know everything you need to start turbocharging your apps with the power of Turbopack. No more sluggish bundling or interminable wait times. Just pure, unadulterated JavaScript bliss 😊.

Now go forth and dev speedily my friends! Let me know if you have any other questions.

Subscribe to JS Dev Journal

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
jamie@example.com
Subscribe