-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
69 lines (55 loc) · 1.59 KB
/
index.js
File metadata and controls
69 lines (55 loc) · 1.59 KB
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
const through = require('through2')
const path = require('path')
module.exports = function (options) {
options = options || {}
let ext = options.ext || 'js'
if (ext[0] != '.') {
ext = '.' + ext
}
const extract = function (sourceContent, sectionName) {
const sectionPatterns = {
template: /<template>([\w\W]*?)<\/template>/,
script: /<script>[\W]*?export[\W]*?default[\W]*?({[\w\W]*?});[\W]*?<\/script>/
}
const pattern = sectionPatterns[sectionName]
if (!pattern) {
return null
}
const match = sourceContent.match(pattern)
return match
? match[1]
: null
}
const convertFormat = function (file) {
let sourceContent = file.contents.toString()
let pathData = path.parse(file.path)
if (pathData.ext != '.vue') {
return null
}
const scriptContent = extract(sourceContent, 'script')
const templateContent = extract(sourceContent, 'template')
let dest = "window.VueComponents = window.VueComponents || {};\n"
dest += "window.VueComponents['"
dest += pathData.name
dest += "'] = "
dest += scriptContent.trim().replace(/}\s*$/, '').trim()
dest += ',\ntemplate: `'
dest += templateContent.replace(/`/gi, '\\`')
dest += '`\n};'
let newPath = path.join(
file.base,
path.dirname(file.relative),
pathData.name + ext,
)
file.path = newPath
return dest
}
return through.obj(function (file, enc, cb) {
var content = convertFormat(file)
if (content != null) {
file.contents = Buffer.from(content)
}
this.push(file)
cb()
})
}