Skip to content

Commit

Permalink
feat(angular): generate public directory instead of assets (#22801)
Browse files Browse the repository at this point in the history
  • Loading branch information
leosvelperez committed May 10, 2024
1 parent 57aa0d0 commit 1408865
Show file tree
Hide file tree
Showing 30 changed files with 218 additions and 112 deletions.
6 changes: 4 additions & 2 deletions docs/generated/packages/angular/executors/application.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,12 @@
},
"output": {
"type": "string",
"default": "",
"description": "Absolute path within the output."
}
},
"additionalProperties": false,
"required": ["glob", "input", "output"]
"required": ["glob", "input"]
},
{ "type": "string" }
]
Expand Down Expand Up @@ -663,11 +664,12 @@
},
"output": {
"type": "string",
"default": "",
"description": "Absolute path within the output."
}
},
"additionalProperties": false,
"required": ["glob", "input", "output"]
"required": ["glob", "input"]
},
{ "type": "string" }
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,12 @@
},
"output": {
"type": "string",
"default": "",
"description": "Absolute path within the output."
}
},
"additionalProperties": false,
"required": ["glob", "input", "output"]
"required": ["glob", "input"]
},
{ "type": "string" }
]
Expand Down Expand Up @@ -572,11 +573,12 @@
},
"output": {
"type": "string",
"default": "",
"description": "Absolute path within the output."
}
},
"additionalProperties": false,
"required": ["glob", "input", "output"]
"required": ["glob", "input"]
},
{ "type": "string" }
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,12 @@
},
"output": {
"type": "string",
"default": "",
"description": "Absolute path within the output."
}
},
"additionalProperties": false,
"required": ["glob", "input", "output"]
"required": ["glob", "input"]
},
{ "type": "string" }
]
Expand Down Expand Up @@ -592,11 +593,12 @@
},
"output": {
"type": "string",
"default": "",
"description": "Absolute path within the output."
}
},
"additionalProperties": false,
"required": ["glob", "input", "output"]
"required": ["glob", "input"]
},
{ "type": "string" }
]
Expand Down
6 changes: 4 additions & 2 deletions docs/generated/packages/angular/executors/webpack-server.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,12 @@
},
"output": {
"type": "string",
"default": "",
"description": "Absolute path within the output."
}
},
"additionalProperties": false,
"required": ["glob", "input", "output"]
"required": ["glob", "input"]
},
{ "type": "string" }
]
Expand Down Expand Up @@ -323,11 +324,12 @@
},
"output": {
"type": "string",
"default": "",
"description": "Absolute path within the output."
}
},
"additionalProperties": false,
"required": ["glob", "input", "output"]
"required": ["glob", "input"]
},
{ "type": "string" }
]
Expand Down
3 changes: 1 addition & 2 deletions e2e/angular/src/misc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ describe('Move Angular Project', () => {
expect(moveOutput).toContain(`CREATE ${newPath}/tsconfig.json`);
expect(moveOutput).toContain(`CREATE ${newPath}/tsconfig.spec.json`);
expect(moveOutput).toContain(`CREATE ${newPath}/.eslintrc.json`);
expect(moveOutput).toContain(`CREATE ${newPath}/src/favicon.ico`);
expect(moveOutput).toContain(`CREATE ${newPath}/public/favicon.ico`);
expect(moveOutput).toContain(`CREATE ${newPath}/src/index.html`);
expect(moveOutput).toContain(`CREATE ${newPath}/src/main.ts`);
expect(moveOutput).toContain(`CREATE ${newPath}/src/styles.css`);
Expand All @@ -52,7 +52,6 @@ describe('Move Angular Project', () => {
);
expect(moveOutput).toContain(`CREATE ${newPath}/src/app/app.component.ts`);
expect(moveOutput).toContain(`CREATE ${newPath}/src/app/app.config.ts`);
expect(moveOutput).toContain(`CREATE ${newPath}/src/assets/.gitkeep`);
});

/**
Expand Down
10 changes: 2 additions & 8 deletions e2e/angular/src/ng-add.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,7 @@ describe('convert Angular CLI workspace to an Nx workspace', () => {
browser: `apps/${project}/src/main.ts`,
polyfills: [`zone.js`],
tsConfig: `apps/${project}/tsconfig.app.json`,
assets: [
`apps/${project}/src/favicon.ico`,
`apps/${project}/src/assets`,
],
assets: [{ glob: '**/*', input: `apps/${project}/public` }],
styles: [`apps/${project}/src/styles.css`],
scripts: [`apps/${project}/src/scripts.js`],
},
Expand Down Expand Up @@ -230,10 +227,7 @@ describe('convert Angular CLI workspace to an Nx workspace', () => {
options: {
polyfills: [`zone.js`, `zone.js/testing`],
tsConfig: `apps/${project}/tsconfig.spec.json`,
assets: [
`apps/${project}/src/favicon.ico`,
`apps/${project}/src/assets`,
],
assets: [{ glob: '**/*', input: `apps/${project}/public` }],
styles: [`apps/${project}/src/styles.css`],
scripts: [`apps/${project}/src/scripts.js`],
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { from } from 'rxjs';
import { switchMap, tap } from 'rxjs/operators';
import { getInstalledAngularVersionInfo } from '../../executors/utilities/angular-version-utils';
import {
getDynamicMfManifestFile,
getDynamicRemotes,
getStaticRemotes,
validateDevRemotes,
Expand All @@ -29,11 +30,7 @@ export function executeModuleFederationDevSSRBuilder(
readProjectsConfigurationFromProjectGraph(projectGraph);
const project = workspaceProjects[context.target.project];

let pathToManifestFile = join(
context.workspaceRoot,
project.sourceRoot,
'assets/module-federation.manifest.json'
);
let pathToManifestFile: string;
if (options.pathToManifestFile) {
const userPathToManifestFile = join(
context.workspaceRoot,
Expand All @@ -50,6 +47,11 @@ export function executeModuleFederationDevSSRBuilder(
}

pathToManifestFile = userPathToManifestFile;
} else {
pathToManifestFile = getDynamicMfManifestFile(
project,
context.workspaceRoot
);
}

validateDevRemotes(options, workspaceProjects);
Expand Down
35 changes: 28 additions & 7 deletions packages/angular/src/builders/utilities/module-federation.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { basename, dirname, join } from 'path';
import { join } from 'path';
import { existsSync, readFileSync } from 'fs';
import { logger, ProjectConfiguration } from '@nx/devkit';
import { registerTsProject } from '@nx/js/src/internal';
Expand All @@ -8,17 +8,18 @@ export function getDynamicRemotes(
context: import('@angular-devkit/architect').BuilderContext,
workspaceProjects: Record<string, ProjectConfiguration>,
remotesToSkip: Set<string>,
pathToManifestFile = join(
context.workspaceRoot,
project.sourceRoot,
'assets/module-federation.manifest.json'
)
pathToManifestFile: string | undefined
): string[] {
pathToManifestFile ??= getDynamicMfManifestFile(
project,
context.workspaceRoot
);

// check for dynamic remotes
// we should only check for dynamic based on what we generate
// and fallback to empty array

if (!existsSync(pathToManifestFile)) {
if (!pathToManifestFile || !existsSync(pathToManifestFile)) {
return [];
}

Expand Down Expand Up @@ -182,3 +183,23 @@ export function validateDevRemotes(
);
}
}

export function getDynamicMfManifestFile(
project: ProjectConfiguration,
workspaceRoot: string
): string | undefined {
// {sourceRoot}/assets/module-federation.manifest.json was the generated
// path for the manifest file in the past. We now generate the manifest
// file at {root}/public/module-federation.manifest.json. This check
// ensures that we can still support the old path for backwards
// compatibility since old projects may still have the manifest file
// at the old path.
return [
join(workspaceRoot, project.root, 'public/module-federation.manifest.json'),
join(
workspaceRoot,
project.sourceRoot,
'assets/module-federation.manifest.json'
),
].find((path) => existsSync(path));
}
3 changes: 2 additions & 1 deletion packages/angular/src/builders/webpack-browser/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -498,11 +498,12 @@
},
"output": {
"type": "string",
"default": "",
"description": "Absolute path within the output."
}
},
"additionalProperties": false,
"required": ["glob", "input", "output"]
"required": ["glob", "input"]
},
{
"type": "string"
Expand Down
3 changes: 2 additions & 1 deletion packages/angular/src/builders/webpack-server/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -271,11 +271,12 @@
},
"output": {
"type": "string",
"default": "",
"description": "Absolute path within the output."
}
},
"additionalProperties": false,
"required": ["glob", "input", "output"]
"required": ["glob", "input"]
},
{
"type": "string"
Expand Down
3 changes: 2 additions & 1 deletion packages/angular/src/executors/application/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -600,11 +600,12 @@
},
"output": {
"type": "string",
"default": "",
"description": "Absolute path within the output."
}
},
"additionalProperties": false,
"required": ["glob", "input", "output"]
"required": ["glob", "input"]
},
{
"type": "string"
Expand Down
3 changes: 2 additions & 1 deletion packages/angular/src/executors/browser-esbuild/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -502,11 +502,12 @@
},
"output": {
"type": "string",
"default": "",
"description": "Absolute path within the output."
}
},
"additionalProperties": false,
"required": ["glob", "input", "output"]
"required": ["glob", "input"]
},
{
"type": "string"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ import { waitForPortOpen } from '@nx/web/src/utils/wait-for-port-open';
import fileServerExecutor from '@nx/web/src/executors/file-server/file-server.impl';
import { createBuilderContext } from 'nx/src/adapter/ngcli-adapter';
import { executeDevServerBuilder } from '../../builders/dev-server/dev-server.impl';
import { validateDevRemotes } from '../../builders/utilities/module-federation';
import {
getDynamicMfManifestFile,
validateDevRemotes,
} from '../../builders/utilities/module-federation';
import { extname, join } from 'path';
import { existsSync } from 'fs';

Expand Down Expand Up @@ -74,12 +77,10 @@ export async function* moduleFederationDevServerExecutor(
return yield* currIter;
}

let pathToManifestFile = join(
context.root,
project.sourceRoot,
'assets/module-federation.manifest.json'
);
if (options.pathToManifestFile) {
let pathToManifestFile: string;
if (!options.pathToManifestFile) {
pathToManifestFile = getDynamicMfManifestFile(project, context.root);
} else {
const userPathToManifestFile = join(
context.root,
options.pathToManifestFile
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,10 @@ exports[`app --project-name-and-root-format=derived should generate correctly wh
"executor": "@angular-devkit/build-angular:application",
"options": {
"assets": [
"apps/my-dir/my-app/src/favicon.ico",
"apps/my-dir/my-app/src/assets",
{
"glob": "**/*",
"input": "apps/my-dir/my-app/public",
},
],
"browser": "apps/my-dir/my-app/src/main.ts",
"index": "apps/my-dir/my-app/src/index.html",
Expand Down Expand Up @@ -444,8 +446,10 @@ exports[`app --project-name-and-root-format=derived should generate correctly wh
"executor": "@angular-devkit/build-angular:application",
"options": {
"assets": [
"apps/my-app/src/favicon.ico",
"apps/my-app/src/assets",
{
"glob": "**/*",
"input": "apps/my-app/public",
},
],
"browser": "apps/my-app/src/main.ts",
"index": "apps/my-app/src/index.html",
Expand Down Expand Up @@ -830,7 +834,7 @@ exports[`app --strict should enable strict type checking: e2e tsconfig.json 1`]
}
`;
exports[`app angular v15 support should import "ApplicationConfig" from "@angular/platform-browser" 1`] = `
exports[`app angular compat support should import "ApplicationConfig" from "@angular/platform-browser" 1`] = `
"import { ApplicationConfig } from '@angular/platform-browser';
import { provideRouter } from '@angular/router';
import { appRoutes } from './app.routes';
Expand Down Expand Up @@ -951,8 +955,10 @@ exports[`app nested should create project configs 1`] = `
"executor": "@angular-devkit/build-angular:application",
"options": {
"assets": [
"my-dir/my-app/src/favicon.ico",
"my-dir/my-app/src/assets",
{
"glob": "**/*",
"input": "my-dir/my-app/public",
},
],
"browser": "my-dir/my-app/src/main.ts",
"index": "my-dir/my-app/src/index.html",
Expand Down Expand Up @@ -1065,8 +1071,10 @@ exports[`app not nested should create project configs 1`] = `
"executor": "@angular-devkit/build-angular:application",
"options": {
"assets": [
"my-app/src/favicon.ico",
"my-app/src/assets",
{
"glob": "**/*",
"input": "my-app/public",
},
],
"browser": "my-app/src/main.ts",
"index": "my-app/src/index.html",
Expand Down
Loading

0 comments on commit 1408865

Please sign in to comment.