question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Is there an example for nextjs?

See original GitHub issue

Hi!

I would love to try esbuild-loader with nextjs. Do you know any examples for a working configuration of nextjs with esbuild-loader?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:40
  • Comments:13 (3 by maintainers)

github_iconTop GitHub Comments

10reactions
kuasha420commented, Jan 7, 2021

Hello,

I’ve tried to configure esbuild with nextjs and got some success. Here’s the config.

const {
  ESBuildPlugin,
  // ESBuildMinifyPlugin
} = require('esbuild-loader');

const tsconfig = require('./tsconfig.json');

module.exports = {
  webpack: (config, { webpack, dev }) => {
    if (!dev) {
      config.plugins.push(
        new webpack.ProvidePlugin({
          /**
           *  Not sure why, This increases bundle size
           * when you would think the oposite would be true!
           */
          // __jsx: ['react', 'createElement'],
          // __fragment: ['react', 'Fragment'],
          React: 'react',
        }),
      );
      config.plugins.push(new ESBuildPlugin());
      const convertToESBuild = (obj) => {
        if (obj.loader === 'next-babel-loader') {
          return {
            loader: 'esbuild-loader',
            options: {
              loader: 'tsx',
              target: 'es2017',
              tsconfigRaw: tsconfig,
              // jsxFactory: '__jsx',
              // jsxFragment: '__fragment',
            },
          };
        }
        return obj;
      };

      const rule = config.module.rules[0];
      if (rule) {
        if (Array.isArray(rule.use)) {
          rule.use = rule.use.map((e) => {
            if (typeof e === 'object') {
              return convertToESBuild(e);
            }
            return e;
          });
        } else {
          rule.use = convertToESBuild(rule.use);
        }
      }

      /**
       *  Not sure why, but ESBuildMinifyPlugin makes the bundle larger.
       *
       * With Default Minimization:
       * Main Bundle: 239 KB
       * Largest Page: 122 KB
       *
       * With ESBuild Minification:
       * Main Bundle: 664 KB
       * Largest Page: 132 KB
       *
       * Probably a configuration error.
       */

      // // Remove Default TerserPlugin
      // config.optimization.minimizer.shift();

      // // Add ESBuild Minify
      // config.optimization.minimizer.unshift(
      //   new ESBuildMinifyPlugin({
      //     target: 'es2017',
      //     minify: true,
      //   }),
      // );
    }
    return config;
  },
};
  1. As you can notice, this is only for prod. In dev, the compilation works fine, but next router breaks.
  2. Haven’t quite figured out how to configure Minimizer yet.
  3. Config inspired from #62
6reactions
privatenumbercommented, Feb 28, 2021

Thanks @tdjsnelling

I added a Next.js example here: https://github.com/privatenumber/esbuild-loader-examples/tree/master/examples/next

Hope that helps.

Closing this as I think it’s addressed but will re-open if there’s anything more to do.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Showcase | Next.js
Meet hundreds of beautiful websites built with Next.js by Vercel · Netflix Jobs · TikTok · Twitch · Hulu · Notion · Target...
Read more >
Examples of next.js - GitHub
No information is available for this page.
Read more >
Next.js tutorial with examples: Build better React apps with Next
Next.js is a React front-end framework that lets you improve your app's user-experience and SEO through server-side rendering.
Read more >
18 Great Examples of Next.js Websites - Pagepro
Examples of big eCommerce Next js websites: ... Deliveroo is a food delivery app that helps with ordering food from restaurants and takeaways....
Read more >
Find your Next.js templates - Vercel
Find your Next.js templates. Jumpstart your app development process with our pre-built Next.js solutions. Filter By: Filter Templates. Clear. Framework.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found