Skip to content
This repository has been archived by the owner on Feb 16, 2024. It is now read-only.

Commit

Permalink
fix: avoid buildStart hook for formatter resolution
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
brawaru committed Oct 23, 2023
1 parent 24c7efd commit 7d11f38
Showing 1 changed file with 8 additions and 53 deletions.
61 changes: 8 additions & 53 deletions src/plugin/plugin.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -75,53 +75,13 @@ export const plugin = createUnplugin<Options_, false>((options_, meta) => {
compileFunc = await getBultinFormatter(format)
}

/** @see https://github.com/unjs/unplugin/issues/293 */
const buildStart = (() => {
let buildStartCall: Promise<void> | 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<RollupPlugin, 'name'>
})()),
rollupWrappingPartial(pluginsWrapping, filter)),
api,
},

Expand All @@ -143,18 +103,13 @@ export const plugin = createUnplugin<Options_, false>((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) {
Expand Down

0 comments on commit 7d11f38

Please sign in to comment.