forked from vuejs/rollup-plugin-vue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvueTransform.js
193 lines (156 loc) · 5.79 KB
/
vueTransform.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import deIndent from 'de-indent'
import htmlMinifier from 'html-minifier'
import parse5 from 'parse5'
import validateTemplate from 'vue-template-validator'
import transpileVueTemplate from 'vue-template-es2015-compiler'
import { relative } from 'path'
import MagicString from 'magic-string'
import debug from './debug'
function getNodeAttrs (node) {
if (node.attrs) {
const attributes = {}
for (const attr of node.attrs) {
attributes[attr.name] = attr.value
}
return attributes
}
return {}
}
/**
* Pad content with empty lines to get correct line number in errors.
*/
function padContent (content) {
return content
.split(/\r?\n/g)
.map(() => '')
.join('\n')
}
/**
* Wrap code inside a with statement inside a function
* This is necessary for Vue 2 template compilation
*/
function wrapRenderFunction (code) {
return `function(){${code}}`
}
function injectRender (script, render, lang, options) {
if (['js', 'babel'].indexOf(lang.toLowerCase()) > -1) {
const matches = /(export default[^{]*\{)/g.exec(script)
if (matches) {
let renderScript = 'module.exports={' +
`render: ${wrapRenderFunction(render.render)},` +
'staticRenderFns: [' +
`${render.staticRenderFns.map(wrapRenderFunction).join(',')}],}`
if (options.stripWith !== false) {
renderScript = transpileVueTemplate(renderScript, options.vue)
}
const result = script.split(matches[1])
.join(renderScript.replace('module.exports={', 'export default {').replace(/\}$/, ''))
return result
}
debug(`No injection location found in: \n${script}\n`)
} else if (options.inject) {
return options.inject(script, render, lang, options)
}
throw new Error('[rollup-plugin-vue] could not find place to inject template in script.')
}
/**
* @param script
* @param template
* @param lang
* @returns {string}
*/
function injectTemplate (script, template, lang, options) {
if (template === undefined) return script
if (['js', 'babel'].indexOf(lang.toLowerCase()) > -1) {
const matches = /(export default[^{]*\{)/g.exec(script)
if (matches) {
return script.split(matches[1])
.join(`${matches[1]} template: ${JSON.stringify(template)},`)
}
debug(`No injection location found in: \n${script}\n`)
} else if (options.inject) {
return options.inject(script, template, lang, options)
}
throw new Error('[rollup-plugin-vue] could not find place to inject template in script.')
}
/**
* Compile template: DeIndent and minify html.
*/
function processTemplate (source, id, content, options) {
if (source === undefined) return undefined
const {node, code} = source
const warnings = validateTemplate(code, content)
if (warnings) {
const relativePath = relative(process.cwd(), id)
warnings.forEach((msg) => {
console.warn(`\n Warning in ${relativePath}:\n ${msg}`)
})
}
/* eslint-disable no-underscore-dangle */
const start = node.content.childNodes[0].__location.startOffset
const end = node.content.childNodes[node.content.childNodes.length - 1].__location.endOffset
const template = deIndent(content.slice(start, end))
/* eslint-enable no-underscore-dangle */
return htmlMinifier.minify(template, options.htmlMinifier)
}
function processScript (source, id, content, options, nodes) {
const template = processTemplate(nodes.template[0], id, content, options, nodes)
const lang = source.attrs.lang || 'js'
const script = deIndent(padContent(content.slice(0, content.indexOf(source.code))) + source.code)
const map = (new MagicString(script)).generateMap({ hires: true })
if (template && options.compileTemplate) {
const render = require('vue-template-compiler').compile(template)
return { map, code: injectRender(script, render, lang, options) }
} else {
return { map, code: injectTemplate(script, template, lang, options) }
}
}
function processStyle (styles, id) {
return styles.map(style => ({
id,
code: deIndent(style.code).trim(),
lang: style.attrs.lang || 'css'
}))
}
function parseTemplate (code) {
const fragment = parse5.parseFragment(code, { locationInfo: true })
const nodes = {
template: [],
script: [],
style: []
}
for (let i = fragment.childNodes.length - 1; i >= 0; i -= 1) {
const name = fragment.childNodes[i].nodeName
if (!(name in nodes)) {
nodes[name] = []
}
nodes[name].push({
node: fragment.childNodes[i],
code: parse5.serialize(fragment.childNodes[i]),
attrs: getNodeAttrs(fragment.childNodes[i])
})
}
if (nodes.script.length === 0) {
nodes.script.push({
node: null,
code: 'export default {\n}',
attrs: {}
})
}
return nodes
}
export default function vueTransform (code, id, options) {
const nodes = parseTemplate(code)
const js = processScript(nodes.script[0], id, code, options, nodes)
const css = processStyle(nodes.style, id, code, options, nodes)
const isProduction = process.env.NODE_ENV === 'production'
const isWithStripped = options.stripWith !== false
if (!isProduction && !isWithStripped) {
js.code = js.code + '\nmodule.exports.render._withStripped = true'
}
if (options.styleToImports === true) {
const style = css.map((s, i) => 'import ' + JSON.stringify(`${id}.${i}.vue.component.${s.lang}`) + ';').join(' ')
return { css, code: style + js.code, map: js.map }
}
return { css, code: js.code, map: js.map }
}