Skip to content

Commit 3fae95c

Browse files
author
Edward Faulkne
committed
handle customized rootURL
1 parent 3fd5bc9 commit 3fae95c

File tree

4 files changed

+73
-21
lines changed

4 files changed

+73
-21
lines changed

src/fastboot-schema.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ function loadConfig(distPath) {
7979
({ appName, config, html, scripts } = loadManifest(distPath, pkg.fastboot, schemaVersion));
8080
} else {
8181
appName = pkg.name;
82-
({ config, html, scripts } = htmlEntrypoint(distPath, pkg.fastboot.htmlEntrypoint));
82+
({ config, html, scripts } = htmlEntrypoint(appName, distPath, pkg.fastboot.htmlEntrypoint));
8383
}
8484

8585
let sandboxRequire = buildWhitelistedRequire(

src/html-entrypoint.js

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const { JSDOM } = require('jsdom');
44
const fs = require('fs');
55
const path = require('path');
66

7-
function htmlEntrypoint(distPath, htmlPath) {
7+
function htmlEntrypoint(appName, distPath, htmlPath) {
88
let html = fs.readFileSync(path.join(distPath, htmlPath), 'utf8');
99
let dom = new JSDOM(html);
1010
let scripts = [];
@@ -19,11 +19,15 @@ function htmlEntrypoint(distPath, htmlPath) {
1919
}
2020
}
2121

22+
let rootURL = getRootURL(appName, config);
23+
2224
for (let element of dom.window.document.querySelectorAll('script,fastboot-script')) {
2325
let src = extractSrc(element);
24-
let ignored = extractIgnore(element);
25-
if (!ignored && isRelativeURL(src)) {
26-
scripts.push(path.join(distPath, src));
26+
if (!extractIgnore(element)) {
27+
let relativeSrc = urlWithin(src, rootURL);
28+
if (relativeSrc) {
29+
scripts.push(path.join(distPath, relativeSrc));
30+
}
2731
}
2832
if (element.tagName === 'FASTBOOT-SCRIPT') {
2933
removeWithWhitespaceTrim(element);
@@ -51,10 +55,20 @@ function extractIgnore(element) {
5155
return false;
5256
}
5357

54-
function isRelativeURL(url) {
55-
return (
56-
url && new URL(url, 'http://_the_current_origin_').origin === 'http://_the_current_origin_'
57-
);
58+
function getRootURL(appName, config) {
59+
let rootURL = (config[appName] && config[appName].rootURL) || '/';
60+
if (!rootURL.endsWith('/')) {
61+
rootURL = rootURL + '/';
62+
}
63+
return rootURL;
64+
}
65+
66+
function urlWithin(candidate, root) {
67+
let candidateURL = new URL(candidate, 'http://_the_current_origin_');
68+
let rootURL = new URL(root, 'http://_the_current_origin_');
69+
if (candidateURL.href.startsWith(rootURL.href)) {
70+
return candidateURL.href.slice(rootURL.href.length);
71+
}
5872
}
5973

6074
// removes an element, and if that element was on a line by itself with nothing

test/html-entrypoint-test.js

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ describe('htmlEntrypoint', function() {
3838

3939
fixturify.writeSync(tmpLocation, project);
4040

41-
let { html, scripts } = htmlEntrypoint(tmpLocation, 'index.html');
41+
let { html, scripts } = htmlEntrypoint('my-app', tmpLocation, 'index.html');
4242

4343
expect(html).to.be.equalHTML(project['index.html']);
4444
expect(scripts).to.deep.equal([]);
@@ -61,7 +61,7 @@ describe('htmlEntrypoint', function() {
6161

6262
fixturify.writeSync(tmpLocation, project);
6363

64-
let { html, scripts } = htmlEntrypoint(tmpLocation, 'index.html');
64+
let { html, scripts } = htmlEntrypoint('my-app', tmpLocation, 'index.html');
6565

6666
expect(html).to.be.equalHTML(project['index.html']);
6767
expect(scripts).to.deep.equal([
@@ -88,7 +88,7 @@ describe('htmlEntrypoint', function() {
8888

8989
fixturify.writeSync(tmpLocation, project);
9090

91-
let { html, scripts } = htmlEntrypoint(tmpLocation, 'index.html');
91+
let { html, scripts } = htmlEntrypoint('my-app', tmpLocation, 'index.html');
9292

9393
expect(html).to.be.equalHTML(`
9494
<html>
@@ -119,7 +119,7 @@ describe('htmlEntrypoint', function() {
119119

120120
fixturify.writeSync(tmpLocation, project);
121121

122-
let { html } = htmlEntrypoint(tmpLocation, 'index.html');
122+
let { html } = htmlEntrypoint('my-app', tmpLocation, 'index.html');
123123

124124
expect(html).to.be.equalHTML(`
125125
<html>
@@ -148,7 +148,7 @@ describe('htmlEntrypoint', function() {
148148

149149
fixturify.writeSync(tmpLocation, project);
150150

151-
let { html, scripts } = htmlEntrypoint(tmpLocation, 'index.html');
151+
let { html, scripts } = htmlEntrypoint('my-app', tmpLocation, 'index.html');
152152

153153
expect(html).to.be.equalHTML(`
154154
<html>
@@ -178,7 +178,7 @@ describe('htmlEntrypoint', function() {
178178

179179
fixturify.writeSync(tmpLocation, project);
180180

181-
let { html, scripts } = htmlEntrypoint(tmpLocation, 'index.html');
181+
let { html, scripts } = htmlEntrypoint('my-app', tmpLocation, 'index.html');
182182

183183
expect(html).to.be.equalHTML(`
184184
<html>
@@ -207,7 +207,7 @@ describe('htmlEntrypoint', function() {
207207

208208
fixturify.writeSync(tmpLocation, project);
209209

210-
let { html, scripts } = htmlEntrypoint(tmpLocation, 'index.html');
210+
let { html, scripts } = htmlEntrypoint('my-app', tmpLocation, 'index.html');
211211

212212
expect(html).to.be.equalHTML(`
213213
<html>
@@ -218,4 +218,47 @@ describe('htmlEntrypoint', function() {
218218
`);
219219
expect(scripts).to.deep.equal([]);
220220
});
221+
222+
it('extracts configs from meta', function() {
223+
let tmpobj = tmp.dirSync();
224+
let tmpLocation = tmpobj.name;
225+
226+
let project = {
227+
'index.html': `
228+
<html>
229+
<meta name="my-app/config/environment" content="%7B%22rootURL%22%3A%22%2Fcustom-root-url%2F%22%7D" >
230+
<body>
231+
<script src="/custom-root-url/bar.js"></script>
232+
</body>
233+
</html>
234+
`,
235+
};
236+
237+
fixturify.writeSync(tmpLocation, project);
238+
let { config } = htmlEntrypoint('my-app', tmpLocation, 'index.html');
239+
expect(config).to.deep.equal({
240+
'my-app': { APP: { autoboot: false }, rootURL: '/custom-root-url/' },
241+
});
242+
});
243+
244+
it('understands customized rootURL', function() {
245+
let tmpobj = tmp.dirSync();
246+
let tmpLocation = tmpobj.name;
247+
248+
let project = {
249+
'index.html': `
250+
<html>
251+
<meta name="my-app/config/environment" content="%7B%22rootURL%22%3A%22%2Fcustom-root-url%2F%22%7D" >
252+
<body>
253+
<script src="/custom-root-url/bar.js"></script>
254+
</body>
255+
</html>
256+
`,
257+
};
258+
259+
fixturify.writeSync(tmpLocation, project);
260+
261+
let { scripts } = htmlEntrypoint('my-app', tmpLocation, 'index.html');
262+
expect(scripts).to.deep.equal([`${tmpLocation}/bar.js`]);
263+
});
221264
});

yarn.lock

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4235,11 +4235,6 @@ simple-dom@^1.4.0:
42354235
"@simple-dom/serializer" "^1.4.0"
42364236
"@simple-dom/void-map" "^1.4.0"
42374237

4238-
simple-html-tokenizer@^0.5.9:
4239-
version "0.5.9"
4240-
resolved "https://registry.yarnpkg.com/simple-html-tokenizer/-/simple-html-tokenizer-0.5.9.tgz#1a83fe97f5a3e39b335fddf71cfe9b0263b581c2"
4241-
integrity sha512-w/3FEDN94r4JQ9WoYrIr8RqDIPZdyNkdpbK9glFady1CAEyD97XWCv8HFetQO21w81e7h7Nh59iYTyG1mUJftg==
4242-
42434238
slash@^3.0.0:
42444239
version "3.0.0"
42454240
resolved "https://registry.yarnpkg.com/slash/-/slash-3.0.0.tgz#6539be870c165adbd5240220dbe361f1bc4d4634"

0 commit comments

Comments
 (0)