Giter VIP home page Giter VIP logo

esbuild-loader's Introduction

esbuild-loader

Speed up your Webpack build with esbuild! 🔥

esbuild is a JavaScript bundler written in Go that supports blazing fast ESNext & TypeScript transpilation and JS minification.

esbuild-loader lets you harness the speed of esbuild in your Webpack build by offering faster alternatives for transpilation (eg. babel-loader/ts-loader) and minification (eg. Terser)!

Curious how much faster your build will be? See what users are saying.

Tip

Are you using TypeScript with Node.js?

Supercharge your Node.js with TypeScript support using tsx!

tsx is a simple, lightweight, and blazing fast alternative to ts-node.

→ Learn more about tsx


Premium sponsor banner

🚀 Install

npm i -D esbuild-loader

🚦 Quick Setup

To leverage esbuild-loader in your Webpack configuration, add a new rule for esbuild-loader matching the files you want to transform, such as .js, .jsx, .ts, or .tsx. Make sure to remove any other loaders you were using before (e.g. babel-loader/ts-loader).

Here's an example of how to set it up in your webpack.config.js:

  module.exports = {
      module: {
          rules: [
-             // Transpile JavaScript
-             {
-                 test: /\.js$/,
-                 use: 'babel-loader'
-             },
-
-             // Compile TypeScript
-             {
-                 test: /\.tsx?$/,
-                 use: 'ts-loader'
-             },
+             // Use esbuild to compile JavaScript & TypeScript
+             {
+                 // Match `.js`, `.jsx`, `.ts` or `.tsx` files
+                 test: /\.[jt]sx?$/,
+                 loader: 'esbuild-loader',
+                 options: {
+                     // JavaScript version to compile to
+                     target: 'es2015'
+                 }
+             },

              // Other rules...
          ],
      },
  }

In this setup, esbuild will automatically determine how to handle each file based on its extension:

  • .js files will be treated as JS (no JSX allowed)
  • .jsx as JSX
  • .ts as TS (no TSX allowed)
  • .tsx as TSX

If you want to force a specific loader on different file extensions (e.g. to allow JSX in .js files), you can use the loader option:

 {
     test: /\.js$/,
     loader: 'esbuild-loader',
     options: {
+        // Treat `.js` files as `.jsx` files
+        loader: 'jsx',

         // JavaScript version to transpile to
         target: 'es2015'
     }
 }

Loader

JavaScript

esbuild-loader can be used in-place of babel-loader to transpile new JavaScript syntax into code compatible with older JavaScript engines.

While this ensures your code can run smoothly across various environments, note that it can bloat your output code (like Babel).

The default target is esnext, which means it doesn't perform any transpilations.

To specify a target JavaScript engine that only supports ES2015, use the following configuration in your webpack.config.js:

 {
     test: /\.jsx?$/,
     loader: 'esbuild-loader',
     options: {
+        target: 'es2015',
     },
 }

For a detailed list of supported transpilations and versions, refer to the esbuild documentation.

TypeScript

esbuild-loader can be used in-place of ts-loader to compile TypeScript.

{
    // `.ts` or `.tsx` files
    test: /\.tsx?$/,
    loader: 'esbuild-loader',
}

Important

It's possible to use loader: 'tsx' for both .ts and .tsx files, but this could lead to unexpected behavior as TypeScript and TSX do not have compatible syntaxes.

→ Read more

tsconfig.json

If you have a tsconfig.json file in your project, esbuild-loader will automatically load it.

If it's under a custom name, you can pass in the path via tsconfig option:

 {
     test: /\.tsx?$/,
     loader: 'esbuild-loader',
     options: {
+        tsconfig: './tsconfig.custom.json',
     },
 },

Behind the scenes: get-tsconfig is used to load the tsconfig, and to also resolve the extends property if it exists.

The tsconfigRaw option can be used to pass in a raw tsconfig object, but it will not resolve the extends property.

Caveats
  • esbuild only supports a subset of tsconfig options (see TransformOptions interface).

  • Enable isolatedModules to avoid mis-compilation with features like re-exporting types.

  • Enable esModuleInterop to make TypeScript's type system compatible with ESM imports.

  • Features that require type interpretation, such as emitDecoratorMetadata and declaration, are not supported.

→ Read more about TypeScript Caveats

tsconfig.json Paths

Use tsconfig-paths-webpack-plugin to add support for tsconfig.json#paths.

Since esbuild-loader only transforms code, it cannot aid Webpack with resolving paths.

Type-checking

esbuild does not type check your code. And according to the esbuild FAQ, it will not be supported.

Consider these type-checking alternatives:

EsbuildPlugin

Minification

Esbuild supports JavaScript minification, offering a faster alternative to traditional JS minifiers like Terser or UglifyJs. Minification is crucial for reducing file size and improving load times in web development. For a comparative analysis of its performance, refer to these minification benchmarks.

In webpack.config.js:

+ const { EsbuildPlugin } = require('esbuild-loader')

  module.exports = {
      ...,

+     optimization: {
+         minimizer: [
+             new EsbuildPlugin({
+                 target: 'es2015'  // Syntax to transpile to (see options below for possible values)
+             })
+         ]
+     },
  }

