MCPcopy
hub / github.com/vitejs/vite / createIsConfiguredAsExternal

Function createIsConfiguredAsExternal

packages/vite/src/node/external.ts:35–125  ·  view source on GitHub ↗
(
  environment: PartialEnvironment,
)

Source from the content-addressed store, hash-verified

33}
34
35export function createIsConfiguredAsExternal(
36 environment: PartialEnvironment,
37): (id: string, importer?: string) => boolean {
38 const { config } = environment
39 const { root, resolve } = config
40 const { external, noExternal } = resolve
41 const noExternalFilter =
42 typeof noExternal !== 'boolean' &&
43 !(Array.isArray(noExternal) && noExternal.length === 0) &&
44 createFilter(undefined, noExternal, { resolve: false })
45
46 const targetConditions = resolve.externalConditions
47
48 const resolveOptions: InternalResolveOptions = {
49 ...resolve,
50 root,
51 isProduction: false,
52 isBuild: true,
53 conditions: targetConditions,
54 }
55
56 const isExternalizable = (
57 id: string,
58 importer: string | undefined,
59 configuredAsExternal: boolean,
60 ): boolean => {
61 if (!bareImportRE.test(id) || id.includes('\0')) {
62 return false
63 }
64 try {
65 const resolved = tryNodeResolve(
66 id,
67 // Skip passing importer in build to avoid externalizing non-hoisted dependencies
68 // unresolvable from root (which would be unresolvable from output bundles also)
69 config.command === 'build' ? undefined : importer,
70 resolveOptions,
71 undefined,
72 false,
73 )
74 if (!resolved) {
75 return false
76 }
77 // Only allow linked packages to be externalized
78 // if they are explicitly configured as external
79 if (!configuredAsExternal && !isInNodeModules(resolved.id)) {
80 return false
81 }
82 return canExternalizeFile(resolved.id)
83 } catch {
84 debug?.(
85 `Failed to node resolve "${id}". Skipping externalizing it by default.`,
86 )
87 // may be an invalid import that's resolved by a plugin
88 return false
89 }
90 }
91
92 // Returns true if it is configured as external, false if it is filtered

Callers 2

createIsExternalFunction · 0.90
createIsExternalFunction · 0.85

Calls 3

getNpmPackageNameFunction · 0.90
createFilterFunction · 0.85
isExternalizableFunction · 0.85

Tested by 1

createIsExternalFunction · 0.72