From 6c7ce3018bae2e36c552196df07c7f425342078a Mon Sep 17 00:00:00 2001 From: Sean Larkin Date: Sun, 17 Jul 2016 20:49:04 -0500 Subject: [PATCH 1/4] fix: add environment configuration replacement --- .../app}/environment.dev.ts | 0 .../app}/environment.prod.ts | 0 addon/ng2/models/webpack-config.ts | 3 +- addon/ng2/utilities/environment-plugin.ts | 87 +++++++++++++++++++ tests/e2e/e2e_workflow.spec.js | 11 ++- 5 files changed, 96 insertions(+), 5 deletions(-) rename addon/ng2/blueprints/ng2/files/{config => __path__/app}/environment.dev.ts (100%) rename addon/ng2/blueprints/ng2/files/{config => __path__/app}/environment.prod.ts (100%) create mode 100644 addon/ng2/utilities/environment-plugin.ts diff --git a/addon/ng2/blueprints/ng2/files/config/environment.dev.ts b/addon/ng2/blueprints/ng2/files/__path__/app/environment.dev.ts similarity index 100% rename from addon/ng2/blueprints/ng2/files/config/environment.dev.ts rename to addon/ng2/blueprints/ng2/files/__path__/app/environment.dev.ts diff --git a/addon/ng2/blueprints/ng2/files/config/environment.prod.ts b/addon/ng2/blueprints/ng2/files/__path__/app/environment.prod.ts similarity index 100% rename from addon/ng2/blueprints/ng2/files/config/environment.prod.ts rename to addon/ng2/blueprints/ng2/files/__path__/app/environment.prod.ts diff --git a/addon/ng2/models/webpack-config.ts b/addon/ng2/models/webpack-config.ts index e5cd3e00f294..95e3ddb3d0d6 100644 --- a/addon/ng2/models/webpack-config.ts +++ b/addon/ng2/models/webpack-config.ts @@ -1,4 +1,5 @@ import { CliConfig } from './config'; +import { NgCliEnvironmentPlugin } from '../utilities/environment-plugin'; import { getWebpackCommonConfig, getWebpackDevConfigPartial, @@ -35,6 +36,7 @@ export class NgCliWebpackConfig { } this.generateConfig(); + this.config.plugins.unshift(new NgCliEnvironmentPlugin({env: this.environment})); } generateConfig(): void { @@ -59,4 +61,3 @@ export class NgCliWebpackConfig { } } } - diff --git a/addon/ng2/utilities/environment-plugin.ts b/addon/ng2/utilities/environment-plugin.ts new file mode 100644 index 000000000000..f9c761279447 --- /dev/null +++ b/addon/ng2/utilities/environment-plugin.ts @@ -0,0 +1,87 @@ +import * as fs from 'fs'; + +interface WebpackPlugin { + apply(compiler: any): void; +} + +interface NgCliEnvrionmentConfig { + env?: string; + file?: string; + alias?: string; +} + +export class NgCliEnvironmentPlugin implements WebpackPlugin { + _file: string; + _alias: string; + _env: string; + + constructor(config: any) { + if (typeof config === 'string') { + config = {env: config}; + } + if (typeof config.env !== 'string') { + throw new Error('must provide env') + } + const ALIAS = { + '"dev"': 'dev', + 'development': 'dev', + '"development"': 'dev', + '"prod"': 'prod', + 'production': 'prod', + '"production"': 'prod', + '"test"': 'test', + 'testing': 'test', + '"testing"': 'test', + }; + const ENV = config.env.toLowerCase(); + + this._file = config.file || 'environment'; + this._alias = config.alias || ALIAS; + this._env = this._alias[ENV] || ENV; + } + + isEnvFile(file: string): boolean { + return file.indexOf(this._file + '.') !== -1; + } + + replaceFile(file: string): any { + debugger; + return file + .replace(this._file, this._file + '.' + this._env); + } + + updateResult(result: any): any { + ['request', 'userRequest', 'resource'] + .filter((key) => { return result[key]; }) + .forEach((key) => { + result[key] = this.replaceFile(result[key]); + }); + + return result; + } + + apply(compiler: any): void { + compiler.plugin('normal-module-factory', (normalModuleFactory: any) => { + normalModuleFactory.plugin('after-resolve', (result, callback) => { + var _resource = result['resource']; + if (!this.isEnvFile(_resource)) { + return callback(null, result); + } + debugger; + var envFile = this.replaceFile(_resource); + + fs.stat(envFile, (err, stats) => { + if (err || !stats.isFile()) { + var errorText = (!err && stats.isFile()) ? 'Is not a file.' : 'Does not exist.'; + console.log('\nWARNING:\n' + envFile + '\n' + errorText + ' ' + 'Using file\n' + _resource + '\n'); + return callback(null, result); + } + // mutate result + var newResult = this.updateResult(result); + return callback(null, newResult); + }); + + }); + }); + } +} diff --git a/tests/e2e/e2e_workflow.spec.js b/tests/e2e/e2e_workflow.spec.js index 3864e7fc9c89..0ad9708e6d58 100644 --- a/tests/e2e/e2e_workflow.spec.js +++ b/tests/e2e/e2e_workflow.spec.js @@ -82,11 +82,14 @@ describe('Basic end-to-end Workflow', function () { expect(sh.exec('git status --porcelain').output).to.be.equal(undefined); }); - xit('Supports production builds config file replacement', function() { - var mainBundlePath = path.join(process.cwd(), 'dist', 'main.js'); + it('Supports production builds config file replacement', function() { + this.timeout(420000); + + sh.exec(`${ngBin} build --dev`); + var mainBundlePath = path.join(process.cwd(), 'dist', 'main.bundle.js'); var mainBundleContent = fs.readFileSync(mainBundlePath, { encoding: 'utf8' }); - // production: true minimized turns into production:!0 - expect(mainBundleContent).to.include('production:!0'); + // production: true minimized turns into production:!1 + expect(mainBundleContent).to.include('production: false'); }); it_mobile('Enables mobile-specific production features in prod builds', () => { From d8f399f70e22bcd1bc2d42ad54becd27daedcfb0 Mon Sep 17 00:00:00 2001 From: Sean Larkin Date: Sun, 17 Jul 2016 21:01:10 -0500 Subject: [PATCH 2/4] remove debuggers --- addon/ng2/utilities/environment-plugin.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/addon/ng2/utilities/environment-plugin.ts b/addon/ng2/utilities/environment-plugin.ts index f9c761279447..6149840e4a8c 100644 --- a/addon/ng2/utilities/environment-plugin.ts +++ b/addon/ng2/utilities/environment-plugin.ts @@ -45,7 +45,6 @@ export class NgCliEnvironmentPlugin implements WebpackPlugin { } replaceFile(file: string): any { - debugger; return file .replace(this._file, this._file + '.' + this._env); } @@ -67,7 +66,6 @@ export class NgCliEnvironmentPlugin implements WebpackPlugin { if (!this.isEnvFile(_resource)) { return callback(null, result); } - debugger; var envFile = this.replaceFile(_resource); fs.stat(envFile, (err, stats) => { From 17a36b824edf24578a75c25f4499196b7a756da6 Mon Sep 17 00:00:00 2001 From: Sean Larkin Date: Mon, 18 Jul 2016 08:30:42 -0500 Subject: [PATCH 3/4] fix: move mobile test above config test because it relies on prod build from previous step --- tests/e2e/e2e_workflow.spec.js | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/tests/e2e/e2e_workflow.spec.js b/tests/e2e/e2e_workflow.spec.js index 0ad9708e6d58..82bd1012ce98 100644 --- a/tests/e2e/e2e_workflow.spec.js +++ b/tests/e2e/e2e_workflow.spec.js @@ -82,16 +82,6 @@ describe('Basic end-to-end Workflow', function () { expect(sh.exec('git status --porcelain').output).to.be.equal(undefined); }); - it('Supports production builds config file replacement', function() { - this.timeout(420000); - - sh.exec(`${ngBin} build --dev`); - var mainBundlePath = path.join(process.cwd(), 'dist', 'main.bundle.js'); - var mainBundleContent = fs.readFileSync(mainBundlePath, { encoding: 'utf8' }); - // production: true minimized turns into production:!1 - expect(mainBundleContent).to.include('production: false'); - }); - it_mobile('Enables mobile-specific production features in prod builds', () => { let indexHtml = fs.readFileSync(path.join(process.cwd(), 'dist/index.html'), 'utf-8'); // Service Worker @@ -109,6 +99,16 @@ describe('Basic end-to-end Workflow', function () { expect(indexHtml).to.match(/app works!/); }); + it('Supports production builds config file replacement', function() { + this.timeout(420000); + + sh.exec(`${ngBin} build --dev`); + var mainBundlePath = path.join(process.cwd(), 'dist', 'main.bundle.js'); + var mainBundleContent = fs.readFileSync(mainBundlePath, { encoding: 'utf8' }); + // production: true minimized turns into production:!1 + expect(mainBundleContent).to.include('production: false'); + }); + it('Can run `ng build` in created project', function () { this.timeout(420000); From a2de51f9b5c510903247a987ce9b6de294e52594 Mon Sep 17 00:00:00 2001 From: Sean Larkin Date: Mon, 18 Jul 2016 12:12:17 -0500 Subject: [PATCH 4/4] fix: removed unneeded interface moved config paths --- .../__path__/app/{ => environments}/environment.dev.ts | 0 .../__path__/app/{ => environments}/environment.prod.ts | 0 .../files/__path__/app/{ => environments}/environment.ts | 0 addon/ng2/blueprints/ng2/files/__path__/app/index.ts | 2 +- addon/ng2/utilities/environment-plugin.ts | 6 ------ tests/e2e/e2e_workflow.spec.js | 4 ++-- 6 files changed, 3 insertions(+), 9 deletions(-) rename addon/ng2/blueprints/ng2/files/__path__/app/{ => environments}/environment.dev.ts (100%) rename addon/ng2/blueprints/ng2/files/__path__/app/{ => environments}/environment.prod.ts (100%) rename addon/ng2/blueprints/ng2/files/__path__/app/{ => environments}/environment.ts (100%) diff --git a/addon/ng2/blueprints/ng2/files/__path__/app/environment.dev.ts b/addon/ng2/blueprints/ng2/files/__path__/app/environments/environment.dev.ts similarity index 100% rename from addon/ng2/blueprints/ng2/files/__path__/app/environment.dev.ts rename to addon/ng2/blueprints/ng2/files/__path__/app/environments/environment.dev.ts diff --git a/addon/ng2/blueprints/ng2/files/__path__/app/environment.prod.ts b/addon/ng2/blueprints/ng2/files/__path__/app/environments/environment.prod.ts similarity index 100% rename from addon/ng2/blueprints/ng2/files/__path__/app/environment.prod.ts rename to addon/ng2/blueprints/ng2/files/__path__/app/environments/environment.prod.ts diff --git a/addon/ng2/blueprints/ng2/files/__path__/app/environment.ts b/addon/ng2/blueprints/ng2/files/__path__/app/environments/environment.ts similarity index 100% rename from addon/ng2/blueprints/ng2/files/__path__/app/environment.ts rename to addon/ng2/blueprints/ng2/files/__path__/app/environments/environment.ts diff --git a/addon/ng2/blueprints/ng2/files/__path__/app/index.ts b/addon/ng2/blueprints/ng2/files/__path__/app/index.ts index 30c9dfb021b8..7aa84b623989 100644 --- a/addon/ng2/blueprints/ng2/files/__path__/app/index.ts +++ b/addon/ng2/blueprints/ng2/files/__path__/app/index.ts @@ -1,2 +1,2 @@ -export * from './environment'; +export * from './environments/environment'; export * from './app.component'; diff --git a/addon/ng2/utilities/environment-plugin.ts b/addon/ng2/utilities/environment-plugin.ts index 6149840e4a8c..6825769d8f95 100644 --- a/addon/ng2/utilities/environment-plugin.ts +++ b/addon/ng2/utilities/environment-plugin.ts @@ -4,12 +4,6 @@ interface WebpackPlugin { apply(compiler: any): void; } -interface NgCliEnvrionmentConfig { - env?: string; - file?: string; - alias?: string; -} - export class NgCliEnvironmentPlugin implements WebpackPlugin { _file: string; _alias: string; diff --git a/tests/e2e/e2e_workflow.spec.js b/tests/e2e/e2e_workflow.spec.js index 82bd1012ce98..053a4d714360 100644 --- a/tests/e2e/e2e_workflow.spec.js +++ b/tests/e2e/e2e_workflow.spec.js @@ -99,13 +99,13 @@ describe('Basic end-to-end Workflow', function () { expect(indexHtml).to.match(/app works!/); }); - it('Supports production builds config file replacement', function() { + it('Supports build config file replacement', function() { this.timeout(420000); sh.exec(`${ngBin} build --dev`); var mainBundlePath = path.join(process.cwd(), 'dist', 'main.bundle.js'); var mainBundleContent = fs.readFileSync(mainBundlePath, { encoding: 'utf8' }); - // production: true minimized turns into production:!1 + expect(mainBundleContent).to.include('production: false'); });