Tip

Utilizing the target option allows for the use of newer JavaScript syntax, enhancing minification effectiveness.

Defining constants

Webpack's DefinePlugin can replaced with EsbuildPlugin to define global constants. This could speed up the build by removing the parsing costs associated with the DefinePlugin.

In webpack.config.js:

- const { DefinePlugin } = require('webpack')
+ const { EsbuildPlugin } = require('esbuild-loader')

  module.exports = {
      // ...,

      plugins:[
-         new DefinePlugin({
-             'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV),
-         })
+         new EsbuildPlugin({
+             define: {
+                 'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV),
+             },
+         }),
      ]
  }

Transpilation

If your project does not use TypeScript, JSX, or any other syntax that requires additional configuration beyond what Webpack provides, you can use EsbuildPlugin for transpilation instead of the loader.

It will be faster because there's fewer files to process, and will produce a smaller output because polyfills will only be added once for the entire build as opposed to per file.

To utilize esbuild for transpilation, simply set the target option on the plugin to specify which syntax support you want.

CSS Minification

Depending on your setup, there are two ways to minify CSS. You should already have CSS loading setup using css-loader.

CSS assets

If the CSS is extracted and emitted as .css file, you can replace CSS minification plugins like css-minimizer-webpack-plugin with the EsbuildPlugin.

Assuming the CSS is extracted using something like MiniCssExtractPlugin, in webpack.config.js:

  const { EsbuildPlugin } = require('esbuild-loader')
  const MiniCssExtractPlugin = require('mini-css-extract-plugin');

  module.exports = {
      // ...,

      optimization: {
          minimizer: [
              new EsbuildPlugin({
                  target: 'es2015',
+                 css: true  // Apply minification to CSS assets
              })
          ]
      },

      module: {
          rules: [
              {
                  test: /\.css$/i,
                  use: [
                      MiniCssExtractPlugin.loader,
                      'css-loader'
                  ]
              }
          ],
      },

      plugins: [
          new MiniCssExtractPlugin()
      ]
  }

CSS in JS

If your CSS is not emitted as a .css file, but rather injected with JavaScript using something like style-loader, you can use the loader for minification.

In webpack.config.js:

  module.exports = {
      // ...,

      module: {
          rules: [
              {
                  test: /\.css$/i,
                  use: [
                      'style-loader',
                      'css-loader',
+                     {
+                         loader: 'esbuild-loader',
+                         options: {
+                             minify: true,
+                         },
+                     },
                  ],
              },
          ],
      },
  }

Bring your own esbuild (Advanced)

esbuild-loader comes with a version of esbuild it has been tested to work with. However, esbuild has a frequent release cadence, and while we try to keep up with the important releases, it can get outdated.

To work around this, you can use the implementation option in the loader or the plugin to pass in your own version of esbuild (eg. a newer one).

Warning

⚠esbuild is not stable yet and can have dramatic differences across releases. Using a different version of esbuild is not guaranteed to work.

+ const esbuild = require('esbuild')

  module.exports = {
      // ...,

      module: {
          rules: [
              {
                  test: ...,
                  loader: 'esbuild-loader',
                  options: {
                      // ...,
+                     implementation: esbuild,
                  },
              },
          ],
      },
  }

Setup examples

If you'd like to see working Webpack builds that use esbuild-loader for basic JS, React, TypeScript, Next.js, etc. check out the examples repo:

→ esbuild-loader examples


Premium sponsor banner

⚙️ Options

Loader

The loader supports all Transform options from esbuild.

Note:

  • Source-maps are automatically configured for you via devtool. sourcemap/sourcefile options are ignored.
  • The root tsconfig.json is automatically detected for you. You don't need to pass in tsconfigRaw unless it's in a different path.

Here are some common configurations and custom options:

tsconfig

Type: string

Pass in the file path to a custom tsconfig file. If the file name is tsconfig.json, it will automatically detect it.

target

Type: string | Array<string>

Default: 'es2015'

The target environment (e.g. es2016, chrome80, esnext).

Read more about it in the esbuild docs.

loader

Type: 'js' | 'jsx' | 'ts' | 'tsx' | 'css' | 'json' | 'text' | 'base64' | 'file' | 'dataurl' | 'binary' | 'default'

Default: 'default'

The loader to use to handle the file. See the type for possible values.

By default, it automatically detects the loader based on the file extension.

Read more about it in the esbuild docs.

jsxFactory

Type: string

Default: React.createElement

Customize the JSX factory function name to use.

Read more about it in the esbuild docs.

jsxFragment

Type: string

Default: React.Fragment

Customize the JSX fragment function name to use.

Read more about it in the esbuild docs.

implementation

Type: { transform: Function }

Custom esbuild-loader option.

Use it to pass in a different esbuild version.

EsbuildPlugin

The loader supports all Transform options from esbuild.

target

Type: string | Array<string>

Default: 'esnext'

Target environment (e.g. 'es2016', ['chrome80', 'esnext'])

Read more about it in the esbuild docs.

Here are some common configurations and custom options:

format

