UNPKG

8.2 kBMarkdownView Raw
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
3Speed 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
9Curious how much faster your build will be? See [what users are saying](https://github.com/privatenumber/esbuild-loader/issues/13).
10
11<sub>If you like this project, please star it & [follow me](https://github.com/privatenumber) to see what other cool projects I'm working on! ❤️</sub>
12
13## 🚀 Install
14
15```bash
16npm i -D esbuild-loader
17```
18
19## 🚦 Quick Setup
20
21### Javascript & JSX transpilation (eg. Babel)
22In `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
48In `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
74If you have a `tsconfig.json` file, esbuild-loader will automatically detect it.
75
76Alternatively, 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/b901055/lib/types.ts#L127-L133) 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)
93You 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
95In `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_
114If 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
119There 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
124If 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
126Assuming 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
165If 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
168In `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
196### Examples
197If 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).
198
199## ⚙️ Options
200
201### Loader
202The loader supports options from [esbuild](https://github.com/evanw/esbuild/blob/b901055/lib/types.ts#L126-L138).
203- `target` `String` (`'es2015'`) - [Environment target](https://esbuild.github.io/api/#target) (e.g. es2016, chrome80, esnext)
204- `loader` `String` (`'js'`) - Which loader to use to handle file
205 - [Possible values](https://github.com/evanw/esbuild/blob/b901055/lib/types.ts#L3): `js`, `jsx`, `ts`, `tsx`, `json`, `text`, `base64`, `file`, `dataurl`, `binary`
206- `jsxFactory` `String` - What to use instead of React.createElement
207- `jsxFragment` `String` - What to use instead of React.Fragment
208
209Enable source-maps via [`devtool`](https://webpack.js.org/configuration/devtool/)
210
211### MinifyPlugin
212- `target` `String|Aray<String>` (`'esnext'`) - [Environment target](https://github.com/evanw/esbuild#javascript-syntax-support) (e.g. `'es2016'`, `['chrome80', 'esnext']`)
213- `minify` `Boolean` (`true`) - Sets all `minify` flags
214- `minifyWhitespace` `Boolean` - Remove whitespace
215- `minifyIdentifiers` `Boolean` - Shorten identifiers
216- `minifySyntax` `Boolean` - Use equivalent but shorter syntax
217- `sourcemap` `Boolean` (defaults to Webpack `devtool`) - Whether to emit sourcemaps
218- `css` `Boolean` (`false`) - Whether to minify CSS files
219- `include` `String|RegExp|Array<String|RegExp>` - Filter assets for inclusion in minification
220- `exclude` `String|RegExp|Array<String|RegExp>` - Filter assets for exclusion in minification
221
222
223## 💼 License
224- MIT &copy; privatenumber
225- MIT &copy; [EGOIST (Kevin Titor)](https://github.com/sponsors/egoist)