Skip to content

Commit 3dcd49b

Browse files
authored
feat(build): update angular-cli.json (#1633)
1 parent 3b226be commit 3dcd49b

30 files changed

+274
-186
lines changed

README.md

+17-8
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,14 @@ The generated project has dependencies that require **Node 4 or greater**.
3737
* [Generating a Route](#generating-a-route)
3838
* [Creating a Build](#creating-a-build)
3939
* [Build Targets and Environment Files](#build-targets-and-environment-files)
40-
* [Bundling](#bundling)
40+
* [Adding extra files to the build](#adding-extra-files-to-the-build)
4141
* [Running Unit Tests](#running-unit-tests)
4242
* [Running End-to-End Tests](#running-end-to-end-tests)
4343
* [Deploying the App via GitHub Pages](#deploying-the-app-via-github-pages)
4444
* [Linting and formatting code](#linting-and-formatting-code)
4545
* [Support for offline applications](#support-for-offline-applications)
4646
* [Commands autocompletion](#commands-autocompletion)
47+
* [Global styles](#global-styles)
4748
* [CSS preprocessor integration](#css-preprocessor-integration)
4849
* [3rd Party Library Installation](#3rd-party-library-installation)
4950
* [Updating angular-cli](#updating-angular-cli)
@@ -126,8 +127,8 @@ A build can specify both a build target (`development` or `production`) and an
126127
environment file to be used with that build. By default, the development build
127128
target is used.
128129

129-
At build time, `src/app/environments/environment.ts` will be replaced by
130-
`src/app/environments/environment.{NAME}.ts` where `NAME` is the argument
130+
At build time, `src/environments/environment.ts` will be replaced by
131+
`src/environments/environment.NAME.ts` where `NAME` is the argument
131132
provided to the `--environment` flag.
132133

133134
These options also apply to the serve command. If you do not pass a value for `environment`,
@@ -145,14 +146,15 @@ ng build --dev
145146
ng build
146147
```
147148

148-
You can also add your own env files other than `dev` and `prod` by creating a
149-
`src/app/environments/environment.{NAME}.ts` and use them by using the `--env=NAME`
150-
flag on the build/serve commands.
149+
You can also add your own env files other than `dev` and `prod` by doing the following:
150+
- create a `src/environments/environment.NAME.ts`
151+
- add `{ NAME: 'src/environments/environment.NAME.ts' }` to the the `apps[0].environments` object in `angular-cli.json`
152+
- use them by using the `--env=NAME` flag on the build/serve commands.
151153

152154
### Bundling
153155

154-
Builds created with the `-prod` flag via `ng build -prod` or `ng serve -prod` bundle
155-
all dependencies into a single file, and make use of tree-shaking techniques.
156+
All builds make use of bundling, and using the `--prod` flag in `ng build --prod`
157+
or `ng serve --prod` will also make use of uglifying and tree-shaking functionality.
156158

157159
### Running unit tests
158160

@@ -240,6 +242,13 @@ ng completion >> ~/.bash_profile
240242
source ~/.bash_profile
241243
```
242244

245+
### Global styles
246+
247+
The `styles.css` file allows users to add global styles and supports
248+
[CSS imports](https://developer.mozilla.org/en/docs/Web/CSS/@import).
249+
250+
If the project is created with the `--style=sass` option, this will be a `.sass`
251+
file instead, and the same applies to `scss/less/styl`.
243252

244253
### CSS Preprocessor integration
245254

addon/ng2/blueprints/component/index.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ module.exports = {
2525

2626
var defaultPrefix = '';
2727
if (this.project.ngConfig &&
28-
this.project.ngConfig.defaults &&
29-
this.project.ngConfig.defaults.prefix) {
30-
defaultPrefix = this.project.ngConfig.defaults.prefix + '-';
28+
this.project.ngConfig.apps[0] &&
29+
this.project.ngConfig.apps[0].prefix) {
30+
defaultPrefix = this.project.ngConfig.apps[0].prefix + '-';
3131
}
3232
var prefix = this.options.prefix ? defaultPrefix : '';
3333
this.selector = stringUtils.dasherize(prefix + parsedPath.name);
@@ -97,7 +97,7 @@ module.exports = {
9797
dir = dirParts.join(path.sep);
9898
}
9999
}
100-
var srcDir = this.project.ngConfig.defaults.sourceDir;
100+
var srcDir = this.project.ngConfig.apps[0].root;
101101
this.appDir = dir.substr(dir.indexOf(srcDir) + srcDir.length);
102102
this.generatePath = dir;
103103
return dir;
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
1-
export * from './environments/environment';
21
export * from './app.component';
32
export * from './app.module';

addon/ng2/blueprints/ng2/files/__path__/main.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
import "./polyfills.ts";
2+
13
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
24
import { enableProdMode } from '@angular/core';
3-
import { AppModule, environment } from './app/';
5+
import { environment } from './environments/environment';
6+
import { AppModule } from './app/';
47

58
if (environment.production) {
69
enableProdMode();

addon/ng2/blueprints/ng2/files/__path__/polyfills.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
// Prefer CoreJS over the polyfills above
1+
// This file includes polyfills needed by Angular 2 and is loaded before
2+
// the app. You can add your own extra polyfills to this file.
23
import 'core-js/es6/symbol';
34
import 'core-js/es6/object';
45
import 'core-js/es6/function';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/* You can add global styles to this file, and also import other style files */

addon/ng2/blueprints/ng2/files/__path__/test.ts

+1-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
1-
import 'core-js/es6';
2-
import 'core-js/es7/reflect';
1+
import "./polyfills.ts";
32

4-
// Typescript emit helpers polyfill
5-
import 'ts-helpers';
6-
7-
import 'zone.js/dist/zone';
83
import 'zone.js/dist/long-stack-trace-zone';
94
import 'zone.js/dist/jasmine-patch';
105
import 'zone.js/dist/async-test';

addon/ng2/blueprints/ng2/files/angular-cli.json

+15-5
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,21 @@
55
},
66
"apps": [
77
{
8-
"main": "<%= sourceDir %>/main.ts",
9-
"tsconfig": "<%= sourceDir %>/tsconfig.json",
10-
"mobile": <%= isMobile %>
8+
"root": "<%= sourceDir %>",
9+
"outDir": "dist",
10+
"assets": "assets",
11+
"index": "index.html",
12+
"main": "main.ts",
13+
"test": "test.ts",
14+
"tsconfig": "tsconfig.json",
15+
"prefix": "<%= prefix %>",
16+
"mobile": <%= isMobile %>,
17+
"styles": "styles.<%= styleExt %>",
18+
"environments": {
19+
"source": "environments/environment.ts",
20+
"prod": "environments/environment.prod.ts",
21+
"dev": "environments/environment.dev.ts"
22+
}
1123
}
1224
],
1325
"addons": [],
@@ -23,8 +35,6 @@
2335
}
2436
},
2537
"defaults": {
26-
"prefix": "<%= prefix %>",
27-
"sourceDir": "<%= sourceDir %>",
2838
"styleExt": "<%= styleExt %>",
2939
"prefixInterfaces": false,
3040
"lazyRoutePrefix": "+"

addon/ng2/blueprints/ng2/files/public/.npmignore

Whitespace-only changes.

addon/ng2/commands/github-pages-deploy.ts

+31-9
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ import * as chalk from 'chalk';
66
import * as fs from 'fs';
77
import * as fse from 'fs-extra';
88
import * as path from 'path';
9-
import * as BuildTask from 'ember-cli/lib/tasks/build';
9+
import * as WebpackBuild from '../tasks/build-webpack';
1010
import * as CreateGithubRepo from '../tasks/create-github-repo';
11+
import { CliConfig } from '../models/config';
1112

1213
const fsReadFile = Promise.denodeify(fs.readFile);
1314
const fsWriteFile = Promise.denodeify(fs.writeFile);
@@ -26,11 +27,16 @@ module.exports = Command.extend({
2627
type: String,
2728
default: 'new gh-pages version',
2829
description: 'The commit message to include with the build, must be wrapped in quotes.'
29-
}, {
30+
}, {
31+
name: 'target',
32+
type: String,
33+
default: 'production',
34+
aliases: ['t', { 'dev': 'development' }, { 'prod': 'production' }]
35+
}, {
3036
name: 'environment',
3137
type: String,
32-
default: 'production',
33-
description: 'The Angular environment to create a build for'
38+
default: '',
39+
aliases: ['e']
3440
}, {
3541
name: 'user-page',
3642
type: Boolean,
@@ -59,24 +65,40 @@ module.exports = Command.extend({
5965
var execOptions = {
6066
cwd: root
6167
};
68+
69+
if (options.environment === ''){
70+
if (options.target === 'development') {
71+
options.environment = 'dev';
72+
}
73+
if (options.target === 'production') {
74+
options.environment = 'prod';
75+
}
76+
}
77+
6278
var projectName = this.project.pkg.name;
6379

80+
const outDir = CliConfig.fromProject().apps[0].outDir;
81+
6482
let ghPagesBranch = 'gh-pages';
6583
let destinationBranch = options.userPage ? 'master' : ghPagesBranch;
6684
let initialBranch;
6785

6886
// declared here so that tests can stub exec
6987
const execPromise = Promise.denodeify(exec);
7088

71-
var buildTask = new BuildTask({
89+
var buildTask = new WebpackBuild({
7290
ui: this.ui,
7391
analytics: this.analytics,
74-
project: this.project
92+
cliProject: this.project,
93+
target: options.target,
94+
environment: options.environment,
95+
outputPath: outDir
7596
});
7697

7798
var buildOptions = {
99+
target: options.target,
78100
environment: options.environment,
79-
outputPath: 'dist/'
101+
outputPath: outDir
80102
};
81103

82104
var createGithubRepoTask = new CreateGithubRepo({
@@ -155,13 +177,13 @@ module.exports = Command.extend({
155177
}
156178

157179
function copyFiles() {
158-
return fsReadDir('dist')
180+
return fsReadDir(outDir)
159181
.then((files) => Promise.all(files.map((file) => {
160182
if (file === '.gitignore'){
161183
// don't overwrite the .gitignore file
162184
return Promise.resolve();
163185
}
164-
return fsCopy(path.join('dist', file), path.join('.', file))
186+
return fsCopy(path.join(outDir, file), path.join('.', file))
165187
})));
166188
}
167189

addon/ng2/commands/serve.ts

-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ module.exports = Command.extend({
4646
{ name: 'live-reload-live-css', type: Boolean, default: true, description: 'Whether to live reload CSS (default true)' },
4747
{ name: 'target', type: String, default: 'development', aliases: ['t', { 'dev': 'development' }, { 'prod': 'production' }] },
4848
{ name: 'environment', type: String, default: '', aliases: ['e'] },
49-
{ name: 'output-path', type: 'Path', default: 'dist/', aliases: ['op', 'out'] },
5049
{ name: 'ssl', type: Boolean, default: false },
5150
{ name: 'ssl-key', type: String, default: 'ssl/server.key' },
5251
{ name: 'ssl-cert', type: String, default: 'ssl/server.crt' }

addon/ng2/models/config.ts

+20-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as fs from 'fs';
22
import * as path from 'path';
3+
import * as chalk from 'chalk';
34

45
const schemaPath = path.resolve(process.env.CLI_ROOT, 'lib/config/schema.json');
56
const schema = require(schemaPath);
@@ -166,6 +167,24 @@ export class CliConfig {
166167

167168
public static fromProject(): any {
168169
const configPath = CliConfig._configFilePath();
169-
return configPath ? require(configPath) : {};
170+
171+
if (!configPath) {
172+
return {};
173+
}
174+
175+
let config = require(configPath);
176+
177+
if (config.defaults.sourceDir || config.defaults.prefix) {
178+
config.apps[0].root = config.apps[0].root || config.defaults.sourceDir;
179+
config.apps[0].prefix = config.apps[0].prefix || config.defaults.prefix;
180+
181+
console.error(chalk.yellow(
182+
'The "defaults.prefix" and "defaults.sourceDir" properties of angular-cli.json '
183+
+ 'are deprecated in favor of "apps[0].root" and "apps[0].prefix".\n'
184+
+ 'Please update in order to avoid errors in future versions of angular-cli.'
185+
));
186+
}
187+
188+
return config;
170189
}
171190
}

0 commit comments

Comments
 (0)