Skip to content

Commit

Permalink
Added support reading app files form index.html
Browse files Browse the repository at this point in the history
  • Loading branch information
dnalagatla committed Jun 5, 2019
1 parent dff28c1 commit a6167f8
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 244 deletions.
39 changes: 39 additions & 0 deletions index.js
Expand Up @@ -19,6 +19,7 @@ const Funnel = require('broccoli-funnel');
const p = require('ember-cli-preprocess-registry/preprocessors');
const fastbootTransform = require('fastboot-transform');
const existsSync = fs.existsSync;
const cheerio = require('cheerio');

let checker;
function getVersionChecker(context) {
Expand Down Expand Up @@ -334,6 +335,7 @@ module.exports = {
},

postBuild(result) {
this._updateAppFilesInManifest(result.directory);
if (this.fastboot) {
// should we reload fastboot if there are only css changes? Seems it maynot be needed.
// TODO(future): we can do a smarter reload here by running fs-tree-diff on files loaded
Expand Down Expand Up @@ -361,4 +363,41 @@ module.exports = {

return checker.for('ember', 'bower');
},

_updateAppFilesInManifest(appDir) {
const appFiles = this._getAppFilesFromIndexHtml(appDir);
const pkgPath = path.join(appDir, 'package.json');
const pkg = require(pkgPath);
const {
fastboot
} = pkg;

// Update manifest.appFiles in package.json, with appFiles defined in index.html
if(fastboot) {
const { manifest: { appFiles: manifestAppFiles } } = fastboot;
manifestAppFiles.splice(0, 1, ...appFiles);
fs.writeFileSync(pkgPath, JSON.stringify(pkg));
}
},

_getAppFilesFromIndexHtml(appDir) {
const indexHtml = fs.readFileSync(path.join(appDir, 'index.html'));
const $ = cheerio.load(indexHtml);
const scriptFileNameRegEx = /([a-zA-Z0-9_\.\-\(\):])+(\.js)/ig;
const filesToSkip = ['vendor.js','vendor-static.js','ember-cli-live-reload.js'];
const appFiles = [];

$('script').each(function(i, elem) {
const src = $(elem).attr('src');
if (src) {
const fileName = src.match(scriptFileNameRegEx)[0];
const filePath = path.join(appDir, 'assets', fileName);
if (fileName && existsSync(filePath) && !filesToSkip.includes(fileName)) {
appFiles.push(path.relative(appDir, filePath));
}
}
});

return appFiles;
}
};
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -45,6 +45,7 @@
"chai": "^4.1.2",
"chai-fs": "^2.0.0",
"chai-string": "^1.4.0",
"cheerio": "^1.0.0-rc.3",
"co": "^4.6.0",
"ember-ajax": "^3.1.0",
"ember-cli": "~3.3.0",
Expand Down

0 comments on commit a6167f8

Please sign in to comment.