|
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 | + |
3 | 8 | var defaultOptions = {
|
4 | 9 | img: 'src',
|
5 |
| - image: 'xlink:href' |
| 10 | + image: 'xlink:href', |
| 11 | + limit: 10 * 1024 |
6 | 12 | }
|
7 | 13 |
|
8 |
| -module.exports = userOptions => { |
| 14 | +module.exports = (userOptions, fileOptions) => { |
9 | 15 | var options = userOptions
|
10 | 16 | ? Object.assign({}, defaultOptions, userOptions)
|
11 | 17 | : defaultOptions
|
12 | 18 |
|
13 | 19 | return {
|
14 | 20 | postTransformNode: node => {
|
15 |
| - transform(node, options) |
| 21 | + transform(node, options, fileOptions) |
16 | 22 | }
|
17 | 23 | }
|
18 | 24 | }
|
19 | 25 |
|
20 |
| -function transform (node, options) { |
| 26 | +function transform (node, options, fileOptions) { |
21 | 27 | for (var tag in options) {
|
22 |
| - if (node.tag === tag && node.attrs) { |
| 28 | + if (tag !== 'limit' && node.tag === tag && node.attrs) { |
23 | 29 | var attributes = options[tag]
|
24 | 30 | if (typeof attributes === 'string') {
|
25 |
| - node.attrs.some(attr => rewrite(attr, attributes)) |
| 31 | + rewrite(node.attrsMap, attributes, fileOptions, options.limit) |
26 | 32 | } else if (Array.isArray(attributes)) {
|
27 |
| - attributes.forEach(item => node.attrs.some(attr => rewrite(attr, item))) |
| 33 | + attributes.forEach(item => rewrite(node.attrsMap, item, fileOptions, options.limit)) |
28 | 34 | }
|
29 | 35 | }
|
30 | 36 | }
|
31 | 37 | }
|
32 | 38 |
|
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 |
39 |
| - } |
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) |
| 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.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)) |
45 | 55 | }
|
46 |
| - attr.value = `require(${value})` |
47 | 56 | }
|
48 |
| - return true |
49 | 57 | }
|
50 | 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') |
| 65 | + } |
| 66 | + } catch (err) { |
| 67 | + console.error('ReadFile Error:' + err) |
| 68 | + } |
| 69 | +} |
| 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