-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmodule.ts
100 lines (91 loc) · 2.24 KB
/
module.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/* eslint-disable @typescript-eslint/ban-ts-comment */
import {
defineNuxtModule,
addVitePlugin,
extendWebpackConfig,
createResolver,
addComponentsDir,
addComponent
} from '@nuxt/kit'
import type { NuxtModule } from '@nuxt/schema'
import type { Config } from 'svgo'
import { SvgLoaderOptions, svgLoader } from './loaders/vite'
export const defaultSvgoConfig: Config = {
plugins: [
{
name: 'preset-default',
params: {
overrides: {
removeViewBox: false
}
}
},
'removeDimensions'
]
}
export type ModuleOptions = SvgLoaderOptions & {
autoImportPath?: string
global?: boolean
}
const nuxtSvgo: NuxtModule<ModuleOptions> = defineNuxtModule({
meta: {
name: 'nuxt-svgo',
configKey: 'svgo',
compatibility: {
// Add -rc.0 due to issue described in https://github.com/nuxt/framework/issues/6699
nuxt: '^3.0.0-rc.0'
}
},
defaults: {
svgo: true,
defaultImport: 'componentext',
autoImportPath: './assets/icons/',
svgoConfig: undefined,
global: true
},
async setup(options) {
const { resolvePath, resolve } = createResolver(import.meta.url)
addComponent({
name: 'nuxt-icon',
filePath: resolve('./runtime/components/nuxt-icon.vue')
})
addVitePlugin(
svgLoader({
...options,
svgoConfig: options.svgoConfig || defaultSvgoConfig
})
)
if (options.autoImportPath) {
addComponentsDir({
path: await resolvePath(options.autoImportPath),
global: options.global,
extensions: ['svg'],
prefix: 'svgo',
watch: true
})
}
extendWebpackConfig((config) => {
// @ts-ignore
const svgRule = config.module.rules.find((rule) => rule.test.test('.svg'))
// @ts-ignore
svgRule.test = /\.(png|jpe?g|gif|webp)$/
config.module.rules.push({
test: /\.svg$/,
use: [
'vue-loader',
{
loader: 'vue-svg-loader',
options: {
svgo: false
}
},
options.svgo && {
loader: 'svgo-loader',
options: options.svgoConfig || defaultSvgoConfig
}
].filter(Boolean)
})
})
}
})
export default nuxtSvgo