|
1 | 1 | import { builtinModules } from 'node:module' |
| 2 | +import path from 'node:path' |
| 3 | +import { blue, underline } from 'ansis' |
2 | 4 | import Debug from 'debug' |
| 5 | +import { RE_DTS, RE_NODE_MODULES } from 'rolldown-plugin-dts/filename' |
3 | 6 | import { shimFile } from '../index' |
4 | | -import { toArray } from '../utils/general' |
| 7 | +import { matchPattern } from '../utils/general' |
5 | 8 | import type { ResolvedOptions } from '../options' |
6 | 9 | import type { PackageJson } from 'pkg-types' |
7 | | -import type { Plugin } from 'rolldown' |
| 10 | +import type { Plugin, PluginContext, ResolveIdExtraOptions } from 'rolldown' |
8 | 11 |
|
9 | 12 | const debug = Debug('tsdown:external') |
10 | 13 |
|
11 | | -export function ExternalPlugin(options: ResolvedOptions): Plugin { |
12 | | - const deps = options.pkg && Array.from(getProductionDeps(options.pkg)) |
| 14 | +export function ExternalPlugin({ |
| 15 | + pkg, |
| 16 | + noExternal, |
| 17 | + inlineOnly, |
| 18 | + skipNodeModulesBundle, |
| 19 | +}: ResolvedOptions): Plugin { |
| 20 | + const deps = pkg && Array.from(getProductionDeps(pkg)) |
| 21 | + |
13 | 22 | return { |
14 | 23 | name: 'tsdown:external', |
15 | 24 | async resolveId(id, importer, extraOptions) { |
16 | | - if (extraOptions.isEntry) return |
17 | | - if (id === shimFile) return |
| 25 | + if (extraOptions.isEntry || !importer) return |
18 | 26 |
|
19 | | - const { noExternal } = options |
20 | | - if (typeof noExternal === 'function' && noExternal(id, importer)) { |
21 | | - return |
22 | | - } |
23 | | - if (noExternal) { |
24 | | - const noExternalPatterns = toArray(noExternal) |
25 | | - if ( |
26 | | - noExternalPatterns.some((pattern) => { |
27 | | - if (pattern instanceof RegExp) { |
28 | | - pattern.lastIndex = 0 |
29 | | - return pattern.test(id) |
30 | | - } |
31 | | - return id === pattern |
32 | | - }) |
33 | | - ) |
34 | | - return |
35 | | - } |
| 27 | + const shouldExternal = await externalStrategy( |
| 28 | + this, |
| 29 | + id, |
| 30 | + importer, |
| 31 | + extraOptions, |
| 32 | + ) |
| 33 | + const nodeBuiltinModule = |
| 34 | + id.startsWith('node:') || builtinModules.includes(id) |
36 | 35 |
|
37 | | - let shouldExternal: boolean | 'absolute' = false |
38 | | - if (options.skipNodeModulesBundle) { |
39 | | - const resolved = await this.resolve(id, importer, extraOptions) |
40 | | - if (!resolved) return resolved |
41 | | - shouldExternal = |
42 | | - resolved.external || /[\\/]node_modules[\\/]/.test(resolved.id) |
43 | | - } |
44 | | - if (deps) { |
45 | | - shouldExternal ||= deps.some( |
46 | | - (dep) => id === dep || id.startsWith(`${dep}/`), |
47 | | - ) |
48 | | - } |
| 36 | + debug('shouldExternal: %s = %s', id, shouldExternal) |
49 | 37 |
|
50 | | - if (shouldExternal) { |
51 | | - debug('External dependency:', id) |
| 38 | + if (shouldExternal === true || shouldExternal === 'absolute') { |
52 | 39 | return { |
53 | 40 | id, |
54 | 41 | external: shouldExternal, |
55 | | - moduleSideEffects: |
56 | | - id.startsWith('node:') || builtinModules.includes(id) |
57 | | - ? false |
58 | | - : undefined, |
| 42 | + moduleSideEffects: nodeBuiltinModule ? false : undefined, |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + if ( |
| 47 | + inlineOnly && |
| 48 | + !RE_DTS.test(importer) && // skip dts files |
| 49 | + !nodeBuiltinModule && // skip node built-in modules |
| 50 | + id[0] !== '.' && // skip relative imports |
| 51 | + !path.isAbsolute(id) // skip absolute imports |
| 52 | + ) { |
| 53 | + const shouldInline = |
| 54 | + shouldExternal === 'no-external' || // force inline |
| 55 | + matchPattern(id, inlineOnly) |
| 56 | + debug('shouldInline: %s = %s', id, shouldInline) |
| 57 | + if (shouldInline) return |
| 58 | + |
| 59 | + const resolved = await this.resolve(id, importer, extraOptions) |
| 60 | + if (!resolved) return |
| 61 | + |
| 62 | + if (RE_NODE_MODULES.test(resolved.id)) { |
| 63 | + throw new Error( |
| 64 | + `${underline(id)} is located in node_modules but is not included in ${blue`inlineOnly`} option. |
| 65 | +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. |
| 66 | +Imported by ${underline(importer)}`, |
| 67 | + ) |
59 | 68 | } |
60 | 69 | } |
61 | 70 | }, |
62 | 71 | } |
| 72 | + |
| 73 | + /** |
| 74 | + * - `true`: always external |
| 75 | + * - `false`: skip, let other plugins handle it |
| 76 | + * - `'absolute'`: external as absolute path |
| 77 | + * - `'no-external'`: skip, but mark as non-external for inlineOnly check |
| 78 | + */ |
| 79 | + async function externalStrategy( |
| 80 | + context: PluginContext, |
| 81 | + id: string, |
| 82 | + importer: string | undefined, |
| 83 | + extraOptions: ResolveIdExtraOptions, |
| 84 | + ): Promise<boolean | 'absolute' | 'no-external'> { |
| 85 | + if (id === shimFile) return false |
| 86 | + |
| 87 | + if (noExternal?.(id, importer)) { |
| 88 | + return 'no-external' |
| 89 | + } |
| 90 | + |
| 91 | + if (skipNodeModulesBundle) { |
| 92 | + const resolved = await context.resolve(id, importer, extraOptions) |
| 93 | + if (!resolved) return false |
| 94 | + return resolved.external || RE_NODE_MODULES.test(resolved.id) |
| 95 | + } |
| 96 | + |
| 97 | + if (deps) { |
| 98 | + return deps.some((dep) => id === dep || id.startsWith(`${dep}/`)) |
| 99 | + } |
| 100 | + |
| 101 | + return false |
| 102 | + } |
63 | 103 | } |
64 | 104 |
|
65 | 105 | /* |
|
0 commit comments