@@ -252,17 +252,44 @@ async function getRequiredSourceFiles(
252252
253253 const graph = JSON . parse ( stdout ) as ModuleGraphJson
254254
255+ // Collect the specifiers of every module reachable through a *code* (runtime)
256+ // import edge. Deno's module graph classifies each dependency as either a `code`
257+ // edge (a real runtime import) or a `type`-only edge (`import type`, `@deno-types`).
258+ // Type-only edges are erased during transpilation and are never loaded at runtime
259+ // (`deno run` doesn't type-check), so the files they point to don't belong in the
260+ // bundle. The entry points themselves are always runtime.
261+ const runtimeSpecifiers = new Set < string > ( graph . roots )
262+ for ( const module of graph . modules ) {
263+ for ( const dependency of module . dependencies ?? [ ] ) {
264+ if ( dependency . code ?. specifier ) {
265+ runtimeSpecifiers . add ( dependency . code . specifier )
266+ }
267+ }
268+ }
269+
255270 // Extract all local files from the module graph
256271 for ( const module of graph . modules ) {
257- if ( module . specifier . startsWith ( 'file://' ) ) {
258- if ( module . error ?. startsWith ( 'Module not found' ) ) {
259- // Module graph contains all found imported/required modules, even if they don't actually exist
260- // This can happen for optional dependencies (dynamic import or require in try/catch).
272+ if ( ! module . specifier . startsWith ( 'file://' ) ) {
273+ continue
274+ }
275+
276+ if ( module . error ) {
277+ // A module reachable only through type-only edges (e.g. a directory specifier
278+ // behind `import type`) can fail to resolve as an ES module. That's safe to
279+ // ignore: the runtime never loads it.
280+ if ( ! runtimeSpecifiers . has ( module . specifier ) ) {
281+ continue
282+ }
283+
284+ if ( module . error . startsWith ( 'Module not found' ) ) {
285+ // Module graph contains all found imported/required modules, even if they don't
286+ // actually exist. This can happen for optional dependencies (dynamic import or
287+ // require in try/catch).
261288 continue
262289 }
263- const filePath = fileURLToPath ( module . specifier )
264- localFiles . add ( filePath )
265290 }
291+
292+ localFiles . add ( fileURLToPath ( module . specifier ) )
266293 }
267294 }
268295
0 commit comments