-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
plugin.js
126 lines (100 loc) · 2.62 KB
/
plugin.js
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import { normalizePath } from 'vite'
import { getViteConfig } from './vite-config'
import { vueTransform } from './vue-transform'
import { createScssTransform } from './scss-transform'
import { parseViteRequest } from './query'
import { jsProdTransform } from './js-transform'
const defaultOptions = {
runMode: 'web-client',
autoImportComponentCase: 'kebab',
sassVariables: true
}
function getConfigPlugin (opts) {
return {
name: 'vite:quasar:vite-conf',
config (viteConf) {
const vueCfg = viteConf.plugins.find(entry => entry.name === 'vite:vue')
if (vueCfg === void 0) {
console.warn('In your Vite config file, please add the Quasar plugin after the Vue one')
process.exit(1)
}
return getViteConfig(opts.runMode, viteConf)
}
}
}
function getScssTransformsPlugin (opts) {
const sassVariables = typeof opts.sassVariables === 'string'
? normalizePath(opts.sassVariables)
: opts.sassVariables
const scssTransform = createScssTransform('scss', sassVariables)
const sassTransform = createScssTransform('sass', sassVariables)
const scssExt = [ '.scss' ]
const sassExt = [ '.sass' ]
return {
name: 'vite:quasar:scss',
enforce: 'pre',
transform (src, id) {
const { is } = parseViteRequest(id)
if (is.style(scssExt) === true) {
return {
code: scssTransform(src),
map: null
}
}
if (is.style(sassExt) === true) {
return {
code: sassTransform(src),
map: null
}
}
return null
}
}
}
function getScriptTransformsPlugin (opts) {
let isDev = false
return {
name: 'vite:quasar:script',
configResolved (resolvedConfig) {
if (resolvedConfig.mode === 'development') {
isDev = true
}
},
transform (src, id) {
const { is } = parseViteRequest(id)
if (is.template() === true) {
return {
code: vueTransform(src, opts.autoImportComponentCase, isDev),
map: null // provide source map if available
}
}
if (isDev === false && is.script() === true) {
return {
code: jsProdTransform(src),
map: null // provide source map if available
}
}
return null
}
}
}
export default function (userOpts = {}) {
const opts = {
...defaultOptions,
...userOpts
}
const plugins = [
getConfigPlugin(opts)
]
if (opts.sassVariables) {
plugins.push(
getScssTransformsPlugin(opts)
)
}
if (opts.runMode !== 'ssr-server') {
plugins.push(
getScriptTransformsPlugin(opts)
)
}
return plugins
}