You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
importtype{ExecutorContext,ProjectGraphProjectNode}from'@nrwl/devkit';import{normalizePath,readJsonFile}from'@nrwl/devkit';import{copySync,readdirSync,readFileSync,removeSync,writeFileSync,}from'fs-extra';import{join,relative}from'path';importtype{NormalizedExecutorOptions}from'../executors/tsc/schema';import{existsSync}from'fs';interfaceInlineProjectNode{name: string;root: string;sourceRoot: string;pathAlias: string;buildOutputPath?: string;}exportinterfaceInlineProjectGraph{nodes: Record<string,InlineProjectNode>;externals: Record<string,InlineProjectNode>;dependencies: Record<string,string[]>;}exportfunctionisInlineGraphEmpty(inlineGraph: InlineProjectGraph): boolean{returnObject.keys(inlineGraph.nodes).length===0;}exportfunctionhandleInliningBuild(context: ExecutorContext,options: NormalizedExecutorOptions,tsConfigPath: string): InlineProjectGraph{consttsConfigJson=readJsonFile(tsConfigPath);constpathAliases=tsConfigJson['compilerOptions']['paths']||readBasePathAliases(context);constinlineGraph=createInlineGraph(context,options,pathAliases);if(isInlineGraphEmpty(inlineGraph)){returninlineGraph;}buildInlineGraphExternals(context,inlineGraph,pathAliases);returninlineGraph;}exportfunctionpostProcessInlinedDependencies(outputPath: string,parentOutputPath: string,inlineGraph: InlineProjectGraph){if(isInlineGraphEmpty(inlineGraph)){return;}constparentDistPath=join(outputPath,parentOutputPath);// move parentOutputmovePackage(parentDistPath,outputPath);constinlinedDepsDestOutputRecord: Record<string,string>={};// move inlined outputsfor(constinlineDependenciesNamesofObject.values(inlineGraph.dependencies)){for(constinlineDependenciesNameofinlineDependenciesNames){constinlineDependency=inlineGraph.nodes[inlineDependenciesName];constdepOutputPath=inlineDependency.buildOutputPath||join(outputPath,inlineDependency.root);constdestDepOutputPath=join(outputPath,inlineDependency.name);constisBuildable=!!inlineDependency.buildOutputPath;if(isBuildable){copySync(depOutputPath,destDepOutputPath,{overwrite: true});}else{movePackage(depOutputPath,destDepOutputPath);}// TODO: hard-coded "src"inlinedDepsDestOutputRecord[inlineDependency.pathAlias]=destDepOutputPath+'/src';}}updateImports(outputPath,inlinedDepsDestOutputRecord);}functionreadBasePathAliases(context: ExecutorContext){returnreadJsonFile(getRootTsConfigPath(context))?.['compilerOptions']['paths'];}exportfunctiongetRootTsConfigPath(context: ExecutorContext): string|null{for(consttsConfigNameof['tsconfig.base.json','tsconfig.json']){consttsConfigPath=join(context.root,tsConfigName);if(existsSync(tsConfigPath)){returntsConfigPath;}}thrownewError('Could not find a root tsconfig.json or tsconfig.base.json file.');}functionemptyInlineGraph(): InlineProjectGraph{return{nodes: {},externals: {},dependencies: {}};}functionprojectNodeToInlineProjectNode(projectNode: ProjectGraphProjectNode,pathAlias='',buildOutputPath=''): InlineProjectNode{return{name: projectNode.name,root: projectNode.data.root,sourceRoot: projectNode.data.sourceRoot,
pathAlias,
buildOutputPath,};}functioncreateInlineGraph(context: ExecutorContext,options: NormalizedExecutorOptions,pathAliases: Record<string,string[]>,projectName: string=context.projectName,inlineGraph: InlineProjectGraph=emptyInlineGraph()){if(options.external==null&&options.internal==null)returninlineGraph;constprojectDependencies=context.projectGraph.dependencies[projectName]||[];if(projectDependencies.length===0)returninlineGraph;if(!inlineGraph.nodes[projectName]){inlineGraph.nodes[projectName]=projectNodeToInlineProjectNode(context.projectGraph.nodes[projectName]);}constimplicitDependencies=context.projectGraph.nodes[projectName].data.implicitDependencies||[];for(constprojectDependencyofprojectDependencies){// skip npm packagesif(projectDependency.target.startsWith('npm')){continue;}// skip implicitDependenciesif(implicitDependencies.includes(projectDependency.target)){continue;}constpathAlias=getPathAliasForPackage(context.projectGraph.nodes[projectDependency.target],pathAliases);constbuildOutputPath=getBuildOutputPath(projectDependency.target,context,options);constshouldInline=/** * if some buildable libraries are marked as internal, */options.internal?.includes(projectDependency.target)||/** * if all buildable libraries are marked as external, * then push the project dependency that doesn't have a build target */(options.external==='all'&&!buildOutputPath)||/** * if all buildable libraries are marked as internal, * then push every project dependency to be inlined */options.external==='none'||/** * if some buildable libraries are marked as external, * then push the project dependency that IS NOT marked as external OR doesn't have a build target */(Array.isArray(options.external)&&options.external.length>0&&!options.external.includes(projectDependency.target))||!buildOutputPath;console.log(shouldInline,projectDependency.target);if(shouldInline){inlineGraph.dependencies[projectName]??=[];inlineGraph.dependencies[projectName].push(projectDependency.target);}inlineGraph.nodes[projectDependency.target]=projectNodeToInlineProjectNode(context.projectGraph.nodes[projectDependency.target],pathAlias,buildOutputPath);if(context.projectGraph.dependencies[projectDependency.target].length>0){inlineGraph=createInlineGraph(context,options,pathAliases,projectDependency.target,inlineGraph);}}returninlineGraph;}functionbuildInlineGraphExternals(context: ExecutorContext,inlineProjectGraph: InlineProjectGraph,pathAliases: Record<string,string[]>){constallNodes={ ...context.projectGraph.nodes};for(const[parent,dependencies]ofObject.entries(inlineProjectGraph.dependencies)){if(allNodes[parent]){deleteallNodes[parent];}for(constdependencyNameofdependencies){constdependencyNode=inlineProjectGraph.nodes[dependencyName];// buildable is still external even if it is a dependencyif(dependencyNode.buildOutputPath){continue;}if(allNodes[dependencyName]){deleteallNodes[dependencyName];}}}for(const[projectName,projectNode]ofObject.entries(allNodes)){if(!inlineProjectGraph.externals[projectName]){inlineProjectGraph.externals[projectName]=projectNodeToInlineProjectNode(projectNode,getPathAliasForPackage(projectNode,pathAliases));}}}functionmovePackage(from: string,to: string){if(from===to)return;copySync(from,to,{overwrite: true});removeSync(from);}functionupdateImports(destOutputPath: string,inlinedDepsDestOutputRecord: Record<string,string>){constimportRegex=newRegExp(Object.keys(inlinedDepsDestOutputRecord).map((pathAlias)=>`["'](${pathAlias})["']`).join('|'),'g');recursiveUpdateImport(destOutputPath,importRegex,inlinedDepsDestOutputRecord);}functionrecursiveUpdateImport(dirPath: string,importRegex: RegExp,inlinedDepsDestOutputRecord: Record<string,string>,rootParentDir?: string){constfiles=readdirSync(dirPath,{withFileTypes: true});for(constfileoffiles){// only check .js and .d.ts filesif(file.isFile()&&(file.name.endsWith('.js')||file.name.endsWith('.d.ts'))){constfilePath=join(dirPath,file.name);constfileContent=readFileSync(filePath,'utf-8');constupdatedContent=fileContent.replace(importRegex,(matched)=>{constresult=matched.replace(/['"]/g,'');// If a match is the same as the rootParentDir, we're checking its own files so we return the matched as in no changes.if(result===rootParentDir)returnmatched;constimportPath=`"${relative(dirPath,inlinedDepsDestOutputRecord[result])}"`;returnnormalizePath(importPath);});writeFileSync(filePath,updatedContent);}elseif(file.isDirectory()){recursiveUpdateImport(join(dirPath,file.name),importRegex,inlinedDepsDestOutputRecord,rootParentDir||file.name);}}}functiongetPathAliasForPackage(packageNode: ProjectGraphProjectNode,pathAliases: Record<string,string[]>): string{if(!packageNode)return'';for(const[alias,paths]ofObject.entries(pathAliases)){if(paths.some((path)=>path.includes(packageNode.data.root))){returnalias;}}return'';}functiongetBuildOutputPath(projectName: string,context: ExecutorContext,options: NormalizedExecutorOptions): string{constprojectTargets=context.projectGraph.nodes[projectName]?.data?.targets;if(!projectTargets)return'';constbuildTarget=options.externalBuildTargets.find((buildTarget)=>projectTargets[buildTarget]);returnbuildTarget ? projectTargets[buildTarget].options['outputPath'] : '';}
d1358f63ec92179924a0595d64a6c9a0a8228723
The text was updated successfully, but these errors were encountered:
hard-coded "src"
parsers/libs/plugins/better-nx-tsc/src/utils/inline.ts
Line 85 in 9c2a0e5
d1358f63ec92179924a0595d64a6c9a0a8228723
The text was updated successfully, but these errors were encountered: