Skip to content

keqingrong/postcss-font-family-correction

Repository files navigation

PostCSS font-family Correction

npm version

A PostCSS plugin to correct font-family values.

.regular {
  font-family: PingFangSC-Regular;
}

.medium {
  font-family: PingFangSC-Medium;
}

.semibold {
  font-family: PingFangSC-Semibold;
}

will be processed to:

.regular {
  font-family: 'PingFang SC', sans-serif;
  font-weight: 400;
}

.medium {
  font-family: 'PingFang SC', sans-serif;
  font-weight: 500;
}

.semibold {
  font-family: 'PingFang SC', sans-serif;
  font-weight: 600;
}

Usage

Step 1: Install plugin:

npm install --save-dev postcss postcss-font-family-correction

Step 2: Check you project for existed PostCSS config: postcss.config.js in the project root, "postcss" section in package.json or postcss in bundle config.

If you do not use PostCSS, add it according to official docs and set this plugin in settings.

Step 3: Add the plugin to plugins list:

module.exports = {
  plugins: [
+   require('postcss-font-family-correction'),
    require('autoprefixer')
  ]
}

Options

  • mappings: font-family mappings
  • preserveComments: Preserve ignore comments
  • clearFontFamily: Clear font-family property
  • overwriteFontWeight: Overwrite existing font-weight property

Example:

module.exports = {
  plugins: [
    require('postcss-font-family-correction')({
      mappings: {},
      preserveComments: true,
      clearFontFamily: false,
      overwriteFontWeight: false,
    }),
  ]
}

mappings

Object format:

postcss([
  require('postcss-font-family-correction')({
    mappings: {
      'PingFangSC-Regular': {
        fontFamily: 'PingFang SC',
        fontWeight: 400
      },
      'PingFangSC-Medium': {
        fontFamily: 'PingFang SC',
        fontWeight: 500
      },
      'PingFangSC-Semibold': {
        fontFamily: 'PingFang SC',
        fontWeight: 600
      }
    }
  })
]);

Array format:

postcss([
  require('postcss-font-family-correction')({
    mappings: {
      'PingFangSC-Regular': ['PingFang SC', 400],
      'PingFangSC-Medium': ['PingFang SC', 500],
      'PingFangSC-Semibold': ['PingFang SC', 600]
    }
  })
]);

preserveComments

.regular {
  /* font-family-correction-ignore-next */
  font-family: PingFangSC-Regular;
}

.medium {
  font-family: PingFangSC-Medium;
}

.semibold {
  font-family: PingFangSC-Semibold; /* font-family-correction-ignore */
}

About

A PostCSS plugin to correct font-family values

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published