Skip to content

Commit 066977c

Browse files
author
linrui
committed
修复无法引入相对路径资源的bug
1 parent 56cdadb commit 066977c

File tree

2 files changed

+31
-13
lines changed

2 files changed

+31
-13
lines changed

lib/template-compiler/index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ module.exports = function (html) {
1616
var vueOptions = this.options.__vueOptions__ || {}
1717
var options = loaderUtils.getOptions(this) || {}
1818

19-
var defaultModules = [transformRequire(options.transformToRequire)]
19+
var defaultModules = [transformRequire(options.transformToRequire, {
20+
outputPath: this.options.output.path,
21+
resourcePath: this.resourcePath
22+
})]
2023
var userModules = vueOptions.compilerModules || options.compilerModules
2124
// for HappyPack cross-process use cases
2225
if (typeof userModules === 'string') {
Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,65 @@
11
// vue compiler module for transforming `<tag>:<attribute>` to `require`
22

3+
var fs = require('fs')
4+
var path = require('path')
5+
var mkdirp = require('mkdirp')
6+
37
var defaultOptions = {
48
img: 'src',
59
image: 'xlink:href'
610
}
711

8-
module.exports = userOptions => {
12+
module.exports = (userOptions, fileOptions) => {
913
var options = userOptions
1014
? Object.assign({}, defaultOptions, userOptions)
1115
: defaultOptions
1216

1317
return {
1418
postTransformNode: node => {
15-
transform(node, options)
19+
transform(node, options, fileOptions)
1620
}
1721
}
1822
}
1923

20-
function transform (node, options) {
24+
function transform (node, options, fileOptions) {
2125
for (var tag in options) {
2226
if (node.tag === tag && node.attrs) {
2327
var attributes = options[tag]
2428
if (typeof attributes === 'string') {
25-
node.attrs.some(attr => rewrite(attr, attributes))
29+
node.attrs.some(attr => rewrite(attr, node.attrsMap, attributes, fileOptions))
2630
} else if (Array.isArray(attributes)) {
27-
attributes.forEach(item => node.attrs.some(attr => rewrite(attr, item)))
31+
attributes.forEach(item => node.attrs.some(attr => rewrite(attr, node.attrsMap, item, fileOptions)))
2832
}
2933
}
3034
}
3135
}
3236

33-
function rewrite (attr, name) {
37+
function rewrite (attr, attrsMap, name, fileOptions) {
3438
if (attr.name === name) {
3539
var value = attr.value
3640
var isStatic = value.charAt(0) === '"' && value.charAt(value.length - 1) === '"'
3741
if (!isStatic) {
3842
return
3943
}
4044
var firstChar = value.charAt(1)
41-
if (firstChar === '.' || firstChar === '~') {
42-
if (firstChar === '~') {
43-
var secondChar = value.charAt(2)
44-
value = '"' + value.slice(secondChar === '/' ? 3 : 2)
45-
}
46-
attr.value = `require(${value})`
45+
if (firstChar === '.') {
46+
// 资源路径
47+
var assetPath = path.join(path.dirname(fileOptions.resourcePath), value.slice(1, -1))
48+
// 重写路径,为了避免重名,在webpack输出目录下新建copy-asset目录,资源保存到这里
49+
var assetOutputPath = path.join('copy-asset', path.relative(process.cwd(), assetPath).replace(/^src/, ''))
50+
attrsMap[name] = `/${assetOutputPath}`
51+
// 拷贝资源
52+
copyAsset(assetPath, path.resolve(fileOptions.outputPath, assetOutputPath))
4753
}
4854
return true
4955
}
5056
}
57+
58+
function copyAsset (from, to) {
59+
var readStream = fs.createReadStream(from)
60+
mkdirp(path.dirname(to), err => {
61+
if (err) console.error(err)
62+
var writeStream = fs.createWriteStream(to)
63+
readStream.pipe(writeStream)
64+
})
65+
}

0 commit comments

Comments
 (0)