Skip to content

Commit

Permalink
feat(build): allow minify options to be supplied
Browse files Browse the repository at this point in the history
  • Loading branch information
JeroenVinke committed Jun 6, 2017
1 parent fd07344 commit 177b0c7
Show file tree
Hide file tree
Showing 7 changed files with 398 additions and 29 deletions.
13 changes: 7 additions & 6 deletions lib/build/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const Convert = require('./convert-source-map');
const fs = require('../file-system');
const SourceInclusion = require('./source-inclusion').SourceInclusion;
const DependencyInclusion = require('./dependency-inclusion').DependencyInclusion;
const Configuration = require('../configuration').Configuration;
const Utils = require('./utils');

exports.Bundle = class {
Expand All @@ -25,7 +26,7 @@ exports.Bundle = class {
this.excludes = (this.includes.exclude || []).map(x => this.createMatcher(x));
this.includes = this.includes.include.map(x => new SourceInclusion(this, x));
}
this.buildOptions = bundler.interpretBuildOptions(config.options, bundler.buildOptions);
this.buildOptions = new Configuration(config.options, bundler.buildOptions.getAllOptions());
this.requiresBuild = true;
this.fileCache = {};
}
Expand Down Expand Up @@ -148,7 +149,7 @@ exports.Bundle = class {

for (let i = 0; i < files.length; ++i) {
let currentFile = files[i];
let sourceMap = buildOptions.sourcemaps ? currentFile.sourceMap : undefined;
let sourceMap = buildOptions.isApplicable('sourcemaps') ? currentFile.sourceMap : undefined;

function fileIsDependency(file) {
return file
Expand Down Expand Up @@ -210,7 +211,7 @@ exports.Bundle = class {
concat.add(undefined, this.writeLoaderCode(platform));
contents = concat.content;

if (buildOptions.rev) {
if (buildOptions.isApplicable('rev')) {
//Generate a unique hash based off of the bundle contents
//Must generate hash after we write the loader config so that any other bundle changes (hash changes) can cause a new hash for the vendor file
this.hash = generateHash(concat.content);
Expand All @@ -222,7 +223,7 @@ exports.Bundle = class {
if (platform.index) {
this.setIndexFileConfigTarget(platform, path.posix.join(outputDir, bundleFileName));
}
} else if (buildOptions.rev) {
} else if (buildOptions.isApplicable('rev')) {
//Generate a unique hash based off of the bundle contents
//Must generate hash after we write the loader config so that any other bundle changes (hash changes) can cause a new hash for the vendor file
this.hash = generateHash(concat.content);
Expand All @@ -237,8 +238,8 @@ exports.Bundle = class {

console.log(`Writing ${bundleFileName}...`);

if (buildOptions.minify) {
let minificationOptions = { fromString: true };
if (buildOptions.isApplicable('minify')) {
let minificationOptions = Object.assign({ fromString: true }, buildOptions.getValue('minify'));

if (needsSourceMap) {
minificationOptions.inSourceMap = Convert.fromJSON(concat.sourceMap).toObject();
Expand Down
23 changes: 2 additions & 21 deletions lib/build/bundler.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const Bundle = require('./bundle').Bundle;
const BundledSource = require('./bundled-source').BundledSource;
const CLIOptions = require('../cli-options').CLIOptions;
const LoaderPlugin = require('./loader-plugin').LoaderPlugin;
const Configuration = require('../configuration').Configuration;
const path = require('path');

exports.Bundler = class {
Expand All @@ -20,7 +21,7 @@ exports.Bundler = class {
rev: false
};

this.buildOptions = this.interpretBuildOptions(project.build.options, defaultBuildOptions);
this.buildOptions = new Configuration(project.build.options, defaultBuildOptions);
this.loaderOptions = project.build.loader;

this.loaderConfig = {
Expand Down Expand Up @@ -53,26 +54,6 @@ exports.Bundler = class {
).then(() => bundler);
}

interpretBuildOptions(options, defaultOptions) {
let env = this.environment;
options = Object.assign({}, defaultOptions, options);

for (let key in options) {
let value = options[key];

if (typeof value === 'boolean') {
continue;
} else if (typeof value === 'string') {
let parts = value.split('&').map(x => x.trim().toLowerCase());
options[key] = parts.indexOf(env) !== -1;
} else {
options[key] = false;
}
}

return options;
}

itemIncludedInBuild(item) {
if (typeof item === 'string' || !item.env) {
return true;
Expand Down
6 changes: 4 additions & 2 deletions lib/build/loader.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

const Configuration = require('../configuration').Configuration;

exports.createLoaderCode = function createLoaderCode(platform, bundler) {
let loaderCode;
let loaderOptions = bundler.loaderOptions;
Expand Down Expand Up @@ -57,7 +59,7 @@ exports.createRequireJSConfig = function createRequireJSConfig(platform, bundler
for (let i = 0; i < bundles.length; ++i) {
let currentBundle = bundles[i];
let currentName = currentBundle.config.name;
let buildOptions = bundler.interpretBuildOptions(currentBundle.config.options, bundler.buildOptions);
let buildOptions = new Configuration(currentBundle.config.options, bundler.buildOptions.getAllOptions());
if (currentName === configName) { //skip over the vendor bundle
continue;
}
Expand All @@ -66,7 +68,7 @@ exports.createRequireJSConfig = function createRequireJSConfig(platform, bundler
bundleMetadata[currentBundle.moduleId] = currentBundle.getBundledModuleIds();
}
//If build revisions are enabled, append the revision hash to the appropriate module id
config.paths[currentBundle.moduleId] = location + '/' + currentBundle.moduleId + (buildOptions.rev && currentBundle.hash ? '-' + currentBundle.hash : '');
config.paths[currentBundle.moduleId] = location + '/' + currentBundle.moduleId + (buildOptions.isApplicable('rev') && currentBundle.hash ? '-' + currentBundle.hash : '');
}

if (includeBundles) {
Expand Down
77 changes: 77 additions & 0 deletions lib/configuration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
'use strict';

const CLIOptions = require('./cli-options').CLIOptions;

exports.Configuration = class {
constructor(options, defaultOptions, environment) {
this.options = Object.assign({}, defaultOptions, options);
this.environment = environment || CLIOptions.getEnvironment();
}

getAllOptions() {
return this.options;
}

getValue(propPath) {
let split = propPath.split('.');
let cur = this.options;

for (let i = 0; i < split.length; i++) {
if (!cur) {
return undefined;
}

cur = cur[split[i]];
}

if (typeof cur === 'object') {
let keys = Object.keys(cur);
let result = undefined;

if (keys.indexOf('default') > -1 && typeof(cur.default) === 'object') {
result = cur.default;
}

for (let i = 0; i < keys.length; i++) {
if (this.matchesEnvironment(this.environment, keys[i])) {
if (typeof(cur[keys[i]]) === 'object') {
result = Object.assign(result || {}, cur[keys[i]]);
} else {
return cur[keys[i]];
}
}
}

return result;
}

return cur;
}

isApplicable(propPath) {
let value = this.getValue(propPath);

if (!value) {
return false;
}

if (typeof value === 'boolean') {
return value;
}

if (typeof value === 'string') {
return this.matchesEnvironment(this.environment, value);
}

if (typeof value === 'object') {
return true;
}

return false;
}

matchesEnvironment(environment, value) {
let parts = value.split('&').map(x => x.trim().toLowerCase());
return parts.indexOf(environment) !== -1;
}
};
9 changes: 9 additions & 0 deletions spec/lib/build/bundle.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,27 @@

const BundlerMock = require('../../mocks/bundler');
const Bundle = require('../../../lib/build/bundle').Bundle;
const CLIOptionsMock = require('../../mocks/cli-options');

describe('the Bundle module', () => {
let sut;
let cliOptionsMock;

beforeEach(() => {
cliOptionsMock = new CLIOptionsMock();
cliOptionsMock.attach();

let bundler = new BundlerMock();
let config = {
name: 'app-bundle.js'
};
sut = new Bundle(bundler, config);
});

afterEach(() => {
cliOptionsMock.detach();
});

it('only prepends items that are included in the build', () => {
let bundler = new BundlerMock();
bundler.itemIncludedInBuild.and.callFake((item) => {
Expand Down
Loading

0 comments on commit 177b0c7

Please sign in to comment.