From 1950ec51e36f07687b4d49d1709e377d55dab065 Mon Sep 17 00:00:00 2001 From: Lotus Date: Mon, 14 Dec 2020 10:24:05 +0300 Subject: [PATCH] feat: remove normal CSS files (#112) * feat: remove normal generation for v1 * feat: remove normal css files for v2 * feat: remove normal CSS files for variable fonts Co-authored-by: Lotus --- scripts/google/packager-v1.js | 18 ++++++++----- scripts/google/packager-v2.js | 23 +++++++++------- scripts/google/variable.js | 49 ++++++++++++++++++++++------------- 3 files changed, 56 insertions(+), 34 deletions(-) diff --git a/scripts/google/packager-v1.js b/scripts/google/packager-v1.js index 084c45a2fe0..51236d81e90 100644 --- a/scripts/google/packager-v1.js +++ b/scripts/google/packager-v1.js @@ -60,7 +60,6 @@ module.exports = function (id) { font.subsets.forEach(subset => { const cssSubset = [] font.weights.forEach(weight => { - const cssWeight = [] font.styles.forEach(style => { // Some fonts may have variants 400, 400i, 700 but not 700i. if (style in font.variants[weight]) { @@ -74,15 +73,20 @@ module.exports = function (id) { woff2Path: makeFontFilePath(subset, weight, style, "woff2"), woffPath: makeFontFilePath(subset, weight, style, "woff"), }) - cssWeight.push(css) - cssSubset.push(css) - const cssStylePath = `${fontDir}/${subset}-${weight}-${style}.css` - fs.writeFileSync(cssStylePath, css) + if (style === "normal") { + const cssPath = `${fontDir}/${subset}-${weight}.css` + fs.writeFileSync(cssPath, css) + + // Should only push normal variants into subset + cssSubset.push(css) + } else { + // If italic or else, define specific style CSS file + const cssStylePath = `${fontDir}/${subset}-${weight}-${style}.css` + fs.writeFileSync(cssStylePath, css) + } } }) - const cssWeightPath = `${fontDir}/${subset}-${weight}.css` - fs.writeFileSync(cssWeightPath, cssWeight.join("")) }) const cssSubsetPath = `${fontDir}/${subset}.css` fs.writeFileSync(cssSubsetPath, cssSubset.join("")) diff --git a/scripts/google/packager-v2.js b/scripts/google/packager-v2.js index fb38f723d9b..471b56e0c4a 100644 --- a/scripts/google/packager-v2.js +++ b/scripts/google/packager-v2.js @@ -86,7 +86,6 @@ module.exports = function (id) { const unicodeKeys = Object.keys(font.unicodeRange) font.weights.forEach(weight => { - const cssWeight = [] font.styles.forEach(style => { const cssStyle = [] unicodeKeys.forEach(subset => { @@ -109,18 +108,24 @@ module.exports = function (id) { unicodeRange: font.unicodeRange[subset], }) cssStyle.push(css) - cssWeight.push(css) } }) + // Write down CSS if (style in font.variants[weight]) { - const cssStylePath = `${fontDir}/${weight}-${style}.css` - fs.writeFileSync(cssStylePath, cssStyle.join("")) + if (style === "normal") { + const cssPath = `${fontDir}/${weight}.css` + fs.writeFileSync(cssPath, cssStyle.join("")) + + // Generate index CSS + if (weight === "400") { + fs.writeFileSync(`${fontDir}/index.css`, cssStyle.join("")) + } + } else { + // If italic or else, define specific style CSS file + const cssStylePath = `${fontDir}/${weight}-${style}.css` + fs.writeFileSync(cssStylePath, cssStyle.join("")) + } } }) - const cssWeightPath = `${fontDir}/${weight}.css` - fs.writeFileSync(cssWeightPath, cssWeight.join("")) - if (weight === "400") { - fs.writeFileSync(`${fontDir}/index.css`, cssWeight.join("")) - } }) } diff --git a/scripts/google/variable.js b/scripts/google/variable.js index a08b9fac5db..80ed1cd63a5 100644 --- a/scripts/google/variable.js +++ b/scripts/google/variable.js @@ -53,7 +53,6 @@ module.exports = function (id) { const variableName = `${font.family}Variable` // wghtOnly CSS Generation - const css = [] font.styles.forEach(style => { const cssStyle = [] font.subsets.forEach(subset => { @@ -69,20 +68,25 @@ module.exports = function (id) { unicodeRange: font.unicodeRange[subset], }) cssStyle.push(cssWght) - css.push(cssWght) }) - const cssStylePath = `${fontDir}/variable-${style}.css` - fs.writeFileSync(cssStylePath, cssStyle.join("")) + + // Write down CSS + if (style === "normal") { + const cssPath = `${fontDir}/variable.css` + fs.writeFileSync(cssPath, cssStyle.join("")) + } else { + // If italic or else, define specific style CSS file + const cssStylePath = `${fontDir}/variable-${style}.css` + fs.writeFileSync(cssStylePath, cssStyle.join("")) + } }) - const cssPath = `${fontDir}/variable.css` - fs.writeFileSync(cssPath, css.join("")) // full CSS Generation if ("full" in fontVariable.variants) { // Wdth requires a different CSS template (font-stretch) if ("wdth" in fontVariable.axes) { - const css = [] font.styles.forEach(style => { + // Preserve the 'normal' style as a flag const origStyle = style if ("slnt" in fontVariable.axes && style === "normal") { // SLNT has a different style linked to it. @@ -105,15 +109,19 @@ module.exports = function (id) { unicodeRange: font.unicodeRange[subset], }) cssStyle.push(cssWght) - css.push(cssWght) }) - const cssStylePath = `${fontDir}/variable-full-${origStyle}.css` - fs.writeFileSync(cssStylePath, cssStyle.join("")) + + // Write down CSS + if (origStyle === "normal") { + const cssPath = `${fontDir}/variable-full.css` + fs.writeFileSync(cssPath, cssStyle.join("")) + } else { + // If italic or else, define specific style CSS file + const cssStylePath = `${fontDir}/variable-full-${origStyle}.css` + fs.writeFileSync(cssStylePath, cssStyle.join("")) + } }) - const cssPath = `${fontDir}/variable-full.css` - fs.writeFileSync(cssPath, css.join("")) } else { - const css = [] font.styles.forEach(style => { const origStyle = style if ("slnt" in fontVariable.axes && style === "normal") { @@ -136,13 +144,18 @@ module.exports = function (id) { unicodeRange: font.unicodeRange[subset], }) cssStyle.push(cssWght) - css.push(cssWght) }) - const cssStylePath = `${fontDir}/variable-full-${origStyle}.css` - fs.writeFileSync(cssStylePath, cssStyle.join("")) + + // Write down CSS + if (origStyle === "normal") { + const cssPath = `${fontDir}/variable-full.css` + fs.writeFileSync(cssPath, cssStyle.join("")) + } else { + // If italic or else, define specific style CSS file + const cssStylePath = `${fontDir}/variable-full-${origStyle}.css` + fs.writeFileSync(cssStylePath, cssStyle.join("")) + } }) - const cssPath = `${fontDir}/variable-full.css` - fs.writeFileSync(cssPath, css.join("")) } } }