Skip to content
This repository was archived by the owner on Aug 16, 2022. It is now read-only.
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 7024fb8

Browse files
committedJan 23, 2018
refactor: sync template-compiler urlToRequire
1 parent 948ef13 commit 7024fb8

File tree

3 files changed

+22
-26
lines changed

3 files changed

+22
-26
lines changed
 

‎src/template-compiler/modules/transform-require.js

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
// vue compiler module for transforming `<tag>:<attribute>` to `require`
2+
3+
const urlToRequire = require('../url-to-require')
4+
25
const defaultOptions = {
36
video: ['src', 'poster'],
47
source: 'src',
@@ -33,18 +36,11 @@ function transform (node, options) {
3336

3437
function rewrite (attr, name) {
3538
if (attr.name === name) {
36-
let value = attr.value
37-
const isStatic = value.charAt(0) === '"' && value.charAt(value.length - 1) === '"'
38-
if (!isStatic) {
39-
return
40-
}
41-
const firstChar = value.charAt(1)
42-
if (firstChar === '.' || firstChar === '~') {
43-
if (firstChar === '~') {
44-
value = '"' + value.slice(2)
45-
}
46-
attr.value = `require(${value})`
39+
const value = attr.value
40+
// only transform static URLs
41+
if (value.charAt(0) === '"' && value.charAt(value.length - 1) === '"') {
42+
attr.value = urlToRequire(value.slice(1, -1))
43+
return true
4744
}
48-
return true
4945
}
5046
}

‎src/template-compiler/modules/transform-srcset.js

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// vue compiler module for transforming `img:srcset` to a number of `require`s
22

3+
const urlToRequire = require('../url-to-require')
4+
35
module.exports = {
46
postTransformNode (node) {
57
const tags = ['img', 'source']
@@ -39,17 +41,3 @@ module.exports = {
3941
}
4042
}
4143
}
42-
43-
function urlToRequire (url) {
44-
// same logic as in transform-require.js
45-
const firstChar = url.charAt(0)
46-
if (firstChar === '.' || firstChar === '~') {
47-
if (firstChar === '~') {
48-
const secondChar = url.charAt(1)
49-
url = url.slice(secondChar === '/' ? 2 : 1)
50-
}
51-
return `require("${url}")`
52-
} else {
53-
return `"${url}"`
54-
}
55-
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module.exports = function urlToRequire (url) {
2+
const firstChar = url.charAt(0)
3+
if (firstChar === '.' || firstChar === '~' || firstChar === '@') {
4+
if (firstChar === '~') {
5+
const secondChar = url.charAt(1)
6+
url = url.slice(secondChar === '/' ? 2 : 1)
7+
}
8+
return `require("${url}")`
9+
} else {
10+
return `"${url}"`
11+
}
12+
}

0 commit comments

Comments
 (0)