Type: 'iife' | 'cjs' | 'esm'

Default:

  • iife if both of these conditions are met:
    • Webpack's target is set to web
    • esbuild's target is not esnext
  • undefined (no format conversion) otherwise

The default is iife when esbuild is configured to support a low target, because esbuild injects helper functions at the top of the code. On the web, having functions declared at the top of a script can pollute the global scope. In some cases, this can lead to a variable collision error. By setting format: 'iife', esbuild wraps the helper functions in an IIFE to prevent them from polluting the global.

Read more about it in the esbuild docs.

minify

Type: boolean

Default: true

Enable JS minification. Enables all minify* flags below.

To have nuanced control over minification, disable this and enable the specific minification you want below.

Read more about it in the esbuild docs.

minifyWhitespace

Type: boolean

Minify JS by removing whitespace.

minifyIdentifiers

Type: boolean

Minify JS by shortening identifiers.

minifySyntax

Type: boolean

Minify JS using equivalent but shorter syntax.

legalComments

Type: 'none' | 'inline' | 'eof' | 'external'

Default: 'inline'

Read more about it in the esbuild docs.

css

Type: boolean

Default: false

Whether to minify CSS files.

include

Type: string | RegExp | Array<string | RegExp>

To only apply the plugin to certain assets, pass in filters include

exclude

Type: string | RegExp | Array<string | RegExp>

To prevent the plugin from applying to certain assets, pass in filters to exclude

implementation

Type: { transform: Function }

Use it to pass in a different esbuild version.

💡 Support

For personalized assistance, take advantage of my Priority Support service.

Whether it's about Webpack configuration, esbuild, or TypeScript, I'm here to guide you every step of the way!

🙋‍♀️ FAQ

Is it possible to use esbuild plugins?

No. esbuild plugins are only available in the build API. And esbuild-loader uses the transform API instead of the build API for two reasons:

  1. The build API is for creating JS bundles, which is what Webpack does. If you want to use esbuild's build API, consider using esbuild directly instead of Webpack.

  2. The build API reads directly from the file-system, but Webpack loaders operate in-memory. Webpack loaders are essentially just functions that are called with the source-code as the input. Not reading from the file-system allows loaders to be chainable. For example, using vue-loader to compile Single File Components (.vue files), then using esbuild-loader to transpile just the JS part of the SFC.

Is it possible to use esbuild's inject option?

No. The inject option is only available in the build API. And esbuild-loader uses the transform API.

However, you can use the Webpack equivalent ProvidePlugin instead.

If you're using React, check out this example on how to auto-import React in your components.

Is it possible to use Babel plugins?

No. If you really need them, consider porting them over to a Webpack loader.

And please don't chain babel-loader and esbuild-loader. The speed gains come from replacing babel-loader.

Why am I not getting a 100x speed improvement as advertised?

Running esbuild as a standalone bundler vs esbuild-loader + Webpack are completely different:

  • esbuild is highly optimized, written in Go, and compiled to native code. Read more about it here.
  • esbuild-loader is handled by Webpack in a JS runtime, which applies esbuild transforms per file. On top of that, there's likely other loaders & plugins in a Webpack config that slow it down.

Using a JS runtime introduces a bottleneck that makes reaching those speeds impossible. However, esbuild-loader can still speed up your build by removing the bottlenecks created by babel-loader, ts-loader, Terser, etc.

💞 Related projects

Node.js enhanced with esbuild to run TypeScript and ESM.

Webpack-integrated Mocha test-runner with Webpack 5 support.

Localize/i18nalize your Webpack build. Optimized for multiple locales!

Sponsors

Premium sponsor banner Premium sponsor banner

esbuild-loader's People

Contributors

airhorns avatar arthurfalcao avatar chevelleboyer avatar connorads avatar d34thwings avatar danburzo avatar dartess avatar davidmurdoch avatar dr-js avatar eamodio avatar egoist avatar eliseumds avatar escapedcat avatar g-plane avatar itsoli avatar johnnyreilly avatar neogeneva avatar panta82 avatar piggynl avatar prettywood avatar privatenumber avatar raygesualdo avatar rowanoulton avatar silverwind avatar zhusjfaker avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

esbuild-loader's Issues

The automated release is failing 🚨

🚨 The automated release from the master branch failed. 🚨

I recommend you give this issue a high priority, so other packages depending on you could benefit from your bug fixes and new features.

You can find below the list of errors reported by semantic-release. Each one of them has to be resolved in order to automatically publish your package. I’m sure you can resolve this 💪.

Errors are usually caused by a misconfiguration or an authentication problem. With each error reported below you will find explanation and guidance to help you to resolve it.

Once all the errors are resolved, semantic-release will release your package the next time you push a commit to the master branch. You can also manually restart the failed CI job that runs semantic-release.

If you are not sure how to resolve this, here is some links that can help you:

If those don’t help, or if this issue is reporting something you think isn’t right, you can always ask the humans behind semantic-release.


No npm token specified.

An npm token must be created and set in the NPM_TOKEN environment variable on your CI environment.

Please make sure to create an npm token and to set it in the NPM_TOKEN environment variable on your CI environment. The token must allow to publish to the registry https://registry.npmjs.org/.


