inline `getModuleDependencies`

master
Brad Cornes 2021-09-10 12:30:03 +01:00
parent 6d88a5c31c
commit a1e085d0cd
3 changed files with 849 additions and 1574 deletions

2380
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -30,6 +30,7 @@
"builtin-modules": "3.2.0",
"chokidar": "3.5.1",
"debounce": "1.2.0",
"detective": "5.2.0",
"dlv": "1.1.3",
"dset": "2.0.1",
"enhanced-resolve": "5.8.0",
@ -44,10 +45,10 @@
"postcss-load-config": "3.0.1",
"postcss-selector-parser": "6.0.2",
"prettier": "2.3.0",
"resolve": "1.20.0",
"rimraf": "3.0.2",
"semver": "7.3.2",
"stack-trace": "0.0.10",
"tailwindcss": "2.0.3",
"terser": "4.6.12",
"typescript": "4.2.4",
"vscode-languageserver": "7.0.0",

View File

@ -1,7 +1,45 @@
import _getModuleDependencies from 'tailwindcss/lib/lib/getModuleDependencies'
import fs from 'fs'
import path from 'path'
import resolve from 'resolve'
import detective from 'detective'
export function getModuleDependencies(modulePath: string): string[] {
return _getModuleDependencies(modulePath)
.map(({ file }) => file)
.filter((file) => file !== modulePath)
}
function createModule(file) {
const source = fs.readFileSync(file, 'utf-8')
const requires = detective(source)
return { file, requires }
}
function _getModuleDependencies(entryFile) {
const rootModule = createModule(entryFile)
const modules = [rootModule]
// Iterate over the modules, even when new
// ones are being added
for (const mdl of modules) {
mdl.requires
.filter((dep) => {
// Only track local modules, not node_modules
return dep.startsWith('./') || dep.startsWith('../')
})
.forEach((dep) => {
try {
const basedir = path.dirname(mdl.file)
const depPath = resolve.sync(dep, { basedir })
const depModule = createModule(depPath)
modules.push(depModule)
} catch (_err) {
// eslint-disable-next-line no-empty
}
})
}
return modules
}