Skip to content

Commit

Permalink
fix: bind esbuild service to compiler instance
Browse files Browse the repository at this point in the history
in order to support running multiple compiler at the same time
  • Loading branch information
egoist committed Jul 3, 2020
1 parent e861e4a commit 8c83022
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ const path = require('path')
const esbuild = require('esbuild')
const { getOptions } = require('loader-utils')

/** @type {import('esbuild').Service} */
let service

const getLoader = (ext) => {
if (ext === '.json') {
return 'json'
Expand All @@ -15,6 +12,9 @@ const getLoader = (ext) => {
module.exports = async function (source) {
const done = this.async()
const options = getOptions(this)
/** @type {import('esbuild').Service} */
const service = this._compiler.$esbuildService

if (!service) {
return done(
new Error(
Expand All @@ -31,7 +31,7 @@ module.exports = async function (source) {
loader: getLoader(ext),
jsxFactory: options.jsxFactory,
jsxFragment: options.jsxFragment,
sourcemap: options.sourceMap
sourcemap: options.sourceMap,
})
done(null, result.js, result.jsSourceMap)
} catch (err) {
Expand All @@ -47,8 +47,8 @@ module.exports.ESBuildPlugin = class ESBuildPlugin {
let watching = false

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

Expand All @@ -61,9 +61,9 @@ module.exports.ESBuildPlugin = class ESBuildPlugin {
})

compiler.hooks.done.tap('esbuild', () => {
if (!watching && service) {
service.stop()
service = undefined
if (!watching && compiler.$esbuildService) {
compiler.$esbuildService.stop()
compiler.$esbuildService = undefined
}
})
}
Expand Down

0 comments on commit 8c83022

Please sign in to comment.