UNPKG

4.76 kBJavaScriptView Raw
1"use strict";
2/**
3 * @license
4 * Copyright Google Inc. All Rights Reserved.
5 *
6 * Use of this source code is governed by an MIT-style license that can be
7 * found in the LICENSE file at https://angular.io/license
8 */
9Object.defineProperty(exports, "__esModule", { value: true });
10const architect_1 = require("@angular-devkit/architect");
11const node_1 = require("@angular-devkit/architect/node");
12const testing_1 = require("@angular-devkit/architect/testing");
13const core_1 = require("@angular-devkit/core");
14const devkitRoot = core_1.normalize(global._DevKitRoot); // tslint:disable-line:no-any
15exports.workspaceRoot = core_1.join(devkitRoot, 'tests/angular_devkit/build_angular/hello-world-app/');
16exports.host = new testing_1.TestProjectHost(exports.workspaceRoot);
17exports.outputPath = core_1.normalize('dist');
18exports.browserTargetSpec = { project: 'app', target: 'build' };
19exports.devServerTargetSpec = { project: 'app', target: 'serve' };
20exports.extractI18nTargetSpec = { project: 'app', target: 'extract-i18n' };
21exports.karmaTargetSpec = { project: 'app', target: 'test' };
22exports.tslintTargetSpec = { project: 'app', target: 'lint' };
23exports.protractorTargetSpec = { project: 'app-e2e', target: 'e2e' };
24async function createArchitect(workspaceRoot) {
25 const registry = new core_1.schema.CoreSchemaRegistry();
26 registry.addPostTransform(core_1.schema.transforms.addUndefinedDefaults);
27 const workspaceSysPath = core_1.getSystemPath(workspaceRoot);
28 const workspace = await core_1.experimental.workspace.Workspace.fromPath(exports.host, exports.host.root(), registry);
29 const architectHost = new testing_1.TestingArchitectHost(workspaceSysPath, workspaceSysPath, new node_1.WorkspaceNodeModulesArchitectHost(workspace, workspaceSysPath));
30 const architect = new architect_1.Architect(architectHost, registry);
31 return {
32 workspace,
33 architectHost,
34 architect,
35 };
36}
37exports.createArchitect = createArchitect;
38async function browserBuild(architect, host, target, overrides, scheduleOptions) {
39 const run = await architect.scheduleTarget(target, overrides, scheduleOptions);
40 const output = (await run.result);
41 expect(output.success).toBe(true);
42 expect(output.outputPath).not.toBeUndefined();
43 const outputPath = core_1.normalize(output.outputPath);
44 const fileNames = await host.list(outputPath).toPromise();
45 const files = fileNames.reduce((acc, path) => {
46 let cache = null;
47 Object.defineProperty(acc, path, {
48 enumerable: true,
49 get() {
50 if (cache) {
51 return cache;
52 }
53 if (!fileNames.includes(path)) {
54 return Promise.reject('No file named ' + path);
55 }
56 cache = host
57 .read(core_1.join(outputPath, path))
58 .toPromise()
59 .then(content => core_1.virtualFs.fileBufferToString(content));
60 return cache;
61 },
62 });
63 return acc;
64 }, {});
65 await run.stop();
66 return {
67 output,
68 files,
69 };
70}
71exports.browserBuild = browserBuild;
72exports.lazyModuleFiles = {
73 'src/app/lazy/lazy-routing.module.ts': `
74 import { NgModule } from '@angular/core';
75 import { Routes, RouterModule } from '@angular/router';
76
77 const routes: Routes = [];
78
79 @NgModule({
80 imports: [RouterModule.forChild(routes)],
81 exports: [RouterModule]
82 })
83 export class LazyRoutingModule { }
84 `,
85 'src/app/lazy/lazy.module.ts': `
86 import { NgModule } from '@angular/core';
87 import { CommonModule } from '@angular/common';
88
89 import { LazyRoutingModule } from './lazy-routing.module';
90
91 @NgModule({
92 imports: [
93 CommonModule,
94 LazyRoutingModule
95 ],
96 declarations: []
97 })
98 export class LazyModule { }
99 `,
100};
101exports.lazyModuleStringImport = {
102 'src/app/app.module.ts': `
103 import { BrowserModule } from '@angular/platform-browser';
104 import { NgModule } from '@angular/core';
105
106 import { AppComponent } from './app.component';
107 import { RouterModule } from '@angular/router';
108
109 @NgModule({
110 declarations: [
111 AppComponent
112 ],
113 imports: [
114 BrowserModule,
115 RouterModule.forRoot([
116 { path: 'lazy', loadChildren: './lazy/lazy.module#LazyModule' }
117 ])
118 ],
119 providers: [],
120 bootstrap: [AppComponent]
121 })
122 export class AppModule { }
123 `,
124};
125exports.lazyModuleFnImport = {
126 'src/app/app.module.ts': exports.lazyModuleStringImport['src/app/app.module.ts'].replace(`loadChildren: './lazy/lazy.module#LazyModule'`, `loadChildren: () => import('./lazy/lazy.module').then(m => m.LazyModule)`),
127};