Skip to content

Commit 446564d

Browse files
committed
feat(new): include app level import paths for style preprocessors
- fixed linter errors - re-run build-config-interface - refactored variable names to preprocessors instead of style - use cliConfig to determine preprocessor for project
1 parent b3ef6a4 commit 446564d

File tree

5 files changed

+157
-24
lines changed

5 files changed

+157
-24
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
export interface CliConfig {
2+
/**
3+
* The global configuration of the project.
4+
*/
5+
project?: {
6+
version?: string;
7+
name?: string;
8+
};
9+
/**
10+
* Properties of the different applications in this project.
11+
*/
12+
apps?: {
13+
root?: string;
14+
outDir?: string;
15+
assets?: string | string[];
16+
deployUrl?: string;
17+
index?: string;
18+
main?: string;
19+
test?: string;
20+
tsconfig?: string;
21+
prefix?: string;
22+
mobile?: boolean;
23+
/**
24+
* Global styles to be included in the build.
25+
*/
26+
styles?: (string | {
27+
[name: string]: any;
28+
input?: string;
29+
})[];
30+
/**
31+
* Global scripts to be included in the build.
32+
*/
33+
scripts?: (string | {
34+
[name: string]: any;
35+
input?: string;
36+
})[];
37+
/**
38+
* Name and corresponding file for environment config.
39+
*/
40+
environments?: {
41+
[name: string]: any;
42+
};
43+
/**
44+
* Options to pass to style loaders in webpack.
45+
*/
46+
webpackStyleLoaderOptions?: {
47+
/**
48+
* Paths to include. Paths will be resolved to project root.
49+
*/
50+
includePaths?: string[];
51+
};
52+
}[];
53+
/**
54+
* Configuration reserved for installed third party addons.
55+
*/
56+
addons?: {
57+
[name: string]: any;
58+
}[];
59+
/**
60+
* Configuration reserved for installed third party packages.
61+
*/
62+
packages?: {
63+
[name: string]: any;
64+
}[];
65+
e2e?: {
66+
protractor?: {
67+
config?: string;
68+
};
69+
};
70+
test?: {
71+
karma?: {
72+
config?: string;
73+
};
74+
};
75+
defaults?: {
76+
styleExt?: string;
77+
prefixInterfaces?: boolean;
78+
poll?: number;
79+
viewEncapsulation?: string;
80+
changeDetection?: string;
81+
inline?: {
82+
style?: boolean;
83+
template?: boolean;
84+
};
85+
spec?: {
86+
class?: boolean;
87+
component?: boolean;
88+
directive?: boolean;
89+
module?: boolean;
90+
pipe?: boolean;
91+
service?: boolean;
92+
};
93+
};
94+
}

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

+15
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,21 @@
115115
"description": "Name and corresponding file for environment config.",
116116
"type": "object",
117117
"additionalProperties": true
118+
},
119+
"webpackStyleLoaderOptions": {
120+
"description": "Options to pass to style loaders in webpack.",
121+
"type": "object",
122+
"properties": {
123+
"includePaths": {
124+
"description": "Paths to include. Paths will be resolved to project root.",
125+
"type": "array",
126+
"items": {
127+
"type": "string"
128+
},
129+
"default": []
130+
}
131+
},
132+
"additionalProperties": false
118133
}
119134
},
120135
"additionalProperties": false

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

+45-5
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ import { SuppressEntryChunksWebpackPlugin } from '../plugins/suppress-entry-chun
55
import { packageChunkSort } from '../utilities/package-chunk-sort';
66
import { BaseHrefWebpackPlugin } from '@angular-cli/base-href-webpack';
77
import { extraEntryParser, makeCssLoaders, getOutputHashFormat } from './webpack-build-utils';
8+
import { CliConfig } from '../lib/config/schema.d';
89

