From d4a98017416d6da97b77a4101ecfb60cee18f91d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=89=E5=92=B2=E6=99=BA=E5=AD=90=20Kevin=20Deng?= Date: Tue, 17 Oct 2023 10:25:35 +0800 Subject: [PATCH] refactor --- src/esbuild/index.ts | 51 ++++++++++++++++++++++++++++++++++---------- src/esbuild/utils.ts | 22 +++++++++++-------- 2 files changed, 53 insertions(+), 20 deletions(-) diff --git a/src/esbuild/index.ts b/src/esbuild/index.ts index dc1115c9..96a1af49 100644 --- a/src/esbuild/index.ts +++ b/src/esbuild/index.ts @@ -49,21 +49,36 @@ export function getEsbuildPlugin>( return undefined } - const { errors, warnings, watchFiles, pluginContext, buildContext } = createPluginContext() + const { errors, warnings, mixedContext } = createPluginContext(context) const isEntry = args.kind === 'entry-point' const result = await plugin.resolveId!.call( - { ...context, ...pluginContext, ...buildContext }, + mixedContext, args.path, // We explicitly have this if statement here for consistency with the integration of other bundlers. // Here, `args.importer` is just an empty string on entry files whereas the equivalent on other bundlers is `undefined.` isEntry ? undefined : args.importer, { isEntry }, ) - if (typeof result === 'string') - return { path: result, namespace: plugin.name, errors, warnings, watchFiles } - else if (typeof result === 'object' && result !== null) - return { path: result.id, external: result.external, namespace: plugin.name, errors, warnings, watchFiles } + if (typeof result === 'string') { + return { + path: result, + namespace: plugin.name, + errors, + warnings, + watchFiles: mixedContext.getWatchFiles(), + } + } + else if (typeof result === 'object' && result !== null) { + return { + path: result.id, + external: result.external, + namespace: plugin.name, + errors, + warnings, + watchFiles: mixedContext.getWatchFiles(), + } + } }) } @@ -71,7 +86,7 @@ export function getEsbuildPlugin>( onLoad({ filter: onLoadFilter }, async (args) => { const id = args.path + args.suffix - const { errors, warnings, watchFiles, pluginContext, buildContext } = createPluginContext() + const { errors, warnings, mixedContext } = createPluginContext(context) // because we use `namespace` to simulate virtual modules, // it is required to forward `resolveDir` for esbuild to find dependencies. @@ -80,7 +95,7 @@ export function getEsbuildPlugin>( let code: string | undefined, map: SourceMap | null | undefined if (plugin.load && (!plugin.loadInclude || plugin.loadInclude(id))) { - const result = await plugin.load.call({ ...context, ...pluginContext, ...buildContext }, id) + const result = await plugin.load.call(mixedContext, id) if (typeof result === 'string') { code = result } @@ -97,7 +112,14 @@ export function getEsbuildPlugin>( if (map) code = processCodeWithSourceMap(map, code) - return { contents: code, errors, warnings, watchFiles, loader: unwrapLoader(loader, code, args.path), resolveDir } + return { + contents: code, + errors, + warnings, + watchFiles: mixedContext.getWatchFiles(), + loader: unwrapLoader(loader, code, args.path), + resolveDir, + } } if (!plugin.transformInclude || plugin.transformInclude(id)) { @@ -108,7 +130,7 @@ export function getEsbuildPlugin>( code = await fs.promises.readFile(args.path, 'utf8') } - const result = await plugin.transform.call({ ...context, ...pluginContext, ...buildContext }, code, id) + const result = await plugin.transform.call(mixedContext, code, id) if (typeof result === 'string') { code = result } @@ -132,7 +154,14 @@ export function getEsbuildPlugin>( if (code) { if (map) code = processCodeWithSourceMap(map, code) - return { contents: code, errors, warnings, watchFiles, loader: unwrapLoader(loader, code, args.path), resolveDir } + return { + contents: code, + errors, + warnings, + watchFiles: mixedContext.getWatchFiles(), + loader: unwrapLoader(loader, code, args.path), + resolveDir, + } } }) } diff --git a/src/esbuild/utils.ts b/src/esbuild/utils.ts index 07fe87d8..d7438595 100644 --- a/src/esbuild/utils.ts +++ b/src/esbuild/utils.ts @@ -111,6 +111,8 @@ export function combineSourcemaps( } export function createBuildContext(initialOptions: BuildOptions): UnpluginBuildContext { + const watchFiles: string[] = [] + return { parse(code: string, opts: any = {}) { return Parser.parse(code, { @@ -133,29 +135,31 @@ export function createBuildContext(initialOptions: BuildOptions): UnpluginBuildC fs.writeFileSync(path.resolve(initialOptions.outdir, outFileName), emittedFile.source) }, getWatchFiles() { - return [] + return watchFiles }, } } -export function createPluginContext() { +export function createPluginContext(context: UnpluginBuildContext) { const errors: PartialMessage[] = [] const warnings: PartialMessage[] = [] - const watchFiles: string[] = [] const pluginContext: UnpluginContext = { error(message) { errors.push({ text: String(message) }) }, warn(message) { warnings.push({ text: String(message) }) }, } - const buildContext: Partial = { - addWatchFile(id) { watchFiles.push(id) }, - getWatchFiles() { return watchFiles }, + + const mixedContext: UnpluginContext & UnpluginBuildContext = { + ...context, + ...pluginContext, + addWatchFile(id: string) { + context.getWatchFiles().push(id) + }, } + return { errors, warnings, - watchFiles, - pluginContext, - buildContext, + mixedContext, } }