-
Notifications
You must be signed in to change notification settings - Fork 12k
/
Copy pathstyles.ts
238 lines (215 loc) · 6.51 KB
/
styles.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import * as path from 'path';
import * as webpack from 'webpack';
import {
PostcssCliResources,
RawCssLoader,
RemoveHashPlugin,
SuppressExtractedTextChunksWebpackPlugin,
} from '../../plugins/webpack';
import { WebpackConfigOptions } from '../build-options';
import { getOutputHashFormat, normalizeExtraEntryPoints } from './utils';
const autoprefixer = require('autoprefixer');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const postcssImports = require('postcss-import');
/**
* Enumerate loaders and their dependencies from this file to let the dependency validator
* know they are used.
*
* require('style-loader')
* require('postcss-loader')
* require('stylus')
* require('stylus-loader')
* require('less')
* require('less-loader')
* require('node-sass')
* require('sass-loader')
*/
export function getStylesConfig(wco: WebpackConfigOptions) {
const { root, buildOptions } = wco;
const entryPoints: { [key: string]: string[] } = {};
const globalStylePaths: string[] = [];
const extraPlugins = [];
const cssSourceMap = buildOptions.stylesSourceMap;
// Determine hashing format.
const hashFormat = getOutputHashFormat(buildOptions.outputHashing as string);
// Convert absolute resource URLs to account for base-href and deploy-url.
const baseHref = buildOptions.baseHref || '';
const deployUrl = buildOptions.deployUrl || '';
const resourcesOutputPath = buildOptions.resourcesOutputPath || '';
const postcssPluginCreator = function (loader: webpack.loader.LoaderContext) {
return [
postcssImports({
resolve: (url: string) => url.startsWith('~') ? url.substr(1) : url,
load: (filename: string) => {
return new Promise<string>((resolve, reject) => {
loader.fs.readFile(filename, (err: Error, data: Buffer) => {
if (err) {
reject(err);
return;
}
const content = data.toString();
resolve(content);
});
});
},
}),
PostcssCliResources({
baseHref,
deployUrl,
resourcesOutputPath,
loader,
filename: `[name]${hashFormat.file}.[ext]`,
}),
autoprefixer(),
];
};
// use includePaths from appConfig
const includePaths: string[] = [];
let lessPathOptions: { paths?: string[] } = {};
if (buildOptions.stylePreprocessorOptions
&& buildOptions.stylePreprocessorOptions.includePaths
&& buildOptions.stylePreprocessorOptions.includePaths.length > 0
) {
buildOptions.stylePreprocessorOptions.includePaths.forEach((includePath: string) =>
includePaths.push(path.resolve(root, includePath)));
lessPathOptions = {
paths: includePaths,
};
}
// Process global styles.
if (buildOptions.styles.length > 0) {
const chunkNames: string[] = [];
normalizeExtraEntryPoints(buildOptions.styles, 'styles').forEach(style => {
const resolvedPath = path.resolve(root, style.input);
// Add style entry points.
if (entryPoints[style.bundleName]) {
entryPoints[style.bundleName].push(resolvedPath);
} else {
entryPoints[style.bundleName] = [resolvedPath];
}
// Add lazy styles to the list.
if (style.lazy) {
chunkNames.push(style.bundleName);
}
// Add global css paths.
globalStylePaths.push(resolvedPath);
});
if (chunkNames.length > 0) {
// Add plugin to remove hashes from lazy styles.
extraPlugins.push(new RemoveHashPlugin({ chunkNames, hashFormat }));
}
}
let dartSass: {} | undefined;
try {
// tslint:disable-next-line:no-implicit-dependencies
dartSass = require('sass');
} catch { }
let fiber: {} | undefined;
if (dartSass) {
try {
// tslint:disable-next-line:no-implicit-dependencies
fiber = require('fibers');
} catch { }
}
// set base rules to derive final rules from
const baseRules: webpack.RuleSetRule[] = [
{ test: /\.css$/, use: [] },
{
test: /\.scss$|\.sass$/,
use: [{
loader: 'sass-loader',
options: {
implementation: dartSass,
fiber,
sourceMap: cssSourceMap,
// bootstrap-sass requires a minimum precision of 8
precision: 8,
includePaths,
},
}],
},
{
test: /\.less$/,
use: [{
loader: 'less-loader',
options: {
sourceMap: cssSourceMap,
javascriptEnabled: true,
...lessPathOptions,
},
}],
},
{
test: /\.styl$/,
use: [{
loader: 'stylus-loader',
options: {
sourceMap: cssSourceMap,
paths: includePaths,
},
}],
},
];
// load component css as raw strings
const rules: webpack.RuleSetRule[] = baseRules.map(({ test, use }) => ({
exclude: globalStylePaths,
test,
use: [
{ loader: 'raw-loader' },
{
loader: 'postcss-loader',
options: {
ident: 'embedded',
plugins: postcssPluginCreator,
sourceMap: cssSourceMap && !buildOptions.hiddenSourceMap ? 'inline' : false,
},
},
...(use as webpack.Loader[]),
],
}));
// load global css as css files
if (globalStylePaths.length > 0) {
rules.push(...baseRules.map(({ test, use }) => {
return {
include: globalStylePaths,
test,
use: [
buildOptions.extractCss ? MiniCssExtractPlugin.loader : 'style-loader',
RawCssLoader,
{
loader: 'postcss-loader',
options: {
ident: buildOptions.extractCss ? 'extracted' : 'embedded',
plugins: postcssPluginCreator,
sourceMap: cssSourceMap
&& !buildOptions.extractCss
&& !buildOptions.hiddenSourceMap
? 'inline' : cssSourceMap,
},
},
...(use as webpack.Loader[]),
],
};
}));
}
if (buildOptions.extractCss) {
extraPlugins.push(
// extract global css from js files into own css file
new MiniCssExtractPlugin({ filename: `[name]${hashFormat.extract}.css` }),
// suppress empty .js files in css only entry points
new SuppressExtractedTextChunksWebpackPlugin(),
);
}
return {
entry: entryPoints,
module: { rules },
plugins: extraPlugins,
};
}