Skip to content

Commit

Permalink
add data-fastboot-ignore to unexpected files
Browse files Browse the repository at this point in the history
  • Loading branch information
xg-wang committed Apr 7, 2021
1 parent e66da34 commit 74a7c59
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 10 deletions.
1 change: 1 addition & 0 deletions packages/ember-cli-fastboot/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,7 @@ module.exports = {
appName,
manifest,
appJsPath: this.app.options.outputPaths.app.js,
outputPaths: this.app.options.outputPaths,
});

// Merge the package.json with the existing tree
Expand Down
52 changes: 42 additions & 10 deletions packages/ember-cli-fastboot/lib/broccoli/html-writer.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@ const Filter = require('broccoli-persistent-filter');
const { JSDOM } = require('jsdom');

module.exports = class BasePageWriter extends Filter {
constructor(inputNodes, { annotation, fastbootConfig, appName, manifest, appJsPath }) {
constructor(inputNodes, { annotation, fastbootConfig, appName, manifest, outputPaths }) {
super(inputNodes, {
annotation,
extensions: ['html'],
targetExtension: 'html',
});
this._manifest = manifest;
this._rootURL = getRootURL(fastbootConfig, appName);
this._appJsPath = appJsPath;
this._appJsPath = outputPaths.app.js;
this._expectedFiles = expectedFiles(outputPaths);
}

getDestFilePath() {
Expand All @@ -29,26 +30,49 @@ module.exports = class BasePageWriter extends Filter {
// do we need to concat rootURL here?
let rootURL = this._rootURL;

ignoreUnexpectedScripts(scriptTags, this._expectedFiles);

let fastbootScripts = this._findFastbootScriptToInsert(scriptTags);

let appJsTag = findAppJsTag(scriptTags, this._appJsPath, rootURL);

insertFastbootScriptsBeforeAppJsTags(fastbootScripts, appJsTag);

return dom.serialize();
}

_findFastbootScriptToInsert(scriptTags) {
let rootURL = this._rootURL;
let scriptSrcs = [];
for (let element of scriptTags) {
scriptSrcs.push(urlWithin(element.getAttribute('src'), rootURL));
}

let fastbootScripts = this._manifest.vendorFiles
return this._manifest.vendorFiles
.concat(this._manifest.appFiles)
.map(src => urlWithin(src, rootURL))
.filter(src => !scriptSrcs.includes(src));
}
};

let appJsTag = findAppJsTag(scriptTags, this._appJsPath, rootURL);
let range = new NodeRange(appJsTag);
function expectedFiles(outputPaths) {
function stripLeadingSlash(filePath) {
return filePath.replace(/^\//, '');
}

for (let src of fastbootScripts) {
range.insertAsScriptTag(src);
}
let appFilePath = stripLeadingSlash(outputPaths.app.js);
let appFastbootFilePath = appFilePath.replace(/\.js$/, '') + '-fastboot.js';
let vendorFilePath = stripLeadingSlash(outputPaths.vendor.js);
return [appFilePath, appFastbootFilePath, vendorFilePath];
}

return dom.serialize();
function ignoreUnexpectedScripts(scriptTags, expectedFiles) {
for (let element of scriptTags) {
if (!expectedFiles.includes(urlWithin(element.getAttribute('src')))) {
element.setAttribute('data-fastboot-ignore', '');
}
}
};
}

function getRootURL(appName, config) {
let rootURL = (config[appName] && config[appName].rootURL) || '/';
Expand All @@ -75,6 +99,14 @@ function findAppJsTag(scriptTags, appJsPath, rootURL) {
}
}

function insertFastbootScriptsBeforeAppJsTags(fastbootScripts, appJsTag) {
let range = new NodeRange(appJsTag);

for (let src of fastbootScripts) {
range.insertAsScriptTag(src);
}
}

class NodeRange {
constructor(initial) {
this.start = initial.ownerDocument.createTextNode('');
Expand Down

0 comments on commit 74a7c59

Please sign in to comment.