Skip to content

Commit

Permalink
feat: ✨ allow sorting of scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
TheDutchCoder committed May 8, 2021
1 parent 50ece78 commit 01b119a
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 12 deletions.
15 changes: 10 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Plugin } from 'vite'
import { hasErrors, filterByPosition } from './utils'
import { hasErrors, filterByPosition, sortScripts } from './utils'

export const positions = ['head', 'body'] as const

Expand All @@ -8,6 +8,7 @@ export type Position = typeof positions[number];
export type Script = {
position: Position;
content: string;
sort?: number,
}

const VitePluginAddScripts = (scripts: Script[]): Plugin => {
Expand All @@ -22,18 +23,22 @@ const VitePluginAddScripts = (scripts: Script[]): Plugin => {
}

// Filter scripts by their position.
const headScripts = filterByPosition(scripts, 'head').map(script => script.content)
const bodyScripts = filterByPosition(scripts, 'body').map(script => script.content)
let headScripts = filterByPosition(scripts, 'head')
let bodyScripts = filterByPosition(scripts, 'body')

// Sort scripts by their sort key.
headScripts = sortScripts(headScripts)
bodyScripts = sortScripts(bodyScripts)

// Append the scripts to the HTML.
html = html.replace(
/<\/head>/,
`${headScripts.join('\n')}\n</head>`
`${headScripts.map(script => script.content).join('\n')}\n</head>`
)

html = html.replace(
/<\/body>/,
`${bodyScripts.join('\n')}\n</body>`
`${bodyScripts.map(script => script.content).join('\n')}\n</body>`
)

return html
Expand Down
27 changes: 26 additions & 1 deletion src/utils/__tests__/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { hasErrors, filterByPosition } from '../index'
import { hasErrors, filterByPosition, sortScripts } from '../index'
import type { Script } from '../../index'

describe('hasErrors', () => {
Expand Down Expand Up @@ -84,3 +84,28 @@ describe('filterByPosition', () => {
expect(bodyResult).toEqual(filteredBodyItems)
})
})

describe('sortScripts', () => {
it('sorts scripts by their sort key', () => {
const items: Script[] = [
{ position: 'head', content: '1', sort: 5 },
{ position: 'head', content: '2', sort: 2 },
{ position: 'head', content: '3' },
{ position: 'head', content: '4', sort: -1 },
{ position: 'head', content: '5', sort: 1 },
]

const sortedItems: Script[] = [
{ position: 'head', content: '4', sort: -1 },
{ position: 'head', content: '3', sort: 0 },
{ position: 'head', content: '5', sort: 1 },
{ position: 'head', content: '2', sort: 2 },
{ position: 'head', content: '1', sort: 5 },
]

const result = sortScripts(items)

expect(result.length).toBe(5)
expect(result).toEqual(sortedItems)
})
})
34 changes: 28 additions & 6 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,36 @@
import { positions } from '../index'
import type { Script, Position } from '../index'

export const hasErrors = (items: Script[]) => {
const hasNoScripts = items.length === 0
const hasInvalidPositions = items.filter(item => !positions.includes(item.position)).length > 0
const hasMissingContent = items.filter(item => !item.hasOwnProperty('content')).length > 0
export const hasErrors = (scripts: Script[]): Boolean => {
const hasNoScripts = scripts.length === 0
const hasInvalidPositions = scripts.filter(script => !positions.includes(script.position)).length > 0
const hasMissingContent = scripts.filter(script => !script.hasOwnProperty('content')).length > 0

return hasNoScripts || hasInvalidPositions || hasMissingContent
}

export const filterByPosition = (items: Script[], position: Position) => {
return items.filter(item => item.position === position)
export const filterByPosition = (scripts: Script[], position: Position): Script[] => {
return scripts.filter(script => script.position === position)
}

export const sortScripts = (scripts: Script[]): Script[] => {
return scripts.sort((first, second) => {
if (!first.sort) {
first.sort = 0
}

if (!second.sort) {
second.sort = 0
}

if (first.sort < second.sort) {
return -1
}

if (first.sort > second.sort) {
return 1
}

return 0
})
}

0 comments on commit 01b119a

Please sign in to comment.