diff --git a/src/types.ts b/src/types.ts index b60b9ade..d0af35d4 100644 --- a/src/types.ts +++ b/src/types.ts @@ -13,6 +13,8 @@ export type Thenable = T | Promise export type TransformResult = string | { code: string; map?: SourceMap | null; } | null | undefined +export type ExternalIdResult = { id: string, external?: boolean } + export interface UnpluginOptions { name: string; enforce?: 'post' | 'pre' | undefined; @@ -20,7 +22,7 @@ export interface UnpluginOptions { transformInclude?: (id: string) => boolean; transform?: (this: UnpluginContext, code: string, id: string) => Thenable; load?: (this: UnpluginContext, id: string) => Thenable - resolveId?: (id: string, importer?: string) => Thenable + resolveId?: (id: string, importer?: string) => Thenable // framework specify extends rollup?: Partial diff --git a/src/webpack/index.ts b/src/webpack/index.ts index bc36cb2f..8b18316a 100644 --- a/src/webpack/index.ts +++ b/src/webpack/index.ts @@ -2,7 +2,7 @@ import fs from 'fs' import { fileURLToPath } from 'url' import path from 'upath' import VirtualModulesPlugin from 'webpack-virtual-modules' -import type { Resolver } from 'enhanced-resolve' +import type { Resolver, ResolveRequest } from 'enhanced-resolve' import type { UnpluginContextMeta, UnpluginInstance, UnpluginFactory, WebpackCompiler, ResolvedUnpluginOptions } from '../types' const _dirname = typeof __dirname !== 'undefined' ? __dirname : path.dirname(fileURLToPath(import.meta.url)) @@ -78,17 +78,21 @@ export function getWebpackPlugin ( const resolver = { apply (resolver: Resolver) { const target = resolver.ensureHook('resolve') - const tap = () => async (request: any, resolveContext: any, callback: any) => { + const tap = () => async (request: ResolveRequest, resolveContext: any, callback: any) => { // filter out invalid requests if (!request.request || request.request.startsWith(plugin.__virtualModulePrefix)) { return callback() } // call hook - let resolved = await plugin.resolveId!(request.request) - if (resolved == null) { + const result = await plugin.resolveId!(request.request) + if (result == null) { return callback() } + let resolved = typeof result === 'string' ? result : result.id + + // TODO: support external + // const isExternal = typeof result === 'string' ? false : result.external === true // if the resolved module is not exists, // we treat it as a virtual module