Good luck with your project ✨

Your semantic-release bot 📦🚀

How does it work with CRA

Hi @egoist , how does this loader work for a react app created by CRA (and TS), I tried but failed, here is my config-overrides.js:

const useEsBuild = () => (config) => {
  const rules = config.module.rules.find((rule) => Array.isArray(rule.oneOf))
    .oneOf
  const jsRule = rules.find(
    (rule) =>
      rule.loader &&
      rule.loader.endsWith('babel-loader/lib/index.js') &&
      rule.include
  )
  if (jsRule) {
    jsRule.loader = 'esbuild-loader'
    jsRule.options = {
      // ...jsRule.options,
      // All options are optional
      target: 'es2015', // default, or 'es20XX', 'esnext'
      jsxFactory: 'React.createElement',
      jsxFragment: 'React.Fragment',
      sourceMap: false, // Enable sourcemap
    }
    addWebpackPlugin(new ESBuildPlugin())(config)
  }

  return config
}


module.exports = override(
  ...,
  useEsBuild()
)

It reports error about Error parsing bundle asset ...:

Error parsing bundle asset ".../build/static/js/0.df163045.chunk.js": no such file
Error parsing bundle asset ".../build/static/js/1.ab303cfe.chunk.js": no such file
Error parsing bundle asset ".../build/static/js/11.7c137ba2.chunk.js": no such file
Error parsing bundle asset ".../build/static/js/12.9a6799eb.chunk.js": no such file
Error parsing bundle asset ".../build/static/js/13.a4ecc160.chunk.js": no such file
Error parsing bundle asset ".../build/static/js/14.78ed2e9d.chunk.js": no such file 
...

It would be very appreciated if you can supply an example, thanks in advance!

Typeissue with Webpack v4 (Cannot find module 'webpack5' or its corresponding type declarations)

We're using Webpack v4 in our projekt.
Now our typecheck fails with:

$ tsc --noEmit
node_modules/esbuild-loader/dist/minify-plugin.d.ts(3,27): error TS2307: Cannot find module 'webpack5' or its corresponding type declarations.
error Command failed with exit code 2.

Might be related to this:
https://github.com/privatenumber/esbuild-loader/blob/develop/src/minify-plugin.ts#L5-L6

This loader seems to solve it with this:
https://github.com/privatenumber/esbuild-loader/blob/develop/package.json#L58

I guess we could also install webpack v5. But only to fix typings this seems a bit weird.
You have any ideas how this could be solved or changed? If you can point to the right direction I'm happy to provide a PR.

Add test, include, and exclude options to ESBuildMinifyPlugin like the Terser Webpack plugin

