Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix(transformer-directives,nuxt): revert enforce: 'pre', disable `m…
…ergeRules` (#1930)

* fix(transformer-directives,nuxt): revert `enforce: 'pre'`, disable `mergeRules`

* fix build
  • Loading branch information
sibbng committed Nov 29, 2022
1 parent 9c89d93 commit ae2bdd5
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 7 deletions.
5 changes: 3 additions & 2 deletions packages/config/src/index.ts
@@ -1,6 +1,6 @@
import { dirname, resolve } from 'path'
import fs from 'fs'
import type { UserConfig } from '@unocss/core'
import type { UserConfig, UserConfigDefaults } from '@unocss/core'
import type { LoadConfigResult, LoadConfigSource } from 'unconfig'
import { createConfigLoader as createLoader } from 'unconfig'

Expand All @@ -10,10 +10,11 @@ export async function loadConfig<U extends UserConfig>(
cwd = process.cwd(),
configOrPath: string | U = cwd,
extraConfigSources: LoadConfigSource[] = [],
defaults: UserConfigDefaults = {},
): Promise<LoadConfigResult<U>> {
let inlineConfig = {} as U
if (typeof configOrPath !== 'string') {
inlineConfig = configOrPath
inlineConfig = Object.assign({}, defaults, configOrPath)
if (inlineConfig.configFile === false) {
return {
config: inlineConfig as U,
Expand Down
1 change: 1 addition & 0 deletions packages/nuxt/package.json
Expand Up @@ -42,6 +42,7 @@
},
"dependencies": {
"@nuxt/kit": "^3.0.0",
"@unocss/config": "workspace:*",
"@unocss/core": "workspace:*",
"@unocss/preset-attributify": "workspace:*",
"@unocss/preset-icons": "workspace:*",
Expand Down
22 changes: 19 additions & 3 deletions packages/nuxt/src/index.ts
Expand Up @@ -4,6 +4,8 @@ import { addComponentsDir, addPluginTemplate, defineNuxtModule, extendWebpackCon
import WebpackPlugin from '@unocss/webpack'
import VitePlugin from '@unocss/vite'
import type { NuxtPlugin } from '@nuxt/schema'
import { loadConfig } from '@unocss/config'
import type { UserConfig } from '@unocss/core'
import { resolveOptions } from './options'
import type { UnocssNuxtOptions } from './types'

Expand All @@ -28,7 +30,7 @@ export default defineNuxtModule<UnocssNuxtOptions>({
icons: false,
wind: false,
},
setup(options, nuxt) {
async setup(options, nuxt) {
// preset shortcuts
resolveOptions(options)

Expand Down Expand Up @@ -56,9 +58,23 @@ export default defineNuxtModule<UnocssNuxtOptions>({
})
}

const { config: unoConfig } = await loadConfig<UserConfig>(process.cwd(), {}, [], options)

if (
nuxt.options.postcss.plugins.cssnano
&& unoConfig.transformers?.some(t => t.name === 'css-directive' && t.enforce !== 'pre')
) {
const preset = nuxt.options.postcss.plugins.cssnano.preset
nuxt.options.postcss.plugins.cssnano = {
preset: [preset?.[0] || 'default', Object.assign(
preset?.[1] || {}, { mergeRules: false },
)],
}
}

nuxt.hook('vite:extend', ({ config }) => {
config.plugins = config.plugins || []
config.plugins.unshift(...VitePlugin({}, options))
config.plugins.unshift(...VitePlugin(unoConfig))
})

// Nuxt 2
Expand All @@ -74,7 +90,7 @@ export default defineNuxtModule<UnocssNuxtOptions>({

extendWebpackConfig((config) => {
config.plugins = config.plugins || []
config.plugins.unshift(WebpackPlugin({}, options))
config.plugins.unshift(WebpackPlugin(unoConfig))
})
},
})
Expand Down
2 changes: 1 addition & 1 deletion packages/shared-integration/src/context.ts
Expand Up @@ -28,7 +28,7 @@ export function createContext<Config extends UserConfig<any> = UserConfig<any>>(
let ready = reloadConfig()

async function reloadConfig() {
const result = await loadConfig(root, configOrPath, extraConfigSources)
const result = await loadConfig(root, configOrPath, extraConfigSources, defaults)
resolveConfigResult(result)

rawConfig = result.config
Expand Down
2 changes: 1 addition & 1 deletion packages/transformer-directives/src/index.ts
Expand Up @@ -37,7 +37,7 @@ export interface TransformerDirectivesContext {
export default function transformerDirectives(options: TransformerDirectivesOptions = {}): SourceCodeTransformer {
return {
name: 'css-directive',
enforce: options?.enforce || 'pre',
enforce: options?.enforce,
idFilter: id => !!id.match(cssIdRE),
transform: (code, id, ctx) => {
return transformDirectives(code, ctx.uno, options, id)
Expand Down
2 changes: 2 additions & 0 deletions pnpm-lock.yaml
Expand Up @@ -371,6 +371,7 @@ importers:
specifiers:
'@nuxt/kit': ^3.0.0
'@nuxt/schema': ^3.0.0
'@unocss/config': workspace:*
'@unocss/core': workspace:*
'@unocss/preset-attributify': workspace:*
'@unocss/preset-icons': workspace:*
Expand All @@ -385,6 +386,7 @@ importers:
unocss: workspace:*
dependencies:
'@nuxt/kit': 3.0.0
'@unocss/config': link:../config
'@unocss/core': link:../core
'@unocss/preset-attributify': link:../preset-attributify
'@unocss/preset-icons': link:../preset-icons
Expand Down

0 comments on commit ae2bdd5

Please sign in to comment.