8000 fix(config): allow minimal config for build/serve (#3835) · davinkevin/angular-cli@f616158 · GitHub
[go: up one dir, main page]

Skip to content

Commit f616158

Browse files
authored
fix(config): allow minimal config for build/serve (angular#3835)
This PR fixes a lot of errors that would show up when `angular-cli.json` was missing some entries. A config as small as the following now works for `ng build/serve`: ``` { "apps": [ { "root": "src", "main": "main.ts" } ] } ``` In future PRs we should verify the rest of the commands work correctly with minimal configs.
1 parent e2b051f commit f616158

File tree

6 files changed

+57
-30
lines changed

6 files changed

+57
-30
lines changed

packages/angular-cli/lib/config/schema.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@
5959
"type": "string"
6060
},
6161
"tsconfig": {
62-
"type": "string"
62+
"type": "string",
63+
"default": "tsconfig.json"
6364
},
6465
"prefix": {
6566
"type": "string"

packages/angular-cli/models/json-schema/schema-tree.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,11 @@ export abstract class NonLeafSchemaTreeNode<T> extends SchemaTreeNode<T> {
134134
if (!schema['oneOf']) {
135135
type = schema['type'];
136136
} else {
137+
let testValue = value || schema['default'];
138+
// Match existing value to one of the schema types
137139
for (let testSchema of schema['oneOf']) {
138-
if ((testSchema['type'] === 'array' && Array.isArray(value))
139-
|| typeof value === testSchema['type']) {
140+
if ((testSchema['type'] === 'array' && Array.isArray(testValue))
141+
|| typeof testValue === testSchema['type']) {
140142
type = testSchema['type'];
141143
schema = testSchema;
142144
break;

packages/angular-cli/models/webpack-build-common.ts

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,8 @@ export function getWebpackCommonConfig(
4646
main: [appMain]
4747
};
4848

49-
if (!(environment in appConfig.environments)) {
50-
throw new SilentError(`Environment "${environment}" does not exist.`);
51-
}
52-
5349
// process global scripts
54-
if (appConfig.scripts.length > 0) {
50+
if (appConfig.scripts && appConfig.scripts.length > 0) {
5551
const globalScripts = extraEntryParser(appConfig.scripts, appRoot, 'scripts');
5652

5753
// add entry points and lazy chunks
@@ -67,7 +63,7 @@ export function getWebpackCommonConfig(
6763
}
6864

6965
// process global styles
70-
if (appConfig.styles.length === 0) {
66+
if (!appConfig.styles || appConfig.styles.length === 0) {
7167
// create css loaders for component css
7268
extraRules.push(...makeCssLoaders());
7369
} else {
@@ -104,6 +100,33 @@ export function getWebpackCommonConfig(
104100
}));
105101
}
106102

103+
// process environment file replacement
104+
if (appConfig.environments) {
105+
if (!('source' in appConfig.environments)) {
106+
throw new SilentError(`Environment configuration does not contain "source" entry.`);
107+
}
108+
if (!(environment in appConfig.environments)) {
109+
throw new SilentError(`Environment "${environment}" does not exist.`);
110+
}
111+
112+
extraPlugins.push(new webpack.NormalModuleReplacementPlugin(
113+
// This plugin is responsible for swapping the environment files.
114+
// Since it takes a RegExp as first parameter, we need to escape the path.
115+
// See https://webpack.github.io/docs/list-of-plugins.html#normalmodulereplacementplugin
116+
new RegExp(path.resolve(appRoot, appConfig.environments['source'])
117+
.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&')),
118+
path.resolve(appRoot, appConfig.environments[environment])
119+
));
120+
}
121+
122+
// process asset entries
123+
if (appConfig.assets) {
124+
extraPlugins.push(new GlobCopyWebpackPlugin({
125+
patterns: appConfig.assets,
126+
globOptions: { cwd: appRoot, dot: true, ignore: '**/.gitkeep' }
127+
}));
128+
}
129+
107130
if (progress) { extraPlugins.push(new ProgressPlugin({ profile: verbose, colors: true })); }
108131

109132
return {
@@ -145,22 +168,10 @@ export function getWebpackCommonConfig(
145168
new BaseHrefWebpackPlugin({
146169
baseHref: baseHref
147170
}),
148-
new webpack.NormalModuleReplacementPlugin(
149-
// This plugin is responsible for swapping the environment files.
150-
// Since it takes a RegExp as first parameter, we need to escape the path.
151-
// See https://webpack.github.io/docs/list-of-plugins.html#normalmodulereplacementplugin
152-
new RegExp(path.resolve(appRoot, appConfig.environments['source'])
153-
.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&')),
154-
path.resolve(appRoot, appConfig.environments[environment])
155-
),
156171
new webpack.optimize.CommonsChunkPlugin({
157172
minChunks: Infinity,
158173
name: 'inline'
159174
}),
160-
new GlobCopyWebpackPlugin({
161-
patterns: appConfig.assets,
162-
globOptions: { cwd: appRoot, dot: true, ignore: '**/.gitkeep' }
163-
}),
164175
new webpack.LoaderOptionsPlugin({
165176
test: /\.(css|scss|sass|less|styl)$/,
166177
options: {

packages/angular-cli/models/webpack-build-typescript.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ const webpackLoader: string = g['angularCliIsLocal']
99

1010

1111
export const getWebpackNonAotConfigPartial = function(projectRoot: string, appConfig: any) {
12+
let exclude = [ '**/*.spec.ts' ];
13+
if (appConfig.test) { exclude.push(path.join(projectRoot, appConfig.root, appConfig.test)); };
1214
return {
1315
module: {
1416
rules: [
@@ -23,10 +25,7 @@ export const getWebpackNonAotConfigPartial = function(projectRoot: string, appCo
2325
new AotPlugin({
2426
tsConfigPath: path.resolve(projectRoot, appConfig.root, appConfig.tsconfig),
2527
mainPath: path.join(projectRoot, appConfig.root, appConfig.main),
26-
exclude: [
27-
path.join(projectRoot, appConfig.root, appConfig.test),
28-
'**/*.spec.ts'
29-
],
28+
exclude: exclude,
3029
skipCodeGeneration: true
3130
}),
3231
]
@@ -35,6 +34,8 @@ export const getWebpackNonAotConfigPartial = function(projectRoot: string, appCo
3534

3635
export const getWebpackAotConfigPartial = function(projectRoot: string, appConfig: any,
3736
i18nFile: string, i18nFormat: string, locale: string) {
37+
let exclude = [ '**/*.spec.ts' ];
38+
if (appConfig.test) { exclude.push(path.join(projectRoot, appConfig.root, appConfig.test)); };
3839
return {
3940
module: {
4041
rules: [
@@ -52,10 +53,7 @@ export const getWebpackAotConfigPartial = function(projectRoot: string, appConfi
5253
i18nFile: i18nFile,
5354
i18nFormat: i18nFormat,
5455
locale: locale,
55-
exclude: [
56-
path.join(projectRoot, appConfig.root, appConfig.test),
57-
'**/*.spec.ts'
58-
]
56+
exclude: exclude
5957
})
6058
]
6159
};

packages/angular-cli/tasks/serve-webpack.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ export default Task.extend({
9898
proxy: proxyConfig,
9999
compress: serveTaskOptions.target === 'production',
100100
watchOptions: {
101-
poll: CliConfig.fromProject().config.defaults.poll
101+
poll: CliConfig.fromProject().config.defaults &&
102+
CliConfig.fromProject().config.defaults.poll
102103
},
103104
https: serveTaskOptions.ssl
104105
};
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { writeFile } from '../../utils/fs';
2+
import { ng } from '../../utils/process';
3+
4+
5+
export default function () {
6+
return Promise.resolve()
7+
.then(() => writeFile('angular-cli.json', JSON.stringify({
8+
apps: [{
9+
root: 'src',
10+
main: 'main.ts'
11+
}]
12+
})))
13+
.then(() => ng('build'));
14+
}

0 commit comments

Comments
 (0)
0