Skip to content

Commit 841235e

Browse files
author
linrui
committed
drop base64
1 parent eb58129 commit 841235e

File tree

2 files changed

+9
-29
lines changed

2 files changed

+9
-29
lines changed

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

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

87
var defaultOptions = {
98
img: 'src',
10-
image: 'xlink:href',
11-
limit: 10 * 1024
9+
image: 'xlink:href'
1210
}
1311

1412
module.exports = (userOptions, fileOptions) => {
@@ -25,46 +23,29 @@ module.exports = (userOptions, fileOptions) => {
2523

2624
function transform (node, options, fileOptions) {
2725
for (var tag in options) {
28-
if (tag !== 'limit' && node.tag === tag && node.attrs) {
26+
if (node.tag === tag && node.attrs) {
2927
var attributes = options[tag]
3028
if (typeof attributes === 'string') {
31-
rewrite(node.attrsMap, attributes, fileOptions, options.limit)
29+
rewrite(node.attrsMap, attributes, fileOptions)
3230
} else if (Array.isArray(attributes)) {
33-
attributes.forEach(item => rewrite(node.attrsMap, item, fileOptions, options.limit))
31+
attributes.forEach(item => rewrite(node.attrsMap, item, fileOptions))
3432
}
3533
}
3634
}
3735
}
3836

39-
function rewrite (attrsMap, name, fileOptions, limit) {
37+
function rewrite (attrsMap, name, fileOptions) {
4038
var value = attrsMap[name]
4139
if (value) {
4240
var firstChar = value.charAt(0)
4341
if (firstChar === '.') {
4442
// 资源路径
4543
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))
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')
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))
6548
}
66-
} catch (err) {
67-
console.error('ReadFile Error:' + err)
6849
}
6950
}
7051

package.json

-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@
5656
"js-beautify": "^1.6.14",
5757
"loader-utils": "^1.1.0",
5858
"lru-cache": "^4.1.1",
59-
"mime": "^2.3.1",
6059
"mkdirp": "^0.5.1",
6160
"postcss": "^6.0.6",
6261
"postcss-load-config": "^1.1.0",

0 commit comments

Comments
 (0)