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 pathdelegate.js
174 lines (150 loc) · 4.61 KB
/
delegate.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
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
import {
createVueFilter,
isVuePartRequest,
createVuePartRequest,
parseVuePartRequest,
resolveVuePart
} from './utils'
import * as path from 'path'
import { parse } from '@vue/component-compiler-utils'
import { createDefaultCompiler, assemble } from '@vue/component-compiler'
import hash from 'hash-sum'
import { relative } from 'path'
export default function vue(opts = {}) {
const isVue = createVueFilter(opts.include, opts.exclude)
const isProduction = process.env.NODE_ENV === 'production'
const compiler = createDefaultCompiler(opts.compiler)
createVuePartRequest.defaultLang = {
...createVuePartRequest.defaultLang,
...opts.defaultLang
}
const blacklisted = new Set(opts.blacklistCustomBlocks || [])
delete opts.include
delete opts.exclude
const descriptors = new WeakMap()
function compileTemplate(id, { functional }, source) {
const { template } = compiler.compileToDescriptor(
id.filename,
`<template ${functional ? 'functional' : ''}>\n${source}\n</template>`
)
if (template.errors && template.errors.length) {
console.error(
'> Errors: ' +
relative(process.cwd(), id.filename) +
'\n' +
template.errors.map(it => ' - ' + it).join('\n')
)
}
if (template.tips && template.tips.length) {
console.log(
'> Tips: ' +
relative(process.cwd(), id.filename) +
'\n' +
template.tips.map(it => ' - ' + it).join('\n')
)
}
return `${template.code}\n export { render, staticRenderFns }`
}
return {
name: 'vue.delegate',
resolveId(id) {
if (isVuePartRequest(id)) {
const ref = parseVuePartRequest(id)
const element = resolveVuePart(descriptors, ref)
if (element.src)
return path.resolve(path.dirname(ref.filename), element.src)
return id
}
},
load(id) {
if (!isVuePartRequest(id)) return
id = parseVuePartRequest(id)
return resolveVuePart(descriptors, id).content
},
async transform(source, filename) {
if (isVue(filename)) {
const descriptor = (descriptors[filename] = parse({
filename,
source,
needMap: true
}))
const scopeId =
'data-v-' +
(isProduction
? hash(path.basename(filename) + source)
: hash(filename + source))
const input = {
scopeId,
styles: [],
customBlocks: []
}
if (descriptor.template) {
input.template = {
code: `
import * as template from '${createVuePartRequest(
filename,
descriptor.template.lang,
'template'
)}'
var render = template.render
var staticRenderFns = template.staticRenderFns
`,
functional: 'functional' in descriptor.template.attrs
}
}
if (descriptor.script) {
input.script = {
code: `
export * from '${createVuePartRequest(
filename,
descriptor.script.lang,
'script'
)}'
import script from '${createVuePartRequest(
filename,
descriptor.script.lang,
'script'
)}'
export default script
`
}
}
const result = assemble(compiler, filename, input, opts)
descriptor.customBlocks.forEach((block, index) => {
if (blacklisted.has(block.type)) return
result.code +=
'\n' +
`export * from '${createVuePartRequest(
filename,
block.attrs.lang ||
createVuePartRequest.defaultLang[block.type] ||
block.type,
'customBlocks',
index
)}'`
})
return result
}
if (isVuePartRequest(filename)) {
const id = parseVuePartRequest(filename)
const element = resolveVuePart(descriptors, id)
if (id.meta.type === 'styles') {
const { styles } = compiler.compileToDescriptor(
id.filename,
`<style ${element.scoped ? 'scoped' : ''} ${
element.module
? 'module' +
(typeof element.module === 'string'
? '="' + element.module + '"'
: '')
: ''
}>\n${source}\n</style>`
)
return styles[0]
} else if (id.meta.type === 'template') {
return compileTemplate(id, element, source)
}
}
}
}
}