Skip to content

Commit

Permalink
fix(angular): fix executor conversion to angular builder (#14345)
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysoo authored Jan 13, 2023
1 parent 6e6ca5f commit 5c2fbf2
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 8 deletions.
77 changes: 77 additions & 0 deletions e2e/angular-core/src/ng-cli.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import * as isCI from 'is-ci';
import {
checkFilesExist,
getSelectedPackageManager,
packageInstall,
readJson,
runCommand,
runNgNew,
tmpProjPath,
uniq,
updateFile,
} from '@nrwl/e2e/utils';
import { PackageManager } from 'nx/src/utils/package-manager';
import { removeSync } from 'fs-extra';

process.env.SELECTED_CLI = 'angular';

describe('using Nx executors and generators with Angular CLI', () => {
let project: string;
let packageManager: PackageManager;

beforeEach(() => {
project = uniq('proj');
packageManager = getSelectedPackageManager();
runNgNew(project, packageManager);
});

afterEach(() => {
if (isCI) {
try {
removeSync(tmpProjPath());
} catch (e) {}
}
});

it('should convert Nx executors into Angular CLI compatible builders', () => {
// update package.json
const packageJson = readJson('package.json');
packageJson.description = 'some description';
updateFile('package.json', JSON.stringify(packageJson, null, 2));

// use vite to build a library
packageInstall('vite', undefined, 'latest');
packageInstall('@nrwl/vite');
const angularJson = readJson('angular.json');
angularJson.projects[project].architect.build = {
builder: '@nrwl/vite:build',
options: {
configFile: 'vite.config.ts',
outputPath: 'dist',
},
};
updateFile('angular.json', JSON.stringify(angularJson, null, 2));
updateFile(
'vite.config.ts',
`
import { defineConfig } from 'vite';
export default defineConfig({
build: {
lib: {
entry: 'src/main.ts',
name: 'my-app',
fileName: 'main',
formats: ['cjs']
}
}
});
`
);
updateFile(`src/main.ts`, `console.log('Hello World');`);

runCommand(`npx ng build ${project}`);

checkFilesExist(`dist/main.js`);
expect(runCommand(`node dist/main.js`)).toMatch(/Hello World/);
});
});
16 changes: 8 additions & 8 deletions packages/nx/src/config/workspaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ import { joinPathFragments } from '../utils/path';
export function workspaceConfigName(
root: string,
opts?: {
includeProjectsFromAngularJson;
_includeProjectsFromAngularJson?: boolean;
}
): 'angular.json' | 'workspace.json' | null {
if (
existsSync(path.join(root, 'angular.json')) &&
// Include projects from angular.json if explicitly required.
// e.g. when invoked from `packages/devkit/src/utils/convert-nx-executor.ts`
(opts?.includeProjectsFromAngularJson ||
(opts?._includeProjectsFromAngularJson ||
// Or if a workspace has `@nrwl/angular` installed then projects from `angular.json` to be considered by Nx.
isNrwlAngularInstalled())
) {
Expand Down Expand Up @@ -98,13 +98,11 @@ export class Workspaces {
(path) => readJsonFile(join(this.root, path))
);

const workspaceFile = workspaceConfigName(this.root, {
includeProjectsFromAngularJson: opts?._includeProjectsFromAngularJson,
});
const workspaceFile = workspaceConfigName(this.root, opts);

if (workspaceFile) {
workspace.projects = this.mergeWorkspaceJsonAndGlobProjects(
this.readFromWorkspaceJson().projects,
this.readFromWorkspaceJson(opts).projects,
workspace.projects
);
}
Expand Down Expand Up @@ -430,9 +428,11 @@ export class Workspaces {
return this.root ? [this.root, __dirname] : [__dirname];
}

private readFromWorkspaceJson() {
private readFromWorkspaceJson(opts?: {
_includeProjectsFromAngularJson?: boolean;
}) {
const rawWorkspace = readJsonFile(
path.join(this.root, workspaceConfigName(this.root))
path.join(this.root, workspaceConfigName(this.root, opts))
);
return resolveNewFormatWithInlineProjects(rawWorkspace, this.root);
}
Expand Down

1 comment on commit 5c2fbf2

@vercel
Copy link

@vercel vercel bot commented on 5c2fbf2 Jan 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

nx-dev – ./

nx-dev-nrwl.vercel.app
nx-dev-git-master-nrwl.vercel.app
nx-five.vercel.app
nx.dev

Please sign in to comment.