Skip to content
This repository has been archived by the owner on May 1, 2020. It is now read-only.

Commit

Permalink
feat(build): replace --dev flag with --prod and add flags --aot, --mi…
Browse files Browse the repository at this point in the history
…nifyJs, --minifyCss, --optimizeJs
  • Loading branch information
jthoms1 committed Dec 8, 2016
1 parent 899e02f commit 99922ce
Show file tree
Hide file tree
Showing 10 changed files with 142 additions and 157 deletions.
71 changes: 7 additions & 64 deletions src/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,7 @@ function buildWorker(context: BuildContext) {
// load any 100% required files to ensure they exist
return validateRequiredFilesExist();
}).then(() => {
if (context.isProd) {
// production build
return buildProd(context);
}

// dev build
return buildDev(context);
return buildProject(context);
});
}

Expand All @@ -64,86 +58,35 @@ function validateRequiredFilesExist() {
return readFileAsync(process.env.IONIC_APP_ENTRY_POINT_PATH);
}

function buildProd(context: BuildContext) {
// sync empty the www/build directory
clean(context);

buildId++;

// async tasks
// these can happen all while other tasks are running
const copyPromise = copy(context);
function buildProject(context: BuildContext) {
var compilePromise = (context.runAot) ? ngc(context) : transpile(context);

// kick off ngc to run the Ahead of Time compiler
return ngc(context)
return compilePromise
.then(() => {
// ngc has finished, now let's bundle it all together
return bundle(context);
})
.then(() => {
// js minify can kick off right away
const jsPromise = minifyJs(context);

// sass needs to finish, then css minify can run when sass is done
const minPromise = (context.runMinifyJs) ? minifyJs(context) : Promise.resolve();
const sassPromise = sass(context)
.then(() => {
return minifyCss(context);
return (context.runMinifyCss) ? minifyCss(context) : Promise.resolve()
});

return Promise.all([
jsPromise,
minPromise,
sassPromise
]);
})
.then(() => {
// kick off the tslint after everything else
// nothing needs to wait on its completion
lint(context);

// ensure the async tasks have fully completed before resolving
return Promise.all([
copyPromise
]);
})
.catch(err => {
throw new BuildError(err);
});
}


function buildDev(context: BuildContext) {
// sync empty the www/build directory
clean(context);

buildId++;

// async tasks
// these can happen all while other tasks are running
const copyPromise = copy(context);

// just bundle, and if that passes then do the rest at the same time
return transpile(context)
.then(() => {
return bundle(context);
})
.then(() => {
return Promise.all([
sass(context),
copyPromise
]);
})
.then(() => {
// kick off the tslint after everything else
// nothing needs to wait on its completion
lint(context);
return Promise.resolve();
})
.catch(err => {
throw new BuildError(err);
});
}


export function buildUpdate(changedFiles: ChangedFile[], context: BuildContext) {
return new Promise(resolve => {
const logger = new Logger('build');
Expand Down
2 changes: 1 addition & 1 deletion src/rollup/ionic-rollup-resolver-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export function ionicRollupResolverPlugin(context: BuildContext) {
}

// remove decorators if prod build
if (context.isProd) {
if (context.optimizeJs) {
file.content = optimizeJavascript(jsSourcePath, file.content);
}

Expand Down
70 changes: 70 additions & 0 deletions src/spec/build.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { BuildContext } from '../util/interfaces';

import * as build from '../build';
import * as bundle from '../bundle';
import * as copy from '../copy';
import * as minify from '../minify';
import * as lint from '../lint';
import * as ngc from '../ngc';
import * as sass from '../sass';
import * as transpile from '../transpile';

describe('build', () => {
beforeEach(() => {
spyOn(copy, 'copy').and.returnValue(Promise.resolve());
spyOn(ngc, 'ngc').and.returnValue(Promise.resolve());
spyOn(bundle, 'bundle').and.returnValue(Promise.resolve());
spyOn(minify, 'minifyJs').and.returnValue(Promise.resolve());
spyOn(sass, 'sass').and.returnValue(Promise.resolve());
spyOn(minify, 'minifyCss').and.returnValue(Promise.resolve());
spyOn(lint, 'lint').and.returnValue(Promise.resolve());
spyOn(transpile, 'transpile').and.returnValue(Promise.resolve());
});

describe('build', () => {
it('isProd', () => {
let context: BuildContext = {
isProd: true,
optimizeJs: true,
runMinifyJs: true,
runMinifyCss: true,
runAot: true
};

build.build(context).then(() => {
expect(copy.copy).toHaveBeenCalled();
expect(ngc.ngc).toHaveBeenCalled();
expect(bundle.bundle).toHaveBeenCalled();
expect(minify.minifyJs).toHaveBeenCalled();
expect(sass.sass).toHaveBeenCalled();
expect(minify.minifyCss).toHaveBeenCalled();
expect(lint.lint).toHaveBeenCalled();

expect(transpile.transpile).not.toHaveBeenCalled();
});
});

it('isDev', () => {
let context: BuildContext = {
isProd: false,
optimizeJs: false,
runMinifyJs: false,
runMinifyCss: false,
runAot: false
};

build.build(context).then(() => {
expect(copy.copy).toHaveBeenCalled();
expect(transpile.transpile).toHaveBeenCalled();
expect(bundle.bundle).toHaveBeenCalled();
expect(sass.sass).toHaveBeenCalled();
expect(lint.lint).toHaveBeenCalled();

expect(ngc.ngc).not.toHaveBeenCalled();
expect(minify.minifyJs).not.toHaveBeenCalled();
expect(minify.minifyCss).not.toHaveBeenCalled();
});
});
});

});
65 changes: 20 additions & 45 deletions src/spec/config.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BuildContext } from '../util/interfaces';
import { bundlerStrategy, generateContext, getConfigValue, getUserConfigFile, getIsProd, replacePathVars } from '../util/config';
import { bundlerStrategy, generateContext, getConfigValue, getUserConfigFile, replacePathVars } from '../util/config';
import { addArgv, setAppPackageJsonData, setProcessEnvVar, setProcessArgs, setProcessEnv, setCwd } from '../util/config';
import { resolve } from 'path';

Expand Down Expand Up @@ -39,60 +39,35 @@ describe('config', () => {

it('should set isProd by default', () => {
const context = generateContext();
expect(context.isProd).toEqual(true);
expect(context.isProd).toEqual(false);
});

it('should create an object when passed nothing', () => {
const context = generateContext();
expect(context).toBeDefined();
});

});

describe('getIsProd', () => {

it('should set isProd false with env var', () => {
context = {};
setProcessEnvVar('IONIC_DEV', 'true');
expect(getIsProd(context)).toEqual(false);
});

it('should set isProd false with package.json string config', () => {
context = {};
setAppPackageJsonData({ config: { ionic_dev: 'true' }});
expect(getIsProd(context)).toEqual(false);
});

it('should set isProd false with package.json config', () => {
context = {};
setAppPackageJsonData({ config: { ionic_dev: true }});
expect(getIsProd(context)).toEqual(false);
});

it('should not reassign isProd when already set', () => {
context = {};
context.isProd = true;
addArgv('--dev');
expect(getIsProd(context)).toEqual(true);
});

it('should set isProd false with short --d arg', () => {
context = {};
addArgv('-d');
expect(getIsProd(context)).toEqual(false);
});

it('should set isProd false with full --dev arg', () => {
context = {};
addArgv('--dev');
expect(getIsProd(context)).toEqual(false);
it('should set default prod specific build flag defaults to false', () => {
const context = generateContext({
isProd: false
});
expect(context.isProd).toEqual(false);
expect(context.runAot).toEqual(false);
expect(context.runMinifyJs).toEqual(false);
expect(context.runMinifyCss).toEqual(false);
expect(context.optimizeJs).toEqual(false);
});

it('should default to isProd true', () => {
context = {};
expect(getIsProd(context)).toEqual(true);
it('should set default prod specific build flags to true when isProd is true', () => {
const context = generateContext({
isProd: true
});
expect(context.isProd).toEqual(true);
expect(context.runAot).toEqual(true);
expect(context.runMinifyJs).toEqual(true);
expect(context.runMinifyCss).toEqual(true);
expect(context.optimizeJs).toEqual(true);
});

});

describe('replacePathVars', () => {
Expand Down
1 change: 0 additions & 1 deletion src/transpile-worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ const context: BuildContext = {};
process.on('message', (incomingMsg: TranspileWorkerMessage) => {
context.rootDir = incomingMsg.rootDir;
context.buildDir = incomingMsg.buildDir;
context.isProd = incomingMsg.isProd;

const workerConfig: TranspileWorkerConfig = {
configFile: incomingMsg.configFile,
Expand Down
5 changes: 1 addition & 4 deletions src/transpile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,6 @@ function runDiagnosticsWorker(context: BuildContext) {
const msg: TranspileWorkerMessage = {
rootDir: context.rootDir,
buildDir: context.buildDir,
isProd: context.isProd,
configFile: getTsConfigPath(context)
};
diagnosticsWorker.send(msg);
Expand All @@ -256,16 +255,14 @@ function runDiagnosticsWorker(context: BuildContext) {
export interface TranspileWorkerMessage {
rootDir?: string;
buildDir?: string;
isProd?: boolean;
configFile?: string;
transpileSuccess?: boolean;
}


function cleanFileNames(context: BuildContext, fileNames: string[]) {
// make sure we're not transpiling the prod when dev and stuff
const removeFileName = (context.isProd) ? 'main.dev.ts' : 'main.prod.ts';
return fileNames.filter(f => (f.indexOf(removeFileName) === -1));
return fileNames;
}

function writeSourceFiles(fileCache: FileCache, sourceFiles: ts.SourceFile[]) {
Expand Down
63 changes: 25 additions & 38 deletions src/util/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,31 @@ export function generateContext(context?: BuildContext): BuildContext {
context.bundler = bundlerStrategy(context);
}

context.isProd = getIsProd(context);
setIonicEnvironment(context.isProd);
context.isProd = [
context.isProd,
hasArg('--prod')
].find(val => typeof val === 'boolean');

// If context is prod then the following flags must be set to true
context.runAot = [
context.runAot,
context.isProd || hasArg('--aot'),
].find(val => typeof val === 'boolean');

context.runMinifyJs = [
context.runMinifyJs,
context.isProd || hasArg('--minifyJs')
].find(val => typeof val === 'boolean');

context.runMinifyCss = [
context.runMinifyCss,
context.isProd || hasArg('--minifyCss')
].find(val => typeof val === 'boolean');

context.optimizeJs = [
context.optimizeJs,
context.isProd || hasArg('--optimizeJs')
].find(val => typeof val === 'boolean');

if (typeof context.isWatch !== 'boolean') {
context.isWatch = hasArg('--watch');
Expand All @@ -75,31 +98,6 @@ export function generateContext(context?: BuildContext): BuildContext {
return context;
}


export function getIsProd(context: BuildContext) {
// only check if isProd hasn't already been manually set
if (typeof context.isProd === 'boolean') {
return context.isProd;
}
if (hasArg('--dev', '-d')) {
// not production: has a --dev or -d cmd line arg
return false;
}

let val = getPackageJsonConfig(context, ENV_VAR_IONIC_DEV.toLowerCase());
if (typeof val === 'boolean') {
return !val;
}

val = getProcessEnvVar(ENV_VAR_IONIC_DEV);
if (typeof val === 'boolean') {
return !val;
}

return true;
}


export function getUserConfigFile(context: BuildContext, task: TaskInfo, userConfigFile: string) {
if (userConfigFile) {
return resolve(userConfigFile);
Expand Down Expand Up @@ -311,12 +309,6 @@ export function isDebugMode() {
return (processEnv.ionic_debug_mode === 'true');
}


export function setIonicEnvironment(isProd: boolean) {
setProcessEnvVar(ENV_VAR_IONIC_ENV, (isProd ? ENV_VAR_PROD : ENV_VAR_DEV));
}


let processArgv: string[];
export function setProcessArgs(argv: string[]) {
processArgv = argv;
Expand Down Expand Up @@ -401,11 +393,6 @@ const TMP_DIR = '.tmp';
const WWW_DIR = 'www';
const WWW_INDEX_FILENAME = 'index.html';

const ENV_VAR_PROD = 'prod';
const ENV_VAR_DEV = 'dev';

const ENV_VAR_IONIC_ENV = 'IONIC_ENV';
const ENV_VAR_IONIC_DEV = 'IONIC_DEV';
const ENV_VAR_ROOT_DIR = 'IONIC_ROOT_DIR';
const ENV_VAR_TMP_DIR = 'IONIC_TMP_DIR';
const ENV_VAR_SRC_DIR = 'IONIC_SRC_DIR';
Expand Down
Loading

0 comments on commit 99922ce

Please sign in to comment.