Skip to content

Commit 9409b86

Browse files
author
linrui
committed
添加对base64的支持
1 parent 033f7d4 commit 9409b86

File tree

2 files changed

+34
-20
lines changed

2 files changed

+34
-20
lines changed

lib/template-compiler/modules/transform-require.js

+31-18
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
var fs = require('fs')
44
var path = require('path')
55
var mkdirp = require('mkdirp')
6+
var mime = require('mime')
67

78
var defaultOptions = {
89
img: 'src',
9-
image: 'xlink:href'
10+
image: 'xlink:href',
11+
limit: 10 * 1024
1012
}
1113

1214
module.exports = (userOptions, fileOptions) => {
@@ -26,32 +28,43 @@ function transform (node, options, fileOptions) {
2628
if (node.tag === tag && node.attrs) {
2729
var attributes = options[tag]
2830
if (typeof attributes === 'string') {
29-
node.attrs.some(attr => rewrite(attr, node.attrsMap, attributes, fileOptions))
31+
rewrite(node.attrsMap, attributes, fileOptions, options.limit)
3032
} else if (Array.isArray(attributes)) {
31-
attributes.forEach(item => node.attrs.some(attr => rewrite(attr, node.attrsMap, item, fileOptions)))
33+
attributes.forEach(item => rewrite(node.attrsMap, item, fileOptions, options.limit))
3234
}
3335
}
3436
}
3537
}
3638

37-
function rewrite (attr, attrsMap, name, fileOptions) {
38-
if (attr.name === name) {
39-
var value = attr.value
40-
var isStatic = value.charAt(0) === '"' && value.charAt(value.length - 1) === '"'
41-
if (!isStatic) {
42-
return
43-
}
44-
var firstChar = value.charAt(1)
39+
function rewrite (attrsMap, name, fileOptions, limit) {
40+
var value = attrsMap[name]
41+
if (value) {
42+
var firstChar = value.charAt(0)
4543
if (firstChar === '.') {
4644
// 资源路径
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))
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))
55+
}
56+
}
57+
}
58+
}
59+
60+
function assetToBase64 (assetPath, limit) {
61+
try {
62+
var buffer = fs.readFileSync(assetPath)
63+
if (buffer.length <= limit) {
64+
return buffer.toString('base64')
5365
}
54-
return true
66+
} catch (err) {
67+
console.error('ReadFile Error:' + err)
5568
}
5669
}
5770

package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -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",
@@ -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.10",
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)