@@ -4,15 +4,16 @@ const Filter = require('broccoli-persistent-filter');
44const { JSDOM } = require ( 'jsdom' ) ;
55
66module . exports = class BasePageWriter extends Filter {
7- constructor ( inputNodes , { annotation, fastbootConfig, appName, manifest, appJsPath } ) {
7+ constructor ( inputNodes , { annotation, fastbootConfig, appName, manifest, outputPaths } ) {
88 super ( inputNodes , {
99 annotation,
1010 extensions : [ 'html' ] ,
1111 targetExtension : 'html' ,
1212 } ) ;
1313 this . _manifest = manifest ;
1414 this . _rootURL = getRootURL ( fastbootConfig , appName ) ;
15- this . _appJsPath = appJsPath ;
15+ this . _appJsPath = outputPaths . app . js ;
16+ this . _expectedFiles = expectedFiles ( outputPaths ) ;
1617 }
1718
1819 getDestFilePath ( ) {
@@ -29,26 +30,49 @@ module.exports = class BasePageWriter extends Filter {
2930 // do we need to concat rootURL here?
3031 let rootURL = this . _rootURL ;
3132
33+ ignoreUnexpectedScripts ( scriptTags , this . _expectedFiles ) ;
34+
35+ let fastbootScripts = this . _findFastbootScriptToInsert ( scriptTags ) ;
36+
37+ let appJsTag = findAppJsTag ( scriptTags , this . _appJsPath , rootURL ) ;
38+
39+ insertFastbootScriptsBeforeAppJsTags ( fastbootScripts , appJsTag ) ;
40+
41+ return dom . serialize ( ) ;
42+ }
43+
44+ _findFastbootScriptToInsert ( scriptTags ) {
45+ let rootURL = this . _rootURL ;
3246 let scriptSrcs = [ ] ;
3347 for ( let element of scriptTags ) {
3448 scriptSrcs . push ( urlWithin ( element . getAttribute ( 'src' ) , rootURL ) ) ;
3549 }
3650
37- let fastbootScripts = this . _manifest . vendorFiles
51+ return this . _manifest . vendorFiles
3852 . concat ( this . _manifest . appFiles )
3953 . map ( src => urlWithin ( src , rootURL ) )
4054 . filter ( src => ! scriptSrcs . includes ( src ) ) ;
55+ }
56+ } ;
4157
42- let appJsTag = findAppJsTag ( scriptTags , this . _appJsPath , rootURL ) ;
43- let range = new NodeRange ( appJsTag ) ;
58+ function expectedFiles ( outputPaths ) {
59+ function stripLeadingSlash ( filePath ) {
60+ return filePath . replace ( / ^ \/ / , '' ) ;
61+ }
4462
45- for ( let src of fastbootScripts ) {
46- range . insertAsScriptTag ( src ) ;
47- }
63+ let appFilePath = stripLeadingSlash ( outputPaths . app . js ) ;
64+ let appFastbootFilePath = appFilePath . replace ( / \. j s $ / , '' ) + '-fastboot.js' ;
65+ let vendorFilePath = stripLeadingSlash ( outputPaths . vendor . js ) ;
66+ return [ appFilePath , appFastbootFilePath , vendorFilePath ] ;
67+ }
4868
49- return dom . serialize ( ) ;
69+ function ignoreUnexpectedScripts ( scriptTags , expectedFiles ) {
70+ for ( let element of scriptTags ) {
71+ if ( ! expectedFiles . includes ( urlWithin ( element . getAttribute ( 'src' ) ) ) ) {
72+ element . setAttribute ( 'data-fastboot-ignore' , '' ) ;
73+ }
5074 }
51- } ;
75+ }
5276
5377function getRootURL ( appName , config ) {
5478 let rootURL = ( config [ appName ] && config [ appName ] . rootURL ) || '/' ;
@@ -75,6 +99,14 @@ function findAppJsTag(scriptTags, appJsPath, rootURL) {
7599 }
76100}
77101
102+ function insertFastbootScriptsBeforeAppJsTags ( fastbootScripts , appJsTag ) {
103+ let range = new NodeRange ( appJsTag ) ;
104+
105+ for ( let src of fastbootScripts ) {
106+ range . insertAsScriptTag ( src ) ;
107+ }
108+ }
109+
78110class NodeRange {
79111 constructor ( initial ) {
80112 this . start = initial . ownerDocument . createTextNode ( '' ) ;
0 commit comments