Skip to content

Commit fdae6ff

Browse files
authored
Merge pull request #16 from linrui1994/fix-relative-asset
修复无法引入相对路径资源的bug
2 parents 9f157b6 + 9409b86 commit fdae6ff

File tree

3 files changed

+57
-25
lines changed

3 files changed

+57
-25
lines changed

lib/template-compiler/index.js

+4-1
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') {
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) {
2228
if (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.join(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}`
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

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "mpvue-loader",
3-
"version": "1.0.11",
3+
"version": "1.0.13",
44
"description": "mpvue single-file component loader for Webpack",
55
"main": "index.js",
66
"repository": {
@@ -55,6 +55,8 @@
5555
"js-beautify": "^1.6.14",
5656
"loader-utils": "^1.1.0",
5757
"lru-cache": "^4.1.1",
58+
"mime": "^2.3.1",
59+
"mkdirp": "^0.5.1",
5860
"postcss": "^6.0.6",
5961
"postcss-load-config": "^1.1.0",
6062
"postcss-selector-parser": "^2.0.0",
@@ -67,7 +69,7 @@
6769
},
6870
"peerDependencies": {
6971
"css-loader": "*",
70-
"mpvue-template-compiler": "^1.0.7"
72+
"mpvue-template-compiler": "^1.0.10"
7173
},
7274
"devDependencies": {
7375
"babel-core": "^6.25.0",
@@ -89,9 +91,8 @@
8991
"lint-staged": "^4.0.2",
9092
"marked": "^0.3.6",
9193
"memory-fs": "^0.4.1",
92-
"mkdirp": "^0.5.1",
93-
"mpvue-template-compiler": "^1.0.7",
9494
"mocha": "^3.4.2",
95+
"mpvue-template-compiler": "^1.0.10",
9596
"node-libs-browser": "^2.0.0",
9697
"normalize-newline": "^3.0.0",
9798
"null-loader": "^0.1.1",

0 commit comments

Comments
 (0)