cleanup(core): move js graph creation its own folder (#14283)

This commit is contained in:
Jason Jean 2023-01-26 10:48:32 -05:00 committed by GitHub
parent e002bd3479
commit dc6da76900
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 98 additions and 73 deletions

View File

@ -103,7 +103,7 @@ describe('js e2e', () => {
const swcHelpersFromDist = readJson(`dist/libs/${lib}/package.json`) const swcHelpersFromDist = readJson(`dist/libs/${lib}/package.json`)
.peerDependencies['@swc/helpers']; .peerDependencies['@swc/helpers'];
expect(satisfies(swcHelpersFromDist, swcHelpersFromRoot)).toBeTruthy(); expect(swcHelpersFromDist).toEqual(swcHelpersFromRoot);
updateJson(`libs/${lib}/.lib.swcrc`, (json) => { updateJson(`libs/${lib}/.lib.swcrc`, (json) => {
json.jsc.externalHelpers = false; json.jsc.externalHelpers = false;

View File

@ -144,11 +144,8 @@ describe('js e2e', () => {
const rootPackageJson = readJson(`package.json`); const rootPackageJson = readJson(`package.json`);
expect( expect(
satisfies( readJson(`dist/libs/${lib}/package.json`).peerDependencies.tslib
readJson(`dist/libs/${lib}/package.json`).peerDependencies.tslib, ).toEqual(rootPackageJson.dependencies.tslib);
rootPackageJson.dependencies.tslib
)
).toBeTruthy();
updateJson(`libs/${lib}/tsconfig.json`, (json) => { updateJson(`libs/${lib}/tsconfig.json`, (json) => {
json.compilerOptions = { ...json.compilerOptions, importHelpers: false }; json.compilerOptions = { ...json.compilerOptions, importHelpers: false };

View File

@ -11,6 +11,7 @@ import {
packageInstall, packageInstall,
promisifiedTreeKill, promisifiedTreeKill,
readFile, readFile,
readJson,
runCLI, runCLI,
runCLIAsync, runCLIAsync,
runCommandUntil, runCommandUntil,
@ -22,7 +23,6 @@ import {
import { exec, execSync } from 'child_process'; import { exec, execSync } from 'child_process';
import * as http from 'http'; import * as http from 'http';
import { getLockFileName } from 'nx/src/lock-file/lock-file'; import { getLockFileName } from 'nx/src/lock-file/lock-file';
import { satisfies } from 'semver';
function getData(port, path = '/api'): Promise<any> { function getData(port, path = '/api'): Promise<any> {
return new Promise((resolve) => { return new Promise((resolve) => {
@ -246,6 +246,7 @@ describe('Build Node apps', () => {
detectPackageManager(tmpProjPath()) detectPackageManager(tmpProjPath())
)}` )}`
); );
const rootPackageJson = JSON.parse(readFile(`package.json`));
const packageJson = JSON.parse( const packageJson = JSON.parse(
readFile(`dist/apps/${nestapp}/package.json`) readFile(`dist/apps/${nestapp}/package.json`)
); );
@ -256,17 +257,22 @@ describe('Build Node apps', () => {
version: '0.0.1', version: '0.0.1',
}) })
); );
expect(
satisfies(packageJson.dependencies['@nestjs/common'], '^9.0.0') expect(packageJson.dependencies['@nestjs/common']).toEqual(
).toBeTruthy(); rootPackageJson.dependencies['@nestjs/common']
expect( );
satisfies(packageJson.dependencies['@nestjs/core'], '^9.0.0') expect(packageJson.dependencies['@nestjs/core']).toEqual(
).toBeTruthy(); rootPackageJson.dependencies['@nestjs/core']
expect( );
satisfies(packageJson.dependencies['reflect-metadata'], '^0.1.13') expect(packageJson.dependencies['reflect-metadata']).toEqual(
).toBeTruthy(); rootPackageJson.dependencies['reflect-metadata']
expect(satisfies(packageJson.dependencies['rxjs'], '^7.0.0')).toBeTruthy(); );
expect(satisfies(packageJson.dependencies['tslib'], '^2.3.0')).toBeTruthy(); expect(packageJson.dependencies['rxjs']).toEqual(
rootPackageJson.dependencies['rxjs']
);
expect(packageJson.dependencies['tslib']).toEqual(
rootPackageJson.dependencies['tslib']
);
const nodeapp = uniq('nodeapp'); const nodeapp = uniq('nodeapp');
runCLI(`generate @nrwl/node:app ${nodeapp} --bundler=webpack`); runCLI(`generate @nrwl/node:app ${nodeapp} --bundler=webpack`);

View File

@ -0,0 +1,11 @@
import { ProjectGraphProcessor } from '../../config/project-graph';
import { ProjectGraphBuilder } from '../../project-graph/project-graph-builder';
import { buildNpmPackageNodes } from './project-graph/build-nodes/build-npm-package-nodes';
export const processProjectGraph: ProjectGraphProcessor = (graph) => {
const builder = new ProjectGraphBuilder(graph);
buildNpmPackageNodes(builder);
return builder.getUpdatedProjectGraph();
};

View File

@ -1,7 +1,7 @@
import { ProjectGraphBuilder } from '../../../../project-graph/project-graph-builder';
import { readJsonFile } from '../../../../utils/fileutils';
import { join } from 'path'; import { join } from 'path';
import { workspaceRoot } from '../../utils/workspace-root'; import { workspaceRoot } from '../../../../utils/workspace-root';
import { readJsonFile } from '../../utils/fileutils';
import { ProjectGraphBuilder } from '../project-graph-builder';
export function buildNpmPackageNodes(builder: ProjectGraphBuilder) { export function buildNpmPackageNodes(builder: ProjectGraphBuilder) {
const packageJson = readJsonFile(join(workspaceRoot, 'package.json')); const packageJson = readJsonFile(join(workspaceRoot, 'package.json'));

View File

@ -1,2 +1 @@
export * from './workspace-projects'; export * from './workspace-projects';
export * from './npm-packages';

View File

@ -12,10 +12,7 @@ import {
writeCache, writeCache,
} from './nx-deps-cache'; } from './nx-deps-cache';
import { buildImplicitProjectDependencies } from './build-dependencies'; import { buildImplicitProjectDependencies } from './build-dependencies';
import { import { buildWorkspaceProjectNodes } from './build-nodes';
buildNpmPackageNodes,
buildWorkspaceProjectNodes,
} from './build-nodes';
import * as os from 'os'; import * as os from 'os';
import { buildExplicitTypescriptAndPackageJsonDependencies } from './build-dependencies/build-explicit-typescript-and-package-json-dependencies'; import { buildExplicitTypescriptAndPackageJsonDependencies } from './build-dependencies/build-explicit-typescript-and-package-json-dependencies';
import { loadNxPlugins } from '../utils/nx-plugin'; import { loadNxPlugins } from '../utils/nx-plugin';
@ -175,13 +172,16 @@ async function buildProjectGraphUsingContext(
performance.mark('build project graph:start'); performance.mark('build project graph:start');
const builder = new ProjectGraphBuilder(partialGraph); const builder = new ProjectGraphBuilder(partialGraph);
builder.setVersion(projectGraphVersion);
buildWorkspaceProjectNodes(ctx, builder, nxJson); buildWorkspaceProjectNodes(ctx, builder, nxJson);
if (!partialGraph) { const initProjectGraph = builder.getUpdatedProjectGraph();
buildNpmPackageNodes(builder);
} const r = await updateProjectGraphWithPlugins(ctx, initProjectGraph);
const updatedBuilder = new ProjectGraphBuilder(r);
for (const proj of Object.keys(cachedFileData)) { for (const proj of Object.keys(cachedFileData)) {
for (const f of builder.graph.nodes[proj].data.files) { for (const f of updatedBuilder.graph.nodes[proj].data.files) {
const cached = cachedFileData[proj][f.file]; const cached = cachedFileData[proj][f.file];
if (cached && cached.deps) { if (cached && cached.deps) {
f.deps = [...cached.deps]; f.deps = [...cached.deps];
@ -192,14 +192,12 @@ async function buildProjectGraphUsingContext(
await buildExplicitDependencies( await buildExplicitDependencies(
jsPluginConfig(nxJson, packageJsonDeps), jsPluginConfig(nxJson, packageJsonDeps),
ctx, ctx,
builder updatedBuilder
); );
buildImplicitProjectDependencies(ctx, builder); buildImplicitProjectDependencies(ctx, updatedBuilder);
builder.setVersion(projectGraphVersion);
const initProjectGraph = builder.getUpdatedProjectGraph();
const r = await updateProjectGraphWithPlugins(ctx, initProjectGraph); const finalGraph = updatedBuilder.getUpdatedProjectGraph();
performance.mark('build project graph:end'); performance.mark('build project graph:end');
performance.measure( performance.measure(
@ -208,7 +206,7 @@ async function buildProjectGraphUsingContext(
'build project graph:end' 'build project graph:end'
); );
return r; return finalGraph;
} }
function jsPluginConfig( function jsPluginConfig(

View File

@ -47,49 +47,63 @@ export interface NxPlugin {
// Allows loadNxPlugins to be called multiple times w/o // Allows loadNxPlugins to be called multiple times w/o
// executing resolution mulitple times. // executing resolution mulitple times.
let nxPluginCache: NxPlugin[] = null; let nxPluginCache: NxPlugin[] = null;
function loadNxPlugin(moduleName: string, paths: string[], root: string) {
let pluginPath: string;
try {
pluginPath = require.resolve(moduleName, {
paths,
});
} catch (e) {
if (e.code === 'MODULE_NOT_FOUND') {
const plugin = resolveLocalNxPlugin(moduleName, root);
if (plugin) {
const main = readPluginMainFromProjectConfiguration(
plugin.projectConfig
);
pluginPath = main ? path.join(root, main) : plugin.path;
} else {
logger.error(`Plugin listed in \`nx.json\` not found: ${moduleName}`);
throw e;
}
} else {
throw e;
}
}
const packageJsonPath = path.join(pluginPath, 'package.json');
const { name } =
!['.ts', '.js'].some((x) => x === path.extname(pluginPath)) && // Not trying to point to a ts or js file
existsSync(packageJsonPath) // plugin has a package.json
? readJsonFile(packageJsonPath) // read name from package.json
: { name: path.basename(pluginPath) }; // use the name of the file we point to
const plugin = require(pluginPath) as NxPlugin;
plugin.name = name;
return plugin;
}
export function loadNxPlugins( export function loadNxPlugins(
plugins?: string[], plugins?: string[],
paths = [workspaceRoot], paths = [workspaceRoot],
root = workspaceRoot root = workspaceRoot
): NxPlugin[] { ): NxPlugin[] {
return plugins?.length const result: NxPlugin[] = [];
? nxPluginCache ||
(nxPluginCache = plugins.map((moduleName) => {
let pluginPath: string;
try {
pluginPath = require.resolve(moduleName, {
paths,
});
} catch (e) {
if (e.code === 'MODULE_NOT_FOUND') {
const plugin = resolveLocalNxPlugin(moduleName, root);
if (plugin) {
const main = readPluginMainFromProjectConfiguration(
plugin.projectConfig
);
pluginPath = main ? path.join(root, main) : plugin.path;
} else {
logger.error(
`Plugin listed in \`nx.json\` not found: ${moduleName}`
);
throw e;
}
} else {
throw e;
}
}
const packageJsonPath = path.join(pluginPath, 'package.json');
const { name } =
!['.ts', '.js'].some((x) => x === path.extname(pluginPath)) && // Not trying to point to a ts or js file
existsSync(packageJsonPath) // plugin has a package.json
? readJsonFile(packageJsonPath) // read name from package.json
: { name: path.basename(pluginPath) }; // use the name of the file we point to
const plugin = require(pluginPath) as NxPlugin;
plugin.name = name;
return plugin; // TODO: This should be specified in nx.json
})) // Temporarily load js as if it were a plugin which is built into nx
: []; // In the future, this will be optional and need to be specified in nx.json
const jsPlugin = require('../plugins/js');
jsPlugin.name = 'index';
result.push(jsPlugin as NxPlugin);
plugins ??= [];
if (!nxPluginCache) {
nxPluginCache = plugins.map((moduleName) =>
loadNxPlugin(moduleName, paths, root)
);
}
return result.concat(nxPluginCache);
} }
export function mergePluginTargetsWithNxTargets( export function mergePluginTargetsWithNxTargets(