Skip to content

Commit 8dd3456

Browse files
committed
feat(new): include app level import paths for style preprocessors
fixed linter errors re-run build-config-interface
1 parent defc269 commit 8dd3456

File tree

5 files changed

+80
-26
lines changed

5 files changed

+80
-26
lines changed

packages/angular-cli/lib/config/schema.d.ts

+18-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export interface CliConfig {
1212
apps?: {
1313
root?: string;
1414
outDir?: string;
15-
assets?: string;
15+
assets?: string | string[];
1616
deployUrl?: string;
1717
index?: string;
1818
main?: string;
@@ -23,17 +23,32 @@ export interface CliConfig {
2323
/**
2424
* Global styles to be included in the build.
2525
*/
26-
styles?: string[];
26+
styles?: (string | {
27+
[name: string]: any;
28+
input?: string;
29+
})[];
2730
/**
2831
* Global scripts to be included in the build.
2932
*/
30-
scripts?: string[];
33+
scripts?: (string | {
34+
[name: string]: any;
35+
input?: string;
36+
})[];
3137
/**
3238
* Name and corresponding file for environment config.
3339
*/
3440
environments?: {
3541
[name: string]: any;
3642
};
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+
};
3752
}[];
3853
/**
3954
* Configuration reserved for installed third party addons.

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

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

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

+46-5
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@ 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 } from './webpack-build-utils';
8+
import { CliConfig } from '../lib/config/schema.d';
9+
810

911
const autoprefixer = require('autoprefixer');
12+
const postcssDiscardComments = require('postcss-discard-comments');
1013
const ProgressPlugin = require('webpack/lib/ProgressPlugin');
1114
const HtmlWebpackPlugin = require('html-webpack-plugin');
1215
const SilentError = require('silent-error');
@@ -27,6 +30,7 @@ export function getWebpackCommonConfig(
2730
projectRoot: string,
2831
environment: string,
2932
appConfig: any,
33+
cliConfig: CliConfig,
3034
baseHref: string,
3135
sourcemap: boolean,
3236
vendorChunk: boolean,
@@ -46,6 +50,39 @@ export function getWebpackCommonConfig(
4650
main: [appMain]
4751
};
4852

53+
// Configure webpack style loaders
54+
55+
/**
56+
* Base settings for webpack style loaders
57+
* @type {Object}
58+
*/
59+
const baseStyleLoaderOptions = {
60+
sourceMap: sourcemap,
61+
};
62+
63+
// set default to base
64+
let styleLoaderOptions = baseStyleLoaderOptions;
65+
66+
if (appConfig.webpackStyleLoaderOptions) {
67+
if (appConfig.webpackStyleLoaderOptions.includePaths) {
68+
// resolve paths relative to project root
69+
let includePaths = appConfig.webpackStyleLoaderOptions.includePaths.map(
70+
(includePath: string) => path.resolve(projectRoot, includePath)
71+
);
72+
if (cliConfig.defaults.styleExt === 'styl') {
73+
// stylus uses paths
74+
styleLoaderOptions = Object.assign({}, baseStyleLoaderOptions, {
75+
paths: includePaths
76+
});
77+
} else {
78+
// less and sass use includePaths
79+
styleLoaderOptions = Object.assign({}, baseStyleLoaderOptions, {
80+
includePaths
81+
});
82+
}
83+
}
84+
}
85+
4986
if (!(environment in appConfig.environments)) {
5087
throw new SilentError(`Environment "${environment}" does not exist.`);
5188
}
@@ -163,11 +200,15 @@ export function getWebpackCommonConfig(
163200
new webpack.LoaderOptionsPlugin({
164201
test: /\.(css|scss|sass|less|styl)$/,
165202
options: {
166-
postcss: [autoprefixer()],
167-
cssLoader: { sourceMap: sourcemap },
168-
sassLoader: { sourceMap: sourcemap },
169-
lessLoader: { sourceMap: sourcemap },
170-
stylusLoader: { sourceMap: sourcemap },
203+
postcss: [
204+
autoprefixer(),
205+
// NOTE: Moved check here for prod build
206+
...(environment === 'prod' ? [postcssDiscardComments] : [])
207+
],
208+
cssLoader: styleLoaderOptions,
209+
sassLoader: styleLoaderOptions,
210+
lessLoader: styleLoaderOptions,
211+
stylusLoader: styleLoaderOptions,
171212
// context needed as a workaround https://github.com/jtangelder/sass-loader/issues/285
172213
context: projectRoot,
173214
},

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

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

86
declare module 'webpack' {
97
export interface LoaderOptionsPlugin {}
@@ -44,22 +42,6 @@ export const getWebpackProdConfigPartial = function(projectRoot: string,
4442
test: /\.js$|\.html$|\.css$/,
4543
threshold: 10240
4644
}),
47-
// LoaderOptionsPlugin needs to be fully duplicated because webpackMerge will replace it.
48-
new webpack.LoaderOptionsPlugin({
49-
test: /\.(css|scss|sass|less|styl)$/,
50-
options: {
51-
postcss: [
52-
autoprefixer(),
53-
postcssDiscardComments
54-
],
55-
cssLoader: { sourceMap: sourcemap },
56-
sassLoader: { sourceMap: sourcemap },
57-
lessLoader: { sourceMap: sourcemap },
58-
stylusLoader: { sourceMap: sourcemap },
59-
// context needed as a workaround https://github.com/jtangelder/sass-loader/issues/285
60-
context: projectRoot,
61-
}
62-
})
6345
]
6446
};
6547
};

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

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ export class NgCliWebpackConfig {
4444
this.ngCliProject.root,
4545
environment,
4646
appConfig,
47+
config.config,
4748
baseHref,
4849
sourcemap,
4950
vendorChunk,

0 commit comments

Comments
 (0)