Skip to content

Commit 8764e47

Browse files
committed
perf: native filter for external plugin
1 parent 9fadadd commit 8764e47

File tree

1 file changed

+41
-36
lines changed

1 file changed

+41
-36
lines changed

src/features/external.ts

Lines changed: 41 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import path from 'node:path'
33
import { blue, underline } from 'ansis'
44
import { createDebug } from 'obug'
55
import { 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'
78
import { shimFile } from './shims.ts'
89
import type { ResolvedConfig } from '../config/index.ts'
910
import 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.
6468
To fix this, either add it to ${blue`inlineOnly`}, declare it as a production or peer dependency in your package.json, or externalize it manually.
6569
Imported by ${underline(importer)}`,
66-
)
70+
)
71+
}
6772
}
68-
}
73+
},
6974
},
7075
}
7176

0 commit comments

Comments
 (0)