@@ -11,6 +11,7 @@ const chalk = require('chalk');
1111
1212const fastbootAppBoot = require ( './lib/utilities/fastboot-app-boot' ) ;
1313const FastBootConfig = require ( './lib/broccoli/fastboot-config' ) ;
14+ const BasePageWriter = require ( './lib/broccoli/base-page-writer' ) ;
1415const fastbootAppFactoryModule = require ( './lib/utilities/fastboot-app-factory-module' ) ;
1516const migrateInitializers = require ( './lib/build-utilities/migrate-initializers' ) ;
1617const SilentError = require ( 'silent-error' ) ;
@@ -60,7 +61,9 @@ module.exports = {
6061 * See: https://ember-cli.com/user-guide/#integration
6162 */
6263 included ( app ) {
63- let assetRev = this . project . addons . find ( addon => addon . name === 'broccoli-asset-rev' ) ;
64+ let assetRev = this . project . addons . find (
65+ ( addon ) => addon . name === 'broccoli-asset-rev'
66+ ) ;
6467 if ( assetRev && ! assetRev . supportsFastboot ) {
6568 throw new SilentError (
6669 'This version of ember-cli-fastboot requires a newer version of broccoli-asset-rev'
@@ -109,7 +112,10 @@ module.exports = {
109112 }
110113
111114 if ( type === 'app-boot' ) {
112- return fastbootAppBoot ( config . modulePrefix , JSON . stringify ( config . APP || { } ) ) ;
115+ return fastbootAppBoot (
116+ config . modulePrefix ,
117+ JSON . stringify ( config . APP || { } )
118+ ) ;
113119 }
114120
115121 // if the fastboot addon is installed, we overwrite the config-module so that the config can be read
@@ -134,15 +140,19 @@ module.exports = {
134140
135141 // check the ember version and conditionally patch the DOM api
136142 if ( this . _getEmberVersion ( ) . lt ( '2.10.0-alpha.1' ) ) {
137- fastbootHtmlBarsTree = this . treeGenerator ( path . resolve ( __dirname , 'fastboot-app-lt-2-9' ) ) ;
138- return tree ? new MergeTrees ( [ tree , fastbootHtmlBarsTree ] ) : fastbootHtmlBarsTree ;
143+ fastbootHtmlBarsTree = this . treeGenerator (
144+ path . resolve ( __dirname , 'fastboot-app-lt-2-9' )
145+ ) ;
146+ return tree
147+ ? new MergeTrees ( [ tree , fastbootHtmlBarsTree ] )
148+ : fastbootHtmlBarsTree ;
139149 }
140150
141151 return tree ;
142152 } ,
143153
144154 _processAddons ( addons , fastbootTrees ) {
145- addons . forEach ( addon => {
155+ addons . forEach ( ( addon ) => {
146156 this . _processAddon ( addon , fastbootTrees ) ;
147157 } ) ;
148158 } ,
@@ -182,7 +192,10 @@ module.exports = {
182192 // check the parent containing the fastboot directory
183193 const projectFastbootPath = path . join ( this . project . root , 'fastboot' ) ;
184194 // ignore the project's fastboot folder if we are an addon, as that is already handled above
185- if ( ! this . project . isEmberCLIAddon ( ) && this . existsSync ( projectFastbootPath ) ) {
195+ if (
196+ ! this . project . isEmberCLIAddon ( ) &&
197+ this . existsSync ( projectFastbootPath )
198+ ) {
186199 let fastbootTree = this . treeGenerator ( projectFastbootPath ) ;
187200 fastbootTrees . push ( fastbootTree ) ;
188201 }
@@ -195,17 +208,28 @@ module.exports = {
195208 let funneledFastbootTrees = new Funnel ( mergedFastBootTree , {
196209 destDir : appName ,
197210 } ) ;
198- const processExtraTree = p . preprocessJs ( funneledFastbootTrees , '/' , this . _name , {
199- registry : this . _appRegistry ,
200- } ) ;
211+ const processExtraTree = p . preprocessJs (
212+ funneledFastbootTrees ,
213+ '/' ,
214+ this . _name ,
215+ {
216+ registry : this . _appRegistry ,
217+ }
218+ ) ;
201219
202220 // FastBoot app factory module
203221 const writeFile = require ( 'broccoli-file-creator' ) ;
204- let appFactoryModuleTree = writeFile ( 'app-factory.js' , fastbootAppFactoryModule ( appName ) ) ;
205-
206- let newProcessExtraTree = new MergeTrees ( [ processExtraTree , appFactoryModuleTree ] , {
207- overwrite : true ,
208- } ) ;
222+ let appFactoryModuleTree = writeFile (
223+ 'app-factory.js' ,
224+ fastbootAppFactoryModule ( appName )
225+ ) ;
226+
227+ let newProcessExtraTree = new MergeTrees (
228+ [ processExtraTree , appFactoryModuleTree ] ,
229+ {
230+ overwrite : true ,
231+ }
232+ ) ;
209233
210234 function stripLeadingSlash ( filePath ) {
211235 return filePath . replace ( / ^ \/ / , '' ) ;
@@ -219,6 +243,7 @@ module.exports = {
219243 return finalFastbootTree ;
220244 } ,
221245
246+ // Note: this hook is ignored when built with embroider
222247 treeForPublic ( tree ) {
223248 let fastbootTree = this . _getFastbootTree ( ) ;
224249 let trees = [ ] ;
@@ -229,7 +254,8 @@ module.exports = {
229254
230255 let newTree = new MergeTrees ( trees ) ;
231256
232- let fastbootConfigTree = this . _buildFastbootConfigTree ( newTree ) ;
257+ let fastbootConfigTree = ( this . _fastbootConfigTree =
258+ this . _buildFastbootConfigTree ( newTree ) ) ;
233259
234260 // Merge the package.json with the existing tree
235261 return new MergeTrees ( [ newTree , fastbootConfigTree ] , { overwrite : true } ) ;
@@ -294,18 +320,41 @@ module.exports = {
294320
295321 _buildFastbootConfigTree ( tree ) {
296322 let appConfig = this . _getHostAppConfig ( ) ;
297- let fastbootAppConfig = appConfig . fastboot ;
298323
299324 return new FastBootConfig ( tree , {
300325 project : this . project ,
301- name : this . app . name ,
302326 outputPaths : this . app . options . outputPaths ,
303327 ui : this . ui ,
304- fastbootAppConfig : fastbootAppConfig ,
305- appConfig : appConfig ,
328+ appConfig,
306329 } ) ;
307330 } ,
308331
332+ _buildHTMLWriter ( tree ) {
333+ let appConfig = this . _getHostAppConfig ( ) ;
334+
335+ return new BasePageWriter ( tree , {
336+ project : this . project ,
337+ appConfig,
338+ appJsPath : this . app . options . outputPaths . app . js ,
339+ outputPaths : this . app . options . outputPaths ,
340+ } ) ;
341+ } ,
342+
343+ /**
344+ * Write fastboot-script tags to the html file
345+ */
346+ postprocessTree ( type , tree ) {
347+ this . _super ( ...arguments ) ;
348+ if ( type === 'all' ) {
349+ let fastbootHTMLTree = this . _buildHTMLWriter ( tree ) ;
350+
351+ // Merge the package.json with the existing tree
352+ return new MergeTrees ( [ tree , fastbootHTMLTree ] , { overwrite : true } ) ;
353+ }
354+
355+ return tree ;
356+ } ,
357+
309358 serverMiddleware ( options ) {
310359 let emberCliVersion = this . _getEmberCliVersion ( ) ;
311360 let app = options . app ;
@@ -316,8 +365,11 @@ module.exports = {
316365
317366 app . use ( ( req , resp , next ) => {
318367 const fastbootQueryParam =
319- req . query . hasOwnProperty ( 'fastboot' ) && req . query . fastboot === 'false' ? false : true ;
320- const enableFastBootServe = ! process . env . FASTBOOT_DISABLED && fastbootQueryParam ;
368+ req . query . hasOwnProperty ( 'fastboot' ) && req . query . fastboot === 'false'
369+ ? false
370+ : true ;
371+ const enableFastBootServe =
372+ ! process . env . FASTBOOT_DISABLED && fastbootQueryParam ;
321373
322374 if ( req . serveUrl && enableFastBootServe ) {
323375 // if it is a base page request, then have fastboot serve the base page
@@ -384,7 +436,10 @@ module.exports = {
384436 * TODO Allow add-ons to provide own options and merge them with the application's options.
385437 */
386438 _fastbootOptionsFor ( environment , project ) {
387- const configPath = path . join ( path . dirname ( project . configPath ( ) ) , 'fastboot.js' ) ;
439+ const configPath = path . join (
440+ path . dirname ( project . configPath ( ) ) ,
441+ 'fastboot.js'
442+ ) ;
388443
389444 if ( fs . existsSync ( configPath ) ) {
390445 return require ( configPath ) ( environment ) ;
0 commit comments