Skip to content

Commit eb58129

Browse files
author
linrui
committed
修复相对路径资源无法正常引入的bug
1 parent 275c32d commit eb58129

File tree

3 files changed

+57
-24
lines changed

3 files changed

+57
-24
lines changed

lib/template-compiler/index.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ 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+
})]
23+
2024
var userModules = vueOptions.compilerModules || options.compilerModules
2125
// for HappyPack cross-process use cases
2226
if (typeof userModules === 'string') {
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,78 @@
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+
var mime = require('mime')
7+
38
var defaultOptions = {
49
img: 'src',
5-
image: 'xlink:href'
10+
image: 'xlink:href',
11+
limit: 10 * 1024
612
}
713

8-
module.exports = userOptions => {
14+
module.exports = (userOptions, fileOptions) => {
915
var options = userOptions
1016
? Object.assign({}, defaultOptions, userOptions)
1117
: defaultOptions
1218

1319
return {
1420
postTransformNode: node => {
15-
transform(node, options)
21+
transform(node, options, fileOptions)
1622
}
1723
}
1824
}
1925

20-
function transform (node, options) {
26+
function transform (node, options, fileOptions) {
2127
for (var tag in options) {
22-
if (node.tag === tag && node.attrs) {
28+
if (tag !== 'limit' && node.tag === tag && node.attrs) {
2329
var attributes = options[tag]
2430
if (typeof attributes === 'string') {
25-
node.attrs.some(attr => rewrite(attr, attributes))
31+
rewrite(node.attrsMap, attributes, fileOptions, options.limit)
2632
} else if (Array.isArray(attributes)) {
27-
attributes.forEach(item => node.attrs.some(attr => rewrite(attr, item)))
33+
attributes.forEach(item => rewrite(node.attrsMap, item, fileOptions, options.limit))
2834
}
2935
}
3036
}
3137
}
3238

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)
39+
function rewrite (attrsMap, name, fileOptions, limit) {
40+
var value = attrsMap[name]
41+
if (value) {
42+
var firstChar = value.charAt(0)
43+
if (firstChar === '.') {
44+
// 资源路径
45+
var assetPath = path.resolve(path.dirname(fileOptions.resourcePath), value)
46+
// 小于limit的资源转base64
47+
var str = assetToBase64(assetPath, limit)
48+
if (str) {
49+
attrsMap[name] = `data:${mime.getType(assetPath) || ''};base64,${str}`
50+
} else {
51+
// 重写路径,为了避免重名,在webpack输出目录下新建copy-asset目录,资源保存到这里
52+
var assetOutputPath = path.join('copy-asset', path.relative(process.cwd(), assetPath).replace(/^src/, ''))
53+
attrsMap[name] = `/${assetOutputPath.split(path.sep).join('/')}`
54+
copyAsset(assetPath, path.resolve(fileOptions.outputPath, assetOutputPath))
4555
}
46-
attr.value = `require(${value})`
4756
}
48-
return true
4957
}
5058
}
59+
60+
function assetToBase64 (assetPath, limit) {
61+
try {
62+
var buffer = fs.readFileSync(assetPath)
63+
if (buffer.length <= limit) {
64+
return buffer.toString('base64')
65+
}
66+
} catch (err) {
67+
console.error('ReadFile Error:' + err)
68+
}
69+
}
70+
71+
function copyAsset (from, to) {
72+
var readStream = fs.createReadStream(from)
73+
mkdirp(path.dirname(to), err => {
74+
if (err) console.error(err)
75+
var writeStream = fs.createWriteStream(to)
76+
readStream.pipe(writeStream)
77+
})
78+
}

package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@
5656
"js-beautify": "^1.6.14",
5757
"loader-utils": "^1.1.0",
5858
"lru-cache": "^4.1.1",
59+
"mime": "^2.3.1",
60+
"mkdirp": "^0.5.1",
5961
"postcss": "^6.0.6",
6062
"postcss-load-config": "^1.1.0",
6163
"postcss-selector-parser": "^2.0.0",
@@ -90,9 +92,8 @@
9092
"lint-staged": "^4.0.2",
9193
"marked": "^0.3.6",
9294
"memory-fs": "^0.4.1",
93-
"mkdirp": "^0.5.1",
94-
"mpvue-template-compiler": "^1.0.7",
9595
"mocha": "^3.4.2",
96+
"mpvue-template-compiler": "^1.0.7",
9697
"node-libs-browser": "^2.0.0",
9798
"normalize-newline": "^3.0.0",
9899
"null-loader": "^0.1.1",

0 commit comments

Comments
 (0)