-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(scripts): replace eval with static analysis (#1978)
- rather than evaluating feature files to find them all, logic now parses code and resolves imports to find entire graph - this makes it easier to shift towards native esm, as the evaluation logic was commonjs-specific
- Loading branch information
Showing
6 changed files
with
99 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { ResolvedRequests, createDependencyResolver, createRequestResolver } from '@file-services/resolve'; | ||
import fs from 'node:fs'; | ||
import path from 'node:path'; | ||
import ts from 'typescript'; | ||
import { isCodeModule } from '../build-constants.js'; | ||
|
||
const { | ||
SyntaxKind: { ImportKeyword }, | ||
forEachChild, | ||
isCallExpression, | ||
isExportDeclaration, | ||
isIdentifier, | ||
isImportDeclaration, | ||
isStringLiteral, | ||
} = ts; | ||
|
||
export function resolveModuleGraph(filePaths: string | string[]): Record<string, ResolvedRequests> { | ||
const resolveRequest = createRequestResolver({ fs: { ...fs, ...path } }); | ||
const dependencyResolver = createDependencyResolver({ | ||
extractRequests(filePath) { | ||
if (!isCodeModule(path.basename(filePath))) { | ||
return []; | ||
} | ||
const fileContents = fs.readFileSync(filePath, 'utf8'); | ||
const sourceFile = ts.createSourceFile(filePath, fileContents, ts.ScriptTarget.ESNext); | ||
return extractModuleRequests(sourceFile); | ||
}, | ||
resolveRequest(filePath, request) { | ||
const contextPath = path.dirname(filePath); | ||
return resolveRequest(contextPath, request).resolvedFile; | ||
}, | ||
}); | ||
return dependencyResolver(filePaths, true); | ||
} | ||
|
||
function extractModuleRequests(sourceFile: ts.SourceFile): string[] { | ||
const specifiers: string[] = []; | ||
|
||
const dynamicImportsFinder = (node: ts.Node): void => { | ||
if (isCallExpression(node)) { | ||
if ( | ||
(isDynamicImportKeyword(node.expression) || isRequireIdentifier(node.expression)) && | ||
node.arguments.length === 1 && | ||
isStringLiteral(node.arguments[0]!) | ||
) { | ||
const [{ text }] = node.arguments; | ||
specifiers.push(text); | ||
return; | ||
} | ||
} | ||
forEachChild(node, dynamicImportsFinder); | ||
}; | ||
|
||
const importsFinder = (node: ts.Node) => { | ||
const isNonTypeImport = isImportDeclaration(node) && !node.importClause?.isTypeOnly; | ||
const isNonTypeExport = isExportDeclaration(node) && !node.isTypeOnly; | ||
|
||
if ((isNonTypeImport || isNonTypeExport) && node.moduleSpecifier && isStringLiteral(node.moduleSpecifier)) { | ||
const originalTarget = node.moduleSpecifier.text; | ||
specifiers.push(originalTarget); | ||
return; | ||
} | ||
|
||
// if not a static import/re-export, might be a dynamic import | ||
// so run that recursive visitor on `node` | ||
forEachChild(node, dynamicImportsFinder); | ||
}; | ||
|
||
forEachChild(sourceFile, importsFinder); | ||
return specifiers; | ||
} | ||
|
||
function isRequireIdentifier(expression: ts.Expression): expression is ts.Identifier { | ||
return isIdentifier(expression) && expression.text === 'require'; | ||
} | ||
|
||
function isDynamicImportKeyword({ kind }: ts.Expression) { | ||
return kind === ImportKeyword; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters