Skip to content

Commit 177b0c7

Browse files
committed
feat(build): allow minify options to be supplied
1 parent fd07344 commit 177b0c7

7 files changed

Lines changed: 398 additions & 29 deletions

File tree

lib/build/bundle.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const Convert = require('./convert-source-map');
55
const fs = require('../file-system');
66
const SourceInclusion = require('./source-inclusion').SourceInclusion;
77
const DependencyInclusion = require('./dependency-inclusion').DependencyInclusion;
8+
const Configuration = require('../configuration').Configuration;
89
const Utils = require('./utils');
910

1011
exports.Bundle = class {
@@ -25,7 +26,7 @@ exports.Bundle = class {
2526
this.excludes = (this.includes.exclude || []).map(x => this.createMatcher(x));
2627
this.includes = this.includes.include.map(x => new SourceInclusion(this, x));
2728
}
28-
this.buildOptions = bundler.interpretBuildOptions(config.options, bundler.buildOptions);
29+
this.buildOptions = new Configuration(config.options, bundler.buildOptions.getAllOptions());
2930
this.requiresBuild = true;
3031
this.fileCache = {};
3132
}
@@ -148,7 +149,7 @@ exports.Bundle = class {
148149

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

153154
function fileIsDependency(file) {
154155
return file
@@ -210,7 +211,7 @@ exports.Bundle = class {
210211
concat.add(undefined, this.writeLoaderCode(platform));
211212
contents = concat.content;
212213

213-
if (buildOptions.rev) {
214+
if (buildOptions.isApplicable('rev')) {
214215
//Generate a unique hash based off of the bundle contents
215216
//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
216217
this.hash = generateHash(concat.content);
@@ -222,7 +223,7 @@ exports.Bundle = class {
222223
if (platform.index) {
223224
this.setIndexFileConfigTarget(platform, path.posix.join(outputDir, bundleFileName));
224225
}
225-
} else if (buildOptions.rev) {
226+
} else if (buildOptions.isApplicable('rev')) {
226227
//Generate a unique hash based off of the bundle contents
227228
//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
228229
this.hash = generateHash(concat.content);
@@ -237,8 +238,8 @@ exports.Bundle = class {
237238

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

240-
if (buildOptions.minify) {
241-
let minificationOptions = { fromString: true };
241+
if (buildOptions.isApplicable('minify')) {
242+
let minificationOptions = Object.assign({ fromString: true }, buildOptions.getValue('minify'));
242243

243244
if (needsSourceMap) {
244245
minificationOptions.inSourceMap = Convert.fromJSON(concat.sourceMap).toObject();

lib/build/bundler.js

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const Bundle = require('./bundle').Bundle;
33
const BundledSource = require('./bundled-source').BundledSource;
44
const CLIOptions = require('../cli-options').CLIOptions;
55
const LoaderPlugin = require('./loader-plugin').LoaderPlugin;
6+
const Configuration = require('../configuration').Configuration;
67
const path = require('path');
78

89
exports.Bundler = class {
@@ -20,7 +21,7 @@ exports.Bundler = class {
2021
rev: false
2122
};
2223

23-
this.buildOptions = this.interpretBuildOptions(project.build.options, defaultBuildOptions);
24+
this.buildOptions = new Configuration(project.build.options, defaultBuildOptions);
2425
this.loaderOptions = project.build.loader;
2526

2627
this.loaderConfig = {
@@ -53,26 +54,6 @@ exports.Bundler = class {
5354
).then(() => bundler);
5455
}
5556

56-
interpretBuildOptions(options, defaultOptions) {
57-
let env = this.environment;
58-
options = Object.assign({}, defaultOptions, options);
59-
60-
for (let key in options) {
61-
let value = options[key];
62-
63-
if (typeof value === 'boolean') {
64-
continue;
65-
} else if (typeof value === 'string') {
66-
let parts = value.split('&').map(x => x.trim().toLowerCase());
67-
options[key] = parts.indexOf(env) !== -1;
68-
} else {
69-
options[key] = false;
70-
}
71-
}
72-
73-
return options;
74-
}
75-
7657
itemIncludedInBuild(item) {
7758
if (typeof item === 'string' || !item.env) {
7859
return true;

lib/build/loader.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
'use strict';
22

3+
const Configuration = require('../configuration').Configuration;
4+
35
exports.createLoaderCode = function createLoaderCode(platform, bundler) {
46
let loaderCode;
57
let loaderOptions = bundler.loaderOptions;
@@ -57,7 +59,7 @@ exports.createRequireJSConfig = function createRequireJSConfig(platform, bundler
5759
for (let i = 0; i < bundles.length; ++i) {
5860
let currentBundle = bundles[i];
5961
let currentName = currentBundle.config.name;
60-
let buildOptions = bundler.interpretBuildOptions(currentBundle.config.options, bundler.buildOptions);
62+
let buildOptions = new Configuration(currentBundle.config.options, bundler.buildOptions.getAllOptions());
6163
if (currentName === configName) { //skip over the vendor bundle
6264
continue;
6365
}
@@ -66,7 +68,7 @@ exports.createRequireJSConfig = function createRequireJSConfig(platform, bundler
6668
bundleMetadata[currentBundle.moduleId] = currentBundle.getBundledModuleIds();
6769
}
6870
//If build revisions are enabled, append the revision hash to the appropriate module id
69-
config.paths[currentBundle.moduleId] = location + '/' + currentBundle.moduleId + (buildOptions.rev && currentBundle.hash ? '-' + currentBundle.hash : '');
71+
config.paths[currentBundle.moduleId] = location + '/' + currentBundle.moduleId + (buildOptions.isApplicable('rev') && currentBundle.hash ? '-' + currentBundle.hash : '');
7072
}
7173

7274
if (includeBundles) {

lib/configuration.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
'use strict';
2+
3+
const CLIOptions = require('./cli-options').CLIOptions;
4+
5+
exports.Configuration = class {
6+
constructor(options, defaultOptions, environment) {
7+
this.options = Object.assign({}, defaultOptions, options);
8+
this.environment = environment || CLIOptions.getEnvironment();
9+
}
10+
11+
getAllOptions() {
12+
return this.options;
13+
}
14+
15+
getValue(propPath) {
16+
let split = propPath.split('.');
17+
let cur = this.options;
18+
19+
for (let i = 0; i < split.length; i++) {
20+
if (!cur) {
21+
return undefined;
22+
}
23+
24+
cur = cur[split[i]];
25+
}
26+
27+
if (typeof cur === 'object') {
28+
let keys = Object.keys(cur);
29+
let result = undefined;
30+
31+
if (keys.indexOf('default') > -1 && typeof(cur.default) === 'object') {
32+
result = cur.default;
33+
}
34+
35+
for (let i = 0; i < keys.length; i++) {
36+
if (this.matchesEnvironment(this.environment, keys[i])) {
37+
if (typeof(cur[keys[i]]) === 'object') {
38+
result = Object.assign(result || {}, cur[keys[i]]);
39+
} else {
40+
return cur[keys[i]];
41+
}
42+
}
43+
}
44+
45+
return result;
46+
}
47+
48+
return cur;
49+
}
50+
51+
isApplicable(propPath) {
52+
let value = this.getValue(propPath);
53+
54+
if (!value) {
55+
return false;
56+
}
57+
58+
if (typeof value === 'boolean') {
59+
return value;
60+
}
61+
62+
if (typeof value === 'string') {
63+
return this.matchesEnvironment(this.environment, value);
64+
}
65+
66+
if (typeof value === 'object') {
67+
return true;
68+
}
69+
70+
return false;
71+
}
72+
73+
matchesEnvironment(environment, value) {
74+
let parts = value.split('&').map(x => x.trim().toLowerCase());
75+
return parts.indexOf(environment) !== -1;
76+
}
77+
};

spec/lib/build/bundle.spec.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,27 @@
22

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

67
describe('the Bundle module', () => {
78
let sut;
9+
let cliOptionsMock;
810

911
beforeEach(() => {
12+
cliOptionsMock = new CLIOptionsMock();
13+
cliOptionsMock.attach();
14+
1015
let bundler = new BundlerMock();
1116
let config = {
1217
name: 'app-bundle.js'
1318
};
1419
sut = new Bundle(bundler, config);
1520
});
1621

22+
afterEach(() => {
23+
cliOptionsMock.detach();
24+
});
25+
1726
it('only prepends items that are included in the build', () => {
1827
let bundler = new BundlerMock();
1928
bundler.itemIncludedInBuild.and.callFake((item) => {

0 commit comments

Comments
 (0)