1 | # esbuild-loader <a href="https://npm.im/esbuild-loader"><img src="https://badgen.net/npm/v/esbuild-loader"></a> <a href="https://npm.im/esbuild-loader"><img src="https://badgen.net/npm/dm/esbuild-loader"></a> <a href="https://packagephobia.now.sh/result?p=esbuild-loader"><img src="https://packagephobia.now.sh/badge?p=esbuild-loader"></a>
|
2 |
|
3 | Speed up your Webpack build with [esbuild](https://github.com/evanw/esbuild)! 🔥
|
4 |
|
5 | [esbuild](https://github.com/evanw/esbuild) is a JavaScript bundler written in Go that supports blazing fast ESNext & TypeScript transpilation and JS minification.
|
6 |
|
7 | [esbuild-loader](https://github.com/privatenumber/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)!
|
8 |
|
9 | Curious how much faster your build will be? See [what users are saying](https://github.com/privatenumber/esbuild-loader/issues/13).
|
10 |
|
11 | <sub>Support this project by ⭐️ starring and sharing it. [Follow me](https://github.com/privatenumber) to see what other cool projects I'm working on! ❤️</sub>
|
12 |
|
13 | ## 🚀 Install
|
14 |
|
15 | ```bash
|
16 | npm i -D esbuild-loader
|
17 | ```
|
18 |
|
19 | ## 🚦 Quick Setup
|
20 |
|
21 | ### Javascript & JSX transpilation (eg. Babel)
|
22 | In `webpack.config.js`:
|
23 |
|
24 | ```diff
|
25 | module.exports = {
|
26 | module: {
|
27 | rules: [
|
28 | - {
|
29 | - test: /\.js$/,
|
30 | - use: 'babel-loader',
|
31 | - },
|
32 | + {
|
33 | + test: /\.js$/,
|
34 | + loader: 'esbuild-loader',
|
35 | + options: {
|
36 | + loader: 'jsx', // Remove this if you're not using JSX
|
37 | + target: 'es2015' // Syntax to compile to (see options below for possible values)
|
38 | + }
|
39 | + },
|
40 |
|
41 | ...
|
42 | ],
|
43 | },
|
44 | }
|
45 | ```
|
46 |
|
47 | ### TypeScript & TSX
|
48 | In `webpack.config.js`:
|
49 |
|
50 | ```diff
|
51 | module.exports = {
|
52 | module: {
|
53 | rules: [
|
54 | - {
|
55 | - test: /\.tsx?$/,
|
56 | - use: 'ts-loader'
|
57 | - },
|
58 | + {
|
59 | + test: /\.tsx?$/,
|
60 | + loader: 'esbuild-loader',
|
61 | + options: {
|
62 | + loader: 'tsx', // Or 'ts' if you don't need tsx
|
63 | + target: 'es2015'
|
64 | + }
|
65 | + },
|
66 |
|
67 | ...
|
68 | ]
|
69 | },
|
70 | }
|
71 | ```
|
72 |
|
73 | #### Configuration
|
74 | If you have a `tsconfig.json` file, esbuild-loader will automatically detect it.
|
75 |
|
76 | Alternatively, you can also pass it in directly via the [`tsconfigRaw` option](https://esbuild.github.io/api/#tsconfig-raw):
|
77 | ```diff
|
78 | {
|
79 | test: /\.tsx?$/,
|
80 | loader: 'esbuild-loader',
|
81 | options: {
|
82 | loader: 'tsx',
|
83 | target: 'es2015',
|
84 | + tsconfigRaw: require('./tsconfig.json')
|
85 | }
|
86 | }
|
87 | ```
|
88 |
|
89 | ⚠️ esbuild only supports a subset of `tsconfig` options [(see `TransformOptions` interface)](https://github.com/evanw/esbuild/blob/88821b7e7d46737f633120f91c65f662eace0bcf/lib/shared/types.ts#L159-L165) and does not do type-checks. It's recommended to use a type-aware IDE or `tsc --noEmit` for type-checking instead. It is also recommended to enable [`isolatedModules`](https://www.typescriptlang.org/tsconfig#isolatedModules) and [`esModuleInterop`](https://www.typescriptlang.org/tsconfig/#esModuleInterop) options in your `tsconfig` by the [esbuild docs](https://esbuild.github.io/content-types/#typescript-caveats).
|
90 |
|
91 |
|
92 | ### JS Minification (eg. Terser)
|
93 | You can replace JS minifiers like Terser or UglifyJs. Checkout the [benchmarks](https://github.com/privatenumber/minification-benchmarks) to see how much faster esbuild is. The `target` option tells esbuild that it can use newer JS syntax to perform better minification.
|
94 |
|
95 | In `webpack.config.js`:
|
96 |
|
97 | ```diff
|
98 | + const { ESBuildMinifyPlugin } = require('esbuild-loader')
|
99 |
|
100 | module.exports = {
|
101 | ...,
|
102 |
|
103 | + optimization: {
|
104 | + minimizer: [
|
105 | + new ESBuildMinifyPlugin({
|
106 | + target: 'es2015' // Syntax to compile to (see options below for possible values)
|
107 | + })
|
108 | + ]
|
109 | + },
|
110 | }
|
111 | ```
|
112 |
|
113 | #### _💁♀️ Protip: Use the minify plugin in-place of the loader to transpile the JS_
|
114 | If you're not using TypeScript, JSX, or any syntax unsupported by Webpack, you can also leverage the minifier for transpilation (as an alternative to Babel). It will be faster because there's less files to work on and will produce a smaller output because the polyfills will only be bundled once for the entire build instead of per file. Simply set the `target` option on the minifier to specify which support level you want.
|
115 |
|
116 |
|
117 | ### CSS Minification
|
118 |
|
119 | There are two ways to minify CSS, depending on your setup. You should already have CSS setup in your build using [`css-loader`](https://github.com/webpack-contrib/css-loader).
|
120 |
|
121 | ⚠️ esbuild currently [doesn't support source-maps for CSS minification](https://github.com/evanw/esbuild/issues/519).
|
122 |
|
123 | #### CSS assets
|
124 | If your CSS is extracted and emitted as a CSS file, you can replace CSS minification plugins like [`css-minimizer-webpack-plugin`](https://github.com/webpack-contrib/css-minimizer-webpack-plugin) or [`optimize-css-assets-webpack-plugin`](https://github.com/NMFR/optimize-css-assets-webpack-plugin) with the same `ESBuildMinifyPlugin` by enabling the `css` option.
|
125 |
|
126 | Assuming the CSS is extracted using something like [MiniCssExtractPlugin](https://github.com/webpack-contrib/mini-css-extract-plugin), in `webpack.config.js`:
|
127 |
|
128 | ```diff
|
129 | const { ESBuildMinifyPlugin } = require('esbuild-loader')
|
130 | const MiniCssExtractPlugin = require('mini-css-extract-plugin');
|
131 |
|
132 | module.exports = {
|
133 | ...,
|
134 |
|
135 | optimization: {
|
136 | minimizer: [
|
137 | new ESBuildMinifyPlugin({
|
138 | target: 'es2015',
|
139 | + css: true // Apply minification to CSS assets
|
140 | })
|
141 | ]
|
142 | },
|
143 |
|
144 | module: {
|
145 | rules: [
|
146 | {
|
147 | test: /\.css$/i,
|
148 | use: [
|
149 | MiniCssExtractPlugin.loader,
|
150 | 'css-loader'
|
151 | ]
|
152 | }
|
153 | ]
|
154 | },
|
155 |
|
156 | plugins: [
|
157 | new MiniCssExtractPlugin()
|
158 | ]
|
159 | }
|
160 | ```
|
161 |
|
162 |
|
163 | #### CSS in JS
|
164 |
|
165 | If your CSS is not emitted as a CSS file, but rather loaded via JS using something like [`style-loader`](https://github.com/webpack-contrib/style-loader), you can use the loader for minification.
|
166 |
|
167 |
|
168 | In `webpack.config.js`:
|
169 |
|
170 | ```diff
|
171 | module.exports = {
|
172 | ...,
|
173 |
|
174 | module: {
|
175 | rules: [
|
176 | {
|
177 | test: /\.css$/i,
|
178 | use: [
|
179 | 'style-loader',
|
180 | 'css-loader',
|
181 | + {
|
182 | + loader: 'esbuild-loader',
|
183 | + options: {
|
184 | + loader: 'css',
|
185 | + minify: true
|
186 | + }
|
187 | + }
|
188 | ]
|
189 | }
|
190 | ]
|
191 | }
|
192 | }
|
193 | ```
|
194 |
|
195 | ### Examples
|
196 | If you'd like to see working Webpack builds that use esbuild-loader for basic JS, React, TypeScript, or Next.js, check out the [examples repo](https://github.com/privatenumber/esbuild-loader-examples).
|
197 |
|
198 | ### Bring your own esbuild (Advanced)
|
199 |
|
200 | esbuild-loader comes with a version of esbuild it has been tested to work with. However, [esbuild has a frequent release cadence](https://github.com/evanw/esbuild/releases), and while we try to keep up with the important releases, it can easily go out of date.
|
201 |
|
202 | Use the `implementation` option in the loader or the minify plugin to pass in your own version of esbuild (eg. a newer one).
|
203 |
|
204 | ⚠️ esbuild is not stable yet and can have dramatic differences across releases. Using a different version of esbuild is not guaranteed to work.
|
205 |
|
206 |
|
207 | ```diff
|
208 | + const esbuild = require('esbuild')
|
209 |
|
210 | ...
|
211 |
|
212 | module.exports = {
|
213 | ...,
|
214 |
|
215 | module: {
|
216 | rules: [
|
217 | {
|
218 | test: ...,
|
219 | loader: 'esbuild-loader',
|
220 | options: {
|
221 | ...,
|
222 | + implementation: esbuild
|
223 | }
|
224 | }
|
225 | ]
|
226 | }
|
227 | }
|
228 | ```
|
229 |
|
230 | _The `implementation` option will be removed once esbuild reaches a stable release. Instead esbuild will become a peerDependency so you always provide your own._
|
231 |
|
232 |
|
233 | ## ⚙️ Options
|
234 |
|
235 | ### Loader
|
236 | The loader supports [all Transform options from esbuild](https://github.com/evanw/esbuild/blob/88821b7e7d46737f633120f91c65f662eace0bcf/lib/shared/types.ts#L158-L172).
|
237 |
|
238 | Note:
|
239 | - Source-maps are automatically configured for you via [`devtool`](https://webpack.js.org/configuration/devtool/). `sourcemap`/`sourcefile` options are ignored.
|
240 | - The root `tsconfig.json` is automatically detected for you. You don't need to pass in [`tsconfigRaw`](https://esbuild.github.io/api/#tsconfig-raw) unless it's in a different path.
|
241 |
|
242 |
|
243 | Here are some common configurations and custom options:
|
244 |
|
245 | #### target
|
246 | Type: `string | Array<string>`
|
247 |
|
248 | Default: `'es2015'`
|
249 |
|
250 | The target environment (e.g. `es2016`, `chrome80`, `esnext`).
|
251 |
|
252 | Read more about it in the [esbuild docs](https://esbuild.github.io/api/#target).
|
253 |
|
254 | #### loader
|
255 | Type: `'js' | 'jsx' | 'ts' | 'tsx' | 'css' | 'json' | 'text' | 'base64' | 'file' | 'dataurl' | 'binary' | 'default'`
|
256 |
|
257 | Default: `'js'`
|
258 |
|
259 | The loader to use to handle the file. See the type for [possible values](https://github.com/evanw/esbuild/blob/88821b7e7d46737f633120f91c65f662eace0bcf/lib/shared/types.ts#L3).
|
260 |
|
261 |
|
262 | Read more about it in the [esbuild docs](https://esbuild.github.io/api/#loader).
|
263 |
|
264 | #### jsxFactory
|
265 | Type: `string`
|
266 |
|
267 | Default: `React.createElement`
|
268 |
|
269 | Customize the JSX factory function name to use.
|
270 |
|
271 | Read more about it in the [esbuild docs](https://esbuild.github.io/api/#jsx-factory).
|
272 |
|
273 | #### jsxFragment
|
274 | Type: `string`
|
275 |
|
276 | Default: `React.Fragment`
|
277 |
|
278 | Customize the JSX fragment function name to use.
|
279 |
|
280 |
|
281 | Read more about it in the [esbuild docs](https://esbuild.github.io/api/#jsx-fragment).
|
282 |
|
283 | #### implementation
|
284 | Type: `{ transform: Function }`
|
285 |
|
286 | _Custom esbuild-loader option._
|
287 |
|
288 | Use it to pass in a [different esbuild version](#bring-your-own-esbuild-advanced).
|
289 |
|
290 | ### MinifyPlugin
|
291 |
|
292 | The loader supports [all Transform options from esbuild](https://github.com/evanw/esbuild/blob/88821b7e7d46737f633120f91c65f662eace0bcf/lib/shared/types.ts#L158-L172).
|
293 |
|
294 | #### target
|
295 | Type: `string | Array<string>`
|
296 |
|
297 | Default: `'esnext'`
|
298 |
|
299 | Target environment (e.g. `'es2016'`, `['chrome80', 'esnext']`)
|
300 |
|
301 | Read more about it in the [esbuild docs](https://esbuild.github.io/api/#target).
|
302 |
|
303 | Here are some common configurations and custom options:
|
304 |
|
305 | #### minify
|
306 | Type: `boolean`
|
307 |
|
308 | Default: `true`
|
309 |
|
310 | Enable JS minification. Enables all `minify*` flags below.
|
311 |
|
312 | To have nuanced control over minification, disable this and enable the specific minification you want below.
|
313 |
|
314 | Read more about it in the [esbuild docs](https://esbuild.github.io/api/#minify).
|
315 |
|
316 | #### minifyWhitespace
|
317 | Type: `boolean`
|
318 |
|
319 | Minify JS by removing whitespace.
|
320 |
|
321 | #### minifyIdentifiers
|
322 | Type: `boolean`
|
323 |
|
324 | Minify JS by shortening identifiers.
|
325 |
|
326 | #### minifySyntax
|
327 | Type: `boolean`
|
328 |
|
329 | Minify JS using equivalent but shorter syntax.
|
330 |
|
331 | #### legalComments
|
332 | Type: `'none' | 'inline' | 'eof'`
|
333 |
|
334 | Default: `'inline'`
|
335 |
|
336 | Read more about it in the [esbuild docs](https://esbuild.github.io/api/#legal-comments).
|
337 |
|
338 | #### sourcemap
|
339 | Type: `boolean`
|
340 |
|
341 | Default: Webpack `devtool` configuration
|
342 |
|
343 | Whether to emit sourcemaps.
|
344 |
|
345 | #### css
|
346 | Type: `boolean`
|
347 |
|
348 | Default: `false`
|
349 |
|
350 | _Custom esbuild-loader option._
|
351 |
|
352 | Whether to minify CSS files.
|
353 |
|
354 | #### include
|
355 | Type: `string | RegExp | Array<string | RegExp>`
|
356 |
|
357 | _Custom esbuild-loader option._
|
358 |
|
359 | Filter assets to include in minification
|
360 |
|
361 | #### exclude
|
362 | Type: `string | RegExp | Array<string | RegExp>`
|
363 |
|
364 | _Custom esbuild-loader option._
|
365 |
|
366 | Filter assets to exclude from minification
|
367 |
|
368 | #### implementation
|
369 | Type: `{ transform: Function }`
|
370 |
|
371 | _Custom esbuild-loader option._
|
372 |
|
373 | Use it to pass in a [different esbuild version](#bring-your-own-esbuild-advanced).
|
374 |
|
375 | ## 🙋♀️ FAQ
|
376 |
|
377 | ### Is it possible to use esbuild plugins?
|
378 | No. esbuild plugins are [only available in the build API](https://esbuild.github.io/plugins/#:~:text=plugins%20can%20also%20only%20be%20used%20with%20the%20build%20api%2C%20not%20with%20the%20transform%20api.). And esbuild-loader uses the transform API instead of the build API for two reasons:
|
379 | 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.
|
380 |
|
381 | 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.
|
382 |
|
383 | ### Is it possible to use esbuild's [inject](https://esbuild.github.io/api/#inject) option?
|
384 |
|
385 | No. The `inject` option is only available in the build API. And esbuild-loader uses the transform API.
|
386 |
|
387 | However, you can use the Webpack equivalent [ProvidePlugin](https://webpack.js.org/plugins/provide-plugin/) instead.
|
388 |
|
389 | If you're using React, check out [this example](https://github.com/privatenumber/esbuild-loader-examples/blob/52ca91b8cb2080de5fc63cc6e9371abfefe1f823/examples/react/webpack.config.js#L39-L41) on how to auto-import React in your components.
|
390 |
|
391 | ### Is it possible to use Babel plugins?
|
392 | No. If you really need them, consider porting them over to a Webpack loader.
|
393 |
|
394 | And please don't chain `babel-loader` and `esbuild-loader`. The speed gains come from replacing `babel-loader`.
|
395 |
|
396 | ### Why am I not getting a [100x speed improvement](https://esbuild.github.io/faq/#benchmark-details) as advertised?
|
397 | Running esbuild as a standalone bundler vs esbuild-loader + Webpack are completely different:
|
398 | - esbuild is highly optimized, written in Go, and compiled to native code. Read more about it [here](https://esbuild.github.io/faq/#why-is-esbuild-fast).
|
399 | - 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.
|
400 |
|
401 | Using any JS bundler 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`](https://twitter.com/wSokra/status/1316274855042584577?s=20), `ts-loader`, Terser, etc.
|
402 |
|
403 | ### Will there be type-checking support?
|
404 | According to the [esbuild FAQ](https://esbuild.github.io/faq/#:~:text=typescript%20type%20checking%20(just%20run%20tsc%20separately)), it will not be supported.
|
405 |
|
406 | However, IDEs like [VSCode](https://code.visualstudio.com/docs/languages/typescript) or [WebStorm](https://www.jetbrains.com/help/webstorm/typescript-support.html) have type-chekcing built in. And you can also run `tsc --noEmit` to type check.
|
407 |
|
408 | ## 🌱 Other Webpack plugins
|
409 |
|
410 | #### [instant-mocha](https://github.com/privatenumber/instant-mocha)
|
411 | Webpack-integrated Mocha test-runner with Webpack 5 support.
|
412 |
|
413 |
|
414 | #### [webpack-localize-assets-plugin](https://github.com/privatenumber/webpack-localize-assets-plugin)
|
415 | Localize/i18nalize your Webpack build. Optimized for multiple locales!
|