Skip to content

Commit c7b333a

Browse files
committed
feat(prod-build): add environment config
1 parent 2fac21b commit c7b333a

File tree

6 files changed

+70
-30
lines changed

6 files changed

+70
-30
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export const environment = {
2+
production: false
3+
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export const environment = {
2+
production: true
3+
};
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
import {bootstrap} from 'angular2/platform/browser';
2+
import {enableProdMode} from 'angular2/core';
3+
import {environment} from './app/environment';
24
import {<%= jsComponentName %>App} from './app/<%= htmlComponentName %>';
35

4-
bootstrap(<%= jsComponentName %>App, []);
6+
if (environment.production) {
7+
enableProdMode();
8+
}
9+
10+
bootstrap(<%= jsComponentName %>App);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// The file for the current environment will overwrite this one during build
2+
// Different environments can be found in ../environments/
3+
// The build system defaults to the dev environment
4+
5+
export const environment = {
6+
production: false
7+
};

lib/broccoli/angular2-app.js

Lines changed: 43 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,12 @@ class Angular2App {
8888
this.initializeAddons();
8989
this.project.addons = this.project.addons.filter(function (addon) {
9090
addon.app = this;
91-
91+
9292
if (!addon.isEnabled || addon.isEnabled()) {
9393
if (addon.included) {
9494
addon.included(this);
9595
}
96-
96+
9797
return addon;
9898
}
9999
}, this);
@@ -133,26 +133,26 @@ class Angular2App {
133133
*/
134134
_contentFor(match, type) {
135135
var content = [];
136-
136+
137137
/*switch (type) {
138138
case 'head': this._contentForHead(content, config); break;
139139
case 'config-module': this._contentForConfigModule(content, config); break;
140140
case 'app-boot': this._contentForAppBoot(content, config); break;
141141
}*/
142-
142+
143143
content = this.project.addons.reduce(function (content, addon) {
144144
var addonContent = addon.contentFor ? addon.contentFor(type) : null;
145145
if (addonContent) {
146146
return content.concat(addonContent);
147147
}
148-
148+
149149
return content;
150150
}, content);
151-
152-
151+
152+
153153
return content.join('\n');
154154
}
155-
155+
156156
/**
157157
* @private
158158
* @method _getReplacePatterns
@@ -177,19 +177,19 @@ class Angular2App {
177177
var files = [
178178
'index.html'
179179
];
180-
180+
181181
var index = new Funnel(this._sourceDir, {
182182
files: files,
183183
description: 'Funnel: index.html'
184184
});
185-
186-
185+
186+
187187
return configReplace(index, {
188188
files: [htmlName],
189189
patterns: this._getReplacePatterns()
190190
});
191191
}
192-
192+
193193
/**
194194
* Returns the source root dir tree.
195195
*
@@ -217,7 +217,7 @@ class Angular2App {
217217
destDir: 'typings'
218218
});
219219
}
220-
220+
221221
/**
222222
* Returns the TS tree.
223223
*
@@ -228,6 +228,7 @@ class Angular2App {
228228
_getTsTree() {
229229
var typingsTree = this._getTypingsTree();
230230
var sourceTree = this._getSourceTree();
231+
var configTree = this._getConfigTree();
231232

232233
var tsConfigPath = path.join(this._sourceDir, 'tsconfig.json');
233234
var tsconfig = JSON.parse(fs.readFileSync(tsConfigPath, 'utf-8'));
@@ -246,27 +247,27 @@ class Angular2App {
246247
});
247248
}
248249
});
249-
250+
250251
// Because the tsconfig does not include the source directory, add this as the first path
251252
// element.
252253
tsconfig.files = tsconfig.files.map(name => path.join(this._sourceDir, name));
253-
254-
var srcAndTypingsTree = mergeTrees([sourceTree, typingsTree]);
255-
var tsTree = new compileWithTypescript(srcAndTypingsTree, tsconfig);
256-
254+
255+
var mergedTree = mergeTrees([sourceTree, typingsTree, configTree], { overwrite: true });
256+
var tsTree = new compileWithTypescript(mergedTree, tsconfig);
257+
257258
var tsTreeExcludes = ['*.d.ts', 'tsconfig.json'];
258259
var excludeSpecFiles = '**/*.spec.*';
259-
260+
260261
if (isProduction) {
261262
tsTreeExcludes.push(excludeSpecFiles);
262263
tsTree = uglify(tsTree);
263264
}
264-
265+
265266
tsTree = new Funnel(tsTree, {
266267
srcDir: this._sourceDir,
267268
exclude: tsTreeExcludes
268269
});
269-
270+
270271
return tsTree;
271272
}
272273

@@ -291,19 +292,19 @@ class Angular2App {
291292
'angular2/bundles/router.dev.js',
292293
'angular2/bundles/upgrade.dev.js'
293294
];
294-
295+
295296
if (this._options.vendorNpmFiles) {
296297
vendorNpmFiles = vendorNpmFiles.concat(this._options.vendorNpmFiles);
297298
}
298-
299+
299300
var vendorNpmTree = new Funnel('node_modules', {
300301
include: vendorNpmFiles,
301302
destDir: 'vendor'
302303
});
303-
304+
304305
return vendorNpmTree;
305306
}
306-
307+
307308
/**
308309
* Returns the `assets` tree.
309310
*
@@ -339,6 +340,23 @@ class Angular2App {
339340
allowEmpty: true
340341
});
341342
}
343+
344+
/**
345+
* Returns the config files tree.
346+
*
347+
* @private
348+
* @method _getConfigTree
349+
* @return {Tree} The config files tree.
350+
*/
351+
_getConfigTree() {
352+
var envConfigFile = isProduction ? 'environment.prod.ts' : 'environment.dev.ts';
353+
// console.log(envConfigFile);
354+
return new Funnel('config', {
355+
include: [envConfigFile],
356+
destDir: 'src/client/app',
357+
getDestinationPath: () => 'environment.ts'
358+
});
359+
}
342360
}
343361