I have a complex Webpack build where we have to use Terser for one chunk (there's some very strict mangling rules we have to follow). I want to use ESBuildMinifyPlugin for the rest of the chunks, but without the ability to filter chunks, we'd be double-minifying the special chunk, which would break things. I dug into the Terser plugin and it seems there's some Webpack code we could use fairly straightforwardly:

https://github.com/webpack-contrib/terser-webpack-plugin/blob/809ef42f3ad128693862a535a647305702d11eb8/src/index.js#L210-L218

The referenced Webpack helper is straightforward as well:

https://github.com/webpack/webpack/blob/2efeb4b5787e868e69233aa9dec469c200fa3b2c/lib/ModuleFilenameHelpers.js#L162-L179

Spread options in loader

This is already being done in minify plugin, wouldn't it make sense to also do this in the loader?
I want to pass "platform" option, but I noticed it won't accept this in it's current state.

Is there an example for nextjs?

Hi!

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

adapt webpack 1.x or 2.x?

in my old project , using webpack 1.x. And I use esbuild-loader replace babel-loader will print error

` building for production.../Users/wanglihui/WebstoreProject/onionfph5/project/node_modules/esbuild-loader/dist/plugin.js:12
compiler.hooks.thisCompilation.tap('esbuild', compilation => {
^

TypeError: Cannot read property 'thisCompilation' of undefined
at ESBuildPlugin.apply (/Users/wanglihui/WebstoreProject/onionfph5/project/node_modules/esbuild-loader/dist/plugin.js:12:24)
at Compiler.apply (/Users/wanglihui/WebstoreProject/onionfph5/project/node_modules/tapable/lib/Tapable.js:164:16)
at WebpackOptionsApply.process (/Users/wanglihui/WebstoreProject/onionfph5/project/node_modules/webpack/lib/WebpackOptionsApply.js:62:18)
at webpack (/Users/wanglihui/WebstoreProject/onionfph5/project/node_modules/webpack/lib/webpack.js:22:48)
at /Users/wanglihui/WebstoreProject/onionfph5/project/build/build.js:52:3
at Array.map ()
at Object. (/Users/wanglihui/WebstoreProject/onionfph5/project/build/build.js:51:14)
at Module._compile (internal/modules/cjs/loader.js:1138:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)
at Module.load (internal/modules/cjs/loader.js:986:32)`

Usage in React app (Webpack 4.44.2)

Hi

Thanks for this project.

I've trying to add the loader in a React app (not create-react-app, app with Webpack 4.44.2) however I'm getting the following error:

ERROR in ./app/application.js
Module build failed (from ./node_modules/esbuild-loader/dist/index.js):
Error: Transform failed with 1 error:
/path/app/application.js:24:6: error: Unexpected "<"
    at failureErrorWithLog (/path/node_modules/esbuild/lib/main.js:933:15)
    at /path/node_modules/esbuild/lib/main.js:832:33
    at handleIncomingPacket (/path/node_modules/esbuild/lib/main.js:552:9)
    at Socket.readFromStdout (/path/node_modules/esbuild/lib/main.js:468:7)
    at Socket.emit (events.js:310:20)
    at addChunk (_stream_readable.js:286:12)
    at readableAddChunk (_stream_readable.js:268:9)
    at Socket.Readable.push (_stream_readable.js:209:10)
    at Pipe.onStreamRead (internal/stream_base_commons.js:186:23)

Line 24 is using JSX:

<Provider store={store}>
 <Component />
</Provider>,

Here is the webpack loader:

loader: 'esbuild-loader',
options: {
  target: 'esnext', // default, or 'es20XX', 'esnext'
  jsxFactory: 'React.createElement',
  jsxFragment: 'React.Fragment',
}

I am wondering if the jsxFactory and jsxFragment are not working. It looks like esbuild-loader is unable to process JSX, despite the options are there.

I looked at the package.json of this project and it looks like my webpack version is supported.

Any suggestions about what I am missing here?

Thanks

Target when using 'minifySyntax' option with minifyer plugin

We just discovered that when using minifySyntax option for the ESBuildMinifyPlugin nullish coalescing operators ( ??) are output which we don't want. Is there a way of setting a target so that for instance only ES5-compatible transforms are performed when minifying syntax?

Support for --bundle

Something I'd like to explore would be using ESBuild for JS bundling, and Webpack for "every that isn't JS" bundling. By that I mean that ESBuild would compile everything that's JS, but would leave the original imports when it detects something that it can't make sense of (for instance, that could be .png imports).

The result would then be a single ESM module that Webpack could parse to extract the remaining imports, transform them into JS, then pack in a final bundle. On the other hand, Webpack wouldn't have to deal with the overall JS bundling, which I would assume is a major part where ESBuild fares better.

Is it something you considered?

don't build project sometimes.

Im using latest version of esbuild-loader, react 16. Sometimes when I change something im my code, my app get white screen. And hard reload doesnt help. Only full project building helps.

Error on building while using node8.17.0

Bug description

Build fails on node version 8.17.0 with following info:

 TypeError: Invalid data, chunk must be a string or buffer, not object
✖ ERROR TypeError: Invalid data, chunk must be a string or buffer, not object
    at Socket.write (net.js:708:11)
    at Object.writeToStdin (/home/user/project/node_modules/esbuild/lib/main.js:1376:19)
    at sendResponse (/home/user/project/node_modules/esbuild/lib/main.js:566:14)
    at /home/user/project/node_modules/esbuild/lib/main.js:620:7
    at Generator.next (<anonymous>)
    at Promise (/home/user/project/node_modules/esbuild/lib/main.js:27:61)
    at new Promise (<anonymous>)
    at __async (/home/user/project/node_modules/esbuild/lib/main.js:9:10)
    at handleRequest (/home/user/project/node_modules/esbuild/lib/main.js:568:40)
    at handleIncomingPacket (/home/user/project/node_modules/esbuild/lib/main.js:635:7)
    at Socket.readFromStdout (/home/user/project/node_modules/esbuild/lib/main.js:520:7)
    at emitOne (events.js:116:13)
    at Socket.emit (events.js:211:7)
    at addChunk (_stream_readable.js:263:12)
    at readableAddChunk (_stream_readable.js:250:11)
    at Socket.Readable.push (_stream_readable.js:208:10)
    at Pipe.onread (net.js:601:20)

I was trying to use newest alpha version of esbuild-loader (3.0.0-alpha.1) and older ones (1.3.1) but with no success. Upgrading node to 10.23.3 is solving this issue, but it would be good to have older nodes support.

Reproduction steps

  1. use node version 8.17.0 and npm 6.13.4
  2. install dependencies
  3. try to build using esbuild-loader

Environment

  • esbuild-loader version: 2.9.1
  • Operating System: Ubuntu20.10
  • Node version: 8.17.0
  • Package manager (npm/yarn/pnpm) and version: 6.13.4

ReferenceError: React is not defined in nextjs / blitzjs

When using nextjs using this next.config.js:

const { ESBuildPlugin } = require('esbuild-loader')
module.exports = {
  webpack: (config, { buildId, dev, isServer, defaultLoaders, webpack }) => {
    config.plugins.push(new ESBuildPlugin())
    const convertToESBuild = (obj) => {
      if (obj.loader === "next-babel-loader") {
        return {
          loader: 'esbuild-loader',
          options: {
            loader: 'tsx',
            target: 'es2015'
          }
        }
      }
      return obj;
    }

    const rule = config.module.rules[1];
    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);
      }
    }
    return config
  },
}

