From fc6fc2431eb6069057371de9ffb08d7d90d3b0c5 Mon Sep 17 00:00:00 2001 From: Thomas Wang Date: Sat, 21 Aug 2021 23:18:21 -0700 Subject: [PATCH 1/2] Fix fastboot-script assets with cutom root url When using custom rootURL specified in environment.js, the fastboot-script is still having relative path to local dist path, not containing rootURL path. The fastboot-script should not be ignored. --- packages/fastboot/src/html-entrypoint.js | 2 ++ packages/fastboot/test/html-entrypoint-test.js | 3 ++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/fastboot/src/html-entrypoint.js b/packages/fastboot/src/html-entrypoint.js index 8e2a2213d..6bafc9e9c 100644 --- a/packages/fastboot/src/html-entrypoint.js +++ b/packages/fastboot/src/html-entrypoint.js @@ -27,6 +27,8 @@ function htmlEntrypoint(appName, distPath, htmlPath) { let relativeSrc = urlWithin(src, rootURL); if (relativeSrc) { scripts.push(path.join(distPath, relativeSrc)); + } else if (element.tagName === 'FASTBOOT-SCRIPT') { + scripts.push(path.join(distPath, src)); } } if (element.tagName === 'FASTBOOT-SCRIPT') { diff --git a/packages/fastboot/test/html-entrypoint-test.js b/packages/fastboot/test/html-entrypoint-test.js index df8030f67..c12286c65 100644 --- a/packages/fastboot/test/html-entrypoint-test.js +++ b/packages/fastboot/test/html-entrypoint-test.js @@ -250,6 +250,7 @@ describe('htmlEntrypoint', function() { + @@ -259,6 +260,6 @@ describe('htmlEntrypoint', function() { fixturify.writeSync(tmpLocation, project); let { scripts } = htmlEntrypoint('my-app', tmpLocation, 'index.html'); - expect(scripts).to.deep.equal([`${tmpLocation}/bar.js`]); + expect(scripts).to.deep.equal([`${tmpLocation}/foo.js`, `${tmpLocation}/bar.js`]); }); }); From dd3841748c518f905e6c07c7b7b799601451fc7a Mon Sep 17 00:00:00 2001 From: Thomas Wang Date: Sun, 22 Aug 2021 16:29:24 -0700 Subject: [PATCH 2/2] Support extracting fastboot specific config from meta tag --- packages/fastboot/src/html-entrypoint.js | 28 ++++-- .../fastboot/test/html-entrypoint-test.js | 93 ++++++++++++++++++- 2 files changed, 110 insertions(+), 11 deletions(-) diff --git a/packages/fastboot/src/html-entrypoint.js b/packages/fastboot/src/html-entrypoint.js index 6bafc9e9c..80edda869 100644 --- a/packages/fastboot/src/html-entrypoint.js +++ b/packages/fastboot/src/html-entrypoint.js @@ -4,21 +4,37 @@ const { JSDOM } = require('jsdom'); const fs = require('fs'); const path = require('path'); +function mergeContent(metaElement, config, configName) { + let name = metaElement.getAttribute('name'); + if (name && name.endsWith(configName)) { + let content = JSON.parse(decodeURIComponent(metaElement.getAttribute('content'))); + content.APP = Object.assign({ autoboot: false }, content.APP); + config[name.slice(0, -1 * configName.length)] = content; + return true; + } + return false; +} + function htmlEntrypoint(appName, distPath, htmlPath) { let html = fs.readFileSync(path.join(distPath, htmlPath), 'utf8'); let dom = new JSDOM(html); - let scripts = []; + let fastbootConfig = {}; let config = {}; for (let element of dom.window.document.querySelectorAll('meta')) { - let name = element.getAttribute('name'); - if (name && name.endsWith('/config/environment')) { - let content = JSON.parse(decodeURIComponent(element.getAttribute('content'))); - content.APP = Object.assign({ autoboot: false }, content.APP); - config[name.slice(0, -1 * '/config/environment'.length)] = content; + mergeContent(element, config, '/config/environment'); + let fastbootMerged = mergeContent(element, fastbootConfig, '/config/fastboot-environment'); + if (fastbootMerged) { + element.remove(); } } + let isFastbootConfigBuilt = Object.keys(fastbootConfig).length > 0; + if (isFastbootConfigBuilt) { + config = fastbootConfig; + } + + let scripts = []; let rootURL = getRootURL(appName, config); for (let element of dom.window.document.querySelectorAll('script,fastboot-script')) { diff --git a/packages/fastboot/test/html-entrypoint-test.js b/packages/fastboot/test/html-entrypoint-test.js index c12286c65..465337412 100644 --- a/packages/fastboot/test/html-entrypoint-test.js +++ b/packages/fastboot/test/html-entrypoint-test.js @@ -222,11 +222,79 @@ describe('htmlEntrypoint', function() { it('extracts configs from meta', function() { let tmpobj = tmp.dirSync(); let tmpLocation = tmpobj.name; + let configObj = { + 'my-app': { + APP: { + autoboot: false, + }, + configKey: 'someValue', + }, + 'my-engine': { + APP: { + autoboot: false, + }, + engineKey: 'engineValue', + }, + }; + let metaTags = Object.entries(configObj) + .map( + ([name, options]) => + `` + ) + .join('\n'); + + let project = { + 'index.html': ` + + + ${metaTags} + + + + + + `, + }; + + fixturify.writeSync(tmpLocation, project); + let { config } = htmlEntrypoint('my-app', tmpLocation, 'index.html'); + expect(config).to.deep.equal(configObj); + }); + + it('support config fallback name "config/environement" when there is no fastboot-environement in HTML', function() { + let tmpobj = tmp.dirSync(); + let tmpLocation = tmpobj.name; + let configObj = { + 'my-app': { + APP: { + autoboot: false, + }, + configKey: 'someValue', + }, + 'my-engine': { + APP: { + autoboot: false, + }, + engineKey: 'engineValue', + }, + }; + let metaTags = Object.entries(configObj) + .map( + ([name, options]) => + `` + ) + .join('\n'); let project = { 'index.html': ` - + + ${metaTags} + @@ -235,20 +303,35 @@ describe('htmlEntrypoint', function() { }; 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/' }, - }); + expect(config).to.deep.equal(configObj); }); it('understands customized rootURL', function() { let tmpobj = tmp.dirSync(); let tmpLocation = tmpobj.name; + let config = { + 'my-app': { + rootURL: '/custom-root-url/', + }, + }; + + let metaTags = Object.entries(config) + .map( + ([name, options]) => + `` + ) + .join('\n'); let project = { 'index.html': ` - + + ${metaTags} +