Skip to content

Commit 6903f78

Browse files
author
Robert Jackson
authored
Merge pull request #854 from ember-fastboot/fastboot-config
2 parents 81db617 + dd38417 commit 6903f78

File tree

2 files changed

+110
-11
lines changed

2 files changed

+110
-11
lines changed

packages/fastboot/src/html-entrypoint.js

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

7+
function mergeContent(metaElement, config, configName) {
8+
let name = metaElement.getAttribute('name');
9+
if (name && name.endsWith(configName)) {
10+
let content = JSON.parse(decodeURIComponent(metaElement.getAttribute('content')));
11+
content.APP = Object.assign({ autoboot: false }, content.APP);
12+
config[name.slice(0, -1 * configName.length)] = content;
13+
return true;
14+
}
15+
return false;
16+
}
17+
718
function htmlEntrypoint(appName, distPath, htmlPath) {
819
let html = fs.readFileSync(path.join(distPath, htmlPath), 'utf8');
920
let dom = new JSDOM(html);
10-
let scripts = [];
1121

22+
let fastbootConfig = {};
1223
let config = {};
1324
for (let element of dom.window.document.querySelectorAll('meta')) {
14-
let name = element.getAttribute('name');
15-
if (name && name.endsWith('/config/environment')) {
16-
let content = JSON.parse(decodeURIComponent(element.getAttribute('content')));
17-
content.APP = Object.assign({ autoboot: false }, content.APP);
18-
config[name.slice(0, -1 * '/config/environment'.length)] = content;
25+
mergeContent(element, config, '/config/environment');
26+
let fastbootMerged = mergeContent(element, fastbootConfig, '/config/fastboot-environment');
27+
if (fastbootMerged) {
28+
element.remove();
1929
}
2030
}
2131

32+
let isFastbootConfigBuilt = Object.keys(fastbootConfig).length > 0;
33+
if (isFastbootConfigBuilt) {
34+
config = fastbootConfig;
35+
}
36+
37+
let scripts = [];
2238
let rootURL = getRootURL(appName, config);
2339

2440
for (let element of dom.window.document.querySelectorAll('script,fastboot-script')) {

packages/fastboot/test/html-entrypoint-test.js

Lines changed: 88 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -222,11 +222,79 @@ describe('htmlEntrypoint', function() {
222222
it('extracts configs from meta', function() {
223223
let tmpobj = tmp.dirSync();
224224
let tmpLocation = tmpobj.name;
225+
let configObj = {
226+
'my-app': {
227+
APP: {
228+
autoboot: false,
229+
},
230+
configKey: 'someValue',
231+
},
232+
'my-engine': {
233+
APP: {
234+
autoboot: false,
235+
},
236+
engineKey: 'engineValue',
237+
},
238+
};
239+
let metaTags = Object.entries(configObj)
240+
.map(
241+
([name, options]) =>
242+
`<meta name="${name}/config/fastboot-environment" content="${encodeURIComponent(
243+
JSON.stringify(options)
244+
)}"></meta>`
245+
)
246+
.join('\n');
247+
248+
let project = {
249+
'index.html': `
250+
<html>
251+
<head>
252+
${metaTags}
253+
</head>
254+
<body>
255+
<script src="/custom-root-url/bar.js"></script>
256+
</body>
257+
</html>
258+
`,
259+
};
260+
261+
fixturify.writeSync(tmpLocation, project);
262+
let { config } = htmlEntrypoint('my-app', tmpLocation, 'index.html');
263+
expect(config).to.deep.equal(configObj);
264+
});
265+
266+
it('support config fallback name "config/environement" when there is no fastboot-environement in HTML', function() {
267+
let tmpobj = tmp.dirSync();
268+
let tmpLocation = tmpobj.name;
269+
let configObj = {
270+
'my-app': {
271+
APP: {
272+
autoboot: false,
273+
},
274+
configKey: 'someValue',
275+
},
276+
'my-engine': {
277+
APP: {
278+
autoboot: false,
279+
},
280+
engineKey: 'engineValue',
281+
},
282+
};
283+
let metaTags = Object.entries(configObj)
284+
.map(
285+
([name, options]) =>
286+
`<meta name="${name}/config/environment" content="${encodeURIComponent(
287+
JSON.stringify(options)
288+
)}"></meta>`
289+
)
290+
.join('\n');
225291

226292
let project = {
227293
'index.html': `
228294
<html>
229-
<meta name="my-app/config/environment" content="%7B%22rootURL%22%3A%22%2Fcustom-root-url%2F%22%7D" >
295+
<head>
296+
${metaTags}
297+
</head>
230298
<body>
231299
<script src="/custom-root-url/bar.js"></script>
232300
</body>
@@ -235,20 +303,35 @@ describe('htmlEntrypoint', function() {
235303
};
236304

237305
fixturify.writeSync(tmpLocation, project);
306+
238307
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-
});
308+
expect(config).to.deep.equal(configObj);
242309
});
243310

244311
it('understands customized rootURL', function() {
245312
let tmpobj = tmp.dirSync();
246313
let tmpLocation = tmpobj.name;
314+
let config = {
315+
'my-app': {
316+
rootURL: '/custom-root-url/',
317+
},
318+
};
319+
320+
let metaTags = Object.entries(config)
321+
.map(
322+
([name, options]) =>
323+
`<meta name="${name}/config/fastboot-environment" content="${encodeURIComponent(
324+
JSON.stringify(options)
325+
)}"></meta>`
326+
)
327+
.join('\n');
247328

248329
let project = {
249330
'index.html': `
250331
<html>
251-
<meta name="my-app/config/environment" content="%7B%22rootURL%22%3A%22%2Fcustom-root-url%2F%22%7D" >
332+
<head>
333+
${metaTags}
334+
</head>
252335
<body>
253336
<fastboot-script src="foo.js"></fastboot-script>
254337
<script src="/custom-root-url/bar.js"></script>

0 commit comments

Comments
 (0)