344362
module.exports = Angular2App;

tests/e2e/e2e_workflow.spec.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,15 @@ describe('Basic end-to-end Workflow', function () {
7373
});
7474
});
7575

76-
it('Supports production builds via `ng build --envinroment=production`', function() {
76+
it('Supports production builds via `ng build --environment=production`', function() {
7777
this.timeout(420000);
7878

7979
return ng(['build', '--environment=production', '--silent'])
8080
.then(function () {
8181
expect(existsSync(path.join(process.cwd(), 'dist'))).to.be.equal(true);
82+
var envPath = path.join(process.cwd(), 'dist', 'app', 'environment.js');
83+
var envContent = fs.readFileSync(envPath, { encoding: 'utf8' });
84+
expect(envContent).to.include('production:true');
8285
})
8386
.then(function () {
8487
// Also does not create new things in GIT.
@@ -224,7 +227,7 @@ describe('Basic end-to-end Workflow', function () {
224227
expect(existsSync(cssFile)).to.be.equal(false);
225228
let scssExample = '.outer {\n .inner { background: #fff; }\n }';
226229
fs.writeFileSync(scssFile, scssExample, 'utf8');
227-
230+
228231
sh.exec('ng build --silent');
229232
let destCss = path.join(process.cwd(), 'dist', 'app', 'test-component', 'test-component.css');
230233
expect(existsSync(destCss)).to.be.equal(true);
@@ -259,7 +262,7 @@ describe('Basic end-to-end Workflow', function () {
259262
expect(existsSync(cssFile)).to.be.equal(false);
260263
let lessExample = '.outer {\n .inner { background: #fff; }\n }';
261264
fs.writeFileSync(lessFile, lessExample, 'utf8');
262-
265+
263266
sh.exec('ng build --silent');
264267
let destCss = path.join(process.cwd(), 'dist', 'app', 'test-component', 'test-component.css');
265268
expect(existsSync(destCss)).to.be.equal(true);
@@ -293,7 +296,7 @@ describe('Basic end-to-end Workflow', function () {
293296
expect(existsSync(cssFile)).to.be.equal(false);
294297
let stylusExample = '.outer {\n .inner { background: #fff; }\n }';
295298
fs.writeFileSync(stylusFile, stylusExample, 'utf8');
296-
299+
297300
sh.exec('ng build --silent');
298301
let destCss = path.join(process.cwd(), 'dist', 'app', 'test-component', 'test-component.css');
299302
expect(existsSync(destCss)).to.be.equal(true);

0 commit comments

Comments
 (0)