-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Resolve transitive dependencies based on package.json #3286
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
seanoch
wants to merge
6
commits into
microsoft:v2
Choose a base branch
from
wix-playground:ata-transitive-deps-fix
base: v2
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
ca3dab8
Support transitive dependencies
seanoch c341f09
renaming
seanoch 6eab50c
revert @types version change
seanoch e37ae04
added new flag to userFacingTypes
seanoch c6f24f2
added resolveDependenciesFromPackageJson to the playground
seanoch 58bd9fc
added changeset
seanoch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -29,9 +29,22 @@ export interface ATABootstrapConfig { | |||||||||||
| fetcher?: typeof fetch | ||||||||||||
| /** If you need a custom logger instead of the console global */ | ||||||||||||
| logger?: Logger | ||||||||||||
| /** Wheather to use package.json as the source of truth for transitive dependencies' versions */ | ||||||||||||
| usePackageJson?: boolean | ||||||||||||
seanoch marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| type ModuleMeta = { state: "loading" } | ||||||||||||
| type PackageJsonDependencies = { | ||||||||||||
| [packageName: string]: string; | ||||||||||||
| }; | ||||||||||||
|
|
||||||||||||
| type PackageJson = { | ||||||||||||
| dependencies?: PackageJsonDependencies; | ||||||||||||
| devDependencies?: PackageJsonDependencies; | ||||||||||||
| peerDependencies?: PackageJsonDependencies; | ||||||||||||
| optionalDependencies? :PackageJsonDependencies; | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| type ModuleMeta = { state: "loading", typesPackageJson?: PackageJson } | ||||||||||||
|
|
||||||||||||
| /** | ||||||||||||
| * The function which starts up type acquisition, | ||||||||||||
|
|
@@ -60,8 +73,8 @@ export const setupTypeAcquisition = (config: ATABootstrapConfig) => { | |||||||||||
| }) | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| async function resolveDeps(initialSourceFile: string, depth: number) { | ||||||||||||
| const depsToGet = getNewDependencies(config, moduleMap, initialSourceFile) | ||||||||||||
| async function resolveDeps(initialSourceFile: string, depth: number, parentPackageJson?: PackageJson) { | ||||||||||||
| const depsToGet = getNewDependencies(config, moduleMap, initialSourceFile, parentPackageJson) | ||||||||||||
|
|
||||||||||||
| // Make it so it won't get re-downloaded | ||||||||||||
| depsToGet.forEach(dep => moduleMap.set(dep.module, { state: "loading" })) | ||||||||||||
|
|
@@ -78,7 +91,7 @@ export const setupTypeAcquisition = (config: ATABootstrapConfig) => { | |||||||||||
| const mightBeOnDT = treesOnly.filter(t => !hasDTS.includes(t)) | ||||||||||||
| const dtTrees = await Promise.all( | ||||||||||||
| // TODO: Switch from 'latest' to the version from the original tree which is user-controlled | ||||||||||||
| mightBeOnDT.map(f => getFileTreeForModuleWithTag(config, `@types/${getDTName(f.moduleName)}`, "latest")) | ||||||||||||
| mightBeOnDT.map(f => getFileTreeForModuleWithTag(config, `@types/${getDTName(f.moduleName)}`, f.version)) | ||||||||||||
| ) | ||||||||||||
|
|
||||||||||||
| const dtTreesOnly = dtTrees.filter(t => !("error" in t)) as NPMTreeMeta[] | ||||||||||||
|
|
@@ -100,6 +113,12 @@ export const setupTypeAcquisition = (config: ATABootstrapConfig) => { | |||||||||||
|
|
||||||||||||
| if (typeof pkgJSON == "string") { | ||||||||||||
| fsMap.set(path, pkgJSON) | ||||||||||||
|
|
||||||||||||
| const moduleMeta = moduleMap.get(tree.moduleName); | ||||||||||||
| if (moduleMeta) { | ||||||||||||
| moduleMeta.typesPackageJson = JSON.parse(pkgJSON) as PackageJson | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| config.delegate.receivedFile?.(pkgJSON, path) | ||||||||||||
| } else { | ||||||||||||
| config.logger?.error(`Could not download package.json for ${tree.moduleName}`) | ||||||||||||
|
|
@@ -124,7 +143,11 @@ export const setupTypeAcquisition = (config: ATABootstrapConfig) => { | |||||||||||
| } | ||||||||||||
|
|
||||||||||||
| // Recurse through deps | ||||||||||||
| await resolveDeps(dtsCode, depth + 1) | ||||||||||||
| let typesPackageJson | ||||||||||||
| if (config.usePackageJson){ | ||||||||||||
| typesPackageJson = moduleMap.get(dts.moduleName)?.typesPackageJson | ||||||||||||
| } | ||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
| await resolveDeps(dtsCode, depth + 1, typesPackageJson) | ||||||||||||
| } | ||||||||||||
| }) | ||||||||||||
| ) | ||||||||||||
|
|
@@ -154,11 +177,23 @@ function treeToDTSFiles(tree: NPMTreeMeta, vfsPrefix: string) { | |||||||||||
| return dtsRefs | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| function hasSemverOperator(version: string) { | ||||||||||||
| return /^[\^~>=<]/.test(version); | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| function findModuleVersionInPackageJson(packageJson: PackageJson, moduleName: string): string | undefined { | ||||||||||||
| const depTypes = ['dependencies', 'devDependencies', 'peerDependencies', 'optionalDependencies'] as const; | ||||||||||||
|
|
||||||||||||
| return depTypes | ||||||||||||
| .map(type => packageJson[type]?.[moduleName]) | ||||||||||||
| .find(version => version !== undefined); | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| /** | ||||||||||||
| * Pull out any potential references to other modules (including relatives) with their | ||||||||||||
| * npm versioning strat too if someone opts into a different version via an inline end of line comment | ||||||||||||
| */ | ||||||||||||
| export const getReferencesForModule = (ts: typeof import("typescript"), code: string) => { | ||||||||||||
| export const getReferencesForModule = (ts: typeof import("typescript"), code: string, parentPackageJson?: PackageJson) => { | ||||||||||||
| const meta = ts.preProcessFile(code) | ||||||||||||
|
|
||||||||||||
| // Ensure we don't try download TypeScript lib references | ||||||||||||
|
|
@@ -178,7 +213,12 @@ export const getReferencesForModule = (ts: typeof import("typescript"), code: st | |||||||||||
| if (!r.fileName.startsWith(".")) { | ||||||||||||
| version = "latest" | ||||||||||||
| const line = code.slice(r.end).split("\n")[0]! | ||||||||||||
| if (line.includes("// types:")) version = line.split("// types: ")[1]!.trim() | ||||||||||||
| if (line.includes("// types:")) { | ||||||||||||
| version = line.split("// types: ")[1]!.trim() | ||||||||||||
| } else if (parentPackageJson) { | ||||||||||||
| const moduleName = mapModuleNameToModule(r.fileName) | ||||||||||||
| version = findModuleVersionInPackageJson(parentPackageJson, moduleName) ?? version | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| return { | ||||||||||||
|
|
@@ -189,8 +229,8 @@ export const getReferencesForModule = (ts: typeof import("typescript"), code: st | |||||||||||
| } | ||||||||||||
|
|
||||||||||||
| /** A list of modules from the current sourcefile which we don't have existing files for */ | ||||||||||||
| export function getNewDependencies(config: ATABootstrapConfig, moduleMap: Map<string, ModuleMeta>, code: string) { | ||||||||||||
| const refs = getReferencesForModule(config.typescript, code).map(ref => ({ | ||||||||||||
| export function getNewDependencies(config: ATABootstrapConfig, moduleMap: Map<string, ModuleMeta>, code: string, parentPackageJson?: PackageJson) { | ||||||||||||
| const refs = getReferencesForModule(config.typescript, code, parentPackageJson).map(ref => ({ | ||||||||||||
| ...ref, | ||||||||||||
| module: mapModuleNameToModule(ref.module), | ||||||||||||
| })) | ||||||||||||
|
|
@@ -210,7 +250,7 @@ export const getFileTreeForModuleWithTag = async ( | |||||||||||
|
|
||||||||||||
| // I think having at least 2 dots is a reasonable approx for being a semver and not a tag, | ||||||||||||
| // we can skip an API request, TBH this is probably rare | ||||||||||||
| if (toDownload.split(".").length < 2) { | ||||||||||||
| if (toDownload.split(".").length < 2 || hasSemverOperator(toDownload)) { | ||||||||||||
| // The jsdelivr API needs a _version_ not a tag. So, we need to switch out | ||||||||||||
| // the tag to the version via an API request. | ||||||||||||
| const response = await getNPMVersionForModuleReference(config, moduleName, toDownload) | ||||||||||||
|
|
||||||||||||
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.