|
| 1 | +import { normalize } from 'path'; |
| 2 | + |
| 3 | +import { updateJsonFile, updateTsConfig } from '../../utils/project'; |
| 4 | +import { expectFileToMatch, writeFile, replaceInFile, prependToFile } from '../../utils/fs'; |
| 5 | +import { ng, exec } from '../../utils/process'; |
| 6 | +import { getGlobalVariable } from '../../utils/env'; |
| 7 | + |
| 8 | +export default function () { |
| 9 | + // Skip this in Appveyor tests. |
| 10 | + if (getGlobalVariable('argv').appveyor) { |
| 11 | + return Promise.resolve(); |
| 12 | + } |
| 13 | + |
| 14 | + return Promise.resolve() |
| 15 | + .then(() => updateJsonFile('.angular-cli.json', configJson => { |
| 16 | + const app = configJson['apps'][0]; |
| 17 | + delete app['polyfills']; |
| 18 | + delete app['styles']; |
| 19 | + app['platform'] = 'server'; |
| 20 | + })) |
| 21 | + .then(() => updateJsonFile('package.json', packageJson => { |
| 22 | + const dependencies = packageJson['dependencies']; |
| 23 | + dependencies['@angular/platform-server'] = '^4.0.0'; |
| 24 | + })) |
| 25 | + .then(() => updateTsConfig(tsConfig => { |
| 26 | + tsConfig['angularCompilerOptions'] = { |
| 27 | + entryModule: 'app/app.module#AppModule' |
| 28 | + }; |
| 29 | + })) |
| 30 | + .then(() => writeFile('./src/main.ts', 'export { AppModule } from \'./app/app.module\';')) |
| 31 | + .then(() => prependToFile('./src/app/app.module.ts', |
| 32 | + 'import { ServerModule } from \'@angular/platform-server\';')) |
| 33 | + .then(() => replaceInFile('./src/app/app.module.ts', /\[\s*BrowserModule/g, |
| 34 | + `[BrowserModule.withServerTransition(\{ appId: 'app' \}), ServerModule`)) |
| 35 | + .then(() => exec(normalize('npm'), 'install', '--silent', '--no-progress')) |
| 36 | + .then(() => ng('build')) |
| 37 | + // files were created successfully |
| 38 | + .then(() => expectFileToMatch('dist/main.bundle.js', |
| 39 | + /__webpack_exports__, "AppModule"/)) |
| 40 | + .then(() => writeFile('./index.js', ` |
| 41 | + require('zone.js/dist/zone-node'); |
| 42 | + require('reflect-metadata'); |
| 43 | + const fs = require('fs'); |
| 44 | + const \{ AppModule \} = require('./dist/main.bundle'); |
| 45 | + const \{ renderModule \} = require('@angular/platform-server'); |
| 46 | +
|
| 47 | + renderModule(AppModule, \{ |
| 48 | + url: '/', |
| 49 | + document: '<app-root></app-root>' |
| 50 | + \}).then(html => \{ |
| 51 | + fs.writeFileSync('dist/index.html', html); |
| 52 | + \}); |
| 53 | + `)) |
| 54 | + .then(() => exec(normalize('node'), 'index.js')) |
| 55 | + .then(() => expectFileToMatch('dist/index.html', |
| 56 | + new RegExp('<h2 _ngcontent-c0="">Here are some links to help you start: </h2>'))) |
| 57 | + .then(() => ng('build', '--aot')) |
| 58 | + // files were created successfully |
| 59 | + .then(() => expectFileToMatch('dist/main.bundle.js', |
| 60 | + /__webpack_exports__, "AppModuleNgFactory"/)) |
| 61 | + .then(() => replaceInFile('./index.js', /AppModule/g, 'AppModuleNgFactory')) |
| 62 | + .then(() => replaceInFile('./index.js', /renderModule/g, 'renderModuleFactory')) |
| 63 | + .then(() => exec(normalize('node'), 'index.js')) |
| 64 | + .then(() => expectFileToMatch('dist/index.html', |
| 65 | + new RegExp('<h2 _ngcontent-c0="">Here are some links to help you start: </h2>'))); |
| 66 | +} |
0 commit comments