Skip to content

Commit

Permalink
fix: make prerender plugin work against compilationAssets version of …
Browse files Browse the repository at this point in the history
…index.html
  • Loading branch information
jeffbcross committed Oct 4, 2016
1 parent 6652ac0 commit 7f109ba
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 41 deletions.
8 changes: 6 additions & 2 deletions packages/angular-cli/models/webpack-build-mobile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import * as path from 'path';
const OfflinePlugin = require('offline-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
import { PrerenderWebpackPlugin } from '../utilities/prerender-webpack-plugin';
import {BaseHrefWebpackPlugin} from '@angular-cli/base-href-webpack';

export const getWebpackMobileConfigPartial = function (projectRoot: string, appConfig: any) {
export const getWebpackMobileConfigPartial = function (projectRoot: string, appConfig: any, baseHref: string) {
// Hardcoded files and paths here should be part of appConfig when
// reworking the mobile app functionality
return {
Expand All @@ -17,6 +18,9 @@ export const getWebpackMobileConfigPartial = function (projectRoot: string, appC
to: path.resolve(projectRoot, appConfig.outDir)
}
]),
new BaseHrefWebpackPlugin({
baseHref: baseHref
}),
new PrerenderWebpackPlugin({
templatePath: path.resolve(projectRoot, appConfig.root, 'index.html'),
configPath: path.resolve(projectRoot, appConfig.root, 'main-app-shell.ts'),
Expand All @@ -26,7 +30,7 @@ export const getWebpackMobileConfigPartial = function (projectRoot: string, appC
};
};

export const getWebpackMobileProdConfigPartial = function (projectRoot: string, appConfig: any) {
export const getWebpackMobileProdConfigPartial = function (projectRoot: string, appConfig: any, baseHref: string) {
return {
entry: {
'sw-install': path.resolve(__dirname, '../utilities/sw-install.js')
Expand Down
4 changes: 2 additions & 2 deletions packages/angular-cli/models/webpack-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ export class NgCliWebpackConfig {
: getWebpackNonAotConfigPartial(this.ngCliProject.root, appConfig);

if (appConfig.mobile) {
let mobileConfigPartial = getWebpackMobileConfigPartial(this.ngCliProject.root, appConfig);
let mobileConfigPartial = getWebpackMobileConfigPartial(this.ngCliProject.root, appConfig, baseHref);
let mobileProdConfigPartial = getWebpackMobileProdConfigPartial(this.ngCliProject.root,
appConfig);
appConfig, baseHref);
baseConfig = webpackMerge(baseConfig, mobileConfigPartial);
if (this.target == 'production') {
targetConfigPartial = webpackMerge(targetConfigPartial, mobileProdConfigPartial);
Expand Down
45 changes: 23 additions & 22 deletions packages/angular-cli/utilities/prerender-bootstrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ const path = require('path');
const ts = require('typescript');

const projectRoot = process.argv[2];
const templatePath = process.argv[3];
const appShellConfig = process.argv[4];
const appShellConfig = process.argv[3];
const projectNodeModulesDir = path.resolve(projectRoot, '../node_modules');

// Require from absolute path to prevent looking up inside of angular-cli
Expand All @@ -38,23 +37,25 @@ const pathToTranspiledConfig = path.resolve(tmpDir, path.relative(projectRoot, a

var platformRef = universal.platformUniversalDynamic();

var platformConfig = {
ngModule: require(pathToTranspiledConfig).Module,
document: fs.readFileSync(templatePath, 'utf-8'),
preboot: false,
baseUrl: '/',
requestUrl: '/',
originUrl: 'localhost:3000'
};


const zone = Zone.current.fork({
name: 'UNIVERSAL prerender',
properties: platformConfig
});
zone.run(() => (platformRef.serializeModule(platformConfig.ngModule, platformConfig))
.then((html) => {
process.stdout.write(html);
}, (err) => {
process.stderr.write(err);
}));
process.stdin.on('data', (template) => {
var platformConfig = {
ngModule: require(pathToTranspiledConfig).Module,
document: template.toString(),
preboot: false,
baseUrl: '/',
requestUrl: '/',
originUrl: 'localhost:3000'
};


const zone = Zone.current.fork({
name: 'UNIVERSAL prerender',
properties: platformConfig
});
zone.run(() => (platformRef.serializeModule(platformConfig.ngModule, platformConfig))
.then((html) => {
process.stdout.write(html);
}, (err) => {
process.stderr.write(err);
}));
})
30 changes: 15 additions & 15 deletions packages/angular-cli/utilities/prerender-webpack-plugin.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const path = require('path');
import { spawn } from 'child_process';
import { spawnSync } from 'child_process';

export interface IWebpackPrerender {
templatePath: string;
Expand All @@ -12,25 +12,25 @@ export class PrerenderWebpackPlugin {

apply(compiler: any) {
compiler.plugin('emit', (compilation: any, callback: Function) => {
let proc = spawn('node', [
let relativeTemplatePath = path.relative(this.options.appPath, this.options.templatePath);
let proc = spawnSync('node', [
`${path.resolve(__dirname, 'prerender-bootstrapper.js')}`,
this.options.appPath,
this.options.templatePath,
this.options.configPath],
{
cwd: this.options.appPath
cwd: this.options.appPath,
input: compilation.assets[relativeTemplatePath].source()
});
proc.stderr.on('data', (e: any) => {
console.error('error in app shell', e.toString());
callback();
});
proc.stdout.on('data', (data: string | Buffer) => {
compilation.assets[path.relative(this.options.appPath, this.options.templatePath)] = {
source: () => data.toString(),
size: () => data.toString().length
};
callback();
});
if (process.stderr.toString()) {
console.error('error in app shell', process.stderr.toString());
return callback();
}

compilation.assets[relativeTemplatePath] = {
source: () => proc.stdout.toString(),
size: () => proc.stdout.toString().length
};
callback();
});
}
};

0 comments on commit 7f109ba

Please sign in to comment.