|
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 | + node.attrs.some(attr => rewrite(attr, 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 => node.attrs.some(attr => rewrite(attr, node.attrsMap, item, fileOptions))) |
28 | 32 | }
|
29 | 33 | }
|
30 | 34 | }
|
31 | 35 | }
|
32 | 36 |
|
33 |
| -function rewrite (attr, name) { |
| 37 | +function rewrite (attr, attrsMap, name, fileOptions) { |
34 | 38 | if (attr.name === name) {
|
35 | 39 | var value = attr.value
|
36 | 40 | var isStatic = value.charAt(0) === '"' && value.charAt(value.length - 1) === '"'
|
37 | 41 | if (!isStatic) {
|
38 | 42 | return
|
39 | 43 | }
|
40 | 44 | 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})` |
| 45 | + if (firstChar === '.') { |
| 46 | + // 资源路径 |
| 47 | + var assetPath = path.join(path.dirname(fileOptions.resourcePath), value.slice(1, -1)) |
| 48 | + // 重写路径,为了避免重名,在webpack输出目录下新建copy-asset目录,资源保存到这里 |
| 49 | + var assetOutputPath = path.join('copy-asset', path.relative(process.cwd(), assetPath).replace(/^src/, '')) |
| 50 | + attrsMap[name] = `/${assetOutputPath}` |
| 51 | + // 拷贝资源 |
| 52 | + copyAsset(assetPath, path.resolve(fileOptions.outputPath, assetOutputPath)) |
47 | 53 | }
|
48 | 54 | return true
|
49 | 55 | }
|
50 | 56 | }
|
| 57 | + |
| 58 | +function copyAsset (from, to) { |
| 59 | + var readStream = fs.createReadStream(from) |
| 60 | + mkdirp(path.dirname(to), err => { |
| 61 | + if (err) console.error(err) |
| 62 | + var writeStream = fs.createWriteStream(to) |
| 63 | + readStream.pipe(writeStream) |
| 64 | + }) |
| 65 | +} |
0 commit comments