Skip to content

Commit

Permalink
fix(array-flat): polyfill flat
Browse files Browse the repository at this point in the history
  • Loading branch information
Thorn Walli committed Nov 29, 2020
1 parent ca56384 commit 9a67da9
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 11 deletions.
5 changes: 5 additions & 0 deletions lib/module.js
Expand Up @@ -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',
Expand Down
6 changes: 4 additions & 2 deletions lib/utils/fontFace.js
@@ -1,5 +1,6 @@
import { extname } from 'path'
import { paramCase, snakeCase } from 'change-case'
import { arrayFlat } from './index'

// Font-Face

Expand Down Expand Up @@ -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)
Expand All @@ -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) {
Expand Down
10 changes: 2 additions & 8 deletions lib/utils/fontLoader.js
@@ -1,3 +1,5 @@
import { arrayFlat } from './index'

export function loadFonts (options) {
options = Object.assign({
ignoredEffectiveTypes: [],
Expand Down Expand Up @@ -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))
Expand Down
8 changes: 8 additions & 0 deletions 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)
}
}
3 changes: 2 additions & 1 deletion 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)
Expand Down Expand Up @@ -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: []
})
}

0 comments on commit 9a67da9

Please sign in to comment.