-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
137 additions
and
72 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"pluv": patch | ||
--- | ||
|
||
Fix the build command incorrectly outputting build artifacts it shouldn't have. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
import path from "node:path"; | ||
import type { CompilerOptions, Program } from "typescript"; | ||
import ts from "typescript"; | ||
|
||
export interface ResolveDependenciesParams { | ||
input: string; | ||
} | ||
|
||
const crawlDependencies = (program: Program): readonly string[] => { | ||
const [entryFileName] = program.getRootFileNames(); | ||
|
||
if (!entryFileName) return []; | ||
|
||
const dependencies = new Set<string>(); | ||
const visited = new Set<string>(); | ||
|
||
const visit = (filePath: string): void => { | ||
if (visited.has(filePath)) return; | ||
|
||
visited.add(filePath); | ||
|
||
const sourceText = ts.sys.readFile(filePath); | ||
|
||
if (!sourceText) return; | ||
|
||
const sourceFile = ts.createSourceFile( | ||
filePath, | ||
sourceText, | ||
ts.ScriptTarget.ESNext, | ||
true, | ||
); | ||
|
||
ts.forEachChild(sourceFile, (node) => { | ||
if (!ts.isImportDeclaration(node)) return; | ||
|
||
const moduleName = node.moduleSpecifier | ||
.getText() | ||
.replace(/(^("|'|`))|(("|'|`)$)/g, ""); | ||
|
||
const resolvedModule = ts.resolveModuleName( | ||
moduleName, | ||
sourceFile.fileName, | ||
program.getCompilerOptions(), | ||
ts.sys, | ||
); | ||
|
||
const resolvedFile = | ||
resolvedModule.resolvedModule?.resolvedFileName; | ||
|
||
if (!resolvedFile) return; | ||
|
||
dependencies.add(resolvedFile); | ||
|
||
visit(resolvedFile); | ||
}); | ||
}; | ||
|
||
visit(entryFileName); | ||
|
||
return Array.from(dependencies.values()) | ||
.map((dependency) => { | ||
const sourceFile = program.getSourceFile(dependency); | ||
|
||
return sourceFile?.fileName; | ||
}) | ||
.filter((fileName): fileName is string => typeof fileName === "string"); | ||
}; | ||
|
||
const getTsConfigPath = () => { | ||
const tsConfigPath = path.resolve(process.cwd(), "tsconfig.json"); | ||
|
||
if (!ts.sys.fileExists(tsConfigPath)) return null; | ||
|
||
return tsConfigPath; | ||
}; | ||
|
||
export const resolveDependencies = ( | ||
params: ResolveDependenciesParams, | ||
): readonly string[] => { | ||
const { input } = params; | ||
|
||
const tsConfigPath = getTsConfigPath(); | ||
const basePath = tsConfigPath ? path.dirname(tsConfigPath) : process.cwd(); | ||
|
||
const compilerOptions: CompilerOptions = { | ||
allowJs: true, | ||
allowSyntheticDefaultImports: true, | ||
checkJs: false, | ||
esModuleInterop: true, | ||
forceConsistentCasingInFileNames: true, | ||
incremental: false, | ||
inlineSources: false, | ||
isolatedModules: true, | ||
jsx: ts.JsxEmit.Preserve, | ||
lib: ["es2021"], | ||
module: ts.ModuleKind.ESNext, | ||
moduleResolution: ts.ModuleResolutionKind.Bundler, | ||
noEmit: true, | ||
noUnusedLocals: false, | ||
noUnusedParameters: false, | ||
preserveWatchOutput: true, | ||
resolveJsonModule: true, | ||
skipLibCheck: true, | ||
sourceMap: false, | ||
strict: true, | ||
target: ts.ScriptTarget.ESNext, | ||
baseUrl: basePath, | ||
}; | ||
|
||
const parsedConfig = ts.parseJsonConfigFileContent( | ||
compilerOptions, | ||
ts.sys, | ||
basePath, | ||
compilerOptions, | ||
tsConfigPath ?? undefined, | ||
); | ||
|
||
const program = ts.createProgram({ | ||
options: parsedConfig.options, | ||
projectReferences: parsedConfig.projectReferences, | ||
rootNames: [path.resolve(basePath, input)], | ||
}); | ||
|
||
const dependencies = crawlDependencies(program); | ||
|
||
return dependencies; | ||
}; |
68 changes: 0 additions & 68 deletions
68
packages/cli/src/utils/buildApp/resolveDependenciesTransformer.ts
This file was deleted.
Oops, something went wrong.