From d64d7b38173d1d90dabf63012e01c34f41203bcf Mon Sep 17 00:00:00 2001 From: EGOIST <0x142857@gmail.com> Date: Sun, 14 Mar 2021 20:15:38 +0800 Subject: [PATCH] feat: upgrade esbuild to 0.9 --- package.json | 2 +- src/index.ts | 112 +++++++++++++++++++++------------------------------ yarn.lock | 8 ++-- 3 files changed, 51 insertions(+), 71 deletions(-) diff --git a/package.json b/package.json index 24d39ba..59361e3 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,7 @@ "cac": "^6.7.2", "chalk": "^4.1.0", "chokidar": "^3.5.1", - "esbuild": "^0.8.54", + "esbuild": "^0.9.2", "globby": "^11.0.2", "joycon": "^2.2.5", "postcss-load-config": "^3.0.1", diff --git a/src/index.ts b/src/index.ts index fd524e1..a5c385a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,7 +3,7 @@ import { dirname, join, extname } from 'path' import { Worker } from 'worker_threads' import colors from 'chalk' import { transform as transformToEs5 } from 'buble' -import { Service, startService, BuildResult } from 'esbuild' +import { build as esbuild, BuildResult } from 'esbuild' import type { MarkRequired, Buildable } from 'ts-essentials' import { getDeps, @@ -87,8 +87,6 @@ export type NormalizedOptions = MarkRequired< 'entryPoints' | 'format' | 'outDir' > -const services: Map = new Map() - export const makeLabel = (input: string, type: 'info' | 'success' | 'error') => colors[type === 'info' ? 'bgBlue' : type === 'error' ? 'bgRed' : 'bgGreen']( colors.black(` ${input.toUpperCase()} `) @@ -116,11 +114,6 @@ export async function runEsbuild( options: NormalizedOptions, { format, css }: { format: Format; css?: Map } ): Promise { - let service = services.get(format) - if (!service) { - service = await startService() - services.set(format, service) - } const pkg = await loadPkg(process.cwd()) const deps = await getDeps(process.cwd()) const external = [...deps, ...(options.external || [])] @@ -143,52 +136,50 @@ export async function runEsbuild( const splitting = options.splitting !== false - if (service) { - try { - result = await service.build({ - entryPoints: options.entryPoints, - format: splitting && format === 'cjs' ? 'esm' : format, - bundle: true, - platform: 'node', - globalName: options.globalName, - jsxFactory: options.jsxFactory, - jsxFragment: options.jsxFragment, - sourcemap: options.sourcemap, - target: options.target === 'es5' ? 'es2016' : options.target, - plugins: [ - // esbuild's `external` option doesn't support RegExp - // So here we use a custom plugin to implement it - externalPlugin(external), - postcssPlugin({ css }), - sveltePlugin({ css }), - ], - define: { - ...options.define, - ...Object.keys(env).reduce((res, key) => { - return { - ...res, - [`process.env.${key}`]: JSON.stringify(env[key]), - } - }, {}), - }, - outdir: - options.legacyOutput && format !== 'cjs' - ? join(outDir, format) - : outDir, - outExtension: options.legacyOutput ? undefined : outExtension, - write: false, - splitting: splitting && (format === 'cjs' || format === 'esm'), - logLevel: 'error', - minify: options.minify, - minifyWhitespace: options.minifyWhitespace, - minifyIdentifiers: options.minifyIdentifiers, - minifySyntax: options.minifySyntax, - keepNames: options.keepNames, - }) - } catch (error) { - console.error(`${makeLabel(format, 'error')} Build failed`) - throw error - } + try { + result = await esbuild({ + entryPoints: options.entryPoints, + format: splitting && format === 'cjs' ? 'esm' : format, + bundle: true, + platform: 'node', + globalName: options.globalName, + jsxFactory: options.jsxFactory, + jsxFragment: options.jsxFragment, + sourcemap: options.sourcemap, + target: options.target === 'es5' ? 'es2016' : options.target, + plugins: [ + // esbuild's `external` option doesn't support RegExp + // So here we use a custom plugin to implement it + externalPlugin(external), + postcssPlugin({ css }), + sveltePlugin({ css }), + ], + define: { + ...options.define, + ...Object.keys(env).reduce((res, key) => { + return { + ...res, + [`process.env.${key}`]: JSON.stringify(env[key]), + } + }, {}), + }, + outdir: + options.legacyOutput && format !== 'cjs' + ? join(outDir, format) + : outDir, + outExtension: options.legacyOutput ? undefined : outExtension, + write: false, + splitting: splitting && (format === 'cjs' || format === 'esm'), + logLevel: 'error', + minify: options.minify, + minifyWhitespace: options.minifyWhitespace, + minifyIdentifiers: options.minifyIdentifiers, + minifySyntax: options.minifySyntax, + keepNames: options.keepNames, + }) + } catch (error) { + console.error(`${makeLabel(format, 'error')} Build failed`) + throw error } // Manually write files @@ -266,13 +257,6 @@ export async function runEsbuild( return result } -function stopServices() { - for (const [name, service] of services.entries()) { - service.stop() - services.delete(name) - } -} - const normalizeOptions = async ( optionsFromConfigFile: Options, optionsOverride: Options @@ -397,13 +381,9 @@ export async function build(_options: Options) { if (options.watch) { await startWatcher() - } else { - stopServices() } } catch (error) { - if (!options.watch) { - stopServices() - } else { + if (options.watch) { startWatcher() } throw error diff --git a/yarn.lock b/yarn.lock index 4274e4d..6559f50 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1464,10 +1464,10 @@ esbuild@^0.8.53: resolved "https://registry.yarnpkg.com/esbuild/-/esbuild-0.8.53.tgz#b408bb0ca1b29dab13d8bbf7d59f59afe6776e86" integrity sha512-GIaYGdMukH58hu+lf07XWAeESBYFAsz8fXnrylHDCbBXKOSNtFmoYA8PhSeSF+3/qzeJ0VjzV9AkLURo5yfu3g== -esbuild@^0.8.54: - version "0.8.54" - resolved "https://registry.yarnpkg.com/esbuild/-/esbuild-0.8.54.tgz#2f32ff80e95c69a0f25b799d76a27c05e2857cdf" - integrity sha512-DJH38OiTgXJxFb/EhHrCrY8eGmtdkTtWymHpN9IYN9AF+4jykT0dQArr7wzFejpVbaB0TMIq2+vfNRWr3LXpvw== +esbuild@^0.9.2: + version "0.9.2" + resolved "https://registry.yarnpkg.com/esbuild/-/esbuild-0.9.2.tgz#7e9fde247c913ed8ee059e2648b0c53f7d00abe5" + integrity sha512-xE3oOILjnmN8PSjkG3lT9NBbd1DbxNqolJ5qNyrLhDWsFef3yTp/KTQz1C/x7BYFKbtrr9foYtKA6KA1zuNAUQ== escalade@^3.1.1: version "3.1.1"