Skip to content
Open
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
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
"generate": "tspc -p tsconfig.build.json && node -e \"require('./tools/build.js').zoneWrapExports().catch(err => { console.error(err); process.exit(1); })\"",
"buildd": "tsc -p tsconfig.build.json && node --trace-warnings ./tools/build.js && npm pack ./dist/packages-dist",
"build:jasmine": "npx tsc -p tsconfig.jasmine.json --module es2015 && cp ./dist/out-tsc/jasmine/tools/jasmine.js ./dist/out-tsc/jasmine/tools/jasmine.mjs && npx tsc -p tsconfig.jasmine.json && cp ./dist/packages-dist/schematics/versions.json ./dist/out-tsc/jasmine/src/schematics",
"changelog": "conventional-changelog -p angular -i CHANGELOG.md -s -r 1"
"changelog": "conventional-changelog -p angular -i CHANGELOG.md -s -r 1",
"update-regions": "node ./tools/update-app-hosting-regions.mts"
},
"husky": {
"hooks": {
Expand Down
5 changes: 5 additions & 0 deletions src/schematics/add/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
"$default": {
"$source": "projectName"
}
},
"deploy": {
"type": "string",
"enum": ["apphosting", "hosting", "none"],
"description": "Where ng deploy should deploy the app: Firebase App Hosting (apphosting), Firebase Hosting (hosting), or no deploy setup (none). Asked interactively when omitted."
}
},
"required": []
Expand Down
145 changes: 145 additions & 0 deletions src/schematics/buildOptions.jasmine.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
import {
BuildTargetDefinition,
browserOutputDirectory,
clientSidePage,
productionBuildOptions,
rendersOnServer,
} from './buildOptions.js';
import 'jasmine';

const applicationBuilder = '@angular/build:application';

describe('productionBuildOptions', () => {

it('overlays the production configuration on the build options', () => {
const buildTarget: BuildTargetDefinition = {
builder: applicationBuilder,
options: { outputPath: 'dist/app', index: 'src/index.html' },
configurations: { production: { outputPath: 'dist/production' } },
};
expect(productionBuildOptions(buildTarget)).toEqual({
outputPath: 'dist/production',
index: 'src/index.html',
});
});

it('is empty for a project with no build entry', () => {
expect(productionBuildOptions(undefined)).toEqual({});
});

});

describe('rendersOnServer', () => {

it('is true for the server output mode', () => {
expect(rendersOnServer({ outputMode: 'server', server: 'src/main.server.ts' })).toBeTrue();
});

it('is false for the static output mode, which prerenders every route', () => {
const options = { outputMode: 'static', server: 'src/main.server.ts', ssr: { entry: 'src/server.ts' } };
expect(rendersOnServer(options)).toBeFalse();
});

it('is true without an output mode when the build has ssr and a server entry', () => {
expect(rendersOnServer({ server: 'src/main.server.ts', ssr: { entry: 'src/server.ts' } })).toBeTrue();
});

it('is false without an output mode for a build that only prerenders', () => {
expect(rendersOnServer({ server: 'src/main.server.ts', prerender: true })).toBeFalse();
});

it('is false for a client-rendered build', () => {
expect(rendersOnServer({ browser: 'src/main.ts' })).toBeFalse();
});

});

describe('clientSidePage', () => {

it('is index.html for a client-rendered build', () => {
expect(clientSidePage({ browser: 'src/main.ts' })).toBe('index.html');
});

it('is index.csr.html for the server output mode', () => {
expect(clientSidePage({ outputMode: 'server', server: 'src/main.server.ts' })).toBe('index.csr.html');
});

it('is index.csr.html for the static output mode', () => {
expect(clientSidePage({ outputMode: 'static', server: 'src/main.server.ts' })).toBe('index.csr.html');
});

it('is index.csr.html without an output mode when the build has ssr', () => {
expect(clientSidePage({ server: 'src/main.server.ts', ssr: true })).toBe('index.csr.html');
});

it('is index.csr.html without an output mode when the build prerenders', () => {
expect(clientSidePage({ server: 'src/main.server.ts', prerender: true })).toBe('index.csr.html');
});

it('keeps the file name of a custom index source file, on a server-rendered build too', () => {
const options = { outputMode: 'server', server: 'src/main.server.ts', index: 'src/main.html' };
expect(clientSidePage(options)).toBe('main.html');
});

it('uses the output name of an index option written as an object', () => {
const options = { index: { input: 'src/index.html', output: 'home.html' } };
expect(clientSidePage(options)).toBe('home.html');
});

});