910
const autoprefixer = require('autoprefixer');
11+
const postcssDiscardComments = require('postcss-discard-comments');
1012
const ProgressPlugin = require('webpack/lib/ProgressPlugin');
1113
const HtmlWebpackPlugin = require('html-webpack-plugin');
1214
const ExtractTextPlugin = require('extract-text-webpack-plugin');
@@ -28,6 +30,7 @@ export function getWebpackCommonConfig(
2830
projectRoot: string,
2931
environment: string,
3032
appConfig: any,
33+
cliConfig: CliConfig,
3134
baseHref: string,
3235
sourcemap: boolean,
3336
vendorChunk: boolean,
@@ -49,6 +52,39 @@ export function getWebpackCommonConfig(
4952
entryPoints['main'] = [path.resolve(appRoot, appConfig.main)];
5053
}
5154

55+
// Configure webpack style loaders
56+
57+
/**
58+
* Base settings for webpack preprocessor loaders
59+
* @type {Object}
60+
*/
61+
const basePreprocessorLoaderOptions = {
62+
sourceMap: sourcemap,
63+
};
64+
65+
// set default to base
66+
let preprocessorLoaderOptions = basePreprocessorLoaderOptions;
67+
68+
if (appConfig.webpackStyleLoaderOptions) {
69+
if (appConfig.webpackStyleLoaderOptions.includePaths) {
70+
// resolve paths relative to project root
71+
let includePaths = appConfig.webpackStyleLoaderOptions.includePaths.map(
72+
(includePath: string) => path.resolve(projectRoot, includePath)
73+
);
74+
if (cliConfig.defaults.styleExt === 'styl' || cliConfig.defaults.styleExt === 'less') {
75+
// stylus and less uses paths
76+
preprocessorLoaderOptions = Object.assign({}, basePreprocessorLoaderOptions, {
77+
paths: includePaths
78+
});
79+
} else {
80+
// sass use includePaths
81+
preprocessorLoaderOptions = Object.assign({}, basePreprocessorLoaderOptions, {
82+
includePaths
83+
});
84+
}
85+
}
86+
}
87+
5288
// determine hashing format
5389
const hashFormat = getOutputHashFormat(outputHashing);
5490

@@ -191,11 +227,15 @@ export function getWebpackCommonConfig(
191227
new webpack.LoaderOptionsPlugin({
192228
test: /\.(css|scss|sass|less|styl)$/,
193229
options: {
194-
postcss: [autoprefixer()],
195-
cssLoader: { sourceMap: sourcemap },
196-
sassLoader: { sourceMap: sourcemap },
197-
lessLoader: { sourceMap: sourcemap },
198-
stylusLoader: { sourceMap: sourcemap },
230+
postcss: [
231+
autoprefixer(),
232+
// NOTE: Moved check here for prod build
233+
...(environment === 'prod' ? [postcssDiscardComments] : [])
234+
],
235+
cssLoader: preprocessorLoaderOptions,
236+
sassLoader: preprocessorLoaderOptions,
237+
lessLoader: preprocessorLoaderOptions,
238+
stylusLoader: preprocessorLoaderOptions,
199239
// context needed as a workaround https://github.com/jtangelder/sass-loader/issues/285
200240
context: projectRoot,
201241
},

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

-18
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import * as path from 'path';
22
import * as webpack from 'webpack';
33
import {CompressionPlugin} from '../lib/webpack/compression-plugin';
4-
const autoprefixer = require('autoprefixer');
5-
const postcssDiscardComments = require('postcss-discard-comments');
64

75
declare module 'webpack' {
86
export interface LoaderOptionsPlugin {}
@@ -37,22 +35,6 @@ export const getWebpackProdConfigPartial = function(projectRoot: string,
3735
test: /\.js$|\.html$|\.css$/,
3836
threshold: 10240
3937
}),
40-
// LoaderOptionsPlugin needs to be fully duplicated because webpackMerge will replace it.
41-
new webpack.LoaderOptionsPlugin({
42-
test: /\.(css|scss|sass|less|styl)$/,
43-
options: {
44-
postcss: [
45-
autoprefixer(),
46-
postcssDiscardComments
47-
],
48-
cssLoader: { sourceMap: sourcemap },
49-
sassLoader: { sourceMap: sourcemap },
50-
lessLoader: { sourceMap: sourcemap },
51-
stylusLoader: { sourceMap: sourcemap },
52-
// context needed as a workaround https://github.com/jtangelder/sass-loader/issues/285
53-
context: projectRoot,
54-
}
55-
})
5638
]
5739
};
5840
};

packages/angular-cli/models/webpack-config.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ export class NgCliWebpackConfig {
3535
deployUrl?: string,
3636
outputHashing?: string
3737
) {
38-
const appConfig = CliConfig.fromProject().config.apps[0];
38+
const cliConfig = CliConfig.fromProject().config;
39+
const appConfig = cliConfig.apps[0];
3940
const projectRoot = this.ngCliProject.root;
4041

4142
appConfig.outDir = outputDir || appConfig.outDir;
@@ -45,6 +46,7 @@ export class NgCliWebpackConfig {
4546
projectRoot,
4647
environment,
4748
appConfig,
49+
cliConfig,
4850
baseHref,
4951
sourcemap,
5052
vendorChunk,

0 commit comments

Comments
 (0)