Skip to content

Commit

Permalink
fix(core): enable details for analyzer (#3021)
Browse files Browse the repository at this point in the history
Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
  • Loading branch information
enkot and antfu committed Aug 25, 2023
1 parent c2d3859 commit 29e53de
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 6 deletions.
1 change: 1 addition & 0 deletions packages/core/src/config.ts
Expand Up @@ -157,6 +157,7 @@ export function resolveConfig<Theme extends object = object>(
extractors,
safelist: getMerged('safelist'),
separators,
details: config.details ?? (config.envMode === 'dev'),
}

for (const p of sources)
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/types.ts
Expand Up @@ -459,7 +459,7 @@ export interface ConfigBase<Theme extends object = object> {
*
* You don't usually need to set this.
*
* @default false
* @default `true` when `envMode` is `dev`, otherwise `false`
*/
details?: boolean
}
Expand Down
6 changes: 5 additions & 1 deletion packages/vite/src/index.ts
@@ -1,3 +1,4 @@
import process from 'node:process'
import type { Plugin } from 'vite'
import type { UnocssPluginContext, UserConfigDefaults } from '@unocss/core'
import UnocssInspector from '@unocss/inspector'
Expand Down Expand Up @@ -31,7 +32,10 @@ export default function UnocssPlugin<Theme extends object>(
configOrPath?: VitePluginConfig<Theme> | string,
defaults: UserConfigDefaults = {},
): Plugin[] {
const ctx = createContext<VitePluginConfig>(configOrPath as any, defaults)
const ctx = createContext<VitePluginConfig>(configOrPath as any, {
envMode: process.env.NODE_ENV === 'development' ? 'dev' : 'build',
...defaults,
})
const inlineConfig = (configOrPath && typeof configOrPath !== 'string') ? configOrPath : {}
const mode = inlineConfig.mode ?? 'global'

Expand Down
6 changes: 5 additions & 1 deletion packages/webpack/src/index.ts
@@ -1,3 +1,4 @@
import process from 'node:process'
import type { UserConfig, UserConfigDefaults } from '@unocss/core'
import type { ResolvedUnpluginOptions, UnpluginOptions } from 'unplugin'
import { createUnplugin } from 'unplugin'
Expand All @@ -23,7 +24,10 @@ export default function WebpackPlugin<Theme extends object>(
defaults?: UserConfigDefaults,
) {
return createUnplugin(() => {
const ctx = createContext<WebpackPluginOptions>(configOrPath as any, defaults)
const ctx = createContext<WebpackPluginOptions>(configOrPath as any, {
envMode: process.env.NODE_ENV === 'development' ? 'dev' : 'build',
...defaults,
})
const { uno, tokens, filter, extract, onInvalidate, tasks, flushTasks } = ctx

let timer: any
Expand Down
73 changes: 70 additions & 3 deletions test/__snapshots__/extended-info.test.ts.snap
Expand Up @@ -14,7 +14,30 @@ Map {
"layer": "a",
"sort": undefined,
},
undefined,
{
"currentSelector": "a",
"rawSelector": "a",
"rules": [
[
"a",
{
"name": "bar1",
},
{
"layer": "a",
},
],
],
"theme": {},
"variantHandlers": [],
"variantMatch": [
"a",
"a",
[],
Set {},
],
"variants": [],
},
undefined,
],
],
Expand All @@ -31,7 +54,30 @@ Map {
"layer": "b",
"sort": undefined,
},
undefined,
{
"currentSelector": "b",
"rawSelector": "b",
"rules": [
[
"b",
{
"name": "bar2",
},
{
"layer": "b",
},
],
],
"theme": {},
"variantHandlers": [],
"variantMatch": [
"b",
"b",
[],
Set {},
],
"variants": [],
},
undefined,
],
],
Expand All @@ -48,7 +94,28 @@ Map {
"layer": "c",
"sort": undefined,
},
undefined,
{
"currentSelector": "c1",
"rawSelector": "c1",
"rules": [
[
/\\^c\\(\\\\d\\+\\)\\$/,
[Function],
{
"layer": "c",
},
],
],
"theme": {},
"variantHandlers": [],
"variantMatch": [
"c1",
"c1",
[],
Set {},
],
"variants": [],
},
undefined,
],
],
Expand Down
11 changes: 11 additions & 0 deletions test/extended-info.test.ts
Expand Up @@ -20,5 +20,16 @@ test('extended-info', async () => {
expect(extended.get('a')?.count).toBe(1)
expect(extended.get('b')?.count).toBe(2)

// clean up `context` to make clear snapshot
Array.from(extended.values()).forEach((i) => {
i.data.forEach((j) => {
if (j[5]) {
// @ts-expect-error removing no-required
delete j[5]?.generator
// @ts-expect-error removing no-required
delete j[5]?.constructCSS
}
})
})
expect(extended).toMatchSnapshot()
})

0 comments on commit 29e53de

Please sign in to comment.