This repository was archived by the owner on Jan 18, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 150
/
Copy pathindex.ts
257 lines (231 loc) · 7.03 KB
/
index.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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
import {
createVueFilter,
createVuePartRequest,
parseVuePartRequest,
resolveVuePart,
isVuePartRequest
} from './utils'
import {
createDefaultCompiler,
assemble,
ScriptOptions,
StyleOptions,
TemplateOptions,
StyleCompileResult
} from '@vue/component-compiler'
import {Plugin} from 'rollup'
import * as path from 'path'
import {parse, SFCDescriptor, SFCBlock} from '@vue/component-compiler-utils'
const hash = require('hash-sum')
export interface VuePluginOptions {
/**
* Include files or directories.
* @default `'.vue'`
*/
include?: string
/**
* Exclude files or directories.
* @default `undefined`
*/
exclude?: string
/**
* Default language for blocks.
*
* @default `{}`
* @example
* ```js
* VuePlugin({ defaultLang: { script: 'ts' } })
* ```
*/
defaultLang?: {
[key: string]: string
},
/**
* Exclude customBlocks for final build.
* @default `['*']`
* @example
* ```js
* VuePlugin({ blackListCustomBlocks: ['markdown', 'test'] })
* ```
*/
blackListCustomBlocks?: string[]
/**
* Include customBlocks for final build.
* @default `[]`
* @example
* ```js
* VuePlugin({ blackListCustomBlocks: ['markdown', 'test'] })
* ```
*/
whiteListCustomBlocks?: string[]
/**
* Inject CSS in JavaScript.
* @default `true`
* @example
* ```js
* VuePlugin({ css: false }) // to extract css
* ```
*/
css?: boolean
/**
* @@vue/component-compiler [#](https://github.com/vuejs/vue-component-compiler#api) script processing options.
*/
script?: ScriptOptions
/**
* @@vue/component-compiler [#](https://github.com/vuejs/vue-component-compiler#api) style processing options.
*/
style?: StyleOptions
/**
* @@vue/component-compiler [#](https://github.com/vuejs/vue-component-compiler#api) template processing options.
*/
template?: TemplateOptions
/**
* @@vue/component-compiler [#](https://github.com/vuejs/vue-component-compiler#api) module name or global function for custom runtime component normalizer.
*/
normalizer?: string
/**
* @@vue/component-compiler [#](https://github.com/vuejs/vue-component-compiler#api) module name or global function for custom style injector factory.
*/
styleInjector?: string
/**
* @@vue/component-compiler [#](https://github.com/vuejs/vue-component-compiler#api) module name or global function for custom style injector factory for SSR environment.
*/
styleInjectorSSR?: string
}
/**
* Rollup plugin for handling .vue files.
*/
export default function VuePlugin(opts: VuePluginOptions = {}): Plugin {
const isVue = createVueFilter(opts.include, opts.exclude)
const isProduction = process.env.NODE_ENV === 'production'
createVuePartRequest.defaultLang = {
...createVuePartRequest.defaultLang,
...opts.defaultLang
}
const shouldExtractCss = opts.css === false
const blacklisted = new Set(opts.blackListCustomBlocks || ['*'])
const whitelisted = new Set(opts.whiteListCustomBlocks || [])
const isAllowed = (customBlockType: string) =>
(!blacklisted.has('*') || !blacklisted.has(customBlockType)) &&
(whitelisted.has('*') || whitelisted.has(customBlockType))
delete opts.css
delete opts.blackListCustomBlocks
delete opts.whiteListCustomBlocks
delete opts.defaultLang
delete opts.include
delete opts.exclude
const compiler = createDefaultCompiler(opts)
const descriptors = new Map<string, SFCDescriptor>()
return {
name: 'VuePlugin',
resolveId(id, importer) {
if (!isVuePartRequest(id)) return
id = path.resolve(path.dirname(importer), id)
const ref = parseVuePartRequest(id)
if (ref) {
const element = resolveVuePart(descriptors, ref)
const src = (element as SFCBlock).src
if (ref.meta.type !== 'styles' && typeof src === 'string') {
if (src.startsWith('.')) {
return path.resolve(path.dirname(ref.filename), src as string)
} else {
return require.resolve(src, { paths: [path.dirname(ref.filename)] })
}
}
return id
}
},
load(id: string) {
const request = parseVuePartRequest(id)
if (!request) return
const element = resolveVuePart(descriptors, request)
return 'code' in element
? (element as any).code as string // .code is set when extract styles is used. { css: false }
: element.content
},
async transform(source: string, filename: string) {
if (isVue(filename)) {
const descriptor = parse({
filename,
source,
needMap: true
})
const scopeId =
'data-v-' +
(isProduction
? hash(path.basename(filename) + source)
: hash(filename + source))
descriptors.set(filename, descriptor)
const input: any = {
scopeId,
styles: descriptor.styles.map(style =>
compiler.compileStyle(filename, scopeId, style)
),
customBlocks: []
}
if (descriptor.template) {
input.template = compiler.compileTemplate(
filename,
descriptor.template
)
if (input.template.errors && input.template.errors.length) {
input.template.errors.map((error: Error) => this.error(error))
}
if (input.template.tips && input.template.tips.length) {
input.template.tips.map((message: string) => this.warn({ message }))
}
}
input.script = descriptor.script
? {
code: `
export * from '${createVuePartRequest(
filename,
descriptor.script.lang || 'js',
'script'
)}'
import script from '${createVuePartRequest(
filename,
descriptor.script.lang || 'js',
'script'
)}'
export default script
`
}
: {code: ''}
if (shouldExtractCss) {
input.styles = input.styles
.map((style: StyleCompileResult, index: number) => {
(descriptor.styles[index] as any).code = style.code
input.script.code +=
'\n' +
`import '${createVuePartRequest(
filename,
'css',
'styles',
index
)}'`
if (style.module || descriptor.styles[index].scoped) {
return {...style, code: ''}
}
})
.filter(Boolean)
}
const result = assemble(compiler, filename, input, opts)
descriptor.customBlocks.forEach((block, index) => {
if (!isAllowed(block.type)) return
result.code +=
'\n' +
`export * from '${createVuePartRequest(
filename,
block.attrs.lang ||
createVuePartRequest.defaultLang[block.type] ||
block.type,
'customBlocks',
index
)}'`
})
return result
}
}
}
}