From 9a67da980cdf8f0c4257365f463b8b6990ed327e Mon Sep 17 00:00:00 2001 From: Thorn Walli Date: Sun, 29 Nov 2020 12:45:18 +0100 Subject: [PATCH] fix(array-flat): polyfill flat --- lib/module.js | 5 +++++ lib/utils/fontFace.js | 6 ++++-- lib/utils/fontLoader.js | 10 ++-------- lib/utils/index.js | 8 ++++++++ lib/worker/index.js | 3 ++- 5 files changed, 21 insertions(+), 11 deletions(-) create mode 100644 lib/utils/index.js diff --git a/lib/module.js b/lib/module.js index e04d6bf..fe82a3d 100644 --- a/lib/module.js +++ b/lib/module.js @@ -71,6 +71,11 @@ function addTemplates (moduleScope, fonts, options) { fonts } }) + moduleScope.addTemplate({ + src: resolve(__dirname, 'utils/index.js'), + fileName: 'nuxt-font-loader-strategy/utils/index.js', + options: { } + }) moduleScope.addTemplate({ src: resolve(__dirname, 'utils/fontFace.js'), fileName: 'nuxt-font-loader-strategy/utils/fontFace.js', diff --git a/lib/utils/fontFace.js b/lib/utils/fontFace.js index d7f0765..d26d410 100644 --- a/lib/utils/fontFace.js +++ b/lib/utils/fontFace.js @@ -1,5 +1,6 @@ import { extname } from 'path' import { paramCase, snakeCase } from 'change-case' +import { arrayFlat } from './index' // Font-Face @@ -35,7 +36,7 @@ export function getFontClasses (pattern, set, properties) { export async function prepareFonts (options, resolve, kebabCaseProps = true) { const { fonts, classPattern } = options - return (await Promise.all(fonts.map((font) => { + const preparedFonts = (await Promise.all(fonts.map((font) => { const fileExtensions = getFileExtensions(font) return font.fontFaces.map((face) => { const sources = prepareSrc(face.src, fileExtensions, resolve) @@ -53,7 +54,8 @@ export async function prepareFonts (options, resolve, kebabCaseProps = true) { local: [].concat(face.local || []) } }) - }))).flat() + }))) + return arrayFlat(preparedFonts) } export function createFontFace (font, baseUrl) { diff --git a/lib/utils/fontLoader.js b/lib/utils/fontLoader.js index 096d2fb..9eff077 100644 --- a/lib/utils/fontLoader.js +++ b/lib/utils/fontLoader.js @@ -1,3 +1,5 @@ +import { arrayFlat } from './index' + export function loadFonts (options) { options = Object.assign({ ignoredEffectiveTypes: [], @@ -101,14 +103,6 @@ async function prefetchFonts (fonts, options, classList = []) { } } -function arrayFlat (array) { - if ('flat' in Array.prototype) { - return array.flat() - } else { - return [].concat.apply([], array) - } -} - export function unlockClasses (classList, unlockDelay) { const cb = () => { document.documentElement.classList.add(...arrayFlat(classList)) diff --git a/lib/utils/index.js b/lib/utils/index.js new file mode 100644 index 0000000..6e07a72 --- /dev/null +++ b/lib/utils/index.js @@ -0,0 +1,8 @@ + +export function arrayFlat (array) { + if ('flat' in Array.prototype) { + return array.flat() + } else { + return [].concat.apply([], array) + } +} diff --git a/lib/worker/index.js b/lib/worker/index.js index 9d6f8d8..0d6092a 100644 --- a/lib/worker/index.js +++ b/lib/worker/index.js @@ -1,4 +1,5 @@ import { resolve } from 'path' +import { arrayFlat } from '../utils/index' export function addWebWorker (moduleScope, fonts, options) { const preparedFonts = prepareFonts(fonts) @@ -47,7 +48,7 @@ function prepareFonts (fonts) { result.sources.push(sources[0].path) return result }, { - classes: fonts.map(font => font.classes).flat(), + classes: arrayFlat(fonts.map(font => font.classes)), sources: [] }) }