Skip to content

Commit

Permalink
handle customized rootURL
Browse files Browse the repository at this point in the history
  • Loading branch information
Edward Faulkne committed May 21, 2020
1 parent 3fd5bc9 commit 3fae95c
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/fastboot-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ function loadConfig(distPath) {
({ appName, config, html, scripts } = loadManifest(distPath, pkg.fastboot, schemaVersion));
} else {
appName = pkg.name;
({ config, html, scripts } = htmlEntrypoint(distPath, pkg.fastboot.htmlEntrypoint));
({ config, html, scripts } = htmlEntrypoint(appName, distPath, pkg.fastboot.htmlEntrypoint));
}

let sandboxRequire = buildWhitelistedRequire(
Expand Down
30 changes: 22 additions & 8 deletions src/html-entrypoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { JSDOM } = require('jsdom');
const fs = require('fs');
const path = require('path');

function htmlEntrypoint(distPath, htmlPath) {
function htmlEntrypoint(appName, distPath, htmlPath) {
let html = fs.readFileSync(path.join(distPath, htmlPath), 'utf8');
let dom = new JSDOM(html);
let scripts = [];
Expand All @@ -19,11 +19,15 @@ function htmlEntrypoint(distPath, htmlPath) {
}
}

let rootURL = getRootURL(appName, config);

for (let element of dom.window.document.querySelectorAll('script,fastboot-script')) {
let src = extractSrc(element);
let ignored = extractIgnore(element);
if (!ignored && isRelativeURL(src)) {
scripts.push(path.join(distPath, src));
if (!extractIgnore(element)) {
let relativeSrc = urlWithin(src, rootURL);
if (relativeSrc) {
scripts.push(path.join(distPath, relativeSrc));
}
}
if (element.tagName === 'FASTBOOT-SCRIPT') {
removeWithWhitespaceTrim(element);
Expand Down Expand Up @@ -51,10 +55,20 @@ function extractIgnore(element) {
return false;
}

function isRelativeURL(url) {
return (
url && new URL(url, 'http://_the_current_origin_').origin === 'http://_the_current_origin_'
);
function getRootURL(appName, config) {
let rootURL = (config[appName] && config[appName].rootURL) || '/';
if (!rootURL.endsWith('/')) {
rootURL = rootURL + '/';
}
return rootURL;
}

function urlWithin(candidate, root) {
let candidateURL = new URL(candidate, 'http://_the_current_origin_');
let rootURL = new URL(root, 'http://_the_current_origin_');
if (candidateURL.href.startsWith(rootURL.href)) {
return candidateURL.href.slice(rootURL.href.length);
}
}

// removes an element, and if that element was on a line by itself with nothing
Expand Down
57 changes: 50 additions & 7 deletions test/html-entrypoint-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ describe('htmlEntrypoint', function() {

fixturify.writeSync(tmpLocation, project);

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

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

fixturify.writeSync(tmpLocation, project);

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

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

fixturify.writeSync(tmpLocation, project);

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

expect(html).to.be.equalHTML(`
<html>
Expand Down Expand Up @@ -119,7 +119,7 @@ describe('htmlEntrypoint', function() {

fixturify.writeSync(tmpLocation, project);

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

expect(html).to.be.equalHTML(`
<html>
Expand Down Expand Up @@ -148,7 +148,7 @@ describe('htmlEntrypoint', function() {

fixturify.writeSync(tmpLocation, project);

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

expect(html).to.be.equalHTML(`
<html>
Expand Down Expand Up @@ -178,7 +178,7 @@ describe('htmlEntrypoint', function() {

fixturify.writeSync(tmpLocation, project);

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

expect(html).to.be.equalHTML(`
<html>
Expand Down Expand Up @@ -207,7 +207,7 @@ describe('htmlEntrypoint', function() {

fixturify.writeSync(tmpLocation, project);

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

expect(html).to.be.equalHTML(`
<html>
Expand All @@ -218,4 +218,47 @@ describe('htmlEntrypoint', function() {
`);
expect(scripts).to.deep.equal([]);
});

it('extracts configs from meta', function() {
let tmpobj = tmp.dirSync();
let tmpLocation = tmpobj.name;

let project = {
'index.html': `
<html>
<meta name="my-app/config/environment" content="%7B%22rootURL%22%3A%22%2Fcustom-root-url%2F%22%7D" >
<body>
<script src="/custom-root-url/bar.js"></script>
</body>
</html>
`,
};

fixturify.writeSync(tmpLocation, project);
let { config } = htmlEntrypoint('my-app', tmpLocation, 'index.html');
expect(config).to.deep.equal({
'my-app': { APP: { autoboot: false }, rootURL: '/custom-root-url/' },
});
});

it('understands customized rootURL', function() {
let tmpobj = tmp.dirSync();
let tmpLocation = tmpobj.name;

let project = {
'index.html': `
<html>
<meta name="my-app/config/environment" content="%7B%22rootURL%22%3A%22%2Fcustom-root-url%2F%22%7D" >
<body>
<script src="/custom-root-url/bar.js"></script>
</body>
</html>
`,
};

fixturify.writeSync(tmpLocation, project);

let { scripts } = htmlEntrypoint('my-app', tmpLocation, 'index.html');
expect(scripts).to.deep.equal([`${tmpLocation}/bar.js`]);
});
});
5 changes: 0 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4235,11 +4235,6 @@ simple-dom@^1.4.0:
"@simple-dom/serializer" "^1.4.0"
"@simple-dom/void-map" "^1.4.0"

simple-html-tokenizer@^0.5.9:
version "0.5.9"
resolved "https://registry.yarnpkg.com/simple-html-tokenizer/-/simple-html-tokenizer-0.5.9.tgz#1a83fe97f5a3e39b335fddf71cfe9b0263b581c2"
integrity sha512-w/3FEDN94r4JQ9WoYrIr8RqDIPZdyNkdpbK9glFady1CAEyD97XWCv8HFetQO21w81e7h7Nh59iYTyG1mUJftg==

slash@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/slash/-/slash-3.0.0.tgz#6539be870c165adbd5240220dbe361f1bc4d4634"
Expand Down

0 comments on commit 3fae95c

Please sign in to comment.