Skip to content

Commit 6a9f6fe

Browse files
authored
Merge pull request #19 from linrui1994/fix-relative-asset
修复相对路径资源无法正常引入的bug
2 parents 16f5d1e + 841235e commit 6a9f6fe

File tree

3 files changed

+36
-23
lines changed

3 files changed

+36
-23
lines changed

lib/template-compiler/index.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ module.exports = function (html) {
1717
var vueOptions = this.options.__vueOptions__ || {}
1818
var options = loaderUtils.getOptions(this) || {}
1919

20-
var defaultModules = [transformRequire(options.transformToRequire)]
20+
var defaultModules = [transformRequire(options.transformToRequire, {
21+
outputPath: this.options.output.path,
22+
resourcePath: this.resourcePath
23+
})]
24+
2125
var userModules = vueOptions.compilerModules || options.compilerModules
2226
// for HappyPack cross-process use cases
2327
if (typeof userModules === 'string') {
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,59 @@
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+
rewrite(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 => rewrite(node.attrsMap, item, fileOptions))
2832
}
2933
}
3034
}
3135
}
3236

33-
function rewrite (attr, name) {
34-
if (attr.name === name) {
35-
var value = attr.value
36-
var isStatic = value.charAt(0) === '"' && value.charAt(value.length - 1) === '"'
37-
if (!isStatic) {
38-
return
39-
}
40-
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})`
37+
function rewrite (attrsMap, name, fileOptions) {
38+
var value = attrsMap[name]
39+
if (value) {
40+
var firstChar = value.charAt(0)
41+
if (firstChar === '.') {
42+
// 资源路径
43+
var assetPath = path.resolve(path.dirname(fileOptions.resourcePath), value)
44+
// 重写路径,为了避免重名,在webpack输出目录下新建copy-asset目录,资源保存到这里
45+
var assetOutputPath = path.join('copy-asset', path.relative(process.cwd(), assetPath).replace(/^src/, ''))
46+
attrsMap[name] = `/${assetOutputPath.split(path.sep).join('/')}`
47+
copyAsset(assetPath, path.resolve(fileOptions.outputPath, assetOutputPath))
4748
}
48-
return true
4949
}
5050
}
51+
52+
function copyAsset (from, to) {
53+
var readStream = fs.createReadStream(from)
54+
mkdirp(path.dirname(to), err => {
55+
if (err) console.error(err)
56+
var writeStream = fs.createWriteStream(to)
57+
readStream.pipe(writeStream)
58+
})
59+
}

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
"js-beautify": "^1.6.14",
5757
"loader-utils": "^1.1.0",
5858
"lru-cache": "^4.1.1",
59+
"mkdirp": "^0.5.1",
5960
"postcss": "^6.0.6",
6061
"postcss-load-config": "^1.1.0",
6162
"postcss-selector-parser": "^2.0.0",
@@ -90,9 +91,8 @@
9091
"lint-staged": "^4.0.2",
9192
"marked": "^0.3.6",
9293
"memory-fs": "^0.4.1",
93-
"mkdirp": "^0.5.1",
94-
"mpvue-template-compiler": "^1.0.7",
9594
"mocha": "^3.4.2",
95+
"mpvue-template-compiler": "^1.0.7",
9696
"node-libs-browser": "^2.0.0",
9797
"normalize-newline": "^3.0.0",
9898
"null-loader": "^0.1.1",

0 commit comments

Comments
 (0)