and using [email protected] with this added on my package.json:


  "resolutions": {
    "webpack": "^5.5.1"
  },

This is the full error message:

ReferenceError: React is not defined
    at Object../app/styles/index.tsx (C:\Users\Riz\Desktop\parentuniv\.blitz\caches\dev\.next\server\pages\_app.js:526:38)
    at __webpack_require__ (C:\Users\Riz\Desktop\parentuniv\.blitz\caches\dev\.next\server\webpack-runtime.js:24:42)
    at Object../pages/_app.tsx (C:\Users\Riz\Desktop\parentuniv\.blitz\caches\dev\.next\server\pages\_app.js:592:68)
    at __webpack_require__ (C:\Users\Riz\Desktop\parentuniv\.blitz\caches\dev\.next\server\webpack-runtime.js:24:42)
    at Function.__webpack_require__.X (C:\Users\Riz\Desktop\parentuniv\.blitz\caches\dev\.next\server\webpack-runtime.js:104:20)
    at C:\Users\Riz\Desktop\parentuniv\.blitz\caches\dev\.next\server\pages\_app.js:902:28
    at Object.<anonymous> (C:\Users\Riz\Desktop\parentuniv\.blitz\caches\dev\.next\server\pages\_app.js:903:3)
    at Module._compile (internal/modules/cjs/loader.js:1076:30)
    at Module._extensions..js (internal/modules/cjs/loader.js:1097:10)
    at Object.newLoader [as .js] (C:\Users\Riz\Desktop\parentuniv\node_modules\pirates\lib\index.js:104:7)
event - build page: /next/dist/pages/_error
event - compiled successfully
wait  - compiling...

24:03:47.813.000         ERROR  Error while processing the request

Anyhow to remove "comments" in minified js?

Using ESBuildMinifyPlugin, is there anyway to remove the comments in the minified js file?

For example:
image

If using terser, we can add easily using comments: false in the output options.

use in Vue

{
    configureWebpack: {
        plugins: [new ESBuildPlugin()
        ],
    },
    chainWebpack: config => {
        // esbuild 配置
        config.module
          .rule('js')
          .test('/.js$/')
          .use('esbuild-loader')
          .loader('esbuild-loader')
          .end()
    }
}

Minification plugin causes error after webpack 5.10.1

Things worked well in webpack v5.10.0, but started to fail after v5.10.1.

It still fails in the latest webpack v5.11.1

Console error

Uncaught TypeError: (intermediate value)(intermediate value)(...) is not a function
    at runtime~app.38fb8d44.js:1
    at runtime~app.38fb8d44.js:3

Corresponding snippet

image


Let me know if the context is not enough, I'm willing to provide more information.

TypeError: The 'compilation' argument must be an instance of Compilation

Bug description

Build is failed when using next-css plugin

Reproduction steps

  1. Add @zeit/next-css plugin to next.config.js
  2. import a css file in tsx component
  3. build

esbuild-loader will fail during build with TypeError: The 'compilation' argument must be an instance of Compilation

Environment

  • esbuild-loader version: 2.9.2
  • Webpack version: 5.24.2
  • Operating System: macOS 10.15.7
  • Node version: 14.4.0
  • Package manager (npm/yarn/pnpm) and version: yarn 1.22.0

More info

Build is successful if next-css plugin is removed and also it works well with next-images plugin.

failed with thread-loader

Hello, I've met problems using esbuild-loader together with thread-loader:

use: [
         'thread-loader',
        {
            loader: 'esbuild-loader',
            options: {
                loader: 'jsx', // Remove this if you're not using JSX
                target: 'esnext' // Syntax to compile to (see options below for possible values)
            }
        }
]

error looks like #16, it can't detect esbuildService:

Cannot read property '$esbuildService' of undefined

Thanks for any help :)

Exposing plugins through the build api

👋 Thanks for sharing this awesome loader!

Are there any plans to leverage the esbuild.build API over the esbuild.transform.

Though still new an experimental, esbuild exposes the ability to extend build functionality through plugins. As per the Plugin docs:

Plugins can also only be used with the build API, not with the transform API.

externals don't work?

It seem react becomes bundled even though I write:
externals: {
react: 'React',
'react-dom': 'ReactDOM'
}

Anyone got externals to work?

fails when used together with individual packaging in serverless

Hi,

I'm using serverless with serverless-webpack.

When using esbuild-loader it works fine if my serverless.yml has

package:
  individually: false

But if I have

package:
  individually: true

it spits out a bunch of errors when I try to build. A small sample:

ERROR in ./node_modules/ldapts/messages/AbandonRequest.js
Module build failed (from ./node_modules/esbuild-loader/src/index.js):
Error: [esbuild-loader] You need to add ESBuildPlugin to your webpack config first
    at Object.module.exports (/dir/node_modules/esbuild-loader/src/index.js:15:7)
 @ ./node_modules/ldapts/messages/index.js 24:13-40
 @ ./node_modules/ldapts/Client.js
 @ ./node_modules/ldapts/index.js
 @ ./src/helpers/getLdapClient.ts
 @ ./src/handlers/addUser.ts

