Skip to content

Commit

Permalink
Added support for embroider build and current build
Browse files Browse the repository at this point in the history
* Update manifest appfiles from index.html
* app-boot type is not supported in config, moved updating appFile to post build app-boot in fastboot
  • Loading branch information
dnalagatla committed Jun 5, 2019
1 parent dff28c1 commit 7f0e8b2
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 250 deletions.
56 changes: 51 additions & 5 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 @@ -106,11 +107,6 @@ module.exports = {
return "<!-- EMBER_CLI_FASTBOOT_TITLE --><!-- EMBER_CLI_FASTBOOT_HEAD -->";
}

if (type === 'app-boot') {
const isModuleUnification = (typeof this.project.isModuleUnification === 'function') && this.project.isModuleUnification();
return fastbootAppModule(config.modulePrefix, JSON.stringify(config.APP || {}), isModuleUnification);
}

// if the fastboot addon is installed, we overwrite the config-module so that the config can be read
// from meta tag/directly for browser build and from Fastboot config for fastboot target.
if (type === 'config-module') {
Expand Down Expand Up @@ -334,6 +330,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 +358,53 @@ 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));

//Auto run is false by default in fastboot. Append boot code at the end of app file
this._appendAppBoot(appDir, appFiles[appFiles.length-1]);
}
},

_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;
},

_appendAppBoot(appDir, appFilePath) {
const config = this._getHostAppConfig();
const isModuleUnification = (typeof this.project.isModuleUnification === 'function') && this.project.isModuleUnification();
const appBoot = fastbootAppModule(config.modulePrefix, JSON.stringify(config.APP || {}), isModuleUnification);

appFilePath = path.resolve(appDir, appFilePath);
fs.appendFileSync(appFilePath, appBoot);
}
};
2 changes: 1 addition & 1 deletion lib/utilities/fastboot-app-module.js
Expand Up @@ -12,7 +12,7 @@ module.exports = function fastbootAppModule(prefix, configAppAsString, isModuleU
return [
"",
"if (typeof FastBoot === 'undefined') {",
" if (!runningTests) {",
" if (typeof runningTests === 'undefined') {",
" require('{{MODULE_PREFIX}}/" + appSuffix + "')['default'].create({{CONFIG_APP}});",
" }",
"}",
Expand Down
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 7f0e8b2

Please sign in to comment.