5
5
* Use of this source code is governed by an MIT-style license that can be
6
6
* found in the LICENSE file at https://angular.io/license
7
7
*/
8
- // tslint:disable
9
- // TODO: cleanup this file, it's copied as is from Angular CLI.
10
8
11
- import * as webpack from 'webpack' ;
12
9
import * as path from 'path' ;
13
- import { SuppressExtractedTextChunksWebpackPlugin } from '../../plugins/webpack' ;
14
- import { getOutputHashFormat } from './utils' ;
10
+ import * as webpack from 'webpack' ;
11
+ import {
12
+ PostcssCliResources ,
13
+ RawCssLoader ,
14
+ RemoveHashPlugin ,
15
+ SuppressExtractedTextChunksWebpackPlugin ,
16
+ } from '../../plugins/webpack' ;
15
17
import { WebpackConfigOptions } from '../build-options' ;
16
- import { findUp } from '../../utilities/find-up' ;
17
- import { RawCssLoader } from '../../plugins/webpack' ;
18
- import { normalizeExtraEntryPoints } from './utils' ;
19
- import { RemoveHashPlugin } from '../../plugins/remove-hash-plugin' ;
18
+ import { getOutputHashFormat , normalizeExtraEntryPoints } from './utils' ;
20
19
21
- const postcssUrl = require ( 'postcss-url' ) ;
22
20
const autoprefixer = require ( 'autoprefixer' ) ;
23
21
const MiniCssExtractPlugin = require ( 'mini-css-extract-plugin' ) ;
24
22
const postcssImports = require ( 'postcss-import' ) ;
25
- const PostcssCliResources = require ( '../../plugins/webpack' ) . PostcssCliResources ;
26
23
27
24
/**
28
25
* Enumerate loaders and their dependencies from this file to let the dependency validator
@@ -38,19 +35,11 @@ const PostcssCliResources = require('../../plugins/webpack').PostcssCliResources
38
35
* require('sass-loader')
39
36
*/
40
37
41
- interface PostcssUrlAsset {
42
- url : string ;
43
- hash : string ;
44
- absolutePath : string ;
45
- }
46
-
47
38
export function getStylesConfig ( wco : WebpackConfigOptions ) {
48
- const { root, projectRoot, buildOptions } = wco ;
49
-
50
- // const appRoot = path.resolve(projectRoot, appConfig.root);
39
+ const { root, buildOptions } = wco ;
51
40
const entryPoints : { [ key : string ] : string [ ] } = { } ;
52
41
const globalStylePaths : string [ ] = [ ] ;
53
- const extraPlugins : any [ ] = [ ] ;
42
+ const extraPlugins = [ ] ;
54
43
const cssSourceMap = buildOptions . sourceMap ;
55
44
56
45
// Determine hashing format.
@@ -62,80 +51,25 @@ export function getStylesConfig(wco: WebpackConfigOptions) {
62
51
const postcssPluginCreator = function ( loader : webpack . loader . LoaderContext ) {
63
52
return [
64
53
postcssImports ( {
65
- resolve : ( url : string , context : string ) => {
66
- return new Promise < string > ( ( resolve , reject ) => {
67
- let hadTilde = false ;
68
- if ( url && url . startsWith ( '~' ) ) {
69
- url = url . substr ( 1 ) ;
70
- hadTilde = true ;
71
- }
72
- loader . resolve ( context , ( hadTilde ? '' : './' ) + url , ( err : Error , result : string ) => {
73
- if ( err ) {
74
- if ( hadTilde ) {
75
- reject ( err ) ;
76
- return ;
77
- }
78
- loader . resolve ( context , url , ( err : Error , result : string ) => {
79
- if ( err ) {
80
- reject ( err ) ;
81
- } else {
82
- resolve ( result ) ;
83
- }
84
- } ) ;
85
- } else {
86
- resolve ( result ) ;
87
- }
88
- } ) ;
89
- } ) ;
90
- } ,
54
+ resolve : ( url : string ) => url . startsWith ( '~' ) ? url . substr ( 1 ) : url ,
91
55
load : ( filename : string ) => {
92
56
return new Promise < string > ( ( resolve , reject ) => {
93
57
loader . fs . readFile ( filename , ( err : Error , data : Buffer ) => {
94
58
if ( err ) {
95
59
reject ( err ) ;
60
+
96
61
return ;
97
62
}
98
63
99
64
const content = data . toString ( ) ;
100
65
resolve ( content ) ;
101
66
} ) ;
102
67
} ) ;
103
- }
104
- } ) ,
105
- postcssUrl ( {
106
- filter : ( { url } : PostcssUrlAsset ) => url . startsWith ( '~' ) ,
107
- url : ( { url } : PostcssUrlAsset ) => {
108
- // Note: This will only find the first node_modules folder.
109
- const nodeModules = findUp ( 'node_modules' , projectRoot ) ;
110
- if ( ! nodeModules ) {
111
- throw new Error ( 'Cannot locate node_modules directory.' )
112
- }
113
- const fullPath = path . join ( nodeModules , url . substr ( 1 ) ) ;
114
- return path . relative ( loader . context , fullPath ) . replace ( / \\ / g, '/' ) ;
115
- }
68
+ } ,
116
69
} ) ,
117
- postcssUrl ( [
118
- {
119
- // Only convert root relative URLs, which CSS-Loader won't process into require().
120
- filter : ( { url } : PostcssUrlAsset ) => url . startsWith ( '/' ) && ! url . startsWith ( '//' ) ,
121
- url : ( { url } : PostcssUrlAsset ) => {
122
- if ( deployUrl . match ( / : \/ \/ / ) || deployUrl . startsWith ( '/' ) ) {
123
- // If deployUrl is absolute or root relative, ignore baseHref & use deployUrl as is.
124
- return `${ deployUrl . replace ( / \/ $ / , '' ) } ${ url } ` ;
125
- } else if ( baseHref . match ( / : \/ \/ / ) ) {
126
- // If baseHref contains a scheme, include it as is.
127
- return baseHref . replace ( / \/ $ / , '' ) +
128
- `/${ deployUrl } /${ url } ` . replace ( / \/ \/ + / g, '/' ) ;
129
- } else {
130
- // Join together base-href, deploy-url and the original URL.
131
- // Also dedupe multiple slashes into single ones.
132
- return `/${ baseHref } /${ deployUrl } /${ url } ` . replace ( / \/ \/ + / g, '/' ) ;
133
- }
134
- }
135
- }
136
- ] ) ,
137
70
PostcssCliResources ( {
138
- deployUrl : loader . loaders [ loader . loaderIndex ] . options . ident == 'extracted' ? '' : deployUrl ,
71
+ baseHref,
72
+ deployUrl,
139
73
loader,
140
74
filename : `[name]${ hashFormat . file } .[ext]` ,
141
75
} ) ,
@@ -164,12 +98,11 @@ export function getStylesConfig(wco: WebpackConfigOptions) {
164
98
165
99
normalizeExtraEntryPoints ( buildOptions . styles , 'styles' ) . forEach ( style => {
166
100
const resolvedPath = path . resolve ( root , style . input ) ;
167
-
168
101
// Add style entry points.
169
102
if ( entryPoints [ style . bundleName ] ) {
170
- entryPoints [ style . bundleName ] . push ( resolvedPath )
103
+ entryPoints [ style . bundleName ] . push ( resolvedPath ) ;
171
104
} else {
172
- entryPoints [ style . bundleName ] = [ resolvedPath ]
105
+ entryPoints [ style . bundleName ] = [ resolvedPath ] ;
173
106
}
174
107
175
108
// Add lazy styles to the list.
@@ -189,18 +122,20 @@ export function getStylesConfig(wco: WebpackConfigOptions) {
189
122
190
123
let dartSass : { } | undefined ;
191
124
try {
125
+ // tslint:disable-next-line:no-implicit-dependencies
192
126
dartSass = require ( 'sass' ) ;
193
127
} catch { }
194
128
195
129
let fiber : { } | undefined ;
196
130
if ( dartSass ) {
197
131
try {
132
+ // tslint:disable-next-line:no-implicit-dependencies
198
133
fiber = require ( 'fibers' ) ;
199
134
} catch { }
200
135
}
201
136
202
137
// set base rules to derive final rules from
203
- const baseRules : webpack . Rule [ ] = [
138
+ const baseRules : webpack . RuleSetRule [ ] = [
204
139
{ test : / \. c s s $ / , use : [ ] } ,
205
140
{
206
141
test : / \. s c s s $ | \. s a s s $ / ,
@@ -212,9 +147,9 @@ export function getStylesConfig(wco: WebpackConfigOptions) {
212
147
sourceMap : cssSourceMap ,
213
148
// bootstrap-sass requires a minimum precision of 8
214
149
precision : 8 ,
215
- includePaths
216
- }
217
- } ]
150
+ includePaths,
151
+ } ,
152
+ } ] ,
218
153
} ,
219
154
{
220
155
test : / \. l e s s $ / ,
@@ -224,23 +159,23 @@ export function getStylesConfig(wco: WebpackConfigOptions) {
224
159
sourceMap : cssSourceMap ,
225
160
javascriptEnabled : true ,
226
161
...lessPathOptions ,
227
- }
228
- } ]
162
+ } ,
163
+ } ] ,
229
164
} ,
230
165
{
231
166
test : / \. s t y l $ / ,
232
167
use : [ {
233
168
loader : 'stylus-loader' ,
234
169
options : {
235
170
sourceMap : cssSourceMap ,
236
- paths : includePaths
237
- }
238
- } ]
239
- }
171
+ paths : includePaths ,
172
+ } ,
173
+ } ] ,
174
+ } ,
240
175
] ;
241
176
242
177
// load component css as raw strings
243
- const rules : webpack . Rule [ ] = baseRules . map ( ( { test, use } ) => ( {
178
+ const rules : webpack . RuleSetRule [ ] = baseRules . map ( ( { test, use } ) => ( {
244
179
exclude : globalStylePaths ,
245
180
test,
246
181
use : [
@@ -250,50 +185,33 @@ export function getStylesConfig(wco: WebpackConfigOptions) {
250
185
options : {
251
186
ident : 'embedded' ,
252
187
plugins : postcssPluginCreator ,
253
- sourceMap : cssSourceMap ? 'inline' : false
254
- }
188
+ sourceMap : cssSourceMap ? 'inline' : false ,
189
+ } ,
255
190
} ,
256
- ...( use as webpack . Loader [ ] )
257
- ]
191
+ ...( use as webpack . Loader [ ] ) ,
192
+ ] ,
258
193
} ) ) ;
259
194
260
195
// load global css as css files
261
196
if ( globalStylePaths . length > 0 ) {
262
197
rules . push ( ...baseRules . map ( ( { test, use } ) => {
263
- const extractTextPlugin = {
198
+ return {
199
+ include : globalStylePaths ,
200
+ test,
264
201
use : [
265
- // style-loader still has issues with relative url()'s with sourcemaps enabled;
266
- // even with the convertToAbsoluteUrls options as it uses 'document.location'
267
- // which breaks when used with routing.
268
- // Once style-loader 1.0 is released the following conditional won't be necessary
269
- // due to this 1.0 PR: https://github.com/webpack-contrib/style-loader/pull/219
270
- { loader : buildOptions . extractCss ? RawCssLoader : 'raw-loader' } ,
202
+ buildOptions . extractCss ? MiniCssExtractPlugin . loader : 'style-loader' ,
203
+ RawCssLoader ,
271
204
{
272
205
loader : 'postcss-loader' ,
273
206
options : {
274
207
ident : buildOptions . extractCss ? 'extracted' : 'embedded' ,
275
208
plugins : postcssPluginCreator ,
276
- sourceMap : cssSourceMap && ! buildOptions . extractCss ? 'inline' : cssSourceMap
277
- }
209
+ sourceMap : cssSourceMap && ! buildOptions . extractCss ? 'inline' : cssSourceMap ,
210
+ } ,
278
211
} ,
279
- ...( use as webpack . Loader [ ] )
212
+ ...( use as webpack . Loader [ ] ) ,
280
213
] ,
281
- // publicPath needed as a workaround https://github.com/angular/angular-cli/issues/4035
282
- publicPath : ''
283
- } ;
284
- const ret : any = {
285
- include : globalStylePaths ,
286
- test,
287
- use : [
288
- buildOptions . extractCss ? MiniCssExtractPlugin . loader : 'style-loader' ,
289
- ...extractTextPlugin . use ,
290
- ]
291
214
} ;
292
- // Save the original options as arguments for eject.
293
- // if (buildOptions.extractCss) {
294
- // ret[pluginArgs] = extractTextPlugin;
295
- // }
296
- return ret ;
297
215
} ) ) ;
298
216
}
299
217
@@ -309,6 +227,6 @@ export function getStylesConfig(wco: WebpackConfigOptions) {
309
227
return {
310
228
entry : entryPoints ,
311
229
module : { rules } ,
312
- plugins : extraPlugins
230
+ plugins : extraPlugins ,
313
231
} ;
314
232
}
0 commit comments