-
Notifications
You must be signed in to change notification settings - Fork 74
Generate dependency manifest for apps #1285
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
Merged
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
5e9a2b1
Get dependencies from imports
4a3ec4f
Match valid package names
75bd68c
Actually get dependencies and write them
78bc775
Remove log
9a12dc0
Better comments
8d11de4
Throw if dependency not found in package.json or hoisted in root
c64cbe7
Add dependencies in package.json
331c1a9
Update snapshots
df53d74
Update esbuild snapshots
aa07cd9
Select fields for package.json
13f5b83
Upgrade ts-morph
75323c1
remove main, files, typings
63abb2e
Bump ts-morph
6fcfef8
Merge remote-tracking branch 'origin/main' into feature/dependency-ma…
b531e10
Add tests
3e6d91e
remove module field
2931383
Create nervous-islands-taste.md
cristiano-belloni 7835e8e
Fix deprecation
62e64b1
Merge branch 'feature/dependency-manifest-strawman' of https://github…
50c93b3
Revert "Fix deprecation"
1f11fec
Merge remote-tracking branch 'origin/main' into feature/dependency-ma…
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 |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| "modular-scripts": minor | ||
| --- | ||
|
|
||
| Generate dependency manifest (package.json) for apps. This includes all the dependencies, either installed via the package's `package.json` or hoisted to the root's `package.json`. | ||
| If a dependency is imported in code but not specified in the `package.json`s, the app will not build anymore. |
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
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
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
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
83 changes: 83 additions & 0 deletions
83
packages/modular-scripts/src/utils/getPackageDependencies.ts
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 |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| import * as path from 'path'; | ||
| import * as fs from 'fs-extra'; | ||
| import { Project } from 'ts-morph'; | ||
| import type { CoreProperties } from '@schemastore/package'; | ||
| import getModularRoot from './getModularRoot'; | ||
| import getLocation from './getLocation'; | ||
| import getWorkspaceInfo from './getWorkspaceInfo'; | ||
|
|
||
| type DependencyManifest = NonNullable<CoreProperties['dependencies']>; | ||
|
|
||
| const npmPackageMatcher = | ||
| /^(@[a-z0-9-~][a-z0-9-._~]*\/)?[a-z0-9-~][a-z0-9-._~]*/; | ||
|
|
||
| /* Get dependencies from import / require declarations, since they could be hoisted to the root workspace. Exclude test files. */ | ||
| function getDependenciesFromSource(workspaceLocation: string) { | ||
| const project = new Project(); | ||
|
LukeSheard marked this conversation as resolved.
|
||
| project.addSourceFilesAtPaths( | ||
| path.join(workspaceLocation, 'src/**/!(*.test){.d.ts,.ts,.js,.jsx,.tsx}'), | ||
| ); | ||
|
|
||
| const dependencySet = new Set( | ||
| project | ||
| .getSourceFiles() | ||
| .flatMap((sourceFile) => | ||
| sourceFile | ||
| .getImportDeclarations() | ||
| .map( | ||
| (declaration) => | ||
| npmPackageMatcher.exec( | ||
| declaration.getModuleSpecifierValue(), | ||
| )?.[0], | ||
| ), | ||
| ) | ||
| .filter(Boolean), | ||
| ) as Set<string>; | ||
|
|
||
| return Array.from(dependencySet); | ||
| } | ||
|
|
||
| export async function getPackageDependencies( | ||
| target: string, | ||
| ): Promise<DependencyManifest> { | ||
| /* This function is based on the assumption that nested package are not supported, so dependencies can be either declared in the | ||
| * target's package.json or hoisted up to the workspace root. | ||
| */ | ||
| const targetLocation = await getLocation(target); | ||
| const workspaceInfo = getWorkspaceInfo(); | ||
|
|
||
| const rootPackageJsonDependencies = | ||
| ( | ||
| fs.readJSONSync( | ||
| path.join(getModularRoot(), 'package.json'), | ||
| ) as CoreProperties | ||
| ).dependencies || {}; | ||
|
|
||
| const targetPackageJsonDependencies = | ||
| ( | ||
| fs.readJSONSync( | ||
| path.join(targetLocation, 'package.json'), | ||
| ) as CoreProperties | ||
| ).dependencies || {}; | ||
|
|
||
| /* Get regular dependencies from package.json (regular) or root package.json (hoisted) | ||
| * Exclude workspace dependencies. Error if a dependency is imported in the source code | ||
| * but not specified in any of the package.jsons | ||
| */ | ||
| const manifest = getDependenciesFromSource(targetLocation) | ||
| .filter((depName) => !(depName in workspaceInfo)) | ||
| .reduce<DependencyManifest>((manifest, depName) => { | ||
| const depVersion = | ||
| targetPackageJsonDependencies[depName] ?? | ||
| rootPackageJsonDependencies[depName]; | ||
| if (!depVersion) { | ||
| throw new Error( | ||
| `Package ${depName} imported in ${target} source but not found in package dependencies or hoisted dependencies`, | ||
| ); | ||
| } | ||
| manifest[depName] = depVersion; | ||
| return manifest; | ||
| }, {}); | ||
|
|
||
| return manifest; | ||
| } | ||
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.