Skip to content

Commit a2342b6

Browse files
alan-agius4vikerman
authored andcommitted
feat(@angular-devkit/build-angular): remove inlining of assets in css (#12027)
BREAKING CHANGE: Assets under 10Kib are not longer inlined in css
1 parent 67e32a8 commit a2342b6

File tree

3 files changed

+21
-153
lines changed

3 files changed

+21
-153
lines changed

packages/angular_devkit/build_angular/src/angular-cli-files/models/webpack-configs/styles.ts

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,6 @@ export function getStylesConfig(wco: WebpackConfigOptions) {
5353
const extraPlugins: any[] = [];
5454
const cssSourceMap = buildOptions.sourceMap;
5555

56-
// Maximum resource size to inline (KiB)
57-
const maximumInlineSize = 10;
5856
// Determine hashing format.
5957
const hashFormat = getOutputHashFormat(buildOptions.outputHashing as string);
6058
// Convert absolute resource URLs to account for base-href and deploy-url.
@@ -134,18 +132,7 @@ export function getStylesConfig(wco: WebpackConfigOptions) {
134132
return `/${baseHref}/${deployUrl}/${url}`.replace(/\/\/+/g, '/');
135133
}
136134
}
137-
},
138-
{
139-
// TODO: inline .cur if not supporting IE (use browserslist to check)
140-
filter: (asset: PostcssUrlAsset) => {
141-
return maximumInlineSize > 0 && !asset.hash && !asset.absolutePath.endsWith('.cur');
142-
},
143-
url: 'inline',
144-
// NOTE: maxSize is in KB
145-
maxSize: maximumInlineSize,
146-
fallback: 'rebase',
147-
},
148-
{ url: 'rebase' },
135+
}
149136
]),
150137
PostcssCliResources({
151138
deployUrl: loader.loaders[loader.loaderIndex].options.ident == 'extracted' ? '' : deployUrl,
@@ -196,7 +183,7 @@ export function getStylesConfig(wco: WebpackConfigOptions) {
196183

197184
if (chunkNames.length > 0) {
198185
// Add plugin to remove hashes from lazy styles.
199-
extraPlugins.push(new RemoveHashPlugin({ chunkNames, hashFormat}));
186+
extraPlugins.push(new RemoveHashPlugin({ chunkNames, hashFormat }));
200187
}
201188
}
202189

@@ -216,7 +203,8 @@ export function getStylesConfig(wco: WebpackConfigOptions) {
216203
const baseRules: webpack.Rule[] = [
217204
{ test: /\.css$/, use: [] },
218205
{
219-
test: /\.scss$|\.sass$/, use: [{
206+
test: /\.scss$|\.sass$/,
207+
use: [{
220208
loader: 'sass-loader',
221209
options: {
222210
implementation: dartSass,
@@ -229,7 +217,8 @@ export function getStylesConfig(wco: WebpackConfigOptions) {
229217
}]
230218
},
231219
{
232-
test: /\.less$/, use: [{
220+
test: /\.less$/,
221+
use: [{
233222
loader: 'less-loader',
234223
options: {
235224
sourceMap: cssSourceMap,
@@ -239,7 +228,8 @@ export function getStylesConfig(wco: WebpackConfigOptions) {
239228
}]
240229
},
241230
{
242-
test: /\.styl$/, use: [{
231+
test: /\.styl$/,
232+
use: [{
243233
loader: 'stylus-loader',
244234
options: {
245235
sourceMap: cssSourceMap,
@@ -251,7 +241,9 @@ export function getStylesConfig(wco: WebpackConfigOptions) {
251241

252242
// load component css as raw strings
253243
const rules: webpack.Rule[] = baseRules.map(({ test, use }) => ({
254-
exclude: globalStylePaths, test, use: [
244+
exclude: globalStylePaths,
245+
test,
246+
use: [
255247
{ loader: 'raw-loader' },
256248
{
257249
loader: 'postcss-loader',
@@ -306,16 +298,17 @@ export function getStylesConfig(wco: WebpackConfigOptions) {
306298
}
307299

308300
if (buildOptions.extractCss) {
309-
// extract global css from js files into own css file
310301
extraPlugins.push(
311-
new MiniCssExtractPlugin({ filename: `[name]${hashFormat.extract}.css` }));
312-
// suppress empty .js files in css only entry points
313-
extraPlugins.push(new SuppressExtractedTextChunksWebpackPlugin());
302+
// extract global css from js files into own css file
303+
new MiniCssExtractPlugin({ filename: `[name]${hashFormat.extract}.css` }),
304+
// suppress empty .js files in css only entry points
305+
new SuppressExtractedTextChunksWebpackPlugin(),
306+
);
314307
}
315308

316309
return {
317310
entry: entryPoints,
318311
module: { rules },
319-
plugins: [].concat(extraPlugins as any)
312+
plugins: extraPlugins
320313
};
321314
}

packages/angular_devkit/build_angular/test/browser/styles_spec_large.ts

Lines changed: 4 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -278,62 +278,6 @@ describe('Browser Builder styles', () => {
278278
});
279279
});
280280

281-
it('inlines resources', (done) => {
282-
host.copyFile('src/spectrum.png', 'src/large.png');
283-
host.writeMultipleFiles({
284-
'src/styles.scss': `
285-
h1 { background: url('./large.png'),
286-
linear-gradient(to bottom, #0e40fa 25%, #0654f4 75%); }
287-
h2 { background: url('./small.svg'); }
288-
p { background: url(./small-id.svg#testID); }
289-
`,
290-
'src/app/app.component.css': `
291-
h3 { background: url('../small.svg'); }
292-
h4 { background: url("../large.png"); }
293-
`,
294-
'src/small.svg': imgSvg,
295-
'src/small-id.svg': imgSvg,
296-
});
297-
298-
const overrides = {
299-
aot: true,
300-
extractCss: true,
301-
styles: [`src/styles.scss`],
302-
};
303-
304-
runTargetSpec(host, browserTargetSpec, overrides).pipe(
305-
tap((buildEvent) => expect(buildEvent.success).toBe(true)),
306-
tap(() => {
307-
const fileName = 'dist/styles.css';
308-
const content = virtualFs.fileBufferToString(host.scopedSync().read(normalize(fileName)));
309-
// Large image should not be inlined, and gradient should be there.
310-
expect(content).toMatch(
311-
/url\(['"]?large\.png['"]?\),\s+linear-gradient\(to bottom, #0e40fa 25%, #0654f4 75%\);/);
312-
// Small image should be inlined.
313-
expect(content).toMatch(/url\(\\?['"]data:image\/svg\+xml/);
314-
// Small image with param should not be inlined.
315-
expect(content).toMatch(/url\(['"]?small-id\.svg#testID['"]?\)/);
316-
}),
317-
tap(() => {
318-
const fileName = 'dist/main.js';
319-
const content = virtualFs.fileBufferToString(host.scopedSync().read(normalize(fileName)));
320-
// Large image should not be inlined.
321-
expect(content).toMatch(/url\((?:['"]|\\')?large\.png(?:['"]|\\')?\)/);
322-
// Small image should be inlined.
323-
expect(content).toMatch(/url\(\\?['"]data:image\/svg\+xml/);
324-
}),
325-
tap(() => {
326-
expect(host.scopedSync().exists(normalize('dist/small.svg'))).toBe(false);
327-
expect(host.scopedSync().exists(normalize('dist/large.png'))).toBe(true);
328-
expect(host.scopedSync().exists(normalize('dist/small-id.svg'))).toBe(true);
329-
}),
330-
// TODO: find a way to check logger/output for warnings.
331-
// if (stdout.match(/postcss-url: \.+: Can't read file '\.+', ignoring/)) {
332-
// throw new Error('Expected no postcss-url file read warnings.');
333-
// }
334-
).toPromise().then(done, done.fail);
335-
});
336-
337281
it(`supports font-awesome imports`, (done) => {
338282
host.writeMultipleFiles({
339283
'src/styles.scss': `
@@ -407,8 +351,6 @@ describe('Browser Builder styles', () => {
407351
h3 { background: url('/assets/component-img-absolute.svg'); }
408352
h4 { background: url('../assets/component-img-relative.png'); }
409353
`,
410-
// Use a small SVG for the absolute image to help validate that it is being referenced,
411-
// because it is so small it would be inlined usually.
412354
'src/assets/global-img-absolute.svg': imgSvg,
413355
'src/assets/component-img-absolute.svg': imgSvg,
414356
});
@@ -427,12 +369,12 @@ describe('Browser Builder styles', () => {
427369
expect(styles).toContain(`url('global-img-relative.png')`);
428370
expect(main).toContain(`url('/assets/component-img-absolute.svg')`);
429371
expect(main).toContain(`url('component-img-relative.png')`);
430-
expect(host.scopedSync().exists(normalize('dist/global-img-absolute.svg')))
431-
.toBe(false);
372+
expect(host.scopedSync().exists(normalize('dist/assets/global-img-absolute.svg')))
373+
.toBe(true);
432374
expect(host.scopedSync().exists(normalize('dist/global-img-relative.png')))
433375
.toBe(true);
434-
expect(host.scopedSync().exists(normalize('dist/component-img-absolute.svg')))
435-
.toBe(false);
376+
expect(host.scopedSync().exists(normalize('dist/assets/component-img-absolute.svg')))
377+
.toBe(true);
436378
expect(host.scopedSync().exists(normalize('dist/component-img-relative.png')))
437379
.toBe(true);
438380
}),

tests/legacy-cli/e2e/tests/build/styles/inline-urls.ts

Lines changed: 0 additions & 67 deletions
This file was deleted.

0 commit comments

Comments
 (0)