Skip to content

Commit f8cb88b

Browse files
joma74znck
authored andcommitted
feat: Support URI fragment in transformed require (#22)
* feat: Support uri fragment in transformed require * chore(doc): add reference to vue-template-loader * chore: issue number corrected to #22 plus minor spelling correction * chore(refactor): make team advised code improvements * feat: Support uri fragment in transformed require * chore: issue number corrected to #22 plus minor spelling correction * chore(refactor): make team advised code improvements * feat: Support uri fragment in transformed require * feat: Support uri fragment in transformed require * chore: issue number corrected to #22 plus minor spelling correction * chore: issue number corrected to #22 plus minor spelling correction * chore(refactor): make team advised code improvements * chore(refactor): make team advised code improvements * chore(linting): applied linting * chore(linting): applied linting * fix: reverted README to upstream version * fix: 2nd try at revert README to upstream version * fix: do uriParse only if sane param
1 parent 22965c4 commit f8cb88b

File tree

2 files changed

+76
-3
lines changed

2 files changed

+76
-3
lines changed

lib/templateCompilerModules/utils.ts

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,47 @@ export interface ASTNode {
88
attrs: Attr[]
99
}
1010

11+
import { UrlWithStringQuery, parse as uriParse } from 'url'
12+
1113
export function urlToRequire(url: string): string {
14+
const returnValue = `"${url}"`
1215
// same logic as in transform-require.js
1316
const firstChar = url.charAt(0)
1417
if (firstChar === '.' || firstChar === '~' || firstChar === '@') {
1518
if (firstChar === '~') {
1619
const secondChar = url.charAt(1)
1720
url = url.slice(secondChar === '/' ? 2 : 1)
1821
}
19-
return `require("${url}")`
20-
} else {
21-
return `"${url}"`
22+
23+
const uriParts = parseUriParts(url)
24+
25+
if (!uriParts.hash) {
26+
return `require("${url}")`
27+
} else {
28+
// support uri fragment case by excluding it from
29+
// the require and instead appending it as string;
30+
// assuming that the path part is sufficient according to
31+
// the above caseing(t.i. no protocol-auth-host parts expected)
32+
return `require("${uriParts.path}") + "${uriParts.hash}"`
33+
}
34+
}
35+
return returnValue
36+
}
37+
38+
/**
39+
* vuejs/component-compiler-utils#22 Support uri fragment in transformed require
40+
* @param urlString an url as a string
41+
*/
42+
function parseUriParts(urlString: string): UrlWithStringQuery {
43+
// initialize return value
44+
const returnValue: UrlWithStringQuery = uriParse('')
45+
if (urlString) {
46+
// A TypeError is thrown if urlString is not a string
47+
// @see https://nodejs.org/api/url.html#url_url_parse_urlstring_parsequerystring_slashesdenotehost
48+
if ('string' === typeof urlString) {
49+
// check is an uri
50+
return uriParse(urlString) // take apart the uri
51+
}
2252
}
53+
return returnValue
2354
}

test/compileTemplate.spec.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,48 @@ test('preprocess pug', () => {
5959
expect(result.errors.length).toBe(0)
6060
})
6161

62+
/**
63+
* vuejs/component-compiler-utils#22 Support uri fragment in transformed require
64+
*/
65+
test('supports uri fragment in transformed require', () => {
66+
const source = //
67+
'<svg>\
68+
<use href="~@svg/file.svg#fragment"></use>\
69+
</svg>'
70+
const result = compileTemplate({
71+
filename: 'svgparticle.html',
72+
source: source,
73+
transformAssetUrls: {
74+
use: 'href'
75+
},
76+
compiler: compiler
77+
})
78+
expect(result.errors.length).toBe(0)
79+
expect(result.code).toMatch(
80+
/href: require\("@svg\/file.svg"\) \+ "#fragment"/
81+
)
82+
})
83+
84+
/**
85+
* vuejs/component-compiler-utils#22 Support uri fragment in transformed require
86+
*/
87+
test('when too short uri then empty require', () => {
88+
const source = //
89+
'<svg>\
90+
<use href="~"></use>\
91+
</svg>'
92+
const result = compileTemplate({
93+
filename: 'svgparticle.html',
94+
source: source,
95+
transformAssetUrls: {
96+
use: 'href'
97+
},
98+
compiler: compiler
99+
})
100+
expect(result.errors.length).toBe(0)
101+
expect(result.code).toMatch(/href: require\(""\)/)
102+
})
103+
62104
test('warn missing preprocessor', () => {
63105
const template = parse({
64106
source: '<template lang="unknownLang">\n' + '</template>\n',

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy