Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): resolve tokens and preflights together #1078

Merged
merged 2 commits into from Jun 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
50 changes: 30 additions & 20 deletions packages/core/src/generator/index.ts
Expand Up @@ -143,8 +143,9 @@ export class UnoGenerator {
const layerSet = new Set<string>(['default'])
const matched = new Set<string>()
const sheet = new Map<string, StringifiedUtil[]>()
let preflightsMap: Record<string, string> = {}

await Promise.all(Array.from(tokens).map(async (raw) => {
const tokenPromises = Array.from(tokens).map(async (raw) => {
if (matched.has(raw))
return

Expand All @@ -156,36 +157,34 @@ export class UnoGenerator {

for (const item of payload) {
const parent = item[3] || ''
const layer = item[4]?.layer
if (!sheet.has(parent))
sheet.set(parent, [])
sheet.get(parent)!.push(item)
if (item[4]?.layer)
layerSet.add(item[4].layer)
if (layer)
layerSet.add(layer)
}
}))

if (preflights) {
this.config.preflights.forEach((i) => {
if (i.layer)
layerSet.add(i.layer)
})
}
})

const layerCache: Record<string, string> = {}
const layers = this.config.sortLayers(Array
.from(layerSet)
.sort((a, b) => ((this.config.layers[a] ?? 0) - (this.config.layers[b] ?? 0)) || a.localeCompare(b)),
)
const preflightPromise = (async () => {
if (!preflights)
return

let preflightsMap: Record<string, string> = {}
if (preflights) {
const preflightContext: PreflightContext = {
generator: this,
theme: this.config.theme,
}

const preflightLayerSet = new Set<string>(['default'])
this.config.preflights.forEach(({ layer }) => {
if (layer) {
layerSet.add(layer)
preflightLayerSet.add(layer)
}
})

preflightsMap = Object.fromEntries(
await Promise.all(layers.map(
await Promise.all(Array.from(preflightLayerSet).map(
async (layer) => {
const preflights = await Promise.all(
this.config.preflights
Expand All @@ -199,8 +198,19 @@ export class UnoGenerator {
},
)),
)
}
})()

await Promise.all([
...tokenPromises,
preflightPromise,
])

const layers = this.config.sortLayers(Array
.from(layerSet)
.sort((a, b) => ((this.config.layers[a] ?? 0) - (this.config.layers[b] ?? 0)) || a.localeCompare(b)),
)

const layerCache: Record<string, string> = {}
const getLayer = (layer: string) => {
if (layerCache[layer])
return layerCache[layer]
Expand Down
48 changes: 48 additions & 0 deletions test/generate-async.test.ts
@@ -0,0 +1,48 @@
import { createGenerator } from '@unocss/core'
import { describe, expect, test } from 'vitest'

describe('generate-async', () => {
test('rule-first', async () => {
const order: number[] = []
const uno = createGenerator({
rules: [
[/^rule$/, () => new Promise(resolve => setTimeout(() => {
order.push(1)
resolve('/* rule */')
}, 10))],
],
preflights: [
{
getCSS: () => new Promise(resolve => setTimeout(() => {
order.push(2)
resolve('/* preflight */')
}, 20)),
},
],
})
await uno.generate('rule')
expect(order).eql([1, 2])
})

test('preflight-first', async () => {
const order: number[] = []
const uno = createGenerator({
rules: [
[/^rule$/, () => new Promise(resolve => setTimeout(() => {
order.push(2)
resolve('/* rule */')
}, 20))],
],
preflights: [
{
getCSS: () => new Promise(resolve => setTimeout(() => {
order.push(1)
resolve('/* preflight */')
}, 10)),
},
],
})
await uno.generate('rule')
expect(order).eql([1, 2])
})
})