Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: decrease activation time #1131

Merged
merged 3 commits into from
Sep 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions apps/vscode/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,12 @@ import {
NxProjectTreeItem,
NxProjectTreeProvider,
} from '@nx-console/vscode/nx-project-view';
import {
getNxConfig,
verifyWorkspace,
WorkspaceCodeLensProvider,
} from '@nx-console/vscode/nx-workspace';
import { environment } from './environments/environment';

import {
WorkspaceJsonSchema,
ProjectJsonSchema,
} from '@nx-console/vscode/json-schema';
import { GeneratorType } from '@nx-console/schema';

let runTargetTreeView: TreeView<RunTargetTreeItem>;
let nxProjectTreeView: TreeView<NxProjectTreeItem>;
Expand All @@ -68,7 +62,7 @@ let cliTaskProvider: CliTaskProvider;
let context: ExtensionContext;
let workspaceFileWatcher: FileSystemWatcher | undefined;

export function activate(c: ExtensionContext) {
export async function activate(c: ExtensionContext) {
try {
const startTime = Date.now();
context = c;
Expand Down Expand Up @@ -131,6 +125,9 @@ export function activate(c: ExtensionContext) {
);

// registers itself as a CodeLensProvider and watches config to dispose/re-register
const { WorkspaceCodeLensProvider } = await import(
'@nx-console/vscode/nx-workspace'
);
new WorkspaceCodeLensProvider(context);
new WorkspaceJsonSchema(context);
new ProjectJsonSchema(context);
Expand Down Expand Up @@ -213,8 +210,9 @@ async function setWorkspace(workspaceJsonPath: string) {
'nxWorkspaceJsonPath',
workspaceJsonPath
);
const { verifyWorkspace } = await import('@nx-console/vscode/nx-workspace');

const { validWorkspaceJson } = verifyWorkspace();
const { validWorkspaceJson } = await verifyWorkspace();
if (!validWorkspaceJson) {
return;
}
Expand Down Expand Up @@ -277,7 +275,8 @@ async function setWorkspace(workspaceJsonPath: string) {
}

async function setApplicationAndLibraryContext(workspaceJsonPath: string) {
const nxConfig = getNxConfig(dirname(workspaceJsonPath));
const { getNxConfig } = await import('@nx-console/vscode/nx-workspace');
const nxConfig = await getNxConfig(dirname(workspaceJsonPath));

commands.executeCommand('setContext', 'nxAppsDir', [
join(
Expand Down
14 changes: 14 additions & 0 deletions apps/vscode/webpack.prod.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module.exports = function transformWebpack(config) {
// the default node webpack config has minimize: false when optimization is set to true
config.output.filename = '[name].js';
config.optimization = {
minimize: true,
concatenateModules: true,
sideEffects: false,
splitChunks: {
chunks: 'all',
},
};

return config;
};
15 changes: 7 additions & 8 deletions libs/server/src/lib/abstract-tree-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import {
} from 'vscode';

export abstract class AbstractTreeProvider<Item extends TreeItem>
implements TreeDataProvider<Item> {
private readonly _onDidChangeTreeData: EventEmitter<
Item | undefined
> = new EventEmitter();
readonly onDidChangeTreeData: Event<Item | undefined> = this
._onDidChangeTreeData.event;
implements TreeDataProvider<Item>
{
private readonly _onDidChangeTreeData: EventEmitter<Item | undefined> =
new EventEmitter();
readonly onDidChangeTreeData: Event<Item | undefined> =
this._onDidChangeTreeData.event;

refresh(): void {
this._onDidChangeTreeData.fire(undefined);
Expand All @@ -22,7 +22,6 @@ export abstract class AbstractTreeProvider<Item extends TreeItem>
return element;
}

abstract getParent(element: Item): Item | null | undefined;

abstract getParent(element: Item): ProviderResult<Item | null | undefined>;
abstract getChildren(element?: Item): ProviderResult<Item[]>;
}
67 changes: 38 additions & 29 deletions libs/vscode/nx-project-view/src/lib/nx-project-tree-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ export class NxProjectTreeProvider extends AbstractTreeProvider<NxProjectTreeIte
});
}

getParent(element: NxProjectTreeItem): NxProjectTreeItem | null | undefined {
async getParent(
element: NxProjectTreeItem
): Promise<NxProjectTreeItem | null | undefined> {
const { project, target } = element.nxProject;

if (target) {
Expand All @@ -55,7 +57,7 @@ export class NxProjectTreeProvider extends AbstractTreeProvider<NxProjectTreeIte
}
}

createNxProjectTreeItem(
async createNxProjectTreeItem(
workspaceJsonLabel: NxProject,
treeItemLabel: string,
hasChildren?: boolean
Expand All @@ -68,8 +70,9 @@ export class NxProjectTreeProvider extends AbstractTreeProvider<NxProjectTreeIte
: TreeItemCollapsibleState.None
);
if (!workspaceJsonLabel.target) {
const projectDef =
this.cliTaskProvider.getProjects()[workspaceJsonLabel.project];
const projectDef = (await this.cliTaskProvider.getProjects())[
workspaceJsonLabel.project
];
if (projectDef) {
item.resourceUri = Uri.file(
join(this.cliTaskProvider.getWorkspacePath(), projectDef.root)
Expand All @@ -83,36 +86,42 @@ export class NxProjectTreeProvider extends AbstractTreeProvider<NxProjectTreeIte
return item;
}

getChildren(parent?: NxProjectTreeItem): ProviderResult<NxProjectTreeItem[]> {
async getChildren(
parent?: NxProjectTreeItem
): Promise<NxProjectTreeItem[] | undefined> {
if (!parent) {
const projects = this.cliTaskProvider.getProjectEntries();
return projects.map(
([name, def]): NxProjectTreeItem =>
this.createNxProjectTreeItem(
{ project: name },
name,
Boolean(def.targets)
)
const projects = await this.cliTaskProvider.getProjectEntries();
return Promise.all(
projects.map(
async ([name, def]): Promise<NxProjectTreeItem> =>
this.createNxProjectTreeItem(
{ project: name },
name,
Boolean(def.targets)
)
)
);
}

const { nxProject } = parent;
const { target, project } = nxProject;
const projectDef = this.cliTaskProvider.getProjects()[project];
const projectDef = (await this.cliTaskProvider.getProjects())[project];

if (!projectDef) {
return;
}

if (!target) {
if (projectDef.targets) {
return Object.keys(projectDef.targets).map(
(name): NxProjectTreeItem =>
this.createNxProjectTreeItem(
{ target: { name }, project },
name,
Boolean(projectDef.targets?.[name].configurations)
)
return Promise.all(
Object.keys(projectDef.targets).map(
async (name): Promise<NxProjectTreeItem> =>
this.createNxProjectTreeItem(
{ target: { name }, project },
name,
Boolean(projectDef.targets?.[name].configurations)
)
)
);
}
} else {
Expand All @@ -129,14 +138,14 @@ export class NxProjectTreeProvider extends AbstractTreeProvider<NxProjectTreeIte
return;
}

return Object.keys(configurations).map((name) => {
const item = this.createNxProjectTreeItem(
{ target: { ...target, configuration: name }, project },
name
);

return item;
});
return Promise.all(
Object.keys(configurations).map(async (name) =>
this.createNxProjectTreeItem(
{ target: { ...target, configuration: name }, project },
name
)
)
);
}
}

Expand Down
6 changes: 4 additions & 2 deletions libs/vscode/nx-workspace/src/lib/get-nx-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ import { NxJsonConfiguration } from '@nrwl/devkit';
import { join } from 'path';
import { getNxWorkspacePackageFileUtils } from './get-nx-workspace-package';

export function getNxConfig(baseDir: string): NxJsonConfiguration {
export async function getNxConfig(
baseDir: string
): Promise<NxJsonConfiguration> {
try {
let cachedNxJson = cacheJson('nx.json', baseDir).json;

if (!cachedNxJson) {
const nxJson = getNxWorkspacePackageFileUtils().readNxJson(
const nxJson = (await getNxWorkspacePackageFileUtils()).readNxJson(
join(baseDir, 'nx.json')
);

Expand Down
10 changes: 6 additions & 4 deletions libs/vscode/nx-workspace/src/lib/get-nx-workspace-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@ import { readAndCacheJsonFile, cacheJson } from '@nx-console/server';
import { getNxWorkspacePackageFileUtils } from './get-nx-workspace-package';
import type { WorkspaceJsonConfiguration } from '@nrwl/devkit';

export function getNxWorkspaceConfig(
export async function getNxWorkspaceConfig(
basedir: string,
workspaceJsonPath: string
): WorkspaceJsonConfiguration {
): Promise<WorkspaceJsonConfiguration> {
// try and use the workspace version of nx
try {
let cachedWorkspaceJson = cacheJson(workspaceJsonPath).json;
if (!cachedWorkspaceJson) {
const workspace = getNxWorkspacePackageFileUtils().readWorkspaceConfig({
const workspace = (
await getNxWorkspacePackageFileUtils()
).readWorkspaceConfig({
format: 'nx',
path: basedir,
} as any);
});
cachedWorkspaceJson = cacheJson(workspaceJsonPath, '', workspace).json;
}
return cachedWorkspaceJson;
Expand Down
39 changes: 25 additions & 14 deletions libs/vscode/nx-workspace/src/lib/get-nx-workspace-package.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,35 @@
import { WorkspaceConfigurationStore } from '@nx-console/vscode/configuration';
import { dirname, join } from 'path';
import type * as NxWorkspacePackage from '@nrwl/workspace';
import * as NxWorkspaceFileUtils from '@nrwl/workspace/src/core/file-utils';
import { getOutputChannel } from '@nx-console/server';

/**
* Get the local installed version of @nrwl/workspace
*/
export function getNxWorkspacePackageFileUtils(): typeof NxWorkspacePackage {
export async function getNxWorkspacePackageFileUtils(): Promise<
typeof NxWorkspaceFileUtils
> {
const workspacePath = dirname(
WorkspaceConfigurationStore.instance.get('nxWorkspaceJsonPath', '')
);

// webpack hacks..
return eval('require')(
join(
workspacePath,
'node_modules',
'@nrwl',
'workspace',
'src',
'core',
'file-utils'
)
);
try {
return import(
/*webpackIgnore: true*/
join(
workspacePath,
'node_modules',
'@nrwl',
'workspace',
'src',
'core',
'file-utils'
)
);
} catch (err) {
getOutputChannel().appendLine(
`Error loading @nrwl/workspace from workspace. Falling back to extension dependency`
);
return NxWorkspaceFileUtils;
}
}
8 changes: 4 additions & 4 deletions libs/vscode/nx-workspace/src/lib/verify-workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import { WorkspaceConfigurationStore } from '@nx-console/vscode/configuration';
import { WorkspaceJsonConfiguration } from '@nrwl/devkit';
import { getNxWorkspaceConfig } from './get-nx-workspace-config';

export function verifyWorkspace(): {
export async function verifyWorkspace(): Promise<{
validWorkspaceJson: boolean;
json: WorkspaceJsonConfiguration;
workspaceType: 'ng' | 'nx';
configurationFilePath: string;
} {
}> {
const workspacePath = dirname(
WorkspaceConfigurationStore.instance.get('nxWorkspaceJsonPath', '')
);
Expand All @@ -29,7 +29,7 @@ export function verifyWorkspace(): {
validWorkspaceJson: true,
// TODO(cammisuli): change all instances to use the new version - basically reverse this to the new format
json: toWorkspaceFormat(
getNxWorkspaceConfig(workspacePath, workspaceJsonPath)
await getNxWorkspaceConfig(workspacePath, workspaceJsonPath)
),
workspaceType: 'nx',
configurationFilePath: workspaceJsonPath,
Expand All @@ -38,7 +38,7 @@ export function verifyWorkspace(): {
return {
validWorkspaceJson: true,
json: toWorkspaceFormat(
getNxWorkspaceConfig(workspacePath, angularJsonPath)
await getNxWorkspaceConfig(workspacePath, angularJsonPath)
),
workspaceType: 'ng',
configurationFilePath: angularJsonPath,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ export class WorkspaceCodeLensProvider implements CodeLensProvider {
* @param document a document matched by the pattern passed to registerCodeLensProvider
* @returns ProjectCodeLens Range locations and properties for the document
*/
provideCodeLenses(document: TextDocument): CodeLens[] | undefined {
async provideCodeLenses(
document: TextDocument
): Promise<CodeLens[] | undefined> {
const lens: CodeLens[] = [];

let projectName = '';
Expand All @@ -71,7 +73,7 @@ export class WorkspaceCodeLensProvider implements CodeLensProvider {
}

const projectLocations = getProjectLocations(document, projectName);
const { validWorkspaceJson, workspaceType } = verifyWorkspace();
const { validWorkspaceJson, workspaceType } = await verifyWorkspace();
if (!validWorkspaceJson) {
return;
}
Expand Down Expand Up @@ -163,9 +165,10 @@ export class WorkspaceCodeLensProvider implements CodeLensProvider {
GlobalConfigurationStore.configurationSection
);
if (affectsNxConsoleConfig) {
const enableWorkspaceConfigCodeLens = GlobalConfigurationStore.instance.get(
'enableWorkspaceConfigCodeLens'
);
const enableWorkspaceConfigCodeLens =
GlobalConfigurationStore.instance.get(
'enableWorkspaceConfigCodeLens'
);
if (enableWorkspaceConfigCodeLens && !this.codeLensProvider) {
this.registerWorkspaceCodeLensProvider(this.context);
} else if (!enableWorkspaceConfigCodeLens && this.codeLensProvider) {
Expand Down
Loading