Skip to content

Commit 435cd18

Browse files
TheLarkInnfilipesilva
authored andcommitted
fix: add environment configuration replacement (#1364)
* fix: add environment configuration replacement * remove debuggers * fix: move mobile test above config test because it relies on prod build from previous step * fix: removed unneeded interface moved config paths
1 parent 860526c commit 435cd18

File tree

7 files changed

+92
-9
lines changed

7 files changed

+92
-9
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
export * from './environment';
1+
export * from './environments/environment';
22
export * from './app.component';

addon/ng2/models/webpack-config.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { CliConfig } from './config';
2+
import { NgCliEnvironmentPlugin } from '../utilities/environment-plugin';
23
import {
34
getWebpackCommonConfig,
45
getWebpackDevConfigPartial,
@@ -35,6 +36,7 @@ export class NgCliWebpackConfig {
3536
}
3637

3738
this.generateConfig();
39+
this.config.plugins.unshift(new NgCliEnvironmentPlugin({env: this.environment}));
3840
}
3941

4042
generateConfig(): void {
@@ -59,4 +61,3 @@ export class NgCliWebpackConfig {
5961
}
6062
}
6163
}
62-
+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import * as fs from 'fs';
2+
3+
interface WebpackPlugin {
4+
apply(compiler: any): void;
5+
}
6+
7+
export class NgCliEnvironmentPlugin implements WebpackPlugin {
8+
_file: string;
9+
_alias: string;
10+
_env: string;
11+
12+
constructor(config: any) {
13+
if (typeof config === 'string') {
14+
config = {env: config};
15+
}
16+
if (typeof config.env !== 'string') {
17+
throw new Error('must provide env')
18+
}
19+
const ALIAS = {
20+
'"dev"': 'dev',
21+
'development': 'dev',
22+
'"development"': 'dev',
23+
'"prod"': 'prod',
24+
'production': 'prod',
25+
'"production"': 'prod',
26+
'"test"': 'test',
27+
'testing': 'test',
28+
'"testing"': 'test',
29+
};
30+
const ENV = config.env.toLowerCase();
31+
32+
this._file = config.file || 'environment';
33+
this._alias = config.alias || ALIAS;
34+
this._env = this._alias[ENV] || ENV;
35+
}
36+
37+
isEnvFile(file: string): boolean {
38+
return file.indexOf(this._file + '.') !== -1;
39+
}
40+
41+
replaceFile(file: string): any {
42+
return file
43+
.replace(this._file, this._file + '.' + this._env);
44+
}
45+
46+
updateResult(result: any): any {
47+
['request', 'userRequest', 'resource']
48+
.filter((key) => { return result[key]; })
49+
.forEach((key) => {
50+
result[key] = this.replaceFile(result[key]);
51+
});
52+
53+
return result;
54+
}
55+
56+
apply(compiler: any): void {
57+
compiler.plugin('normal-module-factory', (normalModuleFactory: any) => {
58+
normalModuleFactory.plugin('after-resolve', (result, callback) => {
59+
var _resource = result['resource'];
60+
if (!this.isEnvFile(_resource)) {
61+
return callback(null, result);
62+
}
63+
var envFile = this.replaceFile(_resource);
64+
65+
fs.stat(envFile, (err, stats) => {
66+
if (err || !stats.isFile()) {
67+
var errorText = (!err && stats.isFile()) ? 'Is not a file.' : 'Does not exist.';
68+
console.log('\nWARNING:\n' + envFile + '\n' + errorText + ' ' + 'Using file\n' + _resource + '\n');
69+
return callback(null, result);
70+
}
71+
// mutate result
72+
var newResult = this.updateResult(result);
73+
return callback(null, newResult);
74+
});
75+
76+
});
77+
});
78+
}
79+
}

tests/e2e/e2e_workflow.spec.js

+10-7
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,6 @@ describe('Basic end-to-end Workflow', function () {
8282
expect(sh.exec('git status --porcelain').output).to.be.equal(undefined);
8383
});
8484

85-
xit('Supports production builds config file replacement', function() {
86-
var mainBundlePath = path.join(process.cwd(), 'dist', 'main.js');
87-
var mainBundleContent = fs.readFileSync(mainBundlePath, { encoding: 'utf8' });
88-
// production: true minimized turns into production:!0
89-
expect(mainBundleContent).to.include('production:!0');
90-
});
91-
9285
it_mobile('Enables mobile-specific production features in prod builds', () => {
9386
let indexHtml = fs.readFileSync(path.join(process.cwd(), 'dist/index.html'), 'utf-8');
9487
// Service Worker
@@ -106,6 +99,16 @@ describe('Basic end-to-end Workflow', function () {
10699
expect(indexHtml).to.match(/app works!/);
107100
});
108101

102+
it('Supports build config file replacement', function() {
103+
this.timeout(420000);
104+
105+
sh.exec(`${ngBin} build --dev`);
106+
var mainBundlePath = path.join(process.cwd(), 'dist', 'main.bundle.js');
107+
var mainBundleContent = fs.readFileSync(mainBundlePath, { encoding: 'utf8' });
108+
109+
expect(mainBundleContent).to.include('production: false');
110+
});
111+
109112
it('Can run `ng build` in created project', function () {
110113
this.timeout(420000);
111114

0 commit comments

Comments
 (0)