From f0fd9400f11cb028c59dd7d75f7e1f5c0edb3b98 Mon Sep 17 00:00:00 2001 From: Emily Xiong Date: Fri, 24 Feb 2023 11:37:23 -0500 Subject: [PATCH 1/2] fix(core): disable js code analysis when typescript is not installed --- packages/vite/src/utils/vite-config-edit-utils.ts | 15 +++++++-------- .../generators/new/__snapshots__/new.spec.ts.snap | 3 --- .../files-integrated-repo/package.json__tmpl__ | 1 - .../new/files-root-app/package.json__tmpl__ | 3 +-- .../generators/new/generate-workspace-files.ts | 2 -- 5 files changed, 8 insertions(+), 16 deletions(-) diff --git a/packages/vite/src/utils/vite-config-edit-utils.ts b/packages/vite/src/utils/vite-config-edit-utils.ts index 9f015eda48693..d7a279658549b 100644 --- a/packages/vite/src/utils/vite-config-edit-utils.ts +++ b/packages/vite/src/utils/vite-config-edit-utils.ts @@ -1,8 +1,7 @@ import { applyChangesToString, ChangeType, Tree } from '@nrwl/devkit'; import { findNodes } from 'nx/src/utils/typescript'; -import type * as ts from 'typescript'; - import { TargetFlags } from './generator-utils'; +import type { Node, ReturnStatement } from 'typescript'; export function ensureViteConfigIsCorrect( tree: Tree, @@ -76,7 +75,7 @@ function handleBuildOrTestNode( return tsquery.replace( updatedFileContent, `PropertyAssignment:has(Identifier[name="${name}"])`, - (node: ts.Node) => { + (node: Node) => { const found = tsquery.query(node, 'ObjectLiteralExpression'); return `${name}: { ...${found?.[0].getText()}, @@ -160,7 +159,7 @@ function handleBuildOrTestNode( function transformCurrentBuildObject( index: number, - returnStatements: ts.ReturnStatement[], + returnStatements: ReturnStatement[], appFileContent: string, buildConfigObject: {} ): string | undefined { @@ -178,7 +177,7 @@ function transformCurrentBuildObject( const newReturnObject = tsquery.replace( returnStatements[index].getText(), 'ObjectLiteralExpression', - (_node: ts.Node) => { + (_node: Node) => { return `{ ...${currentBuildObject}, ...${JSON.stringify(buildConfigObject)} @@ -203,7 +202,7 @@ function transformCurrentBuildObject( } function transformConditionalConfig( - conditionalConfig: ts.Node[], + conditionalConfig: Node[], appFileContent: string, buildConfigObject: {} ): string | undefined { @@ -228,7 +227,7 @@ function transformConditionalConfig( ); const elseKeywordExists = findNodes(ifStatement?.[0], SyntaxKind.ElseKeyword); - const returnStatements: ts.ReturnStatement[] = tsquery.query( + const returnStatements: ReturnStatement[] = tsquery.query( ifStatement[0], 'ReturnStatement' ); @@ -297,7 +296,7 @@ function handlePluginNode( appFileContent = tsquery.replace( file.getText(), 'PropertyAssignment:has(Identifier[name="plugins"])', - (node: ts.Node) => { + (node: Node) => { const found = tsquery.query(node, 'ArrayLiteralExpression'); return `plugins: [ ...${found?.[0].getText()}, diff --git a/packages/workspace/src/generators/new/__snapshots__/new.spec.ts.snap b/packages/workspace/src/generators/new/__snapshots__/new.spec.ts.snap index 0be84be4907ef..22f6c802c62d8 100644 --- a/packages/workspace/src/generators/new/__snapshots__/new.spec.ts.snap +++ b/packages/workspace/src/generators/new/__snapshots__/new.spec.ts.snap @@ -9,7 +9,6 @@ Object { "@nrwl/workspace": "0.0.1", "nx": "0.0.1", "prettier": "^2.6.2", - "typescript": "~4.9.5", }, "license": "MIT", "name": "my-workspace", @@ -26,7 +25,6 @@ Object { "@nrwl/workspace": "0.0.1", "nx": "0.0.1", "prettier": "^2.6.2", - "typescript": "~4.9.5", }, "license": "MIT", "name": "my-workspace", @@ -44,7 +42,6 @@ Object { "@nrwl/workspace": "0.0.1", "nx": "0.0.1", "prettier": "^2.6.2", - "typescript": "~4.9.5", }, "license": "MIT", "name": "my-workspace", diff --git a/packages/workspace/src/generators/new/files-integrated-repo/package.json__tmpl__ b/packages/workspace/src/generators/new/files-integrated-repo/package.json__tmpl__ index 2d8496b0bd0c7..35be3530247aa 100644 --- a/packages/workspace/src/generators/new/files-integrated-repo/package.json__tmpl__ +++ b/packages/workspace/src/generators/new/files-integrated-repo/package.json__tmpl__ @@ -10,7 +10,6 @@ "devDependencies": { "nx": "<%= nxVersion %>", "@nrwl/workspace": "<%= nxVersion %>", - "typescript": "<%= typescriptVersion %>", "prettier": "<%= prettierVersion %>" } } diff --git a/packages/workspace/src/generators/new/files-root-app/package.json__tmpl__ b/packages/workspace/src/generators/new/files-root-app/package.json__tmpl__ index 8a64a50fdfe1a..592e9f5150fa6 100644 --- a/packages/workspace/src/generators/new/files-root-app/package.json__tmpl__ +++ b/packages/workspace/src/generators/new/files-root-app/package.json__tmpl__ @@ -10,7 +10,6 @@ "devDependencies": { "@nrwl/workspace": "<%= nxVersion %>", "nx": "<%= nxVersion %>", - "prettier": "<%= prettierVersion %>", - "typescript": "<%= typescriptVersion %>" + "prettier": "<%= prettierVersion %>" } } diff --git a/packages/workspace/src/generators/new/generate-workspace-files.ts b/packages/workspace/src/generators/new/generate-workspace-files.ts index 27c936e1ffa10..2987773f419d1 100644 --- a/packages/workspace/src/generators/new/generate-workspace-files.ts +++ b/packages/workspace/src/generators/new/generate-workspace-files.ts @@ -12,7 +12,6 @@ import { import { nxVersion, prettierVersion, - typescriptVersion, } from '../../utils/versions'; import { join, join as pathJoin } from 'path'; import { Preset } from '../utils/presets'; @@ -146,7 +145,6 @@ function createFiles(tree: Tree, options: NormalizedSchema) { tmpl: '', cliCommand: 'nx', nxCli: false, - typescriptVersion, prettierVersion, ...(options as object), nxVersion, From e58a3011a3cb7acb078662cb640abff427efd0aa Mon Sep 17 00:00:00 2001 From: Emily Xiong Date: Thu, 23 Feb 2023 02:06:13 -0500 Subject: [PATCH 2/2] fix(core): change require typescript to enstureTypescript --- .../angular-v14/lib/root-router-config.ts | 14 ++-- .../lib/update-app-component-template.ts | 16 ++-- .../angular-v14/lib/update-component-spec.ts | 6 +- .../lib/get-args-default-value.ts | 15 ++-- .../lib/get-component-selector.ts | 10 ++- .../generators/ngrx/lib/add-exports-barrel.ts | 11 ++- .../ngrx/lib/add-imports-to-module.ts | 11 ++- .../generators/ngrx/lib/validate-options.ts | 2 +- .../lib/convert-directive-to-scam.ts | 11 ++- .../scam-pipe/lib/convert-pipe-to-scam.ts | 11 ++- .../lib/convert-scam-to-standalone.ts | 4 +- .../lib/get-component-data-from-ast.ts | 4 +- .../lib/get-module-metadata-from-ast.ts | 4 +- .../lib/selector-exists-in-ast.ts | 4 +- .../lib/verify-is-inline-scam.ts | 2 +- .../scam/lib/convert-component-to-scam.ts | 12 ++- .../setup-mf/lib/add-remote-to-host.ts | 3 +- .../setup-mf/lib/update-host-app-routes.ts | 14 +++- .../utils/storybook-ast/component-info.ts | 33 ++++++-- .../utils/storybook-ast/module-info.ts | 83 +++++++++++++------ .../utils/storybook-ast/storybook-inputs.ts | 29 +++++-- packages/angular/src/utils/get-mf-projects.ts | 2 +- .../src/utils/nx-devkit/route-utils.ts | 3 +- packages/devkit/src/generators/to-js.ts | 8 +- packages/js/src/generators/library/library.ts | 1 - .../src/generators/component/component.ts | 10 ++- .../generators/component/lib/add-import.ts | 9 +- .../new/generate-workspace-files.ts | 5 +- 28 files changed, 228 insertions(+), 109 deletions(-) diff --git a/packages/angular/src/generators/application/angular-v14/lib/root-router-config.ts b/packages/angular/src/generators/application/angular-v14/lib/root-router-config.ts index c315768ae9ef4..5cdfd1997d708 100644 --- a/packages/angular/src/generators/application/angular-v14/lib/root-router-config.ts +++ b/packages/angular/src/generators/application/angular-v14/lib/root-router-config.ts @@ -1,21 +1,25 @@ import type { Tree } from '@nrwl/devkit'; -import type { NormalizedSchema } from './normalized-schema'; - import { insertImport } from '@nrwl/workspace/src/utilities/ast-utils'; -import * as ts from 'typescript'; +import { ensureTypescript } from '@nrwl/js/src/utils/typescript/ensure-typescript'; import { addImportToModule } from '../../../../utils/nx-devkit/ast-utils'; +import type { NormalizedSchema } from './normalized-schema'; + +let tsModule: typeof import('typescript'); export function addRouterRootConfiguration( host: Tree, options: NormalizedSchema ) { + if (!tsModule) { + tsModule = ensureTypescript(); + } const modulePath = `${options.appProjectRoot}/src/app/app.module.ts`; const moduleSource = host.read(modulePath, 'utf-8'); - let sourceFile = ts.createSourceFile( + let sourceFile = tsModule.createSourceFile( modulePath, moduleSource, - ts.ScriptTarget.Latest, + tsModule.ScriptTarget.Latest, true ); diff --git a/packages/angular/src/generators/application/angular-v14/lib/update-app-component-template.ts b/packages/angular/src/generators/application/angular-v14/lib/update-app-component-template.ts index 473a3b45e3389..cde348f017398 100644 --- a/packages/angular/src/generators/application/angular-v14/lib/update-app-component-template.ts +++ b/packages/angular/src/generators/application/angular-v14/lib/update-app-component-template.ts @@ -1,16 +1,20 @@ import type { Tree } from '@nrwl/devkit'; -import type { NormalizedSchema } from './normalized-schema'; - -import * as ts from 'typescript'; +import type * as ts from 'typescript'; import { replaceNodeValue } from '@nrwl/workspace/src/utilities/ast-utils'; +import { ensureTypescript } from '@nrwl/js/src/utils/typescript/ensure-typescript'; import { getDecoratorPropertyValueNode } from '../../../../utils/nx-devkit/ast-utils'; - import { nrwlHomeTemplate } from './nrwl-home-tpl'; +import type { NormalizedSchema } from './normalized-schema'; + +let tsModule: typeof import('typescript'); export function updateAppComponentTemplate( host: Tree, options: NormalizedSchema ) { + if (!tsModule) { + tsModule = ensureTypescript(); + } const content = options.routing ? `${nrwlHomeTemplate.getSelector( options.prefix @@ -35,10 +39,10 @@ export function updateAppComponentTemplate( replaceNodeValue( host, - ts.createSourceFile( + tsModule.createSourceFile( componentPath, host.read(componentPath, 'utf-8'), - ts.ScriptTarget.Latest, + tsModule.ScriptTarget.Latest, true ), componentPath, diff --git a/packages/angular/src/generators/application/angular-v14/lib/update-component-spec.ts b/packages/angular/src/generators/application/angular-v14/lib/update-component-spec.ts index 7f6765cfec918..5d94bdfd862bd 100644 --- a/packages/angular/src/generators/application/angular-v14/lib/update-component-spec.ts +++ b/packages/angular/src/generators/application/angular-v14/lib/update-component-spec.ts @@ -1,17 +1,17 @@ import type { Tree } from '@nrwl/devkit'; -import type { NormalizedSchema } from './normalized-schema'; - import { insertImport } from '@nrwl/workspace/src/utilities/ast-utils'; +import { ensureTypescript } from '@nrwl/js/src/utils/typescript/ensure-typescript'; import { addImportToTestBed, replaceIntoToTestBed, } from '../../../../utils/nx-devkit/ast-utils'; +import type { NormalizedSchema } from './normalized-schema'; let tsModule: typeof import('typescript'); export function updateComponentSpec(host: Tree, options: NormalizedSchema) { if (!tsModule) { - tsModule = require('typescript'); + tsModule = ensureTypescript(); } if (options.skipTests !== true) { const componentSpecPath = `${options.appProjectRoot}/src/app/app.component.spec.ts`; diff --git a/packages/angular/src/generators/component-cypress-spec/lib/get-args-default-value.ts b/packages/angular/src/generators/component-cypress-spec/lib/get-args-default-value.ts index f39cf0c32ecf2..d8763d998b984 100644 --- a/packages/angular/src/generators/component-cypress-spec/lib/get-args-default-value.ts +++ b/packages/angular/src/generators/component-cypress-spec/lib/get-args-default-value.ts @@ -1,5 +1,7 @@ +import { ensureTypescript } from '@nrwl/js/src/utils/typescript/ensure-typescript'; import type { PropertyDeclaration } from 'typescript'; -import { SyntaxKind } from 'typescript'; + +let tsModule: typeof import('typescript'); export function getArgsDefaultValue( property: PropertyDeclaration @@ -7,13 +9,16 @@ export function getArgsDefaultValue( if (!property.initializer) { return undefined; } + if (!tsModule) { + tsModule = ensureTypescript(); + } switch (property.initializer.kind) { - case SyntaxKind.StringLiteral: + case tsModule.SyntaxKind.StringLiteral: const returnString = property.initializer.getText().slice(1, -1); return returnString.replace(/\s/g, '+'); - case SyntaxKind.NumericLiteral: - case SyntaxKind.TrueKeyword: - case SyntaxKind.FalseKeyword: + case tsModule.SyntaxKind.NumericLiteral: + case tsModule.SyntaxKind.TrueKeyword: + case tsModule.SyntaxKind.FalseKeyword: return property.initializer.getText(); default: return undefined; diff --git a/packages/angular/src/generators/component-cypress-spec/lib/get-component-selector.ts b/packages/angular/src/generators/component-cypress-spec/lib/get-component-selector.ts index 06c94bb9abce2..ab3aacdbae95f 100644 --- a/packages/angular/src/generators/component-cypress-spec/lib/get-component-selector.ts +++ b/packages/angular/src/generators/component-cypress-spec/lib/get-component-selector.ts @@ -1,13 +1,19 @@ import type { Tree } from '@nrwl/devkit'; +import { ensureTypescript } from '@nrwl/js/src/utils/typescript/ensure-typescript'; import { findNodes } from 'nx/src/utils/typescript'; import type { PropertyAssignment } from 'typescript'; -import { SyntaxKind } from 'typescript'; + import { getDecoratorMetadata, getTsSourceFile, } from '../../../utils/nx-devkit/ast-utils'; +let tsModule: typeof import('typescript'); + export function getComponentSelector(tree: Tree, path: string): string { + if (!tsModule) { + tsModule = ensureTypescript(); + } const file = getTsSourceFile(tree, path); const componentDecorators = getDecoratorMetadata( @@ -20,7 +26,7 @@ export function getComponentSelector(tree: Tree, path: string): string { } const componentDecorator = componentDecorators[0]; const selectorNode = ( - findNodes(componentDecorator, SyntaxKind.PropertyAssignment).find( + findNodes(componentDecorator, tsModule.SyntaxKind.PropertyAssignment).find( (node: PropertyAssignment) => node.name.getText() === 'selector' ) ); diff --git a/packages/angular/src/generators/ngrx/lib/add-exports-barrel.ts b/packages/angular/src/generators/ngrx/lib/add-exports-barrel.ts index 043fc47a331be..904a011a06209 100644 --- a/packages/angular/src/generators/ngrx/lib/add-exports-barrel.ts +++ b/packages/angular/src/generators/ngrx/lib/add-exports-barrel.ts @@ -1,9 +1,11 @@ import type { Tree } from '@nrwl/devkit'; import { joinPathFragments, names } from '@nrwl/devkit'; +import { ensureTypescript } from '@nrwl/js/src/utils/typescript/ensure-typescript'; import { addGlobal } from '@nrwl/workspace/src/utilities/ast-utils'; -import { createSourceFile, ScriptTarget } from 'typescript'; import type { NormalizedNgRxGeneratorOptions } from './normalize-options'; +let tsModule: typeof import('typescript'); + /** * Add ngrx feature exports to the public barrel in the feature library */ @@ -25,11 +27,14 @@ export function addExportsToBarrel( return; } + if (!tsModule) { + tsModule = ensureTypescript(); + } const indexSourceText = tree.read(indexFilePath, 'utf-8'); - let sourceFile = createSourceFile( + let sourceFile = tsModule.createSourceFile( indexFilePath, indexSourceText, - ScriptTarget.Latest, + tsModule.ScriptTarget.Latest, true ); diff --git a/packages/angular/src/generators/ngrx/lib/add-imports-to-module.ts b/packages/angular/src/generators/ngrx/lib/add-imports-to-module.ts index cbac71a0b526b..dc0f3c172de62 100644 --- a/packages/angular/src/generators/ngrx/lib/add-imports-to-module.ts +++ b/packages/angular/src/generators/ngrx/lib/add-imports-to-module.ts @@ -1,8 +1,8 @@ import type { Tree } from '@nrwl/devkit'; import { names } from '@nrwl/devkit'; import { insertImport } from '@nrwl/workspace/src/utilities/ast-utils'; +import { ensureTypescript } from '@nrwl/js/src/utils/typescript/ensure-typescript'; import type { SourceFile } from 'typescript'; -import { createSourceFile, ScriptTarget } from 'typescript'; import { addImportToModule, addProviderToBootstrapApplication, @@ -11,6 +11,8 @@ import { import type { NormalizedNgRxGeneratorOptions } from './normalize-options'; import { addProviderToRoute } from '../../../utils/nx-devkit/route-utils'; +let tsModule: typeof import('typescript'); + function addRootStoreImport( tree: Tree, isParentStandalone: boolean, @@ -143,12 +145,15 @@ export function addImportsToModule( tree: Tree, options: NormalizedNgRxGeneratorOptions ): void { + if (!tsModule) { + tsModule = ensureTypescript(); + } const parentPath = options.module ?? options.parent; const sourceText = tree.read(parentPath, 'utf-8'); - let sourceFile = createSourceFile( + let sourceFile = tsModule.createSourceFile( parentPath, sourceText, - ScriptTarget.Latest, + tsModule.ScriptTarget.Latest, true ); diff --git a/packages/angular/src/generators/ngrx/lib/validate-options.ts b/packages/angular/src/generators/ngrx/lib/validate-options.ts index dc1f3fdc2b6c8..39906c5fbc970 100644 --- a/packages/angular/src/generators/ngrx/lib/validate-options.ts +++ b/packages/angular/src/generators/ngrx/lib/validate-options.ts @@ -1,5 +1,4 @@ import type { Tree } from '@nrwl/devkit'; -import { tsquery } from '@phenomnomnominal/tsquery'; import { coerce, lt, major } from 'semver'; import { getInstalledAngularVersionInfo, @@ -36,6 +35,7 @@ export function validateOptions( if (lt(angularVersionInfo.version, '14.1.0') || ngrxMajorVersion < 15) { const parentPath = options.parent ?? options.module; const parentContent = tree.read(parentPath, 'utf-8'); + const { tsquery } = require('@phenomnomnominal/tsquery'); const ast = tsquery.ast(parentContent); const NG_MODULE_DECORATOR_SELECTOR = diff --git a/packages/angular/src/generators/scam-directive/lib/convert-directive-to-scam.ts b/packages/angular/src/generators/scam-directive/lib/convert-directive-to-scam.ts index 58e7b67a8de8e..3325130689660 100644 --- a/packages/angular/src/generators/scam-directive/lib/convert-directive-to-scam.ts +++ b/packages/angular/src/generators/scam-directive/lib/convert-directive-to-scam.ts @@ -1,10 +1,12 @@ import type { Tree } from '@nrwl/devkit'; import { joinPathFragments, names } from '@nrwl/devkit'; +import { ensureTypescript } from '@nrwl/js/src/utils/typescript/ensure-typescript'; import { insertImport } from '@nrwl/workspace/src/utilities/ast-utils'; -import { createSourceFile, ScriptTarget } from 'typescript'; import type { FileInfo } from '../../utils/file-info'; import type { NormalizedSchema } from '../schema'; +let tsModule: typeof import('typescript'); + export function convertDirectiveToScam( tree: Tree, directiveFileInfo: FileInfo, @@ -15,6 +17,9 @@ export function convertDirectiveToScam( `Couldn't find directive at path ${directiveFileInfo.filePath} to add SCAM setup.` ); } + if (!tsModule) { + tsModule = ensureTypescript(); + } const directiveNames = names(options.name); const typeNames = names('directive'); @@ -25,10 +30,10 @@ export function convertDirectiveToScam( directiveFileInfo.filePath, 'utf-8' ); - let source = createSourceFile( + let source = tsModule.createSourceFile( directiveFileInfo.filePath, currentDirectiveContents, - ScriptTarget.Latest, + tsModule.ScriptTarget.Latest, true ); diff --git a/packages/angular/src/generators/scam-pipe/lib/convert-pipe-to-scam.ts b/packages/angular/src/generators/scam-pipe/lib/convert-pipe-to-scam.ts index 2feb36c9ef78d..44810800247e2 100644 --- a/packages/angular/src/generators/scam-pipe/lib/convert-pipe-to-scam.ts +++ b/packages/angular/src/generators/scam-pipe/lib/convert-pipe-to-scam.ts @@ -1,10 +1,12 @@ import type { Tree } from '@nrwl/devkit'; import { joinPathFragments, names } from '@nrwl/devkit'; +import { ensureTypescript } from '@nrwl/js/src/utils/typescript/ensure-typescript'; import { insertImport } from '@nrwl/workspace/src/utilities/ast-utils'; -import { createSourceFile, ScriptTarget } from 'typescript'; import type { FileInfo } from '../../utils/file-info'; import type { NormalizedSchema } from '../schema'; +let tsModule: typeof import('typescript'); + export function convertPipeToScam( tree: Tree, pipeFileInfo: FileInfo, @@ -15,6 +17,9 @@ export function convertPipeToScam( `Couldn't find pipe at path ${pipeFileInfo.filePath} to add SCAM setup.` ); } + if (!tsModule) { + tsModule = ensureTypescript(); + } const pipeNames = names(options.name); const typeNames = names('pipe'); @@ -22,10 +27,10 @@ export function convertPipeToScam( if (options.inlineScam) { const currentPipeContents = tree.read(pipeFileInfo.filePath, 'utf-8'); - let source = createSourceFile( + let source = tsModule.createSourceFile( pipeFileInfo.filePath, currentPipeContents, - ScriptTarget.Latest, + tsModule.ScriptTarget.Latest, true ); diff --git a/packages/angular/src/generators/scam-to-standalone/lib/convert-scam-to-standalone.ts b/packages/angular/src/generators/scam-to-standalone/lib/convert-scam-to-standalone.ts index 998e5b73ccdf4..967f5ebf07ed1 100644 --- a/packages/angular/src/generators/scam-to-standalone/lib/convert-scam-to-standalone.ts +++ b/packages/angular/src/generators/scam-to-standalone/lib/convert-scam-to-standalone.ts @@ -1,6 +1,5 @@ -import { Node, SourceFile } from 'typescript'; +import type { Node, SourceFile } from 'typescript'; import { Tree } from 'nx/src/generators/tree'; -import { tsquery } from '@phenomnomnominal/tsquery'; import { parse } from 'path'; import { joinPathFragments } from 'nx/src/utils/path'; @@ -17,6 +16,7 @@ export function convertScamToStandalone( let newComponentContents = ''; const COMPONENT_PROPERTY_SELECTOR = 'ClassDeclaration > Decorator > CallExpression:has(Identifier[name=Component]) ObjectLiteralExpression'; + const { tsquery } = require('@phenomnomnominal/tsquery'); const componentDecoratorMetadataNode = tsquery( componentAST, COMPONENT_PROPERTY_SELECTOR, diff --git a/packages/angular/src/generators/scam-to-standalone/lib/get-component-data-from-ast.ts b/packages/angular/src/generators/scam-to-standalone/lib/get-component-data-from-ast.ts index 81eacb145bc6f..a245350b2e23b 100644 --- a/packages/angular/src/generators/scam-to-standalone/lib/get-component-data-from-ast.ts +++ b/packages/angular/src/generators/scam-to-standalone/lib/get-component-data-from-ast.ts @@ -1,5 +1,4 @@ -import { Tree } from 'nx/src/generators/tree'; -import { tsquery } from '@phenomnomnominal/tsquery'; +import type { Tree } from 'nx/src/generators/tree'; export function getComponentDataFromAST( tree: Tree, @@ -11,6 +10,7 @@ export function getComponentDataFromAST( 'ClassDeclaration:has(Decorator > CallExpression:has(Identifier[name=Component])) > Identifier'; const componentFileContents = tree.read(normalizedComponentPath, 'utf-8'); + const { tsquery } = require('@phenomnomnominal/tsquery'); const componentAST = tsquery.ast(componentFileContents); const componentNode = tsquery(componentAST, COMPONENT_CONTENT_SELECTOR, { diff --git a/packages/angular/src/generators/scam-to-standalone/lib/get-module-metadata-from-ast.ts b/packages/angular/src/generators/scam-to-standalone/lib/get-module-metadata-from-ast.ts index c98d5b5c48982..58447c635c9bf 100644 --- a/packages/angular/src/generators/scam-to-standalone/lib/get-module-metadata-from-ast.ts +++ b/packages/angular/src/generators/scam-to-standalone/lib/get-module-metadata-from-ast.ts @@ -1,5 +1,4 @@ -import { SourceFile } from 'typescript'; -import { tsquery } from '@phenomnomnominal/tsquery'; +import type { SourceFile } from 'typescript'; export function getModuleMetadataFromAST( componentAST: SourceFile, @@ -7,6 +6,7 @@ export function getModuleMetadataFromAST( ) { const NGMODULE_CONTENT_SELECTOR = 'ClassDeclaration:has(Decorator > CallExpression:has(Identifier[name=NgModule]))'; + const { tsquery } = require('@phenomnomnominal/tsquery'); const moduleNodes = tsquery(componentAST, NGMODULE_CONTENT_SELECTOR, { visitAllChildren: true, }); diff --git a/packages/angular/src/generators/scam-to-standalone/lib/selector-exists-in-ast.ts b/packages/angular/src/generators/scam-to-standalone/lib/selector-exists-in-ast.ts index a6dd28e272749..74bdf7c8c3542 100644 --- a/packages/angular/src/generators/scam-to-standalone/lib/selector-exists-in-ast.ts +++ b/packages/angular/src/generators/scam-to-standalone/lib/selector-exists-in-ast.ts @@ -1,6 +1,6 @@ -import { SourceFile } from 'typescript'; -import { tsquery } from '@phenomnomnominal/tsquery'; +import type { SourceFile } from 'typescript'; export function selectorExistsInAST(selector: string, ast: SourceFile) { + const { tsquery } = require('@phenomnomnominal/tsquery'); return tsquery(ast, selector, { visitAllChildren: true }).length > 0; } diff --git a/packages/angular/src/generators/scam-to-standalone/lib/verify-is-inline-scam.ts b/packages/angular/src/generators/scam-to-standalone/lib/verify-is-inline-scam.ts index 054a6e0872ff1..bb414b953a8fc 100644 --- a/packages/angular/src/generators/scam-to-standalone/lib/verify-is-inline-scam.ts +++ b/packages/angular/src/generators/scam-to-standalone/lib/verify-is-inline-scam.ts @@ -1,5 +1,5 @@ import { selectorExistsInAST } from './selector-exists-in-ast'; -import { SourceFile } from 'typescript'; +import type { SourceFile } from 'typescript'; export function verifyIsInlineScam(componentAST: SourceFile) { const NGMODULE_DECORATOR_SELECTOR = diff --git a/packages/angular/src/generators/scam/lib/convert-component-to-scam.ts b/packages/angular/src/generators/scam/lib/convert-component-to-scam.ts index 7b395a3a726cc..5c3a57f623196 100644 --- a/packages/angular/src/generators/scam/lib/convert-component-to-scam.ts +++ b/packages/angular/src/generators/scam/lib/convert-component-to-scam.ts @@ -1,10 +1,12 @@ import type { Tree } from '@nrwl/devkit'; import { joinPathFragments, names } from '@nrwl/devkit'; +import { ensureTypescript } from '@nrwl/js/src/utils/typescript/ensure-typescript'; import { insertImport } from '@nrwl/workspace/src/utilities/ast-utils'; -import { createSourceFile, ScriptTarget } from 'typescript'; import type { FileInfo } from '../../utils/file-info'; import type { NormalizedSchema } from '../schema'; +let tsModule: typeof import('typescript'); + export function convertComponentToScam( tree: Tree, componentFileInfo: FileInfo, @@ -20,15 +22,19 @@ export function convertComponentToScam( const typeNames = names(options.type ?? 'component'); const componentClassName = `${componentNames.className}${typeNames.className}`; + if (!tsModule) { + tsModule = ensureTypescript(); + } + if (options.inlineScam) { const currentComponentContents = tree.read( componentFileInfo.filePath, 'utf-8' ); - let source = createSourceFile( + let source = tsModule.createSourceFile( componentFileInfo.filePath, currentComponentContents, - ScriptTarget.Latest, + tsModule.ScriptTarget.Latest, true ); diff --git a/packages/angular/src/generators/setup-mf/lib/add-remote-to-host.ts b/packages/angular/src/generators/setup-mf/lib/add-remote-to-host.ts index 5a5207cf1219d..41846e96a400b 100644 --- a/packages/angular/src/generators/setup-mf/lib/add-remote-to-host.ts +++ b/packages/angular/src/generators/setup-mf/lib/add-remote-to-host.ts @@ -7,8 +7,6 @@ import { updateJson, } from '@nrwl/devkit'; import type { Schema } from '../schema'; -import { tsquery } from '@phenomnomnominal/tsquery'; -import type * as ts from 'typescript'; import { ArrayLiteralExpression } from 'typescript'; import { insertImport } from '@nrwl/workspace/src/utilities/ast-utils'; import { addRoute } from '../../../utils/nx-devkit/route-utils'; @@ -85,6 +83,7 @@ function addRemoteToStaticHost( } const hostMFConfig = tree.read(hostMFConfigPath, 'utf-8'); + const { tsquery } = require('@phenomnomnominal/tsquery'); const webpackAst = tsquery.ast(hostMFConfig); const mfRemotesNode = tsquery( webpackAst, diff --git a/packages/angular/src/generators/setup-mf/lib/update-host-app-routes.ts b/packages/angular/src/generators/setup-mf/lib/update-host-app-routes.ts index 86d037f4b1d28..e96d0d8aa947a 100644 --- a/packages/angular/src/generators/setup-mf/lib/update-host-app-routes.ts +++ b/packages/angular/src/generators/setup-mf/lib/update-host-app-routes.ts @@ -1,11 +1,17 @@ import { Tree } from 'nx/src/generators/tree'; -import { Schema } from '../schema'; import { readProjectConfiguration } from 'nx/src/generators/utils/project-configuration'; import { generateFiles, joinPathFragments, names } from '@nrwl/devkit'; -import * as ts from 'typescript'; +import { ensureTypescript } from '@nrwl/js/src/utils/typescript/ensure-typescript'; import { addRoute } from '../../../utils/nx-devkit/route-utils'; +import { Schema } from '../schema'; + +let tsModule: typeof import('typescript'); export function updateHostAppRoutes(tree: Tree, options: Schema) { + if (!tsModule) { + tsModule = ensureTypescript(); + } + const { sourceRoot } = readProjectConfiguration(tree, options.appName); const remoteRoutes = @@ -36,10 +42,10 @@ ${remoteRoutes} const hostRootRoutingFile = tree.read(pathToHostRootRoutingFile, 'utf-8'); - let sourceFile = ts.createSourceFile( + let sourceFile = tsModule.createSourceFile( pathToHostRootRoutingFile, hostRootRoutingFile, - ts.ScriptTarget.Latest, + tsModule.ScriptTarget.Latest, true ); diff --git a/packages/angular/src/generators/utils/storybook-ast/component-info.ts b/packages/angular/src/generators/utils/storybook-ast/component-info.ts index 085c6ea84bba1..4c411e8a6f04b 100644 --- a/packages/angular/src/generators/utils/storybook-ast/component-info.ts +++ b/packages/angular/src/generators/utils/storybook-ast/component-info.ts @@ -5,15 +5,17 @@ import { Tree, visitNotIgnoredFiles, } from '@nrwl/devkit'; -import { tsquery } from '@phenomnomnominal/tsquery'; +import { ensureTypescript } from '@nrwl/js/src/utils/typescript/ensure-typescript'; import { basename, dirname, extname, relative } from 'path'; import type { Identifier, SourceFile, Statement } from 'typescript'; -import { SyntaxKind } from 'typescript'; import { getTsSourceFile } from '../../../utils/nx-devkit/ast-utils'; import type { EntryPoint } from './entry-point'; import { getModuleDeclaredComponents } from './module-info'; import { getAllFilesRecursivelyFromDir } from './tree-utilities'; +let tsModule: typeof import('typescript'); +let tsquery: typeof import('@phenomnomnominal/tsquery').tsquery; + export interface ComponentInfo { componentFileName: string; moduleFolderPath: string; @@ -39,8 +41,11 @@ export function getComponentsInfo( return undefined; } + if (!tsModule) { + tsModule = ensureTypescript(); + } const imports = file.statements.filter( - (statement) => statement.kind === SyntaxKind.ImportDeclaration + (statement) => statement.kind === tsModule.SyntaxKind.ImportDeclaration ); const componentsInfo = declaredComponents.map((componentName) => @@ -105,6 +110,10 @@ export function getStandaloneComponentsInfo( } function getStandaloneComponents(tree: Tree, filePath: string): string[] { + if (!tsquery) { + ensureTypescript(); + tsquery = require('@phenomnomnominal/tsquery').tsquery; + } const fileContent = tree.read(filePath, 'utf-8'); const ast = tsquery.ast(fileContent); const components = tsquery( @@ -120,20 +129,23 @@ function getComponentImportPath( componentName: string, imports: Statement[] ): string { + if (!tsModule) { + tsModule = ensureTypescript(); + } const componentImportStatement = imports.find((statement) => { const namedImports = statement .getChildren() - .find((node) => node.kind === SyntaxKind.ImportClause) + .find((node) => node.kind === tsModule.SyntaxKind.ImportClause) .getChildren() - .find((node) => node.kind === SyntaxKind.NamedImports); + .find((node) => node.kind === tsModule.SyntaxKind.NamedImports); if (namedImports === undefined) return false; const importedIdentifiers = namedImports .getChildren() - .find((node) => node.kind === SyntaxKind.SyntaxList) + .find((node) => node.kind === tsModule.SyntaxKind.SyntaxList) .getChildren() - .filter((node) => node.kind === SyntaxKind.ImportSpecifier) + .filter((node) => node.kind === tsModule.SyntaxKind.ImportSpecifier) .map((node) => node.getText()); return importedIdentifiers.includes(componentName); @@ -141,7 +153,7 @@ function getComponentImportPath( const importPath = componentImportStatement .getChildren() - .find((node) => node.kind === SyntaxKind.StringLiteral) + .find((node) => node.kind === tsModule.SyntaxKind.StringLiteral) .getText() .slice(1, -1); @@ -157,6 +169,11 @@ function getComponentInfo( componentName: string ): ComponentInfo { try { + if (!tsquery) { + ensureTypescript(); + tsquery = require('@phenomnomnominal/tsquery').tsquery; + } + const moduleFolderPath = dirname(moduleFilePath); // try to get the component from the same file (inline scam) diff --git a/packages/angular/src/generators/utils/storybook-ast/module-info.ts b/packages/angular/src/generators/utils/storybook-ast/module-info.ts index e951f6b8bd039..d1d0a9f14f40d 100644 --- a/packages/angular/src/generators/utils/storybook-ast/module-info.ts +++ b/packages/angular/src/generators/utils/storybook-ast/module-info.ts @@ -13,11 +13,12 @@ import type { SourceFile, VariableDeclaration, } from 'typescript'; -import { SyntaxKind } from 'typescript'; import { getDecoratorMetadata } from '../../../utils/nx-devkit/ast-utils'; import type { EntryPoint } from './entry-point'; import { ensureTypescript } from '@nrwl/js/src/utils/typescript/ensure-typescript'; +let tsModule: typeof import('typescript'); + export function getModuleDeclaredComponents( file: SourceFile, moduleFilePath: string, @@ -101,19 +102,22 @@ function getDeclaredComponentsInDeclarations( } function getDeclaredComponentNodes(declarationsArray: Node): Node[] { + if (!tsModule) { + tsModule = ensureTypescript(); + } const cmps = declarationsArray .getChildren() - .find((node) => node.kind === SyntaxKind.SyntaxList) + .find((node) => node.kind === tsModule.SyntaxKind.SyntaxList) .getChildren() .map((node) => { - if (node.kind === SyntaxKind.Identifier) { + if (node.kind === tsModule.SyntaxKind.Identifier) { return node; } // if the node is a destructuring, follow the variable - if (node.kind === SyntaxKind.SpreadElement) { + if (node.kind === tsModule.SyntaxKind.SpreadElement) { const declarationVariableNode = node .getChildren() - .find((node) => node.kind === SyntaxKind.Identifier); + .find((node) => node.kind === tsModule.SyntaxKind.Identifier); // try to find the variable declaration in the same component const declarationVariable = getVariableDeclaration( @@ -123,7 +127,7 @@ function getDeclaredComponentNodes(declarationsArray: Node): Node[] { if ( declarationVariable && declarationVariable.initializer.kind === - SyntaxKind.ArrayLiteralExpression + tsModule.SyntaxKind.ArrayLiteralExpression ) { const nodes = getDeclaredComponentNodes( declarationVariable.initializer @@ -158,9 +162,12 @@ function getDeclarationsArray( moduleFilePath: string, projectName: string ): Node | undefined { + if (!tsModule) { + tsModule = ensureTypescript(); + } let declarationArray = declarationsPropertyAssignment .getChildren() - .find((node) => node.kind === SyntaxKind.ArrayLiteralExpression); + .find((node) => node.kind === tsModule.SyntaxKind.ArrayLiteralExpression); if (declarationArray) { return declarationArray; @@ -198,9 +205,12 @@ function getModuleDeclaredComponentsFromVariable( file: SourceFile, declarationsPropertyAssignment: Node ): Node | undefined { + if (!tsModule) { + tsModule = ensureTypescript(); + } let declarationsVariable = declarationsPropertyAssignment .getChildren() - .filter((node) => node.kind === SyntaxKind.Identifier)[1]; + .filter((node) => node.kind === tsModule.SyntaxKind.Identifier)[1]; if (!declarationsVariable) { return undefined; @@ -218,7 +228,7 @@ function getModuleDeclaredComponentsFromVariable( const declarationArray = variableDeclaration .getChildren() - .find((node) => node.kind === SyntaxKind.ArrayLiteralExpression); + .find((node) => node.kind === tsModule.SyntaxKind.ArrayLiteralExpression); return declarationArray; } @@ -231,9 +241,14 @@ function getModuleDeclaredComponentsFromClass( file: SourceFile, declarationsPropertyAssignment: Node ): Node | undefined { + if (!tsModule) { + tsModule = ensureTypescript(); + } const propertyAccessExpression = declarationsPropertyAssignment .getChildren() - .filter((node) => node.kind === SyntaxKind.PropertyAccessExpression)[0]; + .filter( + (node) => node.kind === tsModule.SyntaxKind.PropertyAccessExpression + )[0]; if (!propertyAccessExpression) { return undefined; @@ -242,7 +257,7 @@ function getModuleDeclaredComponentsFromClass( // Should contain 2 identifiers [SomeClass, components] const [clazz, componentsProperty] = propertyAccessExpression .getChildren() - .filter((node) => node.kind === SyntaxKind.Identifier); + .filter((node) => node.kind === tsModule.SyntaxKind.Identifier); if (!clazz || !componentsProperty) { return undefined; @@ -256,18 +271,18 @@ function getModuleDeclaredComponentsFromClass( } const declarationArray = classDeclaration.members - .filter((node) => node.kind === SyntaxKind.PropertyDeclaration) + .filter((node) => node.kind === tsModule.SyntaxKind.PropertyDeclaration) .find((propertyDeclaration) => propertyDeclaration .getChildren() .find( (node) => - node.kind === SyntaxKind.Identifier && + node.kind === tsModule.SyntaxKind.Identifier && node.getText() === componentsProperty.getText() ) ) .getChildren() - .find((node) => node.kind === SyntaxKind.ArrayLiteralExpression); + .find((node) => node.kind === tsModule.SyntaxKind.ArrayLiteralExpression); return declarationArray; } @@ -276,14 +291,21 @@ function getClassDeclaration( className: string, file: SourceFile ): ClassDeclaration | undefined { - const classDeclaration = findNodes(file, SyntaxKind.ClassDeclaration).find( - (classDeclaration) => - classDeclaration - .getChildren() - .find( - (node) => - node.kind === SyntaxKind.Identifier && node.getText() === className - ) + if (!tsModule) { + tsModule = ensureTypescript(); + } + + const classDeclaration = findNodes( + file, + tsModule.SyntaxKind.ClassDeclaration + ).find((classDeclaration) => + classDeclaration + .getChildren() + .find( + (node) => + node.kind === tsModule.SyntaxKind.Identifier && + node.getText() === className + ) ) as ClassDeclaration; return classDeclaration; @@ -293,15 +315,20 @@ function getVariableDeclaration( variableName: string, file: SourceFile ): VariableDeclaration | undefined { + if (!tsModule) { + tsModule = ensureTypescript(); + } + const variableDeclaration = findNodes( file, - SyntaxKind.VariableDeclaration + tsModule.SyntaxKind.VariableDeclaration ).find((variableDeclaration) => variableDeclaration .getChildren() .find( (node) => - node.kind === SyntaxKind.Identifier && node.getText() === variableName + node.kind === tsModule.SyntaxKind.Identifier && + node.getText() === variableName ) ) as VariableDeclaration; @@ -313,14 +340,18 @@ function getNgModuleDeclarationsPropertyAssignment( moduleFilePath: string, projectName: string ): Node | undefined { + if (!tsModule) { + tsModule = ensureTypescript(); + } + const syntaxList = ngModuleDecorator.getChildren().find((node) => { - return node.kind === SyntaxKind.SyntaxList; + return node.kind === tsModule.SyntaxKind.SyntaxList; }); const declarationsPropertyAssignment = syntaxList .getChildren() .find((node) => { return ( - node.kind === SyntaxKind.PropertyAssignment && + node.kind === tsModule.SyntaxKind.PropertyAssignment && node.getChildren()[0].getText() === 'declarations' ); }); diff --git a/packages/angular/src/generators/utils/storybook-ast/storybook-inputs.ts b/packages/angular/src/generators/utils/storybook-ast/storybook-inputs.ts index 3bd28dc526111..6abb07bc2e81e 100644 --- a/packages/angular/src/generators/utils/storybook-ast/storybook-inputs.ts +++ b/packages/angular/src/generators/utils/storybook-ast/storybook-inputs.ts @@ -2,8 +2,10 @@ import type { Tree } from '@nrwl/devkit'; import { findNodes } from 'nx/src/utils/typescript'; import { getSourceNodes } from '@nrwl/workspace/src/utilities/typescript'; import type { PropertyDeclaration } from 'typescript'; -import { SyntaxKind } from 'typescript'; import { getTsSourceFile } from '../../../utils/nx-devkit/ast-utils'; +import { ensureTypescript } from '@nrwl/js/src/utils/typescript/ensure-typescript'; + +let tsModule: typeof import('typescript'); export type KnobType = 'text' | 'boolean' | 'number' | 'select'; export interface InputDescriptor { @@ -16,15 +18,18 @@ export function getInputPropertyDeclarations( tree: Tree, path: string ): PropertyDeclaration[] { + if (!tsModule) { + tsModule = ensureTypescript(); + } const file = getTsSourceFile(tree, path); const decorators = getSourceNodes(file).filter( - (node) => node.kind === SyntaxKind.Decorator + (node) => node.kind === tsModule.SyntaxKind.Decorator ); return decorators .filter((decorator) => - findNodes(decorator, SyntaxKind.Identifier).some( + findNodes(decorator, tsModule.SyntaxKind.Identifier).some( (node) => node.getText() === 'Input' ) ) @@ -39,13 +44,16 @@ export function getComponentProps( ) => string | undefined = getArgsDefaultValue, useDecoratorName = true ): InputDescriptor[] { + if (!tsModule) { + tsModule = ensureTypescript(); + } const props = getInputPropertyDeclarations(tree, componentPath).map( (node) => { const decoratorContent = findNodes( - findNodes(node, SyntaxKind.Decorator).find((n) => + findNodes(node, tsModule.SyntaxKind.Decorator).find((n) => n.getText().startsWith('@Input') ), - SyntaxKind.StringLiteral + tsModule.SyntaxKind.StringLiteral ); const name = useDecoratorName && decoratorContent.length @@ -69,6 +77,9 @@ export function getComponentProps( } export function getKnobType(property: PropertyDeclaration): KnobType { + if (!tsModule) { + tsModule = ensureTypescript(); + } if (property.type) { const typeName = property.type.getText(); const typeNameToKnobType: Record = { @@ -80,10 +91,10 @@ export function getKnobType(property: PropertyDeclaration): KnobType { } if (property.initializer) { const initializerKindToKnobType: Record = { - [SyntaxKind.StringLiteral]: 'text', - [SyntaxKind.NumericLiteral]: 'number', - [SyntaxKind.TrueKeyword]: 'boolean', - [SyntaxKind.FalseKeyword]: 'boolean', + [tsModule.SyntaxKind.StringLiteral]: 'text', + [tsModule.SyntaxKind.NumericLiteral]: 'number', + [tsModule.SyntaxKind.TrueKeyword]: 'boolean', + [tsModule.SyntaxKind.FalseKeyword]: 'boolean', }; return initializerKindToKnobType[property.initializer.kind] || 'text'; } diff --git a/packages/angular/src/utils/get-mf-projects.ts b/packages/angular/src/utils/get-mf-projects.ts index 6605ff41ffe91..fc4202361f579 100644 --- a/packages/angular/src/utils/get-mf-projects.ts +++ b/packages/angular/src/utils/get-mf-projects.ts @@ -1,5 +1,4 @@ import type { Tree } from '@nrwl/devkit'; -import { tsquery } from '@phenomnomnominal/tsquery'; import { forEachExecutorOptions } from '@nrwl/workspace/src/utilities/executor-options-utils'; export function getMFProjects( @@ -22,6 +21,7 @@ export function getMFProjects( return; } const webpackConfig = tree.read(webpackPath, 'utf-8'); + const { tsquery } = require('@phenomnomnominal/tsquery'); const ast = tsquery.ast(webpackConfig); const moduleFederationWebpackConfig = tsquery( ast, diff --git a/packages/angular/src/utils/nx-devkit/route-utils.ts b/packages/angular/src/utils/nx-devkit/route-utils.ts index 29ca10d904df9..ba3dc9e5431f8 100644 --- a/packages/angular/src/utils/nx-devkit/route-utils.ts +++ b/packages/angular/src/utils/nx-devkit/route-utils.ts @@ -19,9 +19,8 @@ export function addRoute( ); } if (!tsModule) { - tsModule = require('typescript'); + tsModule = ensureTypescript(); } - ensureTypescript(); const { tsquery } = require('@phenomnomnominal/tsquery'); let routesFileContents = tree.read(routesFile, 'utf-8'); diff --git a/packages/devkit/src/generators/to-js.ts b/packages/devkit/src/generators/to-js.ts index 709adc79ef586..01c6afb23a9fc 100644 --- a/packages/devkit/src/generators/to-js.ts +++ b/packages/devkit/src/generators/to-js.ts @@ -1,10 +1,16 @@ import type { Tree } from 'nx/src/generators/tree'; +// eslint-disable-next-line @typescript-eslint/no-restricted-imports +import { typescriptVersion } from 'nx/src/utils/versions'; +import { ensurePackage } from '../utils/package-json'; /** * Rename and transpile any new typescript files created to javascript files */ export function toJS(tree: Tree): void { - const { JsxEmit, ScriptTarget, transpile } = require('typescript'); + const { JsxEmit, ScriptTarget, transpile } = ensurePackage( + 'typescript', + typescriptVersion + ); for (const c of tree.listChanges()) { if ( diff --git a/packages/js/src/generators/library/library.ts b/packages/js/src/generators/library/library.ts index 7274ebc04999d..3c563d4b48750 100644 --- a/packages/js/src/generators/library/library.ts +++ b/packages/js/src/generators/library/library.ts @@ -12,7 +12,6 @@ import { names, offsetFromRoot, ProjectConfiguration, - readJson, toJS, Tree, updateJson, diff --git a/packages/react-native/src/generators/component/component.ts b/packages/react-native/src/generators/component/component.ts index 9ba5280263ffd..962162ddc7807 100644 --- a/packages/react-native/src/generators/component/component.ts +++ b/packages/react-native/src/generators/component/component.ts @@ -1,4 +1,3 @@ -import * as ts from 'typescript'; import { Schema } from './schema'; import { applyChangesToString, @@ -12,6 +11,7 @@ import { } from '@nrwl/devkit'; import { NormalizedSchema, normalizeOptions } from './lib/normalize-options'; import { addImport } from './lib/add-import'; +import { ensureTypescript } from '@nrwl/js/src/utils/typescript/ensure-typescript'; export async function reactNativeComponentGenerator( host: Tree, @@ -53,7 +53,11 @@ function createComponentFiles(host: Tree, options: NormalizedSchema) { } } +let tsModule: typeof import('typescript'); function addExportsToBarrel(host: Tree, options: NormalizedSchema) { + if (!tsModule) { + tsModule = ensureTypescript(); + } const workspace = getProjects(host); const isApp = workspace.get(options.project).projectType === 'application'; @@ -64,10 +68,10 @@ function addExportsToBarrel(host: Tree, options: NormalizedSchema) { ); const indexSource = host.read(indexFilePath, 'utf-8'); if (indexSource !== null) { - const indexSourceFile = ts.createSourceFile( + const indexSourceFile = tsModule.createSourceFile( indexFilePath, indexSource, - ts.ScriptTarget.Latest, + tsModule.ScriptTarget.Latest, true ); const changes = applyChangesToString( diff --git a/packages/react-native/src/generators/component/lib/add-import.ts b/packages/react-native/src/generators/component/lib/add-import.ts index 5e51c4bb21f73..46035d9023be9 100644 --- a/packages/react-native/src/generators/component/lib/add-import.ts +++ b/packages/react-native/src/generators/component/lib/add-import.ts @@ -1,12 +1,17 @@ import { findNodes } from 'nx/src/utils/typescript'; -import * as ts from 'typescript'; +import type * as ts from 'typescript'; import { ChangeType, StringChange } from '@nrwl/devkit'; +import { ensureTypescript } from '@nrwl/js/src/utils/typescript/ensure-typescript'; +let tsModule: typeof import('typescript'); export function addImport( source: ts.SourceFile, statement: string ): StringChange[] { - const allImports = findNodes(source, ts.SyntaxKind.ImportDeclaration); + if (!tsModule) { + tsModule = ensureTypescript(); + } + const allImports = findNodes(source, tsModule.SyntaxKind.ImportDeclaration); if (allImports.length > 0) { const lastImport = allImports[allImports.length - 1]; return [ diff --git a/packages/workspace/src/generators/new/generate-workspace-files.ts b/packages/workspace/src/generators/new/generate-workspace-files.ts index 2987773f419d1..58d6ddb2cf927 100644 --- a/packages/workspace/src/generators/new/generate-workspace-files.ts +++ b/packages/workspace/src/generators/new/generate-workspace-files.ts @@ -9,10 +9,7 @@ import { updateJson, writeJson, } from '@nrwl/devkit'; -import { - nxVersion, - prettierVersion, -} from '../../utils/versions'; +import { nxVersion, prettierVersion } from '../../utils/versions'; import { join, join as pathJoin } from 'path'; import { Preset } from '../utils/presets'; import { deduceDefaultBase } from '../../utilities/default-base';