|
| 1 | +import { oneLine, stripIndent } from 'common-tags'; |
| 2 | +import { createTypescriptContext, transformTypescript } from './ast_helpers'; |
| 3 | +import { replaceServerBootstrap } from './replace_server_bootstrap'; |
| 4 | + |
| 5 | +describe('@ngtools/webpack transformers', () => { |
| 6 | + describe('replace_server_bootstrap', () => { |
| 7 | + it('should replace bootstrap', () => { |
| 8 | + const input = stripIndent` |
| 9 | + import { enableProdMode } from '@angular/core'; |
| 10 | + import { platformDynamicServer } from '@angular/platform-server'; |
| 11 | +
|
| 12 | + import { AppModule } from './app/app.module'; |
| 13 | + import { environment } from './environments/environment'; |
| 14 | +
|
| 15 | + if (environment.production) { |
| 16 | + enableProdMode(); |
| 17 | + } |
| 18 | +
|
| 19 | + platformDynamicServer().bootstrapModule(AppModule); |
| 20 | + `; |
| 21 | + |
| 22 | + // tslint:disable:max-line-length |
| 23 | + const output = stripIndent` |
| 24 | + import { enableProdMode } from '@angular/core'; |
| 25 | + import { environment } from './environments/environment'; |
| 26 | +
|
| 27 | + import * as __NgCli_bootstrap_1 from "./app/app.module.ngfactory"; |
| 28 | + import * as __NgCli_bootstrap_2 from "@angular/platform-server"; |
| 29 | +
|
| 30 | + if (environment.production) { |
| 31 | + enableProdMode(); |
| 32 | + } |
| 33 | + __NgCli_bootstrap_2.platformServer().bootstrapModuleFactory(__NgCli_bootstrap_1.AppModuleNgFactory); |
| 34 | + `; |
| 35 | + // tslint:enable:max-line-length |
| 36 | + |
| 37 | + const { program, compilerHost } = createTypescriptContext(input); |
| 38 | + const transformer = replaceServerBootstrap( |
| 39 | + () => true, |
| 40 | + () => ({ path: '/project/src/app/app.module', className: 'AppModule' }), |
| 41 | + () => program.getTypeChecker(), |
| 42 | + ); |
| 43 | + const result = transformTypescript(undefined, [transformer], program, compilerHost); |
| 44 | + |
| 45 | + expect(oneLine`${result}`).toEqual(oneLine`${output}`); |
| 46 | + }); |
| 47 | + |
| 48 | + it('should replace renderModule', () => { |
| 49 | + const input = stripIndent` |
| 50 | + import { enableProdMode } from '@angular/core'; |
| 51 | + import { renderModule } from '@angular/platform-server'; |
| 52 | +
|
| 53 | + import { AppModule } from './app/app.module'; |
| 54 | + import { environment } from './environments/environment'; |
| 55 | +
|
| 56 | + if (environment.production) { |
| 57 | + enableProdMode(); |
| 58 | + } |
| 59 | +
|
| 60 | + renderModule(AppModule, { |
| 61 | + document: '<app-root></app-root>', |
| 62 | + url: '/' |
| 63 | + }); |
| 64 | + `; |
| 65 | + |
| 66 | + // tslint:disable:max-line-length |
| 67 | + const output = stripIndent` |
| 68 | + import { enableProdMode } from '@angular/core'; |
| 69 | + import { environment } from './environments/environment'; |
| 70 | +
|
| 71 | + import * as __NgCli_bootstrap_1 from "./app/app.module.ngfactory"; |
| 72 | + import * as __NgCli_bootstrap_2 from "@angular/platform-server"; |
| 73 | +
|
| 74 | + if (environment.production) { |
| 75 | + enableProdMode(); |
| 76 | + } |
| 77 | + __NgCli_bootstrap_2.renderModuleFactory(__NgCli_bootstrap_1.AppModuleNgFactory, { |
| 78 | + document: '<app-root></app-root>', |
| 79 | + url: '/' |
| 80 | + }); |
| 81 | + `; |
| 82 | + // tslint:enable:max-line-length |
| 83 | + |
| 84 | + const { program, compilerHost } = createTypescriptContext(input); |
| 85 | + const transformer = replaceServerBootstrap( |
| 86 | + () => true, |
| 87 | + () => ({ path: '/project/src/app/app.module', className: 'AppModule' }), |
| 88 | + () => program.getTypeChecker(), |
| 89 | + ); |
| 90 | + const result = transformTypescript(undefined, [transformer], program, compilerHost); |
| 91 | + |
| 92 | + expect(oneLine`${result}`).toEqual(oneLine`${output}`); |
| 93 | + }); |
| 94 | + |
| 95 | + it('should replace when the module is used in a config object', () => { |
| 96 | + const input = stripIndent` |
| 97 | + import * as express from 'express'; |
| 98 | +
|
| 99 | + import { enableProdMode } from '@angular/core'; |
| 100 | + import { ngExpressEngine } from '@nguniversal/express-engine'; |
| 101 | +
|
| 102 | + import { AppModule } from './app/app.module'; |
| 103 | + import { environment } from './environments/environment'; |
| 104 | +
|
| 105 | + if (environment.production) { |
| 106 | + enableProdMode(); |
| 107 | + } |
| 108 | +
|
| 109 | + const server = express(); |
| 110 | + server.engine('html', ngExpressEngine({ |
| 111 | + bootstrap: AppModule |
| 112 | + })); |
| 113 | + `; |
| 114 | + |
| 115 | + // tslint:disable:max-line-length |
| 116 | + const output = stripIndent` |
| 117 | + import * as express from 'express'; |
| 118 | +
|
| 119 | + import { enableProdMode } from '@angular/core'; |
| 120 | + import { ngExpressEngine } from '@nguniversal/express-engine'; |
| 121 | +
|
| 122 | + import { environment } from './environments/environment'; |
| 123 | +
|
| 124 | + import * as __NgCli_bootstrap_1 from "./app/app.module.ngfactory"; |
| 125 | +
|
| 126 | + if (environment.production) { |
| 127 | + enableProdMode(); |
| 128 | + } |
| 129 | +
|
| 130 | + const server = express(); |
| 131 | + server.engine('html', ngExpressEngine({ |
| 132 | + bootstrap: __NgCli_bootstrap_1.AppModuleNgFactory |
| 133 | + })); |
| 134 | + `; |
| 135 | + // tslint:enable:max-line-length |
| 136 | + |
| 137 | + const { program, compilerHost } = createTypescriptContext(input); |
| 138 | + const transformer = replaceServerBootstrap( |
| 139 | + () => true, |
| 140 | + () => ({ path: '/project/src/app/app.module', className: 'AppModule' }), |
| 141 | + () => program.getTypeChecker(), |
| 142 | + ); |
| 143 | + const result = transformTypescript(undefined, [transformer], program, compilerHost); |
| 144 | + |
| 145 | + expect(oneLine`${result}`).toEqual(oneLine`${output}`); |
| 146 | + }); |
| 147 | + |
| 148 | + it('should replace bootstrap when barrel files are used', () => { |
| 149 | + const input = stripIndent` |
| 150 | + import { enableProdMode } from '@angular/core'; |
| 151 | + import { platformDynamicServer } from '@angular/platform-browser-dynamic'; |
| 152 | +
|
| 153 | + import { AppModule } from './app'; |
| 154 | + import { environment } from './environments/environment'; |
| 155 | +
|
| 156 | + if (environment.production) { |
| 157 | + enableProdMode(); |
| 158 | + } |
| 159 | +
|
| 160 | + platformDynamicServer().bootstrapModule(AppModule); |
| 161 | + `; |
| 162 | + |
| 163 | + // tslint:disable:max-line-length |
| 164 | + const output = stripIndent` |
| 165 | + import { enableProdMode } from '@angular/core'; |
| 166 | + import { environment } from './environments/environment'; |
| 167 | +
|
| 168 | + import * as __NgCli_bootstrap_1 from "./app/app.module.ngfactory"; |
| 169 | + import * as __NgCli_bootstrap_2 from "@angular/platform-server"; |
| 170 | +
|
| 171 | + if (environment.production) { |
| 172 | + enableProdMode(); |
| 173 | + } |
| 174 | + __NgCli_bootstrap_2.platformServer().bootstrapModuleFactory(__NgCli_bootstrap_1.AppModuleNgFactory); |
| 175 | + `; |
| 176 | + // tslint:enable:max-line-length |
| 177 | + |
| 178 | + const { program, compilerHost } = createTypescriptContext(input); |
| 179 | + const transformer = replaceServerBootstrap( |
| 180 | + () => true, |
| 181 | + () => ({ path: '/project/src/app/app.module', className: 'AppModule' }), |
| 182 | + () => program.getTypeChecker(), |
| 183 | + ); |
| 184 | + const result = transformTypescript(undefined, [transformer], program, compilerHost); |
| 185 | + |
| 186 | + expect(oneLine`${result}`).toEqual(oneLine`${output}`); |
| 187 | + }); |
| 188 | + |
| 189 | + it('should not replace bootstrap when there is no entry module', () => { |
| 190 | + const input = stripIndent` |
| 191 | + import { enableProdMode } from '@angular/core'; |
| 192 | + import { platformDynamicServer } from '@angular/platform-browser-dynamic'; |
| 193 | +
|
| 194 | + import { AppModule } from './app/app.module'; |
| 195 | + import { environment } from './environments/environment'; |
| 196 | +
|
| 197 | + if (environment.production) { |
| 198 | + enableProdMode(); |
| 199 | + } |
| 200 | +
|
| 201 | + platformDynamicServer().bootstrapModule(AppModule); |
| 202 | + `; |
| 203 | + |
| 204 | + const { program, compilerHost } = createTypescriptContext(input); |
| 205 | + const transformer = replaceServerBootstrap( |
| 206 | + () => true, |
| 207 | + () => undefined, |
| 208 | + () => program.getTypeChecker(), |
| 209 | + ); |
| 210 | + const result = transformTypescript(undefined, [transformer], program, compilerHost); |
| 211 | + |
| 212 | + expect(oneLine`${result}`).toEqual(oneLine`${input}`); |
| 213 | + }); |
| 214 | + }); |
| 215 | +}); |
0 commit comments