Skip to content

Commit

Permalink
fix: respect vue's include and exclude options
Browse files Browse the repository at this point in the history
  • Loading branch information
KaelWD committed Aug 4, 2024
1 parent e1587d8 commit 557713a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
1 change: 1 addition & 0 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module.exports = {
'plugin:import/typescript',
],
rules: {
'no-unused-vars': 'off',
'import/no-extraneous-dependencies': ['error', { devDependencies: false }],
}
}
23 changes: 17 additions & 6 deletions packages/vite-plugin/src/importPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { extname } from 'path'
import { Plugin } from 'vite'
import { Plugin, createFilter } from 'vite'
import { generateImports, Options } from '@vuetify/loader-shared'
import { URLSearchParams } from 'url'

Expand All @@ -13,20 +13,31 @@ function parseId (id: string) {
}

export function importPlugin (options: Options): Plugin {
let filter: (id: unknown) => boolean
return {
name: 'vuetify:import',
configResolved (config) {
if (config.plugins.findIndex(plugin => plugin.name === 'vuetify:import') < config.plugins.findIndex(plugin => plugin.name === 'vite:vue')) {
const vuetifyIdx = config.plugins.findIndex(plugin => plugin.name === 'vuetify:import')
const vueIdx = config.plugins.findIndex(plugin => plugin.name === 'vite:vue')
if (vuetifyIdx < vueIdx) {
throw new Error('Vuetify plugin must be loaded after the vue plugin')
}
const vueOptions = config.plugins[vueIdx].api.options
filter = createFilter(vueOptions.include, vueOptions.exclude)
},
async transform (code, id) {
const { query, path } = parseId(id)

if (
((!query || !('vue' in query)) && extname(path) === '.vue' && !/^import { render as _sfc_render } from ".*"$/m.test(code)) ||
(query && 'vue' in query && (query.type === 'template' || (query.type === 'script' && query.setup === 'true')))
) {
const isVueVirtual = query && 'vue' in query
const isVueFile = !isVueVirtual &&
filter(path) &&
!/^import { render as _sfc_render } from ".*"$/m.test(code)
const isVueTemplate = isVueVirtual && (
query.type === 'template' ||
(query.type === 'script' && query.setup === 'true')
)

if (isVueFile || isVueTemplate) {
const { code: imports, source } = generateImports(code, options)
return {
code: source + imports,
Expand Down

0 comments on commit 557713a

Please sign in to comment.