Skip to content

Commit 71be9ac

Browse files
committedJan 13, 2025·
fix(injectPosition): resolve file paths to determine position
1 parent 078772c commit 71be9ac

File tree

2 files changed

+14
-16
lines changed

2 files changed

+14
-16
lines changed
 

Diff for: ‎src/module.ts

+1-8
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,7 @@ export default defineNuxtModule<ModuleOptions>({
7171

7272
// inject only if this file isn't listed already by user
7373
if (resolvedCss && !resolvedNuxtCss.includes(resolvedCss)) {
74-
let injectPosition: number
75-
try {
76-
injectPosition = resolvers.resolveInjectPosition(nuxt.options.css, cssPathConfig?.injectPosition)
77-
}
78-
catch (e: any) {
79-
throw new Error('failed to resolve Tailwind CSS injection position: ' + e.message)
80-
}
81-
74+
const injectPosition = await resolvers.resolveInjectPosition(resolvedNuxtCss, cssPathConfig?.injectPosition)
8275
nuxt.options.css.splice(injectPosition, 0, resolvedCss)
8376
}
8477

Diff for: ‎src/resolvers.ts

+13-8
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export const resolveEditorSupportConfig = (config: ModuleOptions['editorSupport'
4747
*
4848
* @returns index in the css array
4949
*/
50-
export function resolveInjectPosition(css: string[], position: InjectPosition = 'first') {
50+
export async function resolveInjectPosition(css: string[], position: InjectPosition = 'first') {
5151
if (typeof (position) === 'number') {
5252
return ~~Math.min(position, css.length + 1)
5353
}
@@ -56,18 +56,23 @@ export function resolveInjectPosition(css: string[], position: InjectPosition =
5656
switch (position) {
5757
case 'first': return 0
5858
case 'last': return css.length
59-
default: throw new Error('invalid literal: ' + position)
6059
}
6160
}
6261

63-
if (position.after !== undefined) {
64-
const index = css.indexOf(position.after)
65-
if (index === -1) {
66-
throw new Error('`after` position specifies a file which does not exists on CSS stack: ' + position.after)
62+
if (typeof (position) === 'object') {
63+
const minIndex = 'after' in position ? css.indexOf(await resolvePath(position.after)) + 1 : 0
64+
const maxIndex = 'before' in position ? css.indexOf(await resolvePath(position.before as string)) : css.length
65+
66+
if ([minIndex, maxIndex].includes(-1) || ('after' in position && minIndex === 0)) {
67+
throw new Error(`\`injectPosition\` specifies a file which does not exists on CSS stack: ` + JSON.stringify(position))
68+
}
69+
70+
if (minIndex > maxIndex) {
71+
throw new Error(`\`injectPosition\` specifies a relative location \`${minIndex}\` that cannot be resolved (i.e., \`after\` orders \`before\` may be reversed): ` + JSON.stringify(position))
6772
}
6873

69-
return index + 1
74+
return 'after' in position ? minIndex : maxIndex
7075
}
7176

72-
throw new Error('invalid position: ' + JSON.stringify(position))
77+
throw new Error('invalid `injectPosition`: ' + JSON.stringify(position))
7378
}

0 commit comments

Comments
 (0)
Please sign in to comment.