describe('browserOutputDirectory', () => {

it('is dist/<project>/browser for the application builder with no output path', () => {
expect(browserOutputDirectory('my-app', { builder: applicationBuilder })).toBe('dist/my-app/browser');
});

it('treats the application builder of @angular-devkit/build-angular the same way', () => {
const buildTarget: BuildTargetDefinition = { builder: '@angular-devkit/build-angular:application' };
expect(browserOutputDirectory('my-app', buildTarget)).toBe('dist/my-app/browser');
});

it('adds the browser folder to an output path written as text', () => {
const buildTarget: BuildTargetDefinition = { builder: applicationBuilder, options: { outputPath: 'build/' } };
expect(browserOutputDirectory('my-app', buildTarget)).toBe('build/browser');
});

it('joins the base and browser folders of an output path written as an object', () => {
const buildTarget: BuildTargetDefinition = {
builder: applicationBuilder,
options: { outputPath: { base: 'build', browser: 'public' } },
};
expect(browserOutputDirectory('my-app', buildTarget)).toBe('build/public');
});

it('adds the browser folder to an output path object that has only a base folder', () => {
const buildTarget: BuildTargetDefinition = { builder: applicationBuilder, options: { outputPath: { base: 'build' } } };
expect(browserOutputDirectory('my-app', buildTarget)).toBe('build/browser');
});

it('is the base folder when the browser folder is set to an empty name', () => {
const buildTarget: BuildTargetDefinition = {
builder: applicationBuilder,
options: { outputPath: { base: 'build', browser: '' } },
};
expect(browserOutputDirectory('my-app', buildTarget)).toBe('build');
});

it('uses the output path of the production configuration', () => {
const buildTarget: BuildTargetDefinition = {
builder: applicationBuilder,
options: { outputPath: 'build' },
configurations: { production: { outputPath: 'release' } },
};
expect(browserOutputDirectory('my-app', buildTarget)).toBe('release/browser');
});

it('is the output path itself for the older browser builder', () => {
const buildTarget: BuildTargetDefinition = {
builder: '@angular-devkit/build-angular:browser',
options: { outputPath: 'www' },
};
expect(browserOutputDirectory('my-app', buildTarget)).toBe('www');
});

});
88 changes: 88 additions & 0 deletions src/schematics/buildOptions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/**
* @fileoverview What `ng add` and `ng deploy` need to know about how an Angular project builds,
* read from the project's `build` entry in angular.json: whether the build needs a server, where
* its browser files go, and which file is the client-side page. The rules mirror
* `@angular/build`'s application builder.
*/

import { basename } from 'path';
import { WorkspaceProject } from './interfaces';

/** The options of a `build` entry in angular.json, whose values this file checks before using. */
export type BuildOptions = Record<string, unknown>;

/** One entry under a project's `architect` key in angular.json, such as its `build` entry. */
export type BuildTargetDefinition = NonNullable<WorkspaceProject['architect']>[string];

const isRecord = (value: unknown): value is Record<string, unknown> =>
typeof value === 'object' && value !== null;

/** The options `ng deploy` builds with: the `build` target's options overlaid by its `production` configuration. */
export const productionBuildOptions = (buildTarget: BuildTargetDefinition | undefined): BuildOptions => ({
...buildTarget?.options,
...buildTarget?.configurations?.production,
});

/**
* What `ng add` and `ng deploy` tell the user about Firebase Hosting when the build has the
* `localize` option. Such a build writes one folder per locale and no index page above them.
*/
export const localizedBuildHostingWarning =
'This app is built once per locale (the "localize" build option), which puts each locale in ' +
'its own folder with no index.html above them. The Hosting entry in firebase.json serves a ' +
'single index.html, so pages will not load until that entry is changed by hand to serve the ' +
'locale folders.';

/** Whether the build produces a server that renders pages per request, which static hosting cannot run. */
export const rendersOnServer = (options: BuildOptions): boolean => {
if (options.outputMode === 'server') { return true; }
if (options.outputMode === 'static') { return false; }
return !!options.ssr && !!options.server;
};

/**
* The name the build gives its index page, from the `index` build option. That option is either
* the path of the source file, whose own file name is then used, or an object whose `output` is
* the name. Without either, the name is `index.html`.
*/
const indexFileName = (options: BuildOptions): string => {
const { index } = options;
if (typeof index === 'string') { return basename(index); }
if (isRecord(index) && typeof index.output === 'string' && index.output) { return basename(index.output); }
return 'index.html';
};

/**
* The file a static host should serve for a route that has no file of its own, which is the
* build's index page. When the build also prerenders or renders on a server, the application
* builder writes an index page called `index.html` as `index.csr.html` instead. A page with
* another name keeps its name.
*/
export const clientSidePage = (options: BuildOptions): string => {
const fileName = indexFileName(options);
const prerendersOrRendersOnServer = options.outputMode
? !!options.server
: !!options.ssr || !!options.prerender;
return prerendersOrRendersOnServer && fileName === 'index.html' ? 'index.csr.html' : fileName;
};

/** The directory the browser files are built into, relative to the workspace root, with forward slashes. */
export const browserOutputDirectory = (
projectName: string,
buildTarget: BuildTargetDefinition | undefined,
): string => {
const { outputPath } = productionBuildOptions(buildTarget);
const withoutTrailingSlash = (path: string) => path.replace(/\\/g, '/').replace(/\/+$/, '');

/* Only the application builder nests browser files in a subdirectory. The older builders write
* them straight into `outputPath`. */
if (!buildTarget?.builder.endsWith(':application')) {
return withoutTrailingSlash(typeof outputPath === 'string' ? outputPath : `dist/${projectName}`);
}

const base = typeof outputPath === 'string' ? outputPath
: isRecord(outputPath) && typeof outputPath.base === 'string' ? outputPath.base
: `dist/${projectName}`;
const browser = isRecord(outputPath) && typeof outputPath.browser === 'string' ? outputPath.browser : 'browser';
return withoutTrailingSlash(browser ? `${withoutTrailingSlash(base)}/${browser}` : base);
};
Loading
Loading