ERROR in ./node_modules/ldapts/messages/AddRequest.js
Module build failed (from ./node_modules/esbuild-loader/src/index.js):
Error: [esbuild-loader] You need to add ESBuildPlugin to your webpack config first
    at Object.module.exports (/dir/node_modules/esbuild-loader/src/index.js:15:7)
 @ ./node_modules/ldapts/messages/index.js 25:13-36
 @ ./node_modules/ldapts/Client.js
 @ ./node_modules/ldapts/index.js
 @ ./src/helpers/getLdapClient.ts
 @ ./src/handlers/addUser.ts

If I use ts-loader instead of esbuild-loader both work fine.

The ESBuildPlugin is obviously added to my webpack config. Not sure if this is an issue with esbuild-loader, serverless-webpack, or something else.

[Question] Does the minify plugin support es5 as target?

Hi,

we use the ESBuildMinifyPlugin to minify our JS sources in production mode. The actual esbuild-loader is only active in dev mode (with target: es2019 in case this is relevant).

What happens in our production build now is the following: The minify plugin receives ES5-compatible code and converts certain patterns back to ES6. (The specific example we saw in the final output are ES6 template literals for strings that contained \n).

Is there a way to avoid this behavior, so that the code will stay es5 compatible?

I tried minifySyntax : false, but this doesn't seem to fix all cases. Specifically, the string in const c = '"\'' gets "minified" by wrapping it in backticks, which is not es5 compatible.

Add initial benchmark to compare against babel-loader based approach

It would be cool to see the impact against babel-loader. I wonder if there's some open source project that could be used for figuring out the performance difference. Also pros/cons would be good to understand. At least we would lose any Babel plugins with esbuild but that's not a big deal for some projects.

Can't recognize "const" and generic.

code: fn() => {const = ...}
error: Unexpected "const"

code: props: {[key in keyof T]: [Readonly<T[key]>, (v: T[key]) => void] }
error: Expected "]" but found "T"

Implement flatMap fallback for better Node.js compatibility

The flatMap Array method has only been introduced in Node 11, but Node 10 will be still around until (at least) 2021. I propose a simple fallback for the missing method, to make the loader compatible with more Node.js versions:

diff --git a/src/minify-plugin.js b/src/minify-plugin.js
index 7916f1e..1e8e83f 100644
--- a/src/minify-plugin.js
+++ b/src/minify-plugin.js
@@ -3,6 +3,8 @@ const { RawSource, SourceMapSource } = require('webpack-sources')
 const isJsFile = /\.js$/i
 const pluginName = 'esbuild-minify'
 
