|
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 | + |
3 | 7 | var defaultOptions = {
|
4 | 8 | img: 'src',
|
5 | 9 | image: 'xlink:href'
|
6 | 10 | }
|
7 | 11 |
|
8 |
| -module.exports = userOptions => { |
| 12 | +module.exports = (userOptions, fileOptions) => { |
9 | 13 | var options = userOptions
|
10 | 14 | ? Object.assign({}, defaultOptions, userOptions)
|
11 | 15 | : defaultOptions
|
12 | 16 |
|
13 | 17 | return {
|
14 | 18 | postTransformNode: node => {
|
15 |
| - transform(node, options) |
| 19 | + transform(node, options, fileOptions) |
16 | 20 | }
|
17 | 21 | }
|
18 | 22 | }
|
19 | 23 |
|
20 |
| -function transform (node, options) { |
| 24 | +function transform (node, options, fileOptions) { |
21 | 25 | for (var tag in options) {
|
22 | 26 | if (node.tag === tag && node.attrs) {
|
23 | 27 | var attributes = options[tag]
|
24 | 28 | if (typeof attributes === 'string') {
|
25 |
| - node.attrs.some(attr => rewrite(attr, attributes)) |
| 29 | + rewrite(node.attrsMap, attributes, fileOptions) |
26 | 30 | } else if (Array.isArray(attributes)) {
|
27 |
| - attributes.forEach(item => node.attrs.some(attr => rewrite(attr, item))) |
| 31 | + attributes.forEach(item => rewrite(node.attrsMap, item, fileOptions)) |
28 | 32 | }
|
29 | 33 | }
|
30 | 34 | }
|
31 | 35 | }
|
32 | 36 |
|
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) |
45 |
| - } |
46 |
| - attr.value = `require(${value})` |
| 37 | +function rewrite (attrsMap, name, fileOptions) { |
| 38 | + var value = attrsMap[name] |
| 39 | + if (value) { |
| 40 | + var firstChar = value.charAt(0) |
| 41 | + if (firstChar === '.') { |
| 42 | + // 资源路径 |
| 43 | + var assetPath = path.resolve(path.dirname(fileOptions.resourcePath), value) |
| 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)) |
47 | 48 | }
|
48 |
| - return true |
49 | 49 | }
|
50 | 50 | }
|
| 51 | + |
| 52 | +function copyAsset (from, to) { |
| 53 | + var readStream = fs.createReadStream(from) |
| 54 | + mkdirp(path.dirname(to), err => { |
| 55 | + if (err) console.error(err) |
| 56 | + var writeStream = fs.createWriteStream(to) |
| 57 | + readStream.pipe(writeStream) |
| 58 | + }) |
| 59 | +} |
0 commit comments