| 1 | import fs from 'fs'
|
|---|
| 2 | import path from 'path'
|
|---|
| 3 |
|
|---|
| 4 | let jsExtensions = ['.js', '.cjs', '.mjs']
|
|---|
| 5 |
|
|---|
| 6 | // Given the current file `a.ts`, we want to make sure that when importing `b` that we resolve
|
|---|
| 7 | // `b.ts` before `b.js`
|
|---|
| 8 | //
|
|---|
| 9 | // E.g.:
|
|---|
| 10 | //
|
|---|
| 11 | // a.ts
|
|---|
| 12 | // b // .ts
|
|---|
| 13 | // c // .ts
|
|---|
| 14 | // a.js
|
|---|
| 15 | // b // .js or .ts
|
|---|
| 16 |
|
|---|
| 17 | let jsResolutionOrder = ['', '.js', '.cjs', '.mjs', '.ts', '.cts', '.mts', '.jsx', '.tsx']
|
|---|
| 18 | let tsResolutionOrder = ['', '.ts', '.cts', '.mts', '.tsx', '.js', '.cjs', '.mjs', '.jsx']
|
|---|
| 19 |
|
|---|
| 20 | function resolveWithExtension(file, extensions) {
|
|---|
| 21 | // Try to find `./a.ts`, `./a.ts`, ... from `./a`
|
|---|
| 22 | for (let ext of extensions) {
|
|---|
| 23 | let full = `${file}${ext}`
|
|---|
| 24 | if (fs.existsSync(full) && fs.statSync(full).isFile()) {
|
|---|
| 25 | return full
|
|---|
| 26 | }
|
|---|
| 27 | }
|
|---|
| 28 |
|
|---|
| 29 | // Try to find `./a/index.js` from `./a`
|
|---|
| 30 | for (let ext of extensions) {
|
|---|
| 31 | let full = `${file}/index${ext}`
|
|---|
| 32 | if (fs.existsSync(full)) {
|
|---|
| 33 | return full
|
|---|
| 34 | }
|
|---|
| 35 | }
|
|---|
| 36 |
|
|---|
| 37 | return null
|
|---|
| 38 | }
|
|---|
| 39 |
|
|---|
| 40 | function* _getModuleDependencies(filename, base, seen, ext = path.extname(filename)) {
|
|---|
| 41 | // Try to find the file
|
|---|
| 42 | let absoluteFile = resolveWithExtension(
|
|---|
| 43 | path.resolve(base, filename),
|
|---|
| 44 | jsExtensions.includes(ext) ? jsResolutionOrder : tsResolutionOrder
|
|---|
| 45 | )
|
|---|
| 46 | if (absoluteFile === null) return // File doesn't exist
|
|---|
| 47 |
|
|---|
| 48 | // Prevent infinite loops when there are circular dependencies
|
|---|
| 49 | if (seen.has(absoluteFile)) return // Already seen
|
|---|
| 50 | seen.add(absoluteFile)
|
|---|
| 51 |
|
|---|
| 52 | // Mark the file as a dependency
|
|---|
| 53 | yield absoluteFile
|
|---|
| 54 |
|
|---|
| 55 | // Resolve new base for new imports/requires
|
|---|
| 56 | base = path.dirname(absoluteFile)
|
|---|
| 57 | ext = path.extname(absoluteFile)
|
|---|
| 58 |
|
|---|
| 59 | let contents = fs.readFileSync(absoluteFile, 'utf-8')
|
|---|
| 60 |
|
|---|
| 61 | // Find imports/requires
|
|---|
| 62 | for (let match of [
|
|---|
| 63 | ...contents.matchAll(/import[\s\S]*?['"](.{3,}?)['"]/gi),
|
|---|
| 64 | ...contents.matchAll(/import[\s\S]*from[\s\S]*?['"](.{3,}?)['"]/gi),
|
|---|
| 65 | ...contents.matchAll(/require\(['"`](.+)['"`]\)/gi),
|
|---|
| 66 | ]) {
|
|---|
| 67 | // Bail out if it's not a relative file
|
|---|
| 68 | if (!match[1].startsWith('.')) continue
|
|---|
| 69 |
|
|---|
| 70 | yield* _getModuleDependencies(match[1], base, seen, ext)
|
|---|
| 71 | }
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| 74 | export default function getModuleDependencies(absoluteFilePath) {
|
|---|
| 75 | if (absoluteFilePath === null) return new Set()
|
|---|
| 76 | return new Set(
|
|---|
| 77 | _getModuleDependencies(absoluteFilePath, path.dirname(absoluteFilePath), new Set())
|
|---|
| 78 | )
|
|---|
| 79 | }
|
|---|