@@ -12,6 +12,7 @@ module.exports = class BasePageWriter extends Filter {
1212 } ) ;
1313 this . _manifest = manifest ;
1414 this . _rootURL = getRootURL ( fastbootConfig , appName ) ;
15+ this . _fastbootConfig = fastbootConfig ;
1516 this . _appJsPath = outputPaths . app . js ;
1617 this . _expectedFiles = expectedFiles ( outputPaths ) ;
1718 }
@@ -24,15 +25,46 @@ module.exports = class BasePageWriter extends Filter {
2425
2526 processString ( content ) {
2627 let dom = new JSDOM ( content ) ;
28+ this . _handleConfig ( dom ) ;
29+ this . _handleScripts ( dom ) ;
30+ return dom . serialize ( ) ;
31+ }
32+
33+ _handleConfig ( dom ) {
34+ function findFistConfigMeta ( dom ) {
35+ let metaTags = dom . window . document . querySelectorAll ( 'meta' ) ;
36+ for ( let element of metaTags ) {
37+ let name = element . getAttribute ( 'name' ) ;
38+ if ( name && name . endsWith ( '/config/environment' ) ) {
39+ return element ;
40+ }
41+ }
42+ }
43+ let firstConfigMeta ;
44+ if ( firstConfigMeta ) {
45+ firstConfigMeta = findFistConfigMeta ( dom ) ;
46+ } else {
47+ firstConfigMeta = dom . window . document . createTextNode ( '\n' ) ;
48+ dom . window . document . head . appendChild ( firstConfigMeta ) ;
49+ }
50+ let nodeRange = new NodeRange ( firstConfigMeta ) ;
51+ for ( let [ name , options ] of Object . entries ( this . _fastbootConfig ) ) {
52+ nodeRange . insertJsonAsMetaTag ( `${ name } /config/fastboot-environment` , options ) ;
53+ }
54+ }
55+
56+ _handleScripts ( dom ) {
2757 let scriptTags = dom . window . document . querySelectorAll ( 'script' ) ;
2858
2959 this . _ignoreUnexpectedScripts ( scriptTags ) ;
3060
3161 let fastbootScripts = this . _findFastbootScriptToInsert ( scriptTags ) ;
3262 let appJsTag = findAppJsTag ( scriptTags , this . _appJsPath , this . _rootURL ) ;
33- insertFastbootScriptsBeforeAppJsTags ( fastbootScripts , appJsTag ) ;
63+ if ( ! appJsTag ) {
64+ throw new Error ( 'ember-cli-fastboot cannot find own app script tag' ) ;
65+ }
3466
35- return dom . serialize ( ) ;
67+ insertFastbootScriptsBeforeAppJsTags ( fastbootScripts , appJsTag ) ;
3668 }
3769
3870 _findFastbootScriptToInsert ( scriptTags ) {
@@ -79,6 +111,10 @@ function getRootURL(appName, config) {
79111}
80112
81113function urlWithin ( candidate , root ) {
114+ // this is a null or relative path
115+ if ( ! candidate || ! candidate . startsWith ( '/' ) ) {
116+ return candidate ;
117+ }
82118 let candidateURL = new URL ( candidate , 'http://_the_current_origin_' ) ;
83119 let rootURL = new URL ( root , 'http://_the_current_origin_' ) ;
84120 if ( candidateURL . href . startsWith ( rootURL . href ) ) {
@@ -114,6 +150,18 @@ class NodeRange {
114150 let newTag = this . end . ownerDocument . createElement ( 'fastboot-script' ) ;
115151 newTag . setAttribute ( 'src' , src ) ;
116152 this . insertNode ( newTag ) ;
153+ this . insertNewLine ( ) ;
154+ }
155+
156+ insertJsonAsMetaTag ( name , content ) {
157+ let newTag = this . end . ownerDocument . createElement ( 'meta' ) ;
158+ newTag . setAttribute ( 'name' , name ) ;
159+ newTag . setAttribute ( 'content' , encodeURIComponent ( JSON . stringify ( content ) ) ) ;
160+ this . insertNode ( newTag ) ;
161+ this . insertNewLine ( ) ;
162+ }
163+
164+ insertNewLine ( ) {
117165 this . insertNode ( this . end . ownerDocument . createTextNode ( '\n' ) ) ;
118166 }
119167
0 commit comments