|
1 | 1 | // vue compiler module for transforming `<tag>:<attribute>` to `require`
|
2 | 2 |
|
3 |
| -var fs = require('fs') |
4 |
| -var path = require('path') |
5 |
| -var mkdirp = require('mkdirp') |
6 |
| -var mime = require('mime') |
7 |
| - |
8 | 3 | var defaultOptions = {
|
9 | 4 | img: 'src',
|
10 |
| - image: 'xlink:href', |
11 |
| - limit: 10 * 1024 |
| 5 | + image: 'xlink:href' |
12 | 6 | }
|
13 | 7 |
|
14 |
| -module.exports = (userOptions, fileOptions) => { |
| 8 | +module.exports = userOptions => { |
15 | 9 | var options = userOptions
|
16 | 10 | ? Object.assign({}, defaultOptions, userOptions)
|
17 | 11 | : defaultOptions
|
18 | 12 |
|
19 | 13 | return {
|
20 | 14 | postTransformNode: node => {
|
21 |
| - transform(node, options, fileOptions) |
| 15 | + transform(node, options) |
22 | 16 | }
|
23 | 17 | }
|
24 | 18 | }
|
25 | 19 |
|
26 |
| -function transform (node, options, fileOptions) { |
| 20 | +function transform (node, options) { |
27 | 21 | for (var tag in options) {
|
28 | 22 | if (node.tag === tag && node.attrs) {
|
29 | 23 | var attributes = options[tag]
|
30 | 24 | if (typeof attributes === 'string') {
|
31 |
| - rewrite(node.attrsMap, attributes, fileOptions, options.limit) |
| 25 | + node.attrs.some(attr => rewrite(attr, attributes)) |
32 | 26 | } 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))) |
34 | 28 | }
|
35 | 29 | }
|
36 | 30 | }
|
37 | 31 | }
|
38 | 32 |
|
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 |
56 | 39 | }
|
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})` |
65 | 47 | }
|
66 |
| - } catch (err) { |
67 |
| - console.error('ReadFile Error:' + err) |
| 48 | + return true |
68 | 49 | }
|
69 | 50 | }
|
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 |
| -} |
0 commit comments