+const flatMap = (arr, cb) => arr.flatMap ? arr.flatMap(cb) : [].concat(...arr.map(cb))
+
 class ESBuildMinifyPlugin {
   constructor(options) {
     this.options = { ...options }
@@ -39,7 +41,7 @@ class ESBuildMinifyPlugin {
       compilation.hooks.optimizeChunkAssets.tapPromise(
         pluginName,
         async (chunks) => {
-          const transforms = chunks.flatMap((chunk) => {
+          const transforms = flatMap(chunks, (chunk) => {
             return chunk.files
               .filter((file) => isJsFile.test(file))
               .map(async (file) => {

If there's interest in this, I can make a PR.

tsconfig.json is ignored

Hi everyone,
I've just realized that tsconfig.json is being ignored. How can I tell to esbuild that we have a tsconfig file?

Esbuild loader Cannot read enum property

Can't properly configure webpack with esbuild-loader. I'm getting error in enum.

Cannot read property 'MyProperty' of undefined.

enum MyProperty {
  SomeEnum = 'screen_history',
}

const history = MyProperty.SomeEnum;

My webpack config:

{
        test: /\.js$/,
        loader: 'esbuild-loader',
        options: {
          target: 'es2015',
        },
      },
      {
        test: /\.jsx$/,
        loader: 'esbuild-loader',
        options: {
          loader: 'jsx',
          target: 'es2015',
        },
      },
      {
        test: /\.tsx$/,
        loader: 'esbuild-loader',
        options: {
          loader: 'tsx',
          target: 'es2015',
          tsconfigRaw: require('./tsconfig.json'),
        },
      },
      {
        test: /\.ts$/,
        loader: 'esbuild-loader',
        options: {
          loader: 'ts',
          target: 'es2015',
          tsconfigRaw: require('./tsconfig.json'),
        },
      },


Export wildcard is broken in webpack4

Hi, I discovered an issue when migrating existing projects to use esbuild-loader:

// m.ts
export function foo() {}

export function bar() {}


// index.ts
export * as Utils from "./m";


// webpack.config.js
const { ESBuildPlugin } = require("esbuild-loader");

module.exports = {
  module: {
    rules: [
      {
        test: /\.ts?$/,
        loader: "esbuild-loader",
        options: {
          loader: "ts",
          target: "es2015",
        },
      },
    ],
  },
  resolve: {
    extensions: [".js", ".json", ".ts", ".js"],
  },
  plugins: [new ESBuildPlugin()],
};

Output:

$ yarn webpack
yarn run v1.22.5
warning ../package.json: No license field
$ /...../esbuild-test/node_modules/.bin/webpack
Hash: 8e64292df89454103c53
Version: webpack 4.44.1
Time: 72ms
Built at: 09/07/2020 11:58:33 PM
 1 asset
Entrypoint main = main.js
[0] ./src/index.ts 263 bytes {0} [built] [failed] [1 error]

WARNING in configuration
The 'mode' option has not been set, webpack will fallback to 'production' for this value. Set 'mode' option to 'development' or 'production' to enable defaults for each environment.
You can also set it to 'none' to disable any default behavior. Learn more: https://webpack.js.org/configuration/mode/

ERROR in ./src/index.ts 1:9
Module parse failed: Unexpected token (1:9)
File was processed with these loaders:
 * ./node_modules/esbuild-loader/src/index.js
You may need an additional loader to handle the result of these loaders.
> export * as Utils from "./m";
| 
error Command failed with exit code 2.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

I'm using the latest branch esbuild-peer-dependency:

  "dependencies": {
    "esbuild": "^0.6.32",
    "esbuild-loader": "egoist/esbuild-loader#esbuild-peer-dependency",
    "webpack": "^4.44.1",
    "webpack-cli": "^3.3.12"
  }

However it is fine when calling esbuild directly:

$ yarn esbuild --bundle src/index.ts --outfile=out.js
yarn run v1.22.5
warning ../package.json: No license field
$ /...../esbuild-test/node_modules/.bin/esbuild --bundle src/index.ts --outfile=out.js
✨  Done in 0.06s.

esbuild: Failed to install correctly

I'm trying to use esbuild to compile Typescript, but when I launch webpack-dev-server --mode development --config config/webpack.js I get the following error:

config/webpack.js
/* eslint-disable @typescript-eslint/no-var-requires */
const path = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const { ESBuildPlugin, ESBuildMinifyPlugin } = require('esbuild-loader')

module.exports = (_, argv) => {
  const config = {
    mode: argv.mode,
    target: 'web',
    resolve: {
      extensions: ['.js', '.ts', '.tsx'],
    },
    entry: {
      main: path.resolve('src', 'index.tsx'),
    },
    output: {
      path: path.resolve('dist'),
      filename: '[name].js',
    },
    module: {
      rules: [
        {
          test: /\.tsx?$/,
          use: [
            {
              loader: 'esbuild-loader',
              options: {
                loader: 'tsx',
                target: 'es2015',
              },
            },
          ],
          exclude: '/node_modules/',
          include: path.resolve('src'),
        },
        {
          test: /\.pcss$/i,
          use: [
            {
              loader: 'style-loader',
            },
            {
              loader: 'css-loader',
            },
            {
              loader: 'postcss-loader',
              options: {
                postcssOptions: {
                  config: path.resolve('config', 'postcss.js'),
                },
              },
            },
          ],
          exclude: '/node_modules/',
          include: path.resolve('src'),
        },
        {
          test: /\.(png|svg|jpg|gif|webp)$/,
          use: [
            {
              loader: 'file-loader',
            },
          ],
        },
        {
          test: /\.(woff|woff2|eot|ttf|otf)$/,
          use: [
            {
              loader: 'file-loader',
            },
          ],
        },
      ],
    },
    plugins: [
      new HtmlWebpackPlugin({
        inject: 'head',
        scriptLoading: 'defer',
        title: 'Esports Academy',
      }),
      new ESBuildPlugin(),
    ],
  }

  switch (argv.mode) {
    case 'development':
      return {
        ...config,
        devtool: 'eval-source-map',
        devServer: {
          contentBase: path.resolve('dist'),
          hot: true,
          inline: true,
        },
      }

    default:
    case 'production':
      return {
        ...config,
        optimization: {
          minimize: true,
          minimizer: [
            new ESBuildMinifyPlugin({
              minify: true
            })
          ],
        },
      }
  }
}

And get in the console Failed to compile because of the error esbuild: Failed to install correctly.

I did search, but found nothing about that error.

Can be the fact that I am using it in a Docker environment?

webpack: 4.44.2
webpack-cli: 3.3.12
webpack-dev-server: 3.11.0
esbuild-loader: 2.4.0
typescript: 4.0.2

Use babel-loader and esbuild-loader in webpack at the same time

I have a good idea is that use babel-loader and esbuild-loader both in webpack.
So I have a test is config my webpack is that
image
i want to use esbuild-loader to transform code from high version code to es5.
The babel-loader only use to do some custom babel-plugin sush as babel-plugin-import and react-refresh/babel
image
But i get an error .
image
I don't know what happened.

How to customize the jsxFactory

Is your feature request related to a problem? Please describe.

Describe the solution you'd like

Describe alternatives you've considered

Additional context

With rollup-plugin-esbuild , we can do this to compat with vue jsx,

import vueJsx from 'rollup-plugin-vue-jsx-compat'
import esbuild from 'rollup-plugin-esbuild'

export default {
  // ...
  plugins: [
    vueJsx(),
    esbuild({
      jsxFactory: "vueJsxCompat",
    }),
  ],
}

how can i do this in esbuild-plugin, how to customize the jsxFactory

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.