Git Product home page Git Product logo

unplugin-fonts-ext-ordering-reproduction's Introduction

unplugin-fonts-ext-ordering

The transform field allows to sort the order of the font extensions. vite.config.ts

// This is the order in which the font extensions are sorted.
const FONT_ORDER = ['woff2', 'woff', 'otf', 'ttf', 'svg', 'eot'].map(ext => `.${ext}`)

export default defineConfig({
  plugins: [
    UnFonts({
      custom: {
        display: 'swap',
        families: [
          {
            name: 'Noto Sans',
            src: './src/fonts/*',

            // Logic to sort the order of font extensions.
            transform(font) {
              const orderedFiles = font.files.sort((a, b) =>
                FONT_ORDER.indexOf(a.ext) - FONT_ORDER.indexOf(b.ext))
              font.files = orderedFiles
              return font
            },
          },
        ],
      },
    }),
  ],
})

Previously, otf was loaded first and woff2 was loaded later, resulting in an @font-face like the one below:

@font-face {
  font-family: 'Noto Sans';
  src: url('/src/fonts/NotoSans-Regular.otf') format('opentype')
    url('/src/fonts/NotoSans-Regular.woff2') format('woff2');
  font-weight: 400;
  font-style: normal;
  font-display: swap;
}

Changing the settings to load woff2 first and otf later looks like this:

@font-face {
  font-family: 'Noto Sans';
  src: url('/src/fonts/NotoSans-Regular.woff2') format('woff2')
    url('/src/fonts/NotoSans-Regular.otf') format('opentype');
  font-weight: 400;
  font-style: normal;
  font-display: swap;
}

Check out the vite.config.ts file for the full setup.

unplugin-fonts-ext-ordering-reproduction's People

Contributors

gumball12 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.