-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(js): infer tslib as dependency when using importHelpers
When using "importHelpers" and building with "tsc", the built bundle expects to be supplied the "tslib" package. Currently, it's up to developers to add "tslib" as a dependency to those libraries' package.json files. This PR makes the "tsc" execute infer that "importHelpers" is used, and subsequently add "tslib" as a dependency to the generated package.json ISSUES CLOSED: #9343
- Loading branch information
Showing
4 changed files
with
75 additions
and
7 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
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} tslint" 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