Skip to content

Commit 9bf71d4

Browse files
committed
fix: resolve vuetify relative to cwd
fixes #248
1 parent defb8df commit 9bf71d4

File tree

3 files changed

+12
-26
lines changed

3 files changed

+12
-26
lines changed

packages/shared/src/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import * as path from 'upath'
2+
13
export interface Options {
24
autoImport?: importPluginOptions,
35
styles?: true | 'none' | 'expose' | 'sass',
@@ -12,3 +14,7 @@ export type importPluginOptions =
1214

1315
export { generateImports } from './imports/generateImports'
1416
export { cacheDir, writeStyles } from './styles/writeStyles'
17+
18+
export function resolveVuetifyBase () {
19+
return path.dirname(require.resolve('vuetify/package.json', { paths: [process.cwd()] }))
20+
}

packages/vite-plugin/src/stylesPlugin.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { utimes } from 'fs/promises'
22
import * as path from 'upath'
33
import _debug from 'debug'
4-
import { cacheDir, writeStyles } from '@vuetify/loader-shared'
4+
import { cacheDir, writeStyles, resolveVuetifyBase } from '@vuetify/loader-shared'
55

66
import type { PluginOption, ViteDevServer } from 'vite'
77
import type { Options } from '@vuetify/loader-shared'
@@ -17,7 +17,7 @@ function isSubdir (root: string, test: string) {
1717
const styleImportRegexp = /(@use |meta\.load-css\()['"](vuetify(?:\/lib)?\/styles(?:\/main(?:\.sass)?)?)['"]/
1818

1919
export function stylesPlugin (options: Options): PluginOption {
20-
const vuetifyBase = path.dirname(require.resolve('vuetify/package.json'))
20+
const vuetifyBase = resolveVuetifyBase()
2121
const files = new Set<string>()
2222

2323
let server: ViteDevServer

packages/webpack-plugin/src/plugin.ts

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,10 @@
11
import * as path from 'upath'
22
import { URLSearchParams } from 'url'
3-
import { writeStyles } from '@vuetify/loader-shared'
3+
import { resolveVuetifyBase, writeStyles } from '@vuetify/loader-shared'
44

55
import type { Compiler, NormalModule, Module } from 'webpack'
6-
import type { Resolver, ResolveContext } from 'enhanced-resolve'
76
import type { Options } from '@vuetify/loader-shared'
87

9-
// Can't use require.resolve() for this, it doesn't work with resolve.symlinks
10-
let vuetifyBase: string
11-
async function getVuetifyBase (base: string, context: ResolveContext, resolver: Resolver) {
12-
if (!getVuetifyBase.promise) {
13-
let resolve: (v: any) => void
14-
getVuetifyBase.promise = new Promise((_resolve) => resolve = _resolve)
15-
resolver.resolve({}, base, 'vuetify/package.json', context, (err, vuetifyPath) => {
16-
if (vuetifyPath) {
17-
vuetifyBase = path.dirname(vuetifyPath as string)
18-
}
19-
resolve(true)
20-
})
21-
}
22-
return getVuetifyBase.promise
23-
}
24-
getVuetifyBase.promise = null as Promise<any> | null
25-
268
function isSubdir (root: string, test: string) {
279
const relative = path.relative(root, test)
2810
return relative && !relative.startsWith('..') && !path.isAbsolute(relative)
@@ -55,6 +37,8 @@ export class VuetifyPlugin {
5537
})
5638
}
5739

40+
const vuetifyBase = resolveVuetifyBase()
41+
5842
if (
5943
this.options.styles === 'none' ||
6044
this.options.styles === 'expose'
@@ -71,7 +55,7 @@ export class VuetifyPlugin {
7155
factory.hooks.beforeResolve.tap('vuetify-loader', resolveData => {
7256
if (
7357
resolveData.request.endsWith('.css') &&
74-
isSubdir(path.dirname(require.resolve('vuetify/package.json')), resolveData.context)
58+
isSubdir(vuetifyBase, resolveData.context)
7559
) {
7660
const match = resolveData.request.match(/.*!(.+\.css)$/)
7761
if (match) {
@@ -156,10 +140,6 @@ export class VuetifyPlugin {
156140
resolver
157141
.getHook('resolve')
158142
.tapAsync('vuetify-loader', async (request, context, callback) => {
159-
if (request.path && !vuetifyBase && request.request !== 'vuetify/package.json') {
160-
await getVuetifyBase(request.path, context, resolver)
161-
}
162-
163143
if (!(
164144
request.path &&
165145
request.request?.endsWith('.css') &&

0 commit comments

Comments
 (0)