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

fix: avoid buildStart hook for formatter resolution #49

Merged
merged 1 commit into from
Oct 23, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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