Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
sxzz committed Oct 17, 2023
1 parent 15abe8a commit d4a9801
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 20 deletions.
51 changes: 40 additions & 11 deletions src/esbuild/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,29 +49,44 @@ export function getEsbuildPlugin<UserOptions = Record<string, never>>(
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(),
}
}
})
}

if (plugin.load || plugin.transform) {
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.
Expand All @@ -80,7 +95,7 @@ export function getEsbuildPlugin<UserOptions = Record<string, never>>(
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
}
Expand All @@ -97,7 +112,14 @@ export function getEsbuildPlugin<UserOptions = Record<string, never>>(
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)) {
Expand All @@ -108,7 +130,7 @@ export function getEsbuildPlugin<UserOptions = Record<string, never>>(
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
}
Expand All @@ -132,7 +154,14 @@ export function getEsbuildPlugin<UserOptions = Record<string, never>>(
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,
}
}
})
}
Expand Down
22 changes: 13 additions & 9 deletions src/esbuild/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand All @@ -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<UnpluginBuildContext> = {
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,
}
}

Expand Down

0 comments on commit d4a9801

Please sign in to comment.