-
Notifications
You must be signed in to change notification settings - Fork 12k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(@angular-devkit/build-angular): inline Google and Adobe fonts lo…
…cated in stylesheets `@import url()` to Google and Adobe fonts that are located in global and component CSS will now be inlined when using the esbuild based builders. Input ```css @import url(https://fonts.googleapis.com/css?family=Roboto:300,400,500); ``` Output ```css /* latin */ @font-face { font-family: 'Roboto'; font-style: normal; font-weight: 500; src: url(https://fonts.gstatic.com/s/roboto/v30/KFOlCnqEu92Fr1MmEU9fBBc4AMP6lQ.woff2) format('woff2'); unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+0304, U+0308, U+0329, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD; } ``` Closes #23054
- Loading branch information
1 parent
bf5fbdd
commit f6e67df
Showing
7 changed files
with
231 additions
and
24 deletions.
There are no files selected for viewing
102 changes: 102 additions & 0 deletions
102
...it/build_angular/src/builders/application/tests/options/optimization-fonts-inline_spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC 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 { buildApplication } from '../../index'; | ||
import { APPLICATION_BUILDER_INFO, BASE_OPTIONS, describeBuilder } from '../setup'; | ||
|
||
describeBuilder(buildApplication, APPLICATION_BUILDER_INFO, (harness) => { | ||
describe('Option: "fonts.inline"', () => { | ||
beforeEach(async () => { | ||
await harness.modifyFile('/src/index.html', (content) => | ||
content.replace( | ||
'<head>', | ||
`<head><link href="https://fonts.googleapis.com/css?family=Roboto:300,400,500" rel="stylesheet">`, | ||
), | ||
); | ||
|
||
await harness.writeFile( | ||
'src/styles.css', | ||
'@import url(https://fonts.googleapis.com/css?family=Roboto:300,400,500);', | ||
); | ||
|
||
await harness.writeFile( | ||
'src/app/app.component.css', | ||
'@import url(https://fonts.googleapis.com/css?family=Roboto:300,400,500);', | ||
); | ||
}); | ||
|
||
it(`should not inline fonts when fonts optimization is set to false`, async () => { | ||
harness.useTarget('build', { | ||
...BASE_OPTIONS, | ||
optimization: { | ||
scripts: true, | ||
styles: true, | ||
fonts: false, | ||
}, | ||
styles: ['src/styles.css'], | ||
}); | ||
|
||
const { result } = await harness.executeOnce(); | ||
|
||
expect(result?.success).toBeTrue(); | ||
for (const file of ['styles.css', 'index.html', 'main.js']) { | ||
harness | ||
.expectFile(`dist/browser/${file}`) | ||
.content.toContain(`https://fonts.googleapis.com/css?family=Roboto:300,400,500`); | ||
} | ||
}); | ||
|
||
it(`should inline fonts when fonts optimization is unset`, async () => { | ||
harness.useTarget('build', { | ||
...BASE_OPTIONS, | ||
optimization: { | ||
scripts: true, | ||
styles: true, | ||
fonts: undefined, | ||
}, | ||
styles: ['src/styles.css'], | ||
}); | ||
|
||
const { result } = await harness.executeOnce(); | ||
|
||
expect(result?.success).toBeTrue(); | ||
for (const file of ['styles.css', 'index.html', 'main.js']) { | ||
harness | ||
.expectFile(`dist/browser/${file}`) | ||
.content.not.toContain(`https://fonts.googleapis.com/css?family=Roboto:300,400,500`); | ||
harness | ||
.expectFile(`dist/browser/${file}`) | ||
.content.toMatch(/@font-face{font-family:'?Roboto/); | ||
} | ||
}); | ||
|
||
it(`should inline fonts when fonts optimization is true`, async () => { | ||
harness.useTarget('build', { | ||
...BASE_OPTIONS, | ||
optimization: { | ||
scripts: true, | ||
styles: true, | ||
fonts: true, | ||
}, | ||
styles: ['src/styles.css'], | ||
}); | ||
|
||
const { result } = await harness.executeOnce(); | ||
|
||
expect(result?.success).toBeTrue(); | ||
for (const file of ['styles.css', 'index.html', 'main.js']) { | ||
harness | ||
.expectFile(`dist/browser/${file}`) | ||
.content.not.toContain(`https://fonts.googleapis.com/css?family=Roboto:300,400,500`); | ||
harness | ||
.expectFile(`dist/browser/${file}`) | ||
.content.toMatch(/@font-face{font-family:'?Roboto/); | ||
} | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
...ges/angular_devkit/build_angular/src/tools/esbuild/stylesheets/css-inline-fonts-plugin.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC 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 type { Plugin, PluginBuild } from 'esbuild'; | ||
import { InlineFontsProcessor } from '../../../utils/index-file/inline-fonts'; | ||
import { NormalizedCachedOptions } from '../../../utils/normalize-cache'; | ||
import { LoadResultCache, createCachedLoad } from '../load-result-cache'; | ||
|
||
/** | ||
* Options for the createCssInlineFontsPlugin | ||
* @see createCssInlineFontsPlugin | ||
*/ | ||
export interface CssInlineFontsPluginOptions { | ||
/** Disk cache normalized options */ | ||
cacheOptions?: NormalizedCachedOptions; | ||
/** Load results cache. */ | ||
cache?: LoadResultCache; | ||
} | ||
|
||
/** | ||
* Creates an esbuild {@link Plugin} that inlines fonts imported via import-rule. | ||
* within the build configuration. | ||
*/ | ||
export function createCssInlineFontsPlugin({ | ||
cache, | ||
cacheOptions, | ||
}: CssInlineFontsPluginOptions): Plugin { | ||
return { | ||
name: 'angular-css-inline-fonts-plugin', | ||
setup(build: PluginBuild): void { | ||
const inlineFontsProcessor = new InlineFontsProcessor({ cache: cacheOptions, minify: false }); | ||
|
||
build.onResolve({ filter: /fonts\.googleapis\.com|use\.typekit\.net/ }, (args) => { | ||
// Only attempt to resolve import-rule tokens which only exist inside CSS. | ||
if (args.kind !== 'import-rule') { | ||
return null; | ||
} | ||
|
||
if (!inlineFontsProcessor.canInlineRequest(args.path)) { | ||
return null; | ||
} | ||
|
||
return { | ||
path: args.path, | ||
namespace: 'css-inline-fonts', | ||
}; | ||
}); | ||
|
||
build.onLoad( | ||
{ filter: /./, namespace: 'css-inline-fonts' }, | ||
createCachedLoad(cache, async (args) => { | ||
try { | ||
return { | ||
contents: await inlineFontsProcessor.processURL(args.path), | ||
loader: 'css', | ||
}; | ||
} catch (error) { | ||
return { | ||
loader: 'css', | ||
errors: [ | ||
{ | ||
text: `Failed to inline external stylesheet '${args.path}'.`, | ||
detail: error, | ||
}, | ||
], | ||
}; | ||
} | ||
}), | ||
); | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters