Skip to content

Commit 9f3e074

Browse files
committed
feat(build): update angular-cli.json
1 parent 504a497 commit 9f3e074

26 files changed

+301
-152
lines changed

README.md

+43-7
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ The generated project has dependencies that require **Node 4 or greater**.
3636
* [Creating a Build](#creating-a-build)
3737
* [Build Targets and Environment Files](#build-targets-and-environment-files)
3838
* [Bundling](#bundling)
39+
* [Adding extra files to the build](#adding-extra-files-to-the-build)
3940
* [Running Unit Tests](#running-unit-tests)
4041
* [Running End-to-End Tests](#running-end-to-end-tests)
4142
* [Deploying the App via GitHub Pages](#deploying-the-app-via-github-pages)
@@ -124,8 +125,8 @@ A build can specify both a build target (`development` or `production`) and an
124125
environment file to be used with that build. By default, the development build
125126
target is used.
126127

127-
At build time, `src/app/environments/environment.ts` will be replaced by
128-
`src/app/environments/environment.{NAME}.ts` where `NAME` is the argument
128+
At build time, `src/environments/environment.ts` will be replaced by
129+
`src/environments/environment.NAME.ts` where `NAME` is the argument
129130
provided to the `--environment` flag.
130131

131132
These options also apply to the serve command. If you do not pass a value for `environment`,
@@ -143,14 +144,49 @@ ng build --dev
143144
ng build
144145
```
145146

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

150152
### Bundling
151153

152-
Builds created with the `-prod` flag via `ng build -prod` or `ng serve -prod` bundle
153-
all dependencies into a single file, and make use of tree-shaking techniques.
154+
All builds make use of bundling, and using the `-prod` flag in `ng build -prod`
155+
or `ng serve -prod` will also make use of uglifying and tree-shaking functionality.
156+
157+
### Adding extra files to the build
158+
159+
The `apps[0].additionalEntries` array in `angular-cli.json` allows a user to add more files to the build.
160+
161+
The `additionalEntries` array supports two kinds of entries, with different behaviours.
162+
- strings will be bundled together with the `main` bundle
163+
- objects with `input` and `output` properties will create a new bundle which is loaded before the `main` bundle.
164+
165+
By default it looks like this:
166+
167+
```
168+
apps: [
169+
{
170+
//...
171+
"additionalEntries": [
172+
{ "input": "polyfills.ts", "output": "polyfills.js" },
173+
"scripts.ts",
174+
"styles.css"
175+
],
176+
}
177+
]
178+
```
179+
180+
These entries correspond to the following files in `src/`:
181+
- `polifills.ts` has browser polyfills needed for Angular 2. It will be
182+
bundled separately as `polyfills.js` and loaded before everything else.
183+
- `styles.css` allows users to add global styles and supports
184+
[CSS imports](https://developer.mozilla.org/en/docs/Web/CSS/@import). If the
185+
project is created with the `--style=sass` option, this will be a `.sass` file
186+
instead, and the same applies to `scss/less/styl`. It will be part of the main
187+
bundle.
188+
- `scripts.ts` allows users to add global code outside of the Angular 2 app. It
189+
will also be part of the main bundle.
154190

155191
### Running unit tests
156192

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
22
import { enableProdMode } from '@angular/core';
3-
import { AppModule, environment } from './app/';
3+
import { environment } from './environments/environment';
4+
import { AppModule } from './app/';
45

56
if (environment.production) {
67
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,2 @@
1+
// You can add additional imports to this file and
2+
// they will be loaded before your app
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/* You can add add global styles to this file, and also import other style files */

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

+19-5
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,25 @@
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+
"additionalEntries": [
18+
{ "input": "polyfills.ts", "output": "polyfills.js" },
19+
"scripts.ts",
20+
"styles.<%= styleExt %>"
21+
],
22+
"environments": {
23+
"source": "environments/environment.ts",
24+
"prod": "environments/environment.prod.ts",
25+
"dev": "environments/environment.dev.ts"
26+
}
1127
}
1228
],
1329
"addons": [],
@@ -23,8 +39,6 @@
2339
}
2440
},
2541
"defaults": {
26-
"prefix": "<%= prefix %>",
27-
"sourceDir": "<%= sourceDir %>",
2842
"styleExt": "<%= styleExt %>",
2943
"prefixInterfaces": false,
3044
"lazyRoutePrefix": "+"

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

Whitespace-only changes.

addon/ng2/models/webpack-build-common.ts

+74-19
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,53 @@ import * as webpack from 'webpack';
55
import { ForkCheckerPlugin } from 'awesome-typescript-loader';
66
import { CliConfig } from './config';
77

8-
export function getWebpackCommonConfig(projectRoot: string, sourceDir: string) {
8+
export function getWebpackCommonConfig(projectRoot: string, environment: string, appConfig: any) {
9+
10+
const appRoot = path.resolve(projectRoot, appConfig.root);
11+
const appMain = path.resolve(appRoot, appConfig.main);
12+
13+
const bundledWithMain = appConfig.additionalEntries
14+
.filter(entry => typeof entry === "string")
15+
.map(filename => path.resolve(appRoot, filename));
16+
17+
const separateBundles = appConfig.additionalEntries.filter(entry => typeof entry === "object");
18+
const entries = Object.assign(
19+
{ main: [appMain, ...bundledWithMain] },
20+
separateBundles.reduce((obj, entry) =>
21+
Object.assign(obj, { [entry.output]: path.resolve(appRoot, entry.input) }),
22+
{})
23+
);
24+
25+
const additionalFiles = separateBundles
26+
.map(entry => path.resolve(appRoot, entry.input))
27+
.concat(bundledWithMain);
28+
929
return {
1030
devtool: 'source-map',
1131
resolve: {
1232
extensions: ['', '.ts', '.js'],
13-
root: path.resolve(projectRoot, `./${sourceDir}`)
33+
root: appRoot
1434
},
1535
context: path.resolve(__dirname, './'),
16-
entry: {
17-
main: [path.resolve(projectRoot, `./${sourceDir}/main.ts`)],
18-
polyfills: path.resolve(projectRoot, `./${sourceDir}/polyfills.ts`)
19-
},
36+
entry: entries,
2037
output: {
21-
path: path.resolve(projectRoot, './dist'),
38+
path: path.resolve(projectRoot, appConfig.outDir),
2239
filename: '[name].bundle.js'
2340
},
2441
module: {
42+
<<<<<<< 504a497963252326cf9f2cbccb93e9eb8aab6dfa
43+
=======
44+
preLoaders: [
45+
{
46+
test: /\.js$/,
47+
loader: 'source-map-loader',
48+
exclude: [
49+
path.resolve(appRoot, 'node_modules/rxjs'),
50+
path.resolve(appRoot, 'node_modules/@angular'),
51+
]
52+
}
53+
],
54+
>>>>>>> feat(build): update angular-cli.json
2555
loaders: [
2656
{
2757
test: /\.ts$/,
@@ -30,7 +60,7 @@ export function getWebpackCommonConfig(projectRoot: string, sourceDir: string) {
3060
loader: 'awesome-typescript-loader',
3161
query: {
3262
useForkChecker: true,
33-
tsconfig: path.resolve(projectRoot, `./${sourceDir}/tsconfig.json`)
63+
tsconfig: path.resolve(appRoot, appConfig.tsconfig)
3464
}
3565
},
3666
{
@@ -39,23 +69,47 @@ export function getWebpackCommonConfig(projectRoot: string, sourceDir: string) {
3969
],
4070
exclude: [/\.(spec|e2e)\.ts$/]
4171
},
42-
{ test: /\.json$/, loader: 'json-loader'},
43-
{ test: /\.css$/, loaders: ['raw-loader', 'postcss-loader'] },
44-
{ test: /\.styl$/, loaders: ['raw-loader', 'postcss-loader', 'stylus-loader'] },
45-
{ test: /\.less$/, loaders: ['raw-loader', 'postcss-loader', 'less-loader'] },
46-
{ test: /\.scss$|\.sass$/, loaders: ['raw-loader', 'postcss-loader', 'sass-loader'] },
47-
{ test: /\.(jpg|png)$/, loader: 'url-loader?limit=128000'},
48-
{ test: /\.html$/, loader: 'raw-loader' }
72+
73+
// in main, load css as raw text
74+
       { exclude: additionalFiles, test: /\.css$/, loaders: ['raw-loader', 'postcss-loader'] },
75+
       { exclude: additionalFiles, test: /\.styl$/, loaders: ['raw-loader', 'postcss-loader', 'stylus-loader'] },
76+
       { exclude: additionalFiles, test: /\.less$/, loaders: ['raw-loader', 'postcss-loader', 'less-loader'] },
77+
       { exclude: additionalFiles, test: /\.scss$|\.sass$/, loaders: ['raw-loader', 'postcss-loader', 'sass-loader'] },
78+
79+
// outside of main, load it via style-loader
80+
       { include: additionalFiles, test: /\.css$/, loaders: ['style-loader', 'css-loader', 'postcss-loader'] },
81+
       { include: additionalFiles, test: /\.styl$/, loaders: ['style-loader', 'css-loader', 'postcss-loader', 'stylus-loader'] },
82+
       { include: additionalFiles, test: /\.less$/, loaders: ['style-loader', 'css-loader', 'postcss-loader', 'less-loader'] },
83+
       { include: additionalFiles, test: /\.scss$|\.sass$/, loaders: ['style-loader', 'css-loader', 'postcss-loader', 'sass-loader'] },
84+
85+
       { test: /\.json$/, loader: 'json-loader' },
86+
       { test: /\.(jpg|png)$/, loader: 'url-loader?limit=10000' },
87+
       { test: /\.html$/, loader: 'raw-loader' },
88+
89+
       { test: /\.(woff|woff2)(\?v=\d+\.\d+\.\d+)?$/, loader: 'url?limit=10000&mimetype=application/font-woff' },
90+
       { test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/, loader: 'url?limit=10000&mimetype=application/octet-stream' },
91+
       { test: /\.eot(\?v=\d+\.\d+\.\d+)?$/, loader: 'file' },
92+
       { test: /\.svg(\?v=\d+\.\d+\.\d+)?$/, loader: 'url?limit=10000&mimetype=image/svg+xml' }
4993
]
5094
},
5195
plugins: [
5296
new ForkCheckerPlugin(),
5397
new HtmlWebpackPlugin({
54-
template: path.resolve(projectRoot, `./${sourceDir}/index.html`),
98+
template: path.resolve(appRoot, appConfig.index),
5599
chunksSortMode: 'dependency'
56100
}),
101+
new webpack.NormalModuleReplacementPlugin(
102+
// escape the path to make a regex
103+
// TODO: this isn't working!
104+
new RegExp(path.resolve(appRoot, appConfig.environments.source)
105+
.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&")),
106+
path.resolve(appRoot, appConfig.environments[environment])
107+
),
57108
new webpack.optimize.CommonsChunkPlugin({
58-
name: ['polyfills']
109+
name: separateBundles
110+
.concat('main')
111+
.map(bundle => bundle.output)
112+
.reverse()
59113
}),
60114
new webpack.optimize.CommonsChunkPlugin({
61115
minChunks: Infinity,
@@ -64,9 +118,9 @@ export function getWebpackCommonConfig(projectRoot: string, sourceDir: string) {
64118
sourceMapFilename: 'inline.map'
65119
}),
66120
new CopyWebpackPlugin([{
67-
context: path.resolve(projectRoot, './public'),
121+
context: path.resolve(appRoot, appConfig.assets),
68122
from: '**/*',
69-
to: path.resolve(projectRoot, './dist')
123+
to: path.resolve(projectRoot, appConfig.outDir, appConfig.assets)
70124
}])
71125
],
72126
node: {
@@ -79,3 +133,4 @@ export function getWebpackCommonConfig(projectRoot: string, sourceDir: string) {
79133
}
80134
}
81135
};
136+

addon/ng2/models/webpack-build-development.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { CliConfig } from './config';
22
const path = require('path')
33

4-
export const getWebpackDevConfigPartial = function(projectRoot: string, sourceDir: string) {
4+
export const getWebpackDevConfigPartial = function(projectRoot: string, appConfig: any) {
55
return {
66
debug: true,
77
devtool: 'source-map',
@@ -22,7 +22,7 @@ export const getWebpackDevConfigPartial = function(projectRoot: string, sourceDi
2222
tslint: {
2323
emitErrors: false,
2424
failOnHint: false,
25-
resourcePath: path.resolve(projectRoot, `./${sourceDir}`)
25+
resourcePath: path.resolve(projectRoot, appConfig.root)
2626
},
2727
node: {
2828
fs: 'empty',

addon/ng2/models/webpack-build-mobile.ts

+8-6
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,25 @@ import * as CopyWebpackPlugin from 'copy-webpack-plugin';
55
import { PrerenderWebpackPlugin } from '../utilities/prerender-webpack-plugin.ts';
66
import { CliConfig } from './config';
77

8-
export const getWebpackMobileConfigPartial = function (projectRoot: string, sourceDir: string) {
8+
export const getWebpackMobileConfigPartial = function (projectRoot: string, appConfig: any) {
9+
// Hardcoded files and paths here should be part of appConfig when
10+
// reworking the mobile app functionality
911
return {
1012
plugins: [
1113
new CopyWebpackPlugin([
12-
{from: path.resolve(projectRoot, `./${sourceDir}/icons`), to: path.resolve(projectRoot, './dist/icons')},
13-
{from: path.resolve(projectRoot, `./${sourceDir}/manifest.webapp`), to: path.resolve(projectRoot, './dist')}
14+
{from: path.resolve(projectRoot, appConfig.root, 'icons'), to: path.resolve(projectRoot, appConfig.outDir, 'icons')},
15+
{from: path.resolve(projectRoot, appConfig.root, 'manifest.webapp'), to: path.resolve(projectRoot, appConfig.outDir)}
1416
]),
1517
new PrerenderWebpackPlugin({
1618
templatePath: 'index.html',
17-
configPath: path.resolve(projectRoot, `./${sourceDir}/main-app-shell.ts`),
18-
appPath: path.resolve(projectRoot, `./${sourceDir}`)
19+
configPath: path.resolve(projectRoot, appConfig.root, 'main-app-shell.ts'),
20+
appPath: path.resolve(projectRoot, appConfig.root)
1921
})
2022
]
2123
}
2224
};
2325

24-
export const getWebpackMobileProdConfigPartial = function (projectRoot: string, sourceDir: string) {
26+
export const getWebpackMobileProdConfigPartial = function (projectRoot: string, appConfig: any) {
2527
return {
2628
entry: {
2729
'sw-install': path.resolve(__dirname, '../utilities/sw-install.js')

addon/ng2/models/webpack-build-production.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import * as CompressionPlugin from 'compression-webpack-plugin';
55
import * as webpack from 'webpack';
66
import { CliConfig } from './config';
77

8-
export const getWebpackProdConfigPartial = function(projectRoot: string, sourceDir: string) {
8+
export const getWebpackProdConfigPartial = function(projectRoot: string, appConfig: any) {
99
return {
1010
debug: false,
1111
devtool: 'source-map',
@@ -47,7 +47,7 @@ export const getWebpackProdConfigPartial = function(projectRoot: string, sourceD
4747
tslint: {
4848
emitErrors: true,
4949
failOnHint: true,
50-
resourcePath: path.resolve(projectRoot, `./${sourceDir}`)
50+
resourcePath: path.resolve(projectRoot, appConfig.root)
5151
},
5252
htmlLoader: {
5353
minimize: true,

0 commit comments

Comments
 (0)