-
Notifications
You must be signed in to change notification settings - Fork 51
修复相对路径资源无法正常引入的bug #19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,78 @@ | ||
// vue compiler module for transforming `<tag>:<attribute>` to `require` | ||
|
||
var fs = require('fs') | ||
var path = require('path') | ||
var mkdirp = require('mkdirp') | ||
var mime = require('mime') | ||
|
||
var defaultOptions = { | ||
img: 'src', | ||
image: 'xlink:href' | ||
image: 'xlink:href', | ||
limit: 10 * 1024 | ||
} | ||
|
||
module.exports = userOptions => { | ||
module.exports = (userOptions, fileOptions) => { | ||
var options = userOptions | ||
? Object.assign({}, defaultOptions, userOptions) | ||
: defaultOptions | ||
|
||
return { | ||
postTransformNode: node => { | ||
transform(node, options) | ||
transform(node, options, fileOptions) | ||
} | ||
} | ||
} | ||
|
||
function transform (node, options) { | ||
function transform (node, options, fileOptions) { | ||
for (var tag in options) { | ||
if (node.tag === tag && node.attrs) { | ||
if (tag !== 'limit' && node.tag === tag && node.attrs) { | ||
var attributes = options[tag] | ||
if (typeof attributes === 'string') { | ||
node.attrs.some(attr => rewrite(attr, attributes)) | ||
rewrite(node.attrsMap, attributes, fileOptions, options.limit) | ||
} else if (Array.isArray(attributes)) { | ||
attributes.forEach(item => node.attrs.some(attr => rewrite(attr, item))) | ||
attributes.forEach(item => rewrite(node.attrsMap, item, fileOptions, options.limit)) | ||
} | ||
} | ||
} | ||
} | ||
|
||
function rewrite (attr, name) { | ||
if (attr.name === name) { | ||
var value = attr.value | ||
var isStatic = value.charAt(0) === '"' && value.charAt(value.length - 1) === '"' | ||
if (!isStatic) { | ||
return | ||
} | ||
var firstChar = value.charAt(1) | ||
if (firstChar === '.' || firstChar === '~') { | ||
if (firstChar === '~') { | ||
var secondChar = value.charAt(2) | ||
value = '"' + value.slice(secondChar === '/' ? 3 : 2) | ||
function rewrite (attrsMap, name, fileOptions, limit) { | ||
var value = attrsMap[name] | ||
if (value) { | ||
var firstChar = value.charAt(0) | ||
if (firstChar === '.') { | ||
// 资源路径 | ||
var assetPath = path.resolve(path.dirname(fileOptions.resourcePath), value) | ||
// 小于limit的资源转base64 | ||
var str = assetToBase64(assetPath, limit) | ||
if (str) { | ||
attrsMap[name] = `data:${mime.getType(assetPath) || ''};base64,${str}` | ||
} else { | ||
// 重写路径,为了避免重名,在webpack输出目录下新建copy-asset目录,资源保存到这里 | ||
var assetOutputPath = path.join('copy-asset', path.relative(process.cwd(), assetPath).replace(/^src/, '')) | ||
attrsMap[name] = `/${assetOutputPath.split(path.sep).join('/')}` | ||
copyAsset(assetPath, path.resolve(fileOptions.outputPath, assetOutputPath)) | ||
} | ||
attr.value = `require(${value})` | ||
} | ||
return true | ||
} | ||
} | ||
|
||
function assetToBase64 (assetPath, limit) { | ||
try { | ||
var buffer = fs.readFileSync(assetPath) | ||
if (buffer.length <= limit) { | ||
return buffer.toString('base64') | ||
} | ||
} catch (err) { | ||
console.error('ReadFile Error:' + err) | ||
} | ||
} | ||
|
||
function copyAsset (from, to) { | ||
var readStream = fs.createReadStream(from) | ||
mkdirp(path.dirname(to), err => { | ||
if (err) console.error(err) | ||
var writeStream = fs.createWriteStream(to) | ||
readStream.pipe(writeStream) | ||
}) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
对啦,在小程序里没有必要转 base64 啊。。。一整个就一个离线包,base64 反而可能影响解析速度
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
主要是背景图还有字体啥的本地只能用 base64 格式的
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
好像是没什么必要,背景图那些这里不会处理到,可以把limit默认设置为0吧
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
所以背景图和字体还是不会被转换吗?有这个需求的人好像还挺多
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
在css中引入的背景图是url-loader处理的,如果只有base64支持,可以把url-loader的配置limit调大
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
背景图我记得好像没问题,字体没试过,理论上应该也可以,不知道为什么之前好多人说这个问题。。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里主要是处理 img 标签这类引用相对资源的话,确实不是很有必要转 base64
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Meituan-Dianping/mpvue#321
Meituan-Dianping/mpvue#392