11import dependencyTree from "@11ty/dependency-tree" ;
22import { find , findGraph , mergeGraphs } from "@11ty/dependency-tree-esm" ;
3+ import {
4+ find as findTypeScript ,
5+ findGraph as findTypeScriptGraph ,
6+ } from "@11ty/dependency-tree-typescript" ;
37import { TemplatePath } from "@11ty/eleventy-utils" ;
48import { DepGraph } from "dependency-graph" ;
59
@@ -11,12 +15,33 @@ class JavaScriptDependencies {
1115 return `A problem was encountered looking for JavaScript dependencies in ${ type } file: ${ file } . This only affects --watch and --serve behavior and does not affect your build.` ;
1216 }
1317
18+ static getFlavor ( filePath , isProjectUsingEsm ) {
19+ if (
20+ ( isProjectUsingEsm && ( filePath . endsWith ( ".js" ) || filePath . endsWith ( ".ts" ) ) ) ||
21+ filePath . endsWith ( ".mjs" ) ||
22+ filePath . endsWith ( ".mts" )
23+ ) {
24+ return "esm" ;
25+ }
26+ if (
27+ ( ! isProjectUsingEsm && ( filePath . endsWith ( ".js" ) || filePath . endsWith ( ".ts" ) ) ) ||
28+ filePath . endsWith ( ".cjs" ) ||
29+ filePath . endsWith ( ".cts" )
30+ ) {
31+ return "cjs" ;
32+ }
33+ }
34+
35+ static isTypeScript ( filePath ) {
36+ return filePath . endsWith ( ".ts" ) || filePath . endsWith ( ".cts" ) || filePath . endsWith ( ".mts" ) ;
37+ }
38+
1439 static async getCommonJsDependencies ( inputFiles , isProjectUsingEsm ) {
1540 let depSet = new Set ( ) ;
1641
1742 // TODO does this need to work with aliasing? what other JS extensions will have deps?
1843 let commonJsFiles = inputFiles . filter (
19- ( file ) => ( ! isProjectUsingEsm && file . endsWith ( ".js" ) ) || file . endsWith ( ". cjs") ,
44+ ( file ) => this . getFlavor ( file , isProjectUsingEsm ) === " cjs",
2045 ) ;
2146
2247 for ( let file of commonJsFiles ) {
@@ -42,12 +67,10 @@ class JavaScriptDependencies {
4267 static async getEsmDependencies ( inputFiles , isProjectUsingEsm ) {
4368 let depSet = new Set ( ) ;
4469
45- let esmFiles = inputFiles . filter (
46- ( file ) => ( isProjectUsingEsm && file . endsWith ( ".js" ) ) || file . endsWith ( ".mjs" ) ,
47- ) ;
70+ let esmFiles = inputFiles . filter ( ( file ) => this . getFlavor ( file , isProjectUsingEsm ) === "esm" ) ;
4871 for ( let file of esmFiles ) {
4972 try {
50- let modules = await find ( file ) ;
73+ let modules = await ( this . isTypeScript ( file ) ? findTypeScript : find ) ( file ) ;
5174 for ( let dep of modules ) {
5275 depSet . add ( dep ) ;
5376 }
@@ -67,12 +90,10 @@ class JavaScriptDependencies {
6790
6891 static async getEsmGraph ( inputFiles , isProjectUsingEsm ) {
6992 let rootGraph = new DepGraph ( ) ;
70- let esmFiles = inputFiles . filter (
71- ( file ) => ( isProjectUsingEsm && file . endsWith ( ".js" ) ) || file . endsWith ( ".mjs" ) ,
72- ) ;
93+ let esmFiles = inputFiles . filter ( ( file ) => this . getFlavor ( file , isProjectUsingEsm ) === "esm" ) ;
7394 for ( let file of esmFiles ) {
7495 try {
75- let graph = await findGraph ( file ) ;
96+ let graph = await ( this . isTypeScript ( file ) ? findTypeScriptGraph : findGraph ) ( file ) ;
7697
7798 mergeGraphs ( rootGraph , graph ) ;
7899 } catch ( e ) {
0 commit comments