Skip to content

Commit

Permalink
cleanup(graph): refactor source map to be returned with project graph
Browse files Browse the repository at this point in the history
  • Loading branch information
FrozenPandaz committed Dec 1, 2023
1 parent 4da1206 commit 5df9c47
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 104 deletions.
6 changes: 4 additions & 2 deletions packages/nx/bin/post-install.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { buildProjectGraphWithoutDaemon } from '../src/project-graph/project-graph';
import { buildProjectGraphAndSourceMapsWithoutDaemon } from '../src/project-graph/project-graph';
import { workspaceRoot } from '../src/utils/workspace-root';
import { fileExists } from '../src/utils/fileutils';
import { join } from 'path';
Expand All @@ -20,7 +20,9 @@ import { setupWorkspaceContext } from '../src/utils/workspace-context';
try {
await daemonClient.stop();
} catch (e) {}
const tasks: Array<Promise<any>> = [buildProjectGraphWithoutDaemon()];
const tasks: Array<Promise<any>> = [
buildProjectGraphAndSourceMapsWithoutDaemon(),
];
if (isNxCloudUsed(readNxJson())) {
tasks.push(verifyOrUpdateNxCloudClient(getCloudOptions()));
}
Expand Down
90 changes: 54 additions & 36 deletions packages/nx/src/command-line/graph/graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ import { TaskGraph } from '../../config/task-graph';
import { daemonClient } from '../../daemon/client/client';
import { getRootTsConfigPath } from '../../plugins/js/utils/typescript';
import { pruneExternalNodes } from '../../project-graph/operators';
import { createProjectGraphAsync } from '../../project-graph/project-graph';
import {
createProjectGraphAndSourceMapsAsync,
createProjectGraphAsync,
} from '../../project-graph/project-graph';
import {
createTaskGraph,
mapTargetDefaultsToDependencies,
Expand All @@ -45,7 +48,6 @@ import { HashPlanner, transferProjectGraph } from '../../native';
import { transformProjectGraphForRust } from '../../native/transform-objects';
import { getAffectedGraphNodes } from '../affected/affected';
import { readFileMapCache } from '../../project-graph/nx-deps-cache';
import { getSourceMaps, readSourceMapsFromDisk } from '../../utils/source-maps';
import { ConfigurationSourceMaps } from '../../project-graph/utils/project-configuration-utils';

import { filterUsingGlobPatterns } from '../../hasher/task-hasher';
Expand Down Expand Up @@ -270,7 +272,10 @@ export async function generateGraph(
? args.targets[0]
: args.targets;

const rawGraph = await createProjectGraphAsync({ exitOnError: true });
const { projectGraph: rawGraph, sourceMaps } =
await createProjectGraphAndSourceMapsAsync({
exitOnError: true,
});
let prunedGraph = pruneExternalNodes(rawGraph);

const projects = Object.values(
Expand Down Expand Up @@ -366,26 +371,23 @@ export async function generateGraph(
},
});

const depGraphClientResponse = await createDepGraphClientResponse(
affectedProjects
);
const { projectGraphClientResponse } =
await createProjectGraphAndSourceMapClientResponse(affectedProjects);

const taskGraphClientResponse = await createTaskGraphClientResponse();
const taskInputsReponse = await createExpandedTaskInputResponse(
taskGraphClientResponse,
depGraphClientResponse
projectGraphClientResponse
);

const sourceMapsResponse = await getSourceMaps();

const environmentJs = buildEnvironmentJs(
args.exclude || [],
args.watch,
!!args.file && args.file.endsWith('html') ? 'build' : 'serve',
depGraphClientResponse,
projectGraphClientResponse,
taskGraphClientResponse,
taskInputsReponse,
sourceMapsResponse
sourceMaps
);
html = html.replace(/src="/g, 'src="static/');
html = html.replace(/href="styles/g, 'href="static/styles');
Expand Down Expand Up @@ -502,10 +504,13 @@ async function startServer(
unregisterFileWatcher = await createFileWatcher();
}

currentDepGraphClientResponse = await createDepGraphClientResponse(affected);
currentDepGraphClientResponse.focus = focus;
currentDepGraphClientResponse.groupByFolder = groupByFolder;
currentDepGraphClientResponse.exclude = exclude;
const { projectGraphClientResponse, sourceMapResponse } =
await createProjectGraphAndSourceMapClientResponse(affected);

currentProjectGraphClientResponse = projectGraphClientResponse;
currentProjectGraphClientResponse.focus = focus;
currentProjectGraphClientResponse.groupByFolder = groupByFolder;
currentProjectGraphClientResponse.exclude = exclude;

const app = http.createServer(async (req, res) => {
// parse URL
Expand All @@ -518,7 +523,7 @@ async function startServer(
const sanitizePath = basename(parsedUrl.pathname);
if (sanitizePath === 'project-graph.json') {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify(currentDepGraphClientResponse));
res.end(JSON.stringify(currentProjectGraphClientResponse));
return;
}

Expand Down Expand Up @@ -547,13 +552,13 @@ async function startServer(

if (sanitizePath === 'source-maps.json') {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify(await getSourceMaps()));
res.end(JSON.stringify(currentSourceMapsClientResponse));
return;
}

if (sanitizePath === 'currentHash') {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ hash: currentDepGraphClientResponse.hash }));
res.end(JSON.stringify({ hash: currentProjectGraphClientResponse.hash }));
return;
}

Expand Down Expand Up @@ -599,7 +604,7 @@ async function startServer(
});
}

