Skip to content

Commit 1328d49

Browse files
a3mitskevichKaelWD
andcommitted
feat: add autoImport ignore option (#323)
Co-authored-by: Kael <[email protected]>
1 parent 8cc31a8 commit 1328d49

File tree

8 files changed

+44
-14
lines changed

8 files changed

+44
-14
lines changed

packages/shared/src/imports/generateImports.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { getImports } from './getImports'
22

3-
export function generateImports (source: string) {
4-
const { imports, components, directives } = getImports(source)
3+
export function generateImports (source: string, ignore: string[] = []) {
4+
const { imports, components, directives } = getImports(source, ignore)
55

66
let code = ''
77

packages/shared/src/imports/getImports.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
import { parseTemplate, TemplateMatch } from './parseTemplate'
22
import * as importMap from 'vuetify/dist/json/importMap.json'
33

4-
export function getImports (source: string) {
4+
export function getImports (source: string, ignore: string[] = []) {
55
const { components, directives } = parseTemplate(source)
66
const resolvedComponents: TemplateMatch[] = []
77
const resolvedDirectives: TemplateMatch[] = []
88
const imports = new Map<string, string[]>()
99

1010
if (components.size || directives.size) {
1111
components.forEach(component => {
12-
if (component.name in importMap.components) {
12+
if (component.name in importMap.components && !ignore.includes(component.name)) {
1313
resolvedComponents.push(component)
1414
}
1515
})
1616
directives.forEach(directive => {
17-
if (importMap.directives.includes(directive.name)) {
17+
if (importMap.directives.includes(directive.name) && !ignore.includes(directive.name)) {
1818
resolvedDirectives.push(directive)
1919
}
2020
})

packages/shared/src/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import * as path from 'upath'
2+
import type * as Components from 'vuetify/components'
3+
import type * as Directives from 'vuetify/directives'
24

35
export interface Options {
46
autoImport?: importPluginOptions,
@@ -7,8 +9,12 @@ export interface Options {
79
},
810
}
911

12+
export interface ObjectImportPluginOptions {
13+
ignore: (keyof typeof Components | keyof typeof Directives)[]
14+
}
1015
export type importPluginOptions =
1116
| boolean
17+
| ObjectImportPluginOptions
1218
// | ((source: string, importer: string, isVuetify: boolean) => boolean | null | replace)
1319
// type replace = { symbol: string, from: string, as?: string }
1420

packages/vite-plugin/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,23 @@ import { createVuetify } from 'vuetify'
2626
export default createVuetify()
2727
```
2828

29+
### Ignoring components or directives
30+
```js
31+
// vite.config.js
32+
plugins: [
33+
vue(),
34+
vuetify({
35+
autoImport: {
36+
ignore: [
37+
'VAlert', // Component name
38+
'Ripple', // Directive name
39+
]
40+
}
41+
}),
42+
]
43+
```
44+
Note `ignore` values are case-sensitive
45+
2946
## Style loading
3047
### Customising variables
3148
```js

packages/vite-plugin/src/importPlugin.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { extname } from 'path'
22
import { Plugin } from 'vite'
3-
import { generateImports } from '@vuetify/loader-shared'
3+
import { generateImports, importPluginOptions, isObject } from '@vuetify/loader-shared'
44
import { parse as parseUrl, URLSearchParams } from 'url'
55

66
function parseId (id: string) {
@@ -12,7 +12,8 @@ function parseId (id: string) {
1212
}
1313
}
1414

15-
export function importPlugin (): Plugin {
15+
export function importPlugin (_options: importPluginOptions): Plugin {
16+
const options = isObject(_options) ? _options : { ignore: [] };
1617
return {
1718
name: 'vuetify:import',
1819
configResolved (config) {
@@ -27,7 +28,7 @@ export function importPlugin (): Plugin {
2728
((!query || !('vue' in query)) && extname(path) === '.vue' && !/^import { render as _sfc_render } from ".*"$/m.test(code)) ||
2829
(query && 'vue' in query && (query.type === 'template' || (query.type === 'script' && query.setup === 'true')))
2930
) {
30-
const { code: imports, source } = generateImports(code)
31+
const { code: imports, source } = generateImports(code, options.ignore)
3132
return {
3233
code: source + imports,
3334
map: null,

packages/vite-plugin/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export default function vuetify (_options: Options = {}): Plugin[] {
1313

1414
const plugins: Plugin[] = []
1515
if (options.autoImport) {
16-
plugins.push(importPlugin())
16+
plugins.push(importPlugin(options.autoImport))
1717
}
1818
if (includes(['none', 'sass'], options.styles) || isObject(options.styles)) {
1919
plugins.push(stylesPlugin(options))

packages/webpack-plugin/src/plugin.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ export class VuetifyPlugin {
3434
}
3535

3636
apply (compiler: Compiler) {
37-
if (this.options.autoImport) {
37+
const autoImport = this.options.autoImport
38+
if (autoImport) {
3839
compiler.options.module.rules.unshift({
3940
resourceQuery: query => {
4041
if (!query) return false
@@ -44,7 +45,10 @@ export class VuetifyPlugin {
4445
(qs.get('type') === 'script' && qs.has('setup'))
4546
)
4647
},
47-
use: { loader: require.resolve('./scriptLoader') },
48+
use: {
49+
loader: require.resolve('./scriptLoader'),
50+
options: isObject(autoImport) ? autoImport : undefined
51+
},
4852
})
4953
}
5054

packages/webpack-plugin/src/scriptLoader.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { LoaderDefinitionFunction } from 'webpack'
2-
import { generateImports } from '@vuetify/loader-shared'
2+
import { generateImports, ObjectImportPluginOptions } from '@vuetify/loader-shared'
33

44
export default (function VuetifyLoader (content, sourceMap) {
55
if (this.data?.skip) {
@@ -9,10 +9,12 @@ export default (function VuetifyLoader (content, sourceMap) {
99
this.async()
1010
this.cacheable()
1111

12-
const { code: imports, source } = generateImports(content)
12+
const options = this.getOptions()
13+
14+
const { code: imports, source } = generateImports(content, options?.ignore)
1315

1416
this.callback(null, source + imports, sourceMap)
15-
} as LoaderDefinitionFunction)
17+
} as LoaderDefinitionFunction<ObjectImportPluginOptions | undefined>)
1618

1719
export const pitch = (function VuetifyLoaderPitch (remainingRequest, precedingRequest, data) {
1820
if (this.loaders.some(loader => loader.path.endsWith('vue-loader/dist/pitcher.js'))) {

0 commit comments

Comments
 (0)