mirror of
https://github.com/unjs/unplugin.git
synced 2026-02-01 16:56:37 +00:00
feat: resolveId supports Rollup externals (#18)
Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
This commit is contained in:
parent
73f550ad25
commit
3c756ddd62
@ -13,6 +13,8 @@ export type Thenable<T> = T | Promise<T>
|
||||
|
||||
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<TransformResult>;
|
||||
load?: (this: UnpluginContext, id: string) => Thenable<TransformResult>
|
||||
resolveId?: (id: string, importer?: string) => Thenable<string | null | undefined>
|
||||
resolveId?: (id: string, importer?: string) => Thenable<string | ExternalIdResult | null | undefined>
|
||||
|
||||
// framework specify extends
|
||||
rollup?: Partial<RollupPlugin>
|
||||
|
||||
@ -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<UserOptions = {}> (
|
||||
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
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user