Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(@angular-devkit/build-angular): enable font inlining optimizations #18926

Merged
merged 4 commits into from Oct 14, 2020
Merged

feat(@angular-devkit/build-angular): enable font inlining optimizations #18926

merged 4 commits into from Oct 14, 2020

Conversation

alan-agius4
Copy link
Collaborator

@alan-agius4 alan-agius4 commented Sep 29, 2020

With this change we inline Google fonts and icons in the index html file when optimization is enabled.

Before

<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">

After

<style>
  @font-face {
    font-family: 'Material Icons';
    font-style: normal;
    font-weight: 400;
    src: url(https://fonts.gstatic.com/s/materialicons/v55/flUhRq6tzZclQEJ-Vdg-IuiaDsNcIhQ8tQ.woff2) format('woff2');
  }

  .material-icons {
    font-family: 'Material Icons';
    font-weight: normal;
    font-style: normal;
    font-size: 24px;
    line-height: 1;
    letter-spacing: normal;
    text-transform: none;
    display: inline-block;
    white-space: nowrap;
    word-wrap: normal;
    direction: ltr;
  }
</style>

To opt-out of this feature set optimization.fonts: false or optimization.fonts.inline: false in the browser builder options.

Example:

"configurations": {
  "production": {
    "fileReplacements": [
      {
        "replace": "src/environments/environment.ts",
        "with": "src/environments/environment.prod.ts"
      }
    ],
    "optimization": {
      "fonts": false
    },     

More information about the motivation for this feature can be found: #18730

Note: internet access is required during the build for this optimization to work.

@alan-agius4 alan-agius4 added the target: major This PR is targeted for the next major release label Sep 29, 2020
@alan-agius4 alan-agius4 removed the request for review from clydin October 8, 2020 11:33
@alan-agius4 alan-agius4 marked this pull request as draft October 8, 2020 11:33
@alan-agius4 alan-agius4 changed the title feat(@angular-devkit/build-angular): add font inliner feat(@angular-devkit/build-angular): enable font inlining optimizations Oct 9, 2020
@alan-agius4 alan-agius4 marked this pull request as ready for review October 9, 2020 10:09
Copy link
Contributor

@kyliau kyliau left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some minor comments

packages/angular/cli/lib/config/schema.json Outdated Show resolved Hide resolved
fonts: FontsClass,
};

export function normalizeOptimization(optimization: OptimizationUnion = false): NormalizeOptimizationOptions {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not exactly sure what the default value here means - isn't the default in the schema true?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Strictly speaking the default is defined in the schema which is false. But in the typings these are still marked as optional.

This is the base functionality needed to inline Google fonts and Icons in HTML.

The processor does a couple of things:
1. When support for older devices is needed where woff2 is not supported it will inline definitions for both woff1 and woff2
2. Will remove comments and whitespaces when it's `minifyInlinedCSS` is enabled.
3. Cache responses so to resuse the font response during watch mode.

Note: this is still an internal implementation which users cannot leverage just yet.
With this change we inline Google fonts and icons in the index html file when optimization is enabled.

**Before**
```html
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
```
**After**
```html
<style>
  @font-face {
    font-family: 'Material Icons';
    font-style: normal;
    font-weight: 400;
    src: url(https://fonts.gstatic.com/s/materialicons/v55/flUhRq6tzZclQEJ-Vdg-IuiaDsNcIhQ8tQ.woff2) format('woff2');
  }

  .material-icons {
    font-family: 'Material Icons';
    font-weight: normal;
    font-style: normal;
    font-size: 24px;
    line-height: 1;
    letter-spacing: normal;
    text-transform: none;
    display: inline-block;
    white-space: nowrap;
    word-wrap: normal;
    direction: ltr;
  }
</style>
```

To opt-out of this feature set `optimization.fonts: false` or `optimization.fonts.inline: false` in the browser builder options.

Example:
```js
"configurations": {
  "production": {
    "fileReplacements": [
      {
        "replace": "src/environments/environment.ts",
        "with": "src/environments/environment.prod.ts"
      }
    ],
    "optimization": {
      "fonts": false
    },
```

More information about the motivation for this feature can be found: #18730

Note: internet access is required during the build for this optimization to work.
@alan-agius4 alan-agius4 added the action: merge The PR is ready for merge by the caretaker label Oct 14, 2020
@mgechev mgechev merged commit 871dd6a into angular:master Oct 14, 2020
@alan-agius4 alan-agius4 deleted the add-font-inliner branch October 14, 2020 18:58
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Nov 14, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
action: merge The PR is ready for merge by the caretaker target: major This PR is targeted for the next major release
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants