Skip to content

Commit

Permalink
feat: upgrade esbuild to 0.9.2 (#125)
Browse files Browse the repository at this point in the history
Co-authored-by: Hiroki Osame <[email protected]>
  • Loading branch information
itsoli and privatenumber committed Mar 15, 2021
1 parent bb74edb commit 1ace5de
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 92 deletions.
21 changes: 2 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ npm i -D esbuild-loader
In `webpack.config.js`:

```diff
+ const { ESBuildPlugin } = require('esbuild-loader')

module.exports = {
module: {
rules: [
Expand All @@ -43,18 +41,13 @@ In `webpack.config.js`:
...
],
},
plugins: [
+ new ESBuildPlugin()
]
}
```

### TypeScript & TSX
In `webpack.config.js`:

```diff
+ const { ESBuildPlugin } = require('esbuild-loader')

module.exports = {
module: {
rules: [
Expand All @@ -74,9 +67,6 @@ In `webpack.config.js`:
...
]
},
plugins: [
+ new ESBuildPlugin()
]
}
```

Expand All @@ -103,10 +93,7 @@ You can replace JS minifiers like Terser or UglifyJs. Checkout the [benchmarks](
In `webpack.config.js`:

```diff
+ const {
+ ESBuildPlugin,
+ ESBuildMinifyPlugin
+ } = require('esbuild-loader')
+ const { ESBuildMinifyPlugin } = require('esbuild-loader')

module.exports = {
...,
Expand All @@ -119,15 +106,11 @@ In `webpack.config.js`:
+ })
+ ]
+ },

plugins: [
+ new ESBuildPlugin()
]
}
```

#### _💁‍♀️ Protip: Use the minify plugin in-place of the loader to transpile your JS_
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.
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.

### Examples
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).
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
]
},
"dependencies": {
"esbuild": "^0.8.57",
"esbuild": "^0.9.2",
"joycon": "^2.2.5",
"json5": "^2.2.0",
"loader-utils": "^2.0.0",
Expand Down
8 changes: 4 additions & 4 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 1 addition & 7 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
import {Service, TransformOptions} from 'esbuild';
import {TransformOptions} from 'esbuild';
import {Except} from 'type-fest';
import webpack from 'webpack';

type Compiler = webpack.Compiler & {
$esbuildService?: Service;
};

type Filter = string | RegExp;
type FilterObject = {
Expand All @@ -16,7 +11,6 @@ type LoaderOptions = Except<TransformOptions, 'sourcemap' | 'sourcefile'>;
type MinifyPluginOptions = Except<TransformOptions, 'sourcefile'> & FilterObject;

export {
Compiler,
LoaderOptions,
MinifyPluginOptions,
};
15 changes: 3 additions & 12 deletions src/loader.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import fs from 'fs';
import path from 'path';
import {transform} from 'esbuild';
import {getOptions} from 'loader-utils';
import webpack from 'webpack';
import JoyCon, {LoadResult} from 'joycon';
import JSON5 from 'json5';
import {Compiler, LoaderOptions} from './interfaces';
import {LoaderOptions} from './interfaces';

const joycon = new JoyCon();

Expand All @@ -31,16 +32,6 @@ async function ESBuildLoader(
): Promise<void> {
const done = this.async()!;
const options: LoaderOptions = getOptions(this);
const service = (this._compiler as Compiler).$esbuildService;

if (!service) {
done(
new Error(
'[esbuild-loader] You need to add ESBuildPlugin to your webpack config first',
),
);
return;
}

const transformOptions = {
...options,
Expand Down Expand Up @@ -69,7 +60,7 @@ async function ESBuildLoader(
}

try {
const {code, map} = await service.transform(source, transformOptions);
const {code, map} = await transform(source, transformOptions);
done(null, code, map && JSON.parse(map));
} catch (error: unknown) {
done(error as Error);
Expand Down
19 changes: 5 additions & 14 deletions src/minify-plugin.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import assert from 'assert';
import {transform} from 'esbuild';
import {RawSource, SourceMapSource} from 'webpack-sources';
import webpack from 'webpack';
import {matchObject} from 'webpack/lib/ModuleFilenameHelpers';
import {Compiler, MinifyPluginOptions} from './interfaces';
import {MinifyPluginOptions} from './interfaces';

type Asset = webpack.compilation.Asset;

Expand Down Expand Up @@ -57,10 +57,8 @@ class ESBuildMinifyPlugin {
}
}

apply(compiler: Compiler): void {
apply(compiler: webpack.Compiler): void {
compiler.hooks.compilation.tap(pluginName, compilation => {
assert(compiler.$esbuildService, '[esbuild-loader] You need to add ESBuildPlugin to your webpack config first');

const meta = JSON.stringify({
name: 'esbuild-loader',
version,
Expand Down Expand Up @@ -118,14 +116,7 @@ class ESBuildMinifyPlugin {
compilation: webpack.compilation.Compilation,
assetNames: string[],
): Promise<void> {
const {
options: {
devtool,
},
$esbuildService,
} = compilation.compiler as Compiler;

assert($esbuildService, '[esbuild-loader] You need to add ESBuildPlugin to your webpack config first');
const {options: {devtool}} = compilation.compiler;

const sourcemap = (
// TODO: drop support for esbuild sourcemap in future so it all goes through WP API
Expand All @@ -147,7 +138,7 @@ class ESBuildMinifyPlugin {
{info, source: assetSource},
]) => {
const {source, map} = assetSource.sourceAndMap();
const result = await $esbuildService.transform(source.toString(), {
const result = await transform(source.toString(), {
...transformOptions,
sourcemap,
sourcefile: assetName,
Expand Down
35 changes: 2 additions & 33 deletions src/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,6 @@
import {startService} from 'esbuild';
import {Compiler} from './interfaces';

class ESBuildPlugin {
apply(compiler: Compiler): void {
let watching = false;

const safeStartService = async () => {
if (!compiler.$esbuildService) {
compiler.$esbuildService = await startService();
}
};

compiler.hooks.thisCompilation.tap('esbuild', compilation => {
compilation.hooks.childCompiler.tap('esbuild', childCompiler => {
childCompiler.$esbuildService = compiler.$esbuildService;
});
});

compiler.hooks.run.tapPromise('esbuild', async () => {
await safeStartService();
});

compiler.hooks.watchRun.tapPromise('esbuild', async () => {
watching = true;
await safeStartService();
});

compiler.hooks.done.tap('esbuild', () => {
if (!watching && compiler.$esbuildService) {
compiler.$esbuildService.stop();
compiler.$esbuildService = undefined;
}
});
apply() {
console.warn('[esbuild-loader] ESBuildPlugin is no longer required for usage and will be removed in the next major release. Please refer to the docs and release notes for more info.');
}
}

Expand Down
3 changes: 1 addition & 2 deletions test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import fs from 'fs';
import {ufs} from 'unionfs';
import {Volume, DirectoryJSON} from 'memfs';
import {runInNewContext} from 'vm';
import {ESBuildPlugin} from '../dist/index.js';
import {
Configuration as Wp4Configuration,
Stats,
Expand Down Expand Up @@ -64,7 +63,7 @@ export async function build(
},
],
},
plugins: [new ESBuildPlugin()],
plugins: [],
};

configure?.(config);
Expand Down

0 comments on commit 1ace5de

Please sign in to comment.