-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathtransform-require.js
62 lines (55 loc) · 1.96 KB
/
transform-require.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
// vue compiler module for transforming `<tag>:<attribute>` to `require`
var fs = require('fs')
var path = require('path')
var mkdirp = require('mkdirp')
var defaultOptions = {
img: 'src',
image: 'xlink:href'
}
module.exports = (userOptions, fileOptions, emitError) => {
var options = userOptions
? Object.assign({}, defaultOptions, userOptions)
: defaultOptions
return {
postTransformNode: node => {
transform(node, options, fileOptions, emitError)
}
}
}
function transform (node, options, fileOptions, emitError) {
for (var tag in options) {
if (node.tag === tag && node.attrs) {
var attributes = options[tag]
if (typeof attributes === 'string') {
rewrite(node.attrsMap, attributes, fileOptions, emitError)
} else if (Array.isArray(attributes)) {
attributes.forEach(item => rewrite(node.attrsMap, item, fileOptions, emitError))
}
}
}
}
function rewrite (attrsMap, name, fileOptions, emitError) {
var value = attrsMap[name]
if (value) {
var firstChar = value.charAt(0)
if (firstChar === '.' || firstChar === '~') {
// 资源路径
var assetPath = firstChar === '.'
? path.resolve(path.dirname(fileOptions.resourcePath), value)
: path.resolve(process.cwd(), 'node_modules', value.slice(1))
// 重写路径,为了避免重名,在webpack输出目录下新建copy-asset目录,资源保存到这里
var assetOutputPath = path.join('copy-asset', path.relative(process.cwd(), assetPath).replace(/^src/, ''))
attrsMap[name] = `/${assetOutputPath.split(path.sep).join('/')}`
copyAsset(assetPath, path.resolve(fileOptions.outputPath, assetOutputPath), emitError)
}
}
}
function copyAsset (from, to, emitError) {
var readStream = fs.createReadStream(from)
readStream.on('error', emitError)
mkdirp(path.dirname(to), err => {
if (err) emitError(err)
var writeStream = fs.createWriteStream(to)
readStream.pipe(writeStream)
})
}