let currentDepGraphClientResponse: ProjectGraphClientResponse = {
let currentProjectGraphClientResponse: ProjectGraphClientResponse = {
hash: null,
projects: [],
dependencies: {},
Expand All @@ -613,6 +618,7 @@ let currentDepGraphClientResponse: ProjectGraphClientResponse = {
groupByFolder: false,
exclude: [],
};
let currentSourceMapsClientResponse: ConfigurationSourceMaps = {};

function debounce(fn: (...args) => void, time: number) {
let timeout: NodeJS.Timeout;
Expand All @@ -638,14 +644,17 @@ function createFileWatcher() {
} else if (changes !== null && changes.changedFiles.length > 0) {
output.note({ title: 'Recalculating project graph...' });

const newGraphClientResponse = await createDepGraphClientResponse();
const { projectGraphClientResponse, sourceMapResponse } =
await createProjectGraphAndSourceMapClientResponse();

if (
newGraphClientResponse.hash !== currentDepGraphClientResponse.hash
projectGraphClientResponse.hash !==
currentProjectGraphClientResponse.hash
) {
output.note({ title: 'Graph changes updated.' });

currentDepGraphClientResponse = newGraphClientResponse;
currentProjectGraphClientResponse = projectGraphClientResponse;
currentSourceMapsClientResponse = sourceMapResponse;
} else {
output.note({ title: 'No graph changes found.' });
}
Expand All @@ -654,14 +663,18 @@ function createFileWatcher() {
);
}

async function createDepGraphClientResponse(
async function createProjectGraphAndSourceMapClientResponse(
affected: string[] = []
): Promise<ProjectGraphClientResponse> {
): Promise<{
projectGraphClientResponse: ProjectGraphClientResponse;
sourceMapResponse: ConfigurationSourceMaps;
}> {
performance.mark('project graph watch calculation:start');

let graph = pruneExternalNodes(
await createProjectGraphAsync({ exitOnError: true })
);
const { projectGraph, sourceMaps } =
await createProjectGraphAndSourceMapsAsync({ exitOnError: true });

let graph = pruneExternalNodes(projectGraph);
let fileMap = readFileMapCache().fileMap.projectFileMap;
performance.mark('project graph watch calculation:end');
performance.mark('project graph response generation:start');
Expand Down Expand Up @@ -690,13 +703,16 @@ async function createDepGraphClientResponse(
);

return {
...currentDepGraphClientResponse,
hash,
layout,
projects,
dependencies,
affected,
fileMap,
projectGraphClientResponse: {
...currentProjectGraphClientResponse,
hash,
layout,
projects,
dependencies,
affected,
fileMap,
},
sourceMapResponse: sourceMaps,
};
}

Expand Down Expand Up @@ -882,9 +898,11 @@ async function getExpandedTaskInputs(
if (inputs) {
return expandInputs(
inputs,
currentDepGraphClientResponse.projects.find((p) => p.name === project),
currentProjectGraphClientResponse.projects.find(
(p) => p.name === project
),
allWorkspaceFiles,
currentDepGraphClientResponse
currentProjectGraphClientResponse
);
}
return {};
Expand Down
17 changes: 13 additions & 4 deletions packages/nx/src/daemon/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { Message, SocketMessenger } from './socket-messenger';
import { safelyCleanUpExistingProcess } from '../cache';
import { Hash } from '../../hasher/task-hasher';
import { Task, TaskGraph } from '../../config/task-graph';
import { ConfigurationSourceMaps } from '../../project-graph/utils/project-configuration-utils';

const DAEMON_ENV_SETTINGS = {
...process.env,
Expand Down Expand Up @@ -124,9 +125,17 @@ export class DaemonClient {
return this.sendToDaemonViaQueue({ type: 'REQUEST_SHUTDOWN' });
}

async getProjectGraph(): Promise<ProjectGraph> {
return (await this.sendToDaemonViaQueue({ type: 'REQUEST_PROJECT_GRAPH' }))
.projectGraph;
async getProjectGraphAndSourceMaps(): Promise<{
projectGraph: ProjectGraph;
sourceMaps: ConfigurationSourceMaps;
}> {
const response = await this.sendToDaemonViaQueue({
type: 'REQUEST_PROJECT_GRAPH',
});
return {
projectGraph: response.projectGraph,
sourceMaps: response.sourceMaps,
};
}

async getAllFileData(): Promise<FileData[]> {
Expand Down Expand Up @@ -162,7 +171,7 @@ export class DaemonClient {
} | null
) => void
): Promise<UnregisterCallback> {
await this.getProjectGraph();
await this.getProjectGraphAndSourceMaps();
let messenger: SocketMessenger | undefined;

await this.queue.sendToQueue(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ export async function handleRequestProjectGraph(): Promise<HandlerResult> {

const serializedResult = serializeResult(
result.error,
result.serializedProjectGraph
result.serializedProjectGraph,
result.serializedSourceMaps
);
if (!serializedResult) {
return {
Expand Down

0 comments on commit 5df9c47

Please sign in to comment.