Skip to content

Commit

Permalink
fix(nuxt): temporary revert #3448 (#3482)
Browse files Browse the repository at this point in the history
Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
  • Loading branch information
zyyv and antfu committed Jan 2, 2024
1 parent 61b5d62 commit b3905de
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 33 deletions.
17 changes: 7 additions & 10 deletions packages/nuxt/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import VitePlugin from '@unocss/vite'
import type { NuxtPlugin } from '@nuxt/schema'
import { loadConfig } from '@unocss/config'
import type { UserConfig } from '@unocss/core'
import { resolveInjectPosition, resolveOptions } from './options'
import { resolveOptions } from './options'
import type { UnocssNuxtOptions } from './types'

export { UnocssNuxtOptions }
Expand All @@ -25,7 +25,6 @@ export default defineNuxtModule<UnocssNuxtOptions>({
autoImport: true,
preflight: false,
components: true,
injectPosition: 'first',

// presets
uno: true,
Expand All @@ -38,20 +37,18 @@ export default defineNuxtModule<UnocssNuxtOptions>({
// preset shortcuts
resolveOptions(options)

if (options.autoImport) {
options.mode ??= 'global'
const InjectModes: VitePluginConfig['mode'][] = ['global', 'dist-chunk']
nuxt.options.css = nuxt.options.css ?? []
options.mode ??= 'global'
const InjectModes: VitePluginConfig['mode'][] = ['global', 'dist-chunk']

if (InjectModes.includes(options.mode) && !nuxt.options.css.includes('uno.css')) {
const position = resolveInjectPosition(nuxt.options.css, options.injectPosition)
nuxt.options.css.splice(position, 0, 'uno.css')
}
if (options.injectPosition != null)
console.warn('[unocss/nuxt] options `injectPosition` is temporary removed due to the incompatibility with Nuxt 3.9. We are seeking for better solution. It\'s not effective at this moment.')

if (options.autoImport) {
addPluginTemplate({
filename: 'unocss.mjs',
getContents: () => {
const lines = [
InjectModes.includes(options.mode) ? 'import \'uno.css\'' : '',
isNuxt2()
? 'export default () => {}'
: 'import { defineNuxtPlugin } from \'#imports\'; export default defineNuxtPlugin(() => {})',
Expand Down
23 changes: 0 additions & 23 deletions packages/nuxt/src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,26 +36,3 @@ export function resolveOptions(options: UnocssNuxtOptions) {
options.content.pipeline.exclude.push(/\?macro=true/)
}
}

export function resolveInjectPosition(css: string[], position: UnocssNuxtOptions['injectPosition']) {
if (typeof (position) === 'number')
return ~~Math.min(position, css.length + 1)

if (typeof (position) === 'string') {
switch (position) {
case 'first': return 0
case 'last': return css.length
default: throw new Error(`invalid literal: ${position}`)
}
}

if (position?.after !== undefined) {
const index = css.indexOf(position.after)
if (index === -1)
throw new Error(`\`after\` position specifies a file which does not exists on CSS stack: ${position.after}`)

return index + 1
}

throw new Error(`invalid position: ${JSON.stringify(position)}`)
}
1 change: 1 addition & 0 deletions packages/nuxt/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export interface UnocssNuxtOptions extends UserConfig {
* Adjust the position of the `uno.css` injection. (Depends on `mode`)
*
* @default 'first'
* @deprecated Temporarily removed, will be added back in the future.
*/
injectPosition?: 'first' | 'last' | number | { after?: string }

Expand Down

0 comments on commit b3905de

Please sign in to comment.