Skip to content

Commit

Permalink
optimize closure build
Browse files Browse the repository at this point in the history
  • Loading branch information
steveblue committed Mar 31, 2019
1 parent 572f05f commit 1817d91
Show file tree
Hide file tree
Showing 10 changed files with 365 additions and 68 deletions.
48 changes: 48 additions & 0 deletions backend/router.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// router.js

const express = require('express');
const router = require('express').Router();
const compression = require('compression');
const config = require(process.cwd()+'/angular.json');


module.exports = function(app) {
'use strict';
let projectRoot = config.projects[config.defaultProject].architect.build.options.outputPath;
// ROUTER CONFIG


app.use(function(req, res, next) {

// CAUTION: Wide open CORS!
res.header('Access-Control-Allow-Origin', req.headers.origin);
res.header('Access-Control-Allow-Credentials', 'true');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
res.header('Access-Control-Allow-Headers', 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type');

if (req.method == 'OPTIONS' ) {
res.send(200);
}
else {
next();
}

});


// ROUTES

app.use(compression());

app.use('/', express.static( process.cwd() + '/' + projectRoot ));


app.get('*', function (req, res) {
res.sendFile('index.html', { root: process.cwd() + '/' + projectRoot });
});



return router;

};
72 changes: 72 additions & 0 deletions backend/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
"use strict";
// server.js

const express = require('express');
const http = require('http');
const fs = require('fs');
const app = express();
const config = require(process.cwd()+'/angular.json');
const serverConfig = {
dev: require(process.cwd()+'/config/server.config.dev.js'),
prod: require(process.cwd()+'/config/server.config.prod.js')
};

let projectRoot = config.projects[config.defaultProject].architect.build.options.outputPath;
let env = process.env.NODE_ENV || 'dev';
const port = serverConfig[env].port || process.env.PORT;
const host = serverConfig[env].origin;
let canWatch = false;
let server;

process.argv.forEach(function(arg){

if (arg.includes('watch')) {
canWatch = arg.split('=')[1].trim() === 'true' ? true : false;
}

});


// Livereload Server Start

let live = function() {
let livereload = require('livereload');
let liveserver = livereload.createServer({
port: 35729
});
liveserver.watch([process.cwd() + '/'+projectRoot+'/assets',
process.cwd() + '/'+projectRoot+'/src',
process.cwd() + '/'+projectRoot+'/style',
process.cwd() + '/'+projectRoot+'/*.html',
process.cwd() + '/'+projectRoot+'/*.js',
process.cwd() + '/'+projectRoot+'/*.css']);
console.log('Livereload available at '+host+':'+35729);
};

// Create Server

server = http.createServer(app);

if (canWatch === true) {

live();

}


// Express Middleware



// Load Modules

const routes = require('./router')(app);

// Server Start

server.listen(port);

console.log('Express available at '+host+':'+port);


module.exports = app;
67 changes: 11 additions & 56 deletions build_tools/src/closure/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,70 +10,26 @@ import { catchError, mapTo, concatMap } from 'rxjs/operators';

import { ClosureBuilderSchema } from './schema.interface';

export function rollupRxJS(
options: ClosureBuilderSchema,
context: BuilderContext
): Observable<{}> {
context.reportStatus('rollup rxjs');
return new Observable((observer) => {

let rollup;
const editFile = (filePath) => {
return new Promise((res) => {
readFile(filePath, 'utf-8', (error, stdout) => {
let pack = JSON.parse(stdout);
pack.es2015 = pack.es2015.replace('_esm2015', '_fesm2015');
writeFile(filePath, JSON.stringify(pack), () => {
res(filePath);
})
});
});
};

if (process.platform === 'win32') {
rollup = spawn('cmd', ['/c', join(context.workspaceRoot, 'node_modules', '.bin', 'rollup'), '-c', join('rollup.rxjs.js')]);
} else {
rollup = spawn(join(context.workspaceRoot, 'node_modules', '.bin', 'rollup'), ['-c', join('rollup.rxjs.js')]);
}

// rollup.stdout.on('data', (msg) => {
// log.message(msg);
// });

rollup.on('exit', () => {

Promise.all([editFile('node_modules/rxjs/package.json'),
editFile('node_modules/rxjs/operators/package.json'),
editFile('node_modules/rxjs/ajax/package.json'),
editFile('node_modules/rxjs/testing/package.json'),
editFile('node_modules/rxjs/webSocket/package.json')])
.then(data => {
context.reportProgress(options.step++, options.tally, 'rollup rxjs');
observer.next(data);
});

});
});
}

export function closure(
options: ClosureBuilderSchema,
options: ClosureBuilderSchema,
context: BuilderContext
): Observable<{}> {
context.reportStatus('closure');
return new Observable((observer) => {

// TODO: make these configurable
const jarPath = join('node_modules', 'google-closure-compiler-java', 'compiler.jar');
const warningLevel = 'QUIET';
const confPath = normalize('closure.conf');
const outFile = './dist/build_repo/main.js';
const manifestPath = normalize('closure/manifest.MF');
const target = context.target ? context.target : { project: 'app' };
const jarPath = options.jarPath ? options.jarPath : join('node_modules', 'google-closure-compiler-java', 'compiler.jar');
const warningLevel = options.warningLevel ? options.warningLevel : 'QUIET';
const confPath = options.closureConfig ? normalize(options.closureConfig) : normalize('closure.conf');
const outFile = options.outFile ? options.outFile : `./dist/${target.project}/main.js`;
const manifestPath = options.manifest ? normalize(options.manifest) : normalize('closure/manifest.MF');

exec(`java -jar ${jarPath} --warning_level=${warningLevel} --flagfile ${confPath} --js_output_file ${outFile} --output_manifest=${manifestPath}`,
{},
(error, stdout, stderr) => {

if (stderr.includes('ERROR')) {
observer.error(error);
}
context.reportProgress(options.step++, options.tally, 'closure');
observer.next(stdout);
});
Expand All @@ -85,11 +41,10 @@ export function executeClosure(
context: BuilderContext
): Observable<BuilderOutput> {
options.step = 0;
options.tally = 5;
options.tally = 3;
return of(context).pipe(
concatMap( results => ngc(options, context) ),
concatMap( results => compileMain(options, context) ),
concatMap( results => rollupRxJS(options, context) ),
concatMap( results => closure(options, context) ),
mapTo({ success: true }),
catchError(error => {
Expand Down
4 changes: 4 additions & 0 deletions build_tools/src/closure/schema.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@ export interface ClosureBuilderSchema {
watch?: boolean;
step: number;
tally: number;
jarPath?: string;
warningLevel?: string;
manifest?: string;
outFile?: string;
}
16 changes: 16 additions & 0 deletions build_tools/src/closure/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,22 @@
"type": "boolean",
"description": "Run build when files change.",
"default": false
},
"jarPath": {
"type": "string",
"description": "The path to the compiler.jar file."
},
"warningLevel": {
"type": "string",
"description": "The logging warning level for Closure Compiler."
},
"manifest": {
"type": "string",
"description": "Location to print the manifest."
},
"outFile": {
"type": "string",
"description": "Location to output the bundle."
}
},
"additionalProperties": false,
Expand Down
11 changes: 6 additions & 5 deletions closure.conf
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
--compilation_level=ADVANCED_OPTIMIZATIONS
--language_in=ECMASCRIPT6
--language_out=ECMASCRIPT5
--variable_renaming_report=closure/variable_renaming_report
--property_renaming_report=closure/property_renaming_report
Expand All @@ -8,7 +7,8 @@
--warning_level=QUIET
--dependency_mode=STRICT
--rewrite_polyfills=false
--module_resolution=NODE
--jscomp_off=checkVars


--externs closure.externs.js
--externs node_modules/zone.js/dist/zone_externs.js
Expand All @@ -18,10 +18,10 @@
--js node_modules/tslib/tslib.es6.js

--js node_modules/rxjs/package.json
--js node_modules/rxjs/_fesm2015/index.js

--js node_modules/rxjs/_esm2015/index.js
--js node_modules/rxjs/_esm2015/internal/**.js
--js node_modules/rxjs/operators/package.json
--js node_modules/rxjs/_fesm2015/operators/index.js
--js node_modules/rxjs/_esm2015/operators/index.js

--js node_modules/@angular/core/package.json
--js node_modules/@angular/core/fesm2015/core.js
Expand Down Expand Up @@ -52,6 +52,7 @@

--js out-tsc/**.js

--module_resolution=node
--package_json_entry_names es2015
--process_common_js_modules

Expand Down
6 changes: 6 additions & 0 deletions config/server.config.dev.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const config = {
origin: 'localhost',
port: 4200
};

module.exports = config;
6 changes: 6 additions & 0 deletions config/server.config.prod.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const config = {
origin: 'localhost',
port: 4200
};

module.exports = config;
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
"@angular/platform-browser": "~8.0.0-beta.10",
"@angular/platform-browser-dynamic": "~8.0.0-beta.10",
"@angular/router": "~8.0.0-beta.10",
"compression": "^1.7.4",
"express": "^4.16.4",
"http": "^0.0.0",
"rollup-plugin-node-resolve": "^4.0.1",
"rollup-plugin-typescript2": "^0.20.1",
"rxjs": "~6.4.0",
Expand Down
Loading

0 comments on commit 1817d91

Please sign in to comment.