Skip to content

Commit 275c32d

Browse files
authored
Merge pull request #18 from mpvue/revert-16-fix-relative-asset
Revert "修复无法引入相对路径资源的bug"
2 parents 4cc294f + aeedf75 commit 275c32d

File tree

3 files changed

+25
-57
lines changed

3 files changed

+25
-57
lines changed

lib/template-compiler/index.js

+1-4
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,7 @@ module.exports = function (html) {
1616
var vueOptions = this.options.__vueOptions__ || {}
1717
var options = loaderUtils.getOptions(this) || {}
1818

19-
var defaultModules = [transformRequire(options.transformToRequire, {
20-
outputPath: this.options.output.path,
21-
resourcePath: this.resourcePath
22-
})]
19+
var defaultModules = [transformRequire(options.transformToRequire)]
2320
var userModules = vueOptions.compilerModules || options.compilerModules
2421
// for HappyPack cross-process use cases
2522
if (typeof userModules === 'string') {
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,50 @@
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-
83
var defaultOptions = {
94
img: 'src',
10-
image: 'xlink:href',
11-
limit: 10 * 1024
5+
image: 'xlink:href'
126
}
137

14-
module.exports = (userOptions, fileOptions) => {
8+
module.exports = userOptions => {
159
var options = userOptions
1610
? Object.assign({}, defaultOptions, userOptions)
1711
: defaultOptions
1812

1913
return {
2014
postTransformNode: node => {
21-
transform(node, options, fileOptions)
15+
transform(node, options)
2216
}
2317
}
2418
}
2519

26-
function transform (node, options, fileOptions) {
20+
function transform (node, options) {
2721
for (var tag in options) {
2822
if (node.tag === tag && node.attrs) {
2923
var attributes = options[tag]
3024
if (typeof attributes === 'string') {
31-
rewrite(node.attrsMap, attributes, fileOptions, options.limit)
25+
node.attrs.some(attr => rewrite(attr, attributes))
3226
} else if (Array.isArray(attributes)) {
33-
attributes.forEach(item => rewrite(node.attrsMap, item, fileOptions, options.limit))
27+
attributes.forEach(item => node.attrs.some(attr => rewrite(attr, item)))
3428
}
3529
}
3630
}
3731
}
3832

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))
55-
}
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
5639
}
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')
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)
45+
}
46+
attr.value = `require(${value})`
6547
}
66-
} catch (err) {
67-
console.error('ReadFile Error:' + err)
48+
return true
6849
}
6950
}
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

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "mpvue-loader",
3-
"version": "1.0.13",
3+
"version": "1.0.11",
44
"description": "mpvue single-file component loader for Webpack",
55
"main": "index.js",
66
"repository": {
@@ -56,8 +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",
60-
"mkdirp": "^0.5.1",
6159
"postcss": "^6.0.6",
6260
"postcss-load-config": "^1.1.0",
6361
"postcss-selector-parser": "^2.0.0",
@@ -70,7 +68,7 @@
7068
},
7169
"peerDependencies": {
7270
"css-loader": "*",
73-
"mpvue-template-compiler": "^1.0.10"
71+
"mpvue-template-compiler": "^1.0.7"
7472
},
7573
"devDependencies": {
7674
"babel-core": "^6.25.0",
@@ -92,8 +90,9 @@
9290
"lint-staged": "^4.0.2",
9391
"marked": "^0.3.6",
9492
"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.10",
9796
"node-libs-browser": "^2.0.0",
9897
"normalize-newline": "^3.0.0",
9998
"null-loader": "^0.1.1",

0 commit comments

Comments
 (0)