From 7d11f38fdd8e114026198be9c1397bc61e8ada68 Mon Sep 17 00:00:00 2001 From: Alexander Sorokin <10401817+brawaru@users.noreply.github.com> Date: Mon, 23 Oct 2023 16:02:58 +0200 Subject: [PATCH] fix: avoid buildStart hook for formatter resolution This hook is extremely unreliable and may be called several times at once, so it's best to avoid it and instead completely move formatter resolution to the transform hook, even though that doesn't make sense concern-wise. --- src/plugin/plugin.ts | 61 ++++++-------------------------------------- 1 file changed, 8 insertions(+), 53 deletions(-) diff --git a/src/plugin/plugin.ts b/src/plugin/plugin.ts index e9df633..780a869 100644 --- a/src/plugin/plugin.ts +++ b/src/plugin/plugin.ts @@ -1,6 +1,6 @@ import { dataToEsm } from '@rollup/pluginutils' import type { CompileFn } from '@formatjs/cli-lib' -import { createUnplugin, type RollupPlugin } from 'unplugin' +import { createUnplugin } from 'unplugin' import { basePluginName } from '../shared/consts.ts' import { createOptionsResolver, @@ -75,53 +75,13 @@ export const plugin = createUnplugin((options_, meta) => { compileFunc = await getBultinFormatter(format) } - /** @see https://github.com/unjs/unplugin/issues/293 */ - const buildStart = (() => { - let buildStartCall: Promise | undefined - - function buildStart() { - if (buildStartCall != null) { - throw new BaseError('buildStart() has already been called') - } - - return (buildStartCall = (async () => { - try { - return await resolveFormatter() - } finally { - buildStartCall = undefined - } - })()) - } - - return { - run: buildStart, - async waitForCompletion() { - await buildStartCall - }, - } - })() - return { name: basePluginName, rollup: { ...(pluginsWrapping.use && meta.framework === 'rollup' && - (() => { - // unplugin doesn't have hook merging, so this is the way to go - const { buildStart: pluginsWrap } = rollupWrappingPartial( - pluginsWrapping, - filter, - ) - - return { - async buildStart(rollupOptions) { - await buildStart.run() - - pluginsWrap?.call(this, rollupOptions) - }, - } satisfies Omit - })()), + rollupWrappingPartial(pluginsWrapping, filter)), api, }, @@ -143,18 +103,13 @@ export const plugin = createUnplugin((options_, meta) => { return filter(id) }, - buildStart: buildStart.run, - async transform(code, id) { - try { - await buildStart.waitForCompletion() - } catch (err) { - this.error( - new BaseError('Previously invoked buildStart() failed', { - cause: err, - }), - ) - return + if (compileFunc == null) { + try { + await resolveFormatter() + } catch (err) { + this.error(err) + } } if (compileFunc == null) {