Skip to content

Commit

Permalink
feat: Upgrade to esbuild 0.9 (#210)
Browse files Browse the repository at this point in the history
BREAKING CHANGE:

Require esbuild 0.9 or above.

Recently esbuild 0.9 was released with some breaking changes.
A breaking change broke this plugin: esbuild.startService function
is being removed.

This commit change peer and devDependencies to esbuild 0.9 and
also removing usage of service in the plugin. Instead it use
transform function directly.

All tests passed and example is built just fine.
  • Loading branch information
Izzuddin Natsir committed Mar 9, 2021
1 parent 1e59116 commit def1fe5
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 39 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"@types/jest": "^26.0.20",
"@types/mock-fs": "^4.13.0",
"@types/node": "14.14.32",
"esbuild": "^0.8.0",
"esbuild": "^0.9.0",
"jest": "^26.6.3",
"mock-fs": "^4.13.0",
"prettier": "^2.2.1",
Expand All @@ -35,7 +35,7 @@
"strip-json-comments": "^3.1.1"
},
"peerDependencies": {
"esbuild": ">=0.7.0"
"esbuild": ">=0.9.0"
},
"engines": {
"node": ">=12"
Expand Down
38 changes: 5 additions & 33 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { existsSync, statSync } from 'fs'
import { extname, resolve, dirname, join, relative } from 'path'
import { Plugin, PluginContext } from 'rollup'
import { startService, Loader, Service, TransformResult } from 'esbuild'
import { transform, Loader, TransformResult } from 'esbuild'
import { createFilter, FilterPattern } from '@rollup/pluginutils'
import { getOptions } from './options'

Expand Down Expand Up @@ -66,15 +66,6 @@ export default (options: Options = {}): Plugin => {
options.exclude || EXCLUDE_REGEXP
)

let service: Service | undefined

const stopService = () => {
if (service) {
service.stop()
service = undefined
}
}

// The order is:
// buildStart -> resolveId -> transform -> buildEnd -> renderChunk -> generateBundle

Expand All @@ -89,12 +80,6 @@ export default (options: Options = {}): Plugin => {
return {
name: 'esbuild',

async buildStart() {
if (!service) {
service = await startService()
}
},

resolveId(importee, importer) {
if (importer && importee[0] === '.') {
const resolved = resolve(
Expand All @@ -119,7 +104,7 @@ export default (options: Options = {}): Plugin => {
const ext = extname(id)
const loader = loaders[ext]

if (!loader || !service) {
if (!loader) {
return null
}

Expand All @@ -130,7 +115,7 @@ export default (options: Options = {}): Plugin => {

target = options.target || defaultOptions.target || 'es2017'

const result = await service.transform(code, {
const result = await transform(code, {
loader,
target,
jsxFactory: options.jsxFactory || defaultOptions.jsxFactory,
Expand All @@ -150,16 +135,9 @@ export default (options: Options = {}): Plugin => {
)
},

buildEnd(error) {
// Stop the service early if there's error
if (error && !this.meta.watchMode) {
stopService()
}
},

async renderChunk(code) {
if (options.minify && service) {
const result = await service.transform(code, {
if (options.minify) {
const result = await transform(code, {
loader: 'js',
minify: true,
target,
Expand All @@ -173,12 +151,6 @@ export default (options: Options = {}): Plugin => {
}
return null
},

generateBundle() {
if (!this.meta.watchMode) {
stopService()
}
},
}
}

Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1275,10 +1275,10 @@ error-ex@^1.3.1:
dependencies:
is-arrayish "^0.2.1"

esbuild@^0.8.0:
version "0.8.0"
resolved "https://registry.yarnpkg.com/esbuild/-/esbuild-0.8.0.tgz#3173e851303dce0682ebbcbaf6072b991dd6f60e"
integrity sha512-xCHJpLRlU0NIANQHNsiMDNC/HlrKoye7iH5YOcoZNurauUZgMhjmm9PCal+Oo9ARYZrWxN15mykbfCX//UEvng==
esbuild@^0.9.0:
version "0.9.0"
resolved "https://registry.yarnpkg.com/esbuild/-/esbuild-0.9.0.tgz#b8320df85048ed1637c6b59ee52abba248936d3c"
integrity sha512-IqYFO7ZKHf0y4uJpJfGqInmSRn8jMPMbyI1W0Y2PSjSjJcVP538tC8TleJAS4Y8QeqwajqBTwFKayWVzYlMIgg==

escape-string-regexp@^1.0.5:
version "1.0.5"
Expand Down

0 comments on commit def1fe5

Please sign in to comment.