@@ -3,7 +3,8 @@ import path from 'node:path'
33import { blue , underline } from 'ansis'
44import { createDebug } from 'obug'
55import { RE_DTS , RE_NODE_MODULES } from 'rolldown-plugin-dts/filename'
6- import { matchPattern } from '../utils/general.ts'
6+ import { and , id , importerId , include } from 'rolldown/filter'
7+ import { matchPattern , typeAssert } from '../utils/general.ts'
78import { shimFile } from './shims.ts'
89import type { ResolvedConfig } from '../config/index.ts'
910import type { PackageJson } from 'pkg-types'
@@ -21,51 +22,55 @@ export function ExternalPlugin({
2122
2223 return {
2324 name : 'tsdown:external' ,
24- async resolveId ( id , importer , extraOptions ) {
25- if ( extraOptions . isEntry || ! importer ) return
25+ resolveId : {
26+ filter : [ include ( and ( id ( / ^ [ ^ . ] / ) , importerId ( / ./ ) ) ) ] ,
27+ async handler ( id , importer , extraOptions ) {
28+ if ( extraOptions . isEntry ) return
29+ typeAssert ( importer )
2630
27- const shouldExternal = await externalStrategy (
28- this ,
29- id ,
30- importer ,
31- extraOptions ,
32- )
33- const nodeBuiltinModule = isBuiltin ( id )
31+ const shouldExternal = await externalStrategy (
32+ this ,
33+ id ,
34+ importer ,
35+ extraOptions ,
36+ )
37+ const nodeBuiltinModule = isBuiltin ( id )
3438
35- debug ( 'shouldExternal: %s = %s' , id , shouldExternal )
39+ debug ( 'shouldExternal: %s = %s' , id , shouldExternal )
3640
37- if ( shouldExternal === true || shouldExternal === 'absolute' ) {
38- return {
39- id,
40- external : shouldExternal ,
41- moduleSideEffects : nodeBuiltinModule ? false : undefined ,
41+ if ( shouldExternal === true || shouldExternal === 'absolute' ) {
42+ return {
43+ id,
44+ external : shouldExternal ,
45+ moduleSideEffects : nodeBuiltinModule ? false : undefined ,
46+ }
4247 }
43- }
4448
45- if (
46- inlineOnly &&
47- ! RE_DTS . test ( importer ) && // skip dts files
48- ! nodeBuiltinModule && // skip node built-in modules
49- id [ 0 ] !== '.' && // skip relative imports
50- ! path . isAbsolute ( id ) // skip absolute imports
51- ) {
52- const shouldInline =
53- shouldExternal === 'no-external' || // force inline
54- matchPattern ( id , inlineOnly )
55- debug ( 'shouldInline: %s = %s' , id , shouldInline )
56- if ( shouldInline ) return
49+ if (
50+ inlineOnly &&
51+ ! RE_DTS . test ( importer ) && // skip dts files
52+ ! nodeBuiltinModule && // skip node built-in modules
53+ id [ 0 ] !== '.' && // skip relative imports
54+ ! path . isAbsolute ( id ) // skip absolute imports
55+ ) {
56+ const shouldInline =
57+ shouldExternal === 'no-external' || // force inline
58+ matchPattern ( id , inlineOnly )
59+ debug ( 'shouldInline: %s = %s' , id , shouldInline )
60+ if ( shouldInline ) return
5761
58- const resolved = await this . resolve ( id , importer , extraOptions )
59- if ( ! resolved ) return
62+ const resolved = await this . resolve ( id , importer , extraOptions )
63+ if ( ! resolved ) return
6064
61- if ( RE_NODE_MODULES . test ( resolved . id ) ) {
62- throw new Error (
63- `${ underline ( id ) } is located in node_modules but is not included in ${ blue `inlineOnly` } option.
65+ if ( RE_NODE_MODULES . test ( resolved . id ) ) {
66+ throw new Error (
67+ `${ underline ( id ) } is located in node_modules but is not included in ${ blue `inlineOnly` } option.
6468To fix this, either add it to ${ blue `inlineOnly` } , declare it as a production or peer dependency in your package.json, or externalize it manually.
6569Imported by ${ underline ( importer ) } ` ,
66- )
70+ )
71+ }
6772 }
68- }
73+ } ,
6974 } ,
7075 }
7176
0 commit comments