Skip to content

Commit fe12d34

Browse files
committed
chore: simplify config, deprecates npm_deps
Fix #629, #579
1 parent 662ea13 commit fe12d34

File tree

6 files changed

+62
-32
lines changed

6 files changed

+62
-32
lines changed

tools/config/project.config.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,26 @@
11
import {join} from 'path';
2-
import {SeedConfig, normalizeDependencies} from './seed.config';
2+
import {SeedConfig} from './seed.config';
3+
import {InjectableDependency} from './seed.config.interfaces';
34

45
export class ProjectConfig extends SeedConfig {
56
PROJECT_TASKS_DIR = join(process.cwd(), this.TOOLS_DIR, 'tasks', 'project');
67

78
constructor() {
89
super();
910
// this.APP_TITLE = 'Put name of your app here';
10-
let additional_deps: Array<any> = [
11+
let additional_deps: InjectableDependency[] = [
1112
// {src: 'jquery/dist/jquery.min.js', inject: 'libs'},
1213
// {src: 'lodash/lodash.min.js', inject: 'libs'},
1314
];
1415

15-
this.DEV_NPM_DEPENDENCIES = this.DEV_DEPENDENCIES.concat(normalizeDependencies(additional_deps));
16-
this.PROD_NPM_DEPENDENCIES = this.PROD_NPM_DEPENDENCIES.concat(normalizeDependencies(additional_deps));
16+
const oldDeps = this.NPM_DEPENDENCIES;
17+
18+
this.NPM_DEPENDENCIES = oldDeps.concat(additional_deps);
1719

1820
this.APP_ASSETS = [
1921
// {src: `${this.ASSETS_SRC}/css/toastr.min.css`, inject: true},
2022
// {src: `${this.APP_DEST}/assets/scss/global.css`, inject: true},
2123
{ src: `${this.ASSETS_SRC}/main.css`, inject: true },
2224
];
23-
24-
this.DEV_DEPENDENCIES = this.DEV_NPM_DEPENDENCIES.concat(this.APP_ASSETS);
25-
this.PROD_DEPENDENCIES = this.PROD_NPM_DEPENDENCIES.concat(this.APP_ASSETS);
2625
}
2726
}

tools/config/seed.config.interface.ts renamed to tools/config/seed.config.interfaces.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ export interface InjectableDependency {
22
src: string;
33
inject: string | boolean;
44
vendor?: boolean;
5+
env?: string[] | string;
56
}

tools/config/seed.config.ts

Lines changed: 49 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
import {readFileSync} from 'fs';
22
import {argv} from 'yargs';
33
import {normalize, join} from 'path';
4-
import {InjectableDependency} from './seed.config.interface';
4+
import {InjectableDependency} from './seed.config.interfaces';
55

6-
const ENVIRONMENTS = {
6+
interface Environments {
7+
DEVELOPMENT: string;
8+
PRODUCTION: string;
9+
[key: string]: string;
10+
}
11+
12+
const ENVIRONMENTS: Environments = {
713
DEVELOPMENT: 'dev',
814
PRODUCTION: 'prod'
915
};
@@ -50,26 +56,16 @@ export class SeedConfig {
5056

5157
NG2LINT_RULES = customRules();
5258

53-
54-
// Declare NPM dependencies (Note that globs should not be injected).
55-
DEV_NPM_DEPENDENCIES: InjectableDependency[] = normalizeDependencies([
59+
NPM_DEPENDENCIES: InjectableDependency[] = normalizeDependencies([
5660
{ src: 'systemjs/dist/system-polyfills.src.js', inject: 'shims' },
5761
{ src: 'reflect-metadata/Reflect.js', inject: 'shims' },
5862
{ src: 'es6-shim/es6-shim.js', inject: 'shims' },
5963
{ src: 'systemjs/dist/system.src.js', inject: 'shims' },
6064
{ src: 'angular2/bundles/angular2-polyfills.js', inject: 'shims' },
61-
{ src: 'rxjs/bundles/Rx.js', inject: 'libs' },
62-
{ src: 'angular2/bundles/angular2.js', inject: 'libs' },
63-
{ src: 'angular2/bundles/router.js', inject: 'libs' },
64-
{ src: 'angular2/bundles/http.js', inject: 'libs' }
65-
]);
66-
67-
PROD_NPM_DEPENDENCIES: InjectableDependency[] = normalizeDependencies([
68-
{ src: 'systemjs/dist/system-polyfills.src.js', inject: 'shims' },
69-
{ src: 'reflect-metadata/Reflect.js', inject: 'shims' },
70-
{ src: 'es6-shim/es6-shim.min.js', inject: 'shims' },
71-
{ src: 'systemjs/dist/system.js', inject: 'shims' },
72-
{ src: 'angular2/bundles/angular2-polyfills.min.js', inject: 'libs' }
65+
{ src: 'rxjs/bundles/Rx.js', inject: 'libs', env: ENVIRONMENTS.DEVELOPMENT },
66+
{ src: 'angular2/bundles/angular2.js', inject: 'libs', env: ENVIRONMENTS.DEVELOPMENT },
67+
{ src: 'angular2/bundles/router.js', inject: 'libs', env: ENVIRONMENTS.DEVELOPMENT },
68+
{ src: 'angular2/bundles/http.js', inject: 'libs', env: ENVIRONMENTS.DEVELOPMENT }
7369
]);
7470

7571
// Declare local files that needs to be injected
@@ -78,9 +74,33 @@ export class SeedConfig {
7874
];
7975

8076

81-
DEV_DEPENDENCIES = this.DEV_NPM_DEPENDENCIES.concat(this.APP_ASSETS);
82-
PROD_DEPENDENCIES = this.PROD_NPM_DEPENDENCIES.concat(this.APP_ASSETS);
77+
get PROD_DEPENDENCIES(): InjectableDependency[] {
78+
console.warn('The property "PROD_DEPENDENCIES" is deprecated. Consider using "DEPENDENCIES" instead.');
79+
return normalizeDependencies(this.NPM_DEPENDENCIES.filter(filterDependency.bind(null, ENVIRONMENTS.PRODUCTION)))
80+
.concat(this.APP_ASSETS.filter(filterDependency.bind(null, ENVIRONMENTS.PRODUCTION)));
81+
}
82+
83+
get DEV_DEPENDENCIES(): InjectableDependency[] {
84+
console.warn('The property "DEV_DEPENDENCIES" is deprecated. Consider using "DEPENDENCIES" instead.');
85+
return normalizeDependencies(this.NPM_DEPENDENCIES.filter(filterDependency.bind(null, ENVIRONMENTS.DEVELOPMENT)))
86+
.concat(this.APP_ASSETS.filter(filterDependency.bind(null, ENVIRONMENTS.DEVELOPMENT)));
87+
}
88+
89+
set DEV_DEPENDENCIES(val: InjectableDependency[]) {
90+
console.warn('The property "DEV_DEPENDENCIES" is deprecated. Consider using "DEPENDENCIES" instead.');
91+
}
92+
93+
set PROD_DEPENDENCIES(val: InjectableDependency[]) {
94+
console.warn('The property "PROD_DEPENDENCIES" is deprecated. Consider using "DEPENDENCIES" instead.');
95+
}
96+
97+
DEV_NPM_DEPENDENCIES: InjectableDependency[] = null;
98+
PROD_NPM_DEPENDENCIES: InjectableDependency[] = null;
8399

100+
get DEPENDENCIES(): InjectableDependency[] {
101+
return normalizeDependencies(this.NPM_DEPENDENCIES.filter(filterDependency.bind(null, this.ENV)))
102+
.concat(this.APP_ASSETS.filter(filterDependency.bind(null, this.ENV)));
103+
}
84104

85105
// ----------------
86106
// SystemsJS Configuration.
@@ -137,6 +157,16 @@ export class SeedConfig {
137157
// --------------
138158
// Utils.
139159

160+
function filterDependency(env: string, d: InjectableDependency): boolean {
161+
if (!d.env) {
162+
d.env = Object.keys(ENVIRONMENTS).map(k => ENVIRONMENTS[k]);
163+
}
164+
if (!(d.env instanceof Array)) {
165+
(<any>d).env = [d.env];
166+
}
167+
return d.env.indexOf(env) >= 0;
168+
}
169+
140170
export function normalizeDependencies(deps: InjectableDependency[]) {
141171
deps
142172
.filter((d:InjectableDependency) => !/\*/.test(d.src)) // Skip globs

tools/tasks/seed/build.bundles.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import * as gulp from 'gulp';
22
import * as gulpLoadPlugins from 'gulp-load-plugins';
33
import * as merge from 'merge-stream';
4-
import {PROD_DEPENDENCIES, JS_PROD_SHIMS_BUNDLE, JS_DEST} from '../../config';
4+
import {DEPENDENCIES, JS_PROD_SHIMS_BUNDLE, JS_DEST} from '../../config';
55
const plugins = <any>gulpLoadPlugins();
66

77
export = () => merge(bundleShims());
88

99
function getShims() {
10-
let libs = PROD_DEPENDENCIES
10+
let libs = DEPENDENCIES
1111
.filter(d => /\.js$/.test(d.src));
1212

1313
return libs.filter(l => l.inject === 'shims')

tools/tasks/seed/build.html_css.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import * as merge from 'merge-stream';
44
import * as autoprefixer from 'autoprefixer';
55
import * as cssnano from 'cssnano';
66
import {join} from 'path';
7-
import {APP_SRC, TMP_DIR, CSS_PROD_BUNDLE, CSS_DEST, APP_DEST, BROWSER_LIST, ENV, getEnvDependencies} from '../../config';
7+
import {APP_SRC, TMP_DIR, CSS_PROD_BUNDLE, CSS_DEST, APP_DEST, BROWSER_LIST, ENV, DEPENDENCIES} from '../../config';
88
const plugins = <any>gulpLoadPlugins();
99

1010
const processors = [
@@ -47,7 +47,7 @@ function processExternalCss() {
4747
}
4848

4949
function getExternalCss() {
50-
return getEnvDependencies().filter(d => /\.css$/.test(d.src));
50+
return DEPENDENCIES.filter(d => /\.css$/.test(d.src));
5151
}
5252

5353

tools/tasks/seed/build.index.dev.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as gulp from 'gulp';
22
import * as gulpLoadPlugins from 'gulp-load-plugins';
33
import {join} from 'path';
44
import * as slash from 'slash';
5-
import {APP_SRC, APP_DEST, APP_BASE, DEV_DEPENDENCIES} from '../../config';
5+
import {APP_SRC, APP_DEST, APP_BASE, DEPENDENCIES} from '../../config';
66
import {templateLocals} from '../../utils';
77
const plugins = <any>gulpLoadPlugins();
88

@@ -24,7 +24,7 @@ function inject(name?: string) {
2424
}
2525

2626
function getInjectablesDependenciesRef(name?: string) {
27-
return DEV_DEPENDENCIES
27+
return DEPENDENCIES
2828
.filter(dep => dep['inject'] && dep['inject'] === (name || true))
2929
.map(mapPath);
3030
}

0 commit comments

Comments
 (0)