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
feat(js): infer tslib as a dependency when using importHelpers #9350
Merged
AgentEnder
merged 1 commit into
nrwl:master
from
gioragutt:giorag/infer-tslib-dep-when-using-import-helpers
Mar 16, 2022
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { ExecutorContext, getPackageManagerCommand } from '@nrwl/devkit'; | ||
import { readCachedProjectGraph } from '@nrwl/workspace/src/core/project-graph'; | ||
import { DependentBuildableProjectNode } from '@nrwl/workspace/src/utilities/buildable-libs-utils'; | ||
import { readTsConfig } from '@nrwl/workspace/src/utilities/typescript'; | ||
import { NormalizedExecutorOptions } from './schema'; | ||
|
||
const tslibNodeName = 'npm:tslib'; | ||
|
||
function shouldAddTslibDependency( | ||
tsConfig: string, | ||
dependencies: DependentBuildableProjectNode[] | ||
): boolean { | ||
if (dependencies.some((dep) => dep.name === tslibNodeName)) { | ||
return false; | ||
} | ||
|
||
const config = readTsConfig(tsConfig); | ||
return !!config.options.importHelpers; | ||
} | ||
|
||
export function addTslibDependencyIfNeeded( | ||
options: NormalizedExecutorOptions, | ||
context: ExecutorContext, | ||
dependencies: DependentBuildableProjectNode[] | ||
): void { | ||
if (!shouldAddTslibDependency(options.tsConfig, dependencies)) { | ||
return; | ||
} | ||
|
||
const depGraph = readCachedProjectGraph(); | ||
const tslibNode = depGraph.externalNodes[tslibNodeName]; | ||
|
||
if (!tslibNode) { | ||
const pmc = getPackageManagerCommand(); | ||
throw new Error( | ||
`"importHelpers" is enabled for ${context.targetName} but tslib is not installed. Use "${pmc.add} tslib" to install it.` | ||
); | ||
} | ||
|
||
const tslibDependency: DependentBuildableProjectNode = { | ||
name: tslibNodeName, | ||
outputs: [], | ||
node: tslibNode, | ||
}; | ||
|
||
dependencies.push(tslibDependency); | ||
} |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@AgentEnder does this seem like the right approach? Perhaps there's a way to make it so that
tslib
will be part of the dependencies in the first place, by altering the graph somehow?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is fine for the time being, eventually a lot of the js specific logic should be pulled out to use
processProjectGraph
like a community plugin would, to truly separate the core and at that point then some of this may make more sense there. Since this is specific to thetsc
executor (and not wanted for theswc
executor), I think this makes sense.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I figured there's something missing to do what I had in mind. Thanks for the feedback 👍🏻 I'll push a fix momentarily