Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve FastBoot dependencies at compile time #493

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 46 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,50 @@ module.exports = {
treeForPublic() {
const fastbootEnabled = process.env.FASTBOOT_DISABLED !== 'true'
&& !!this.project.findAddonByName('ember-cli-fastboot');
return !this.parent.parent && fastbootEnabled
? this._super.treeForPublic.apply(this, arguments) : null;

const include = !this.parent.parent && fastbootEnabled;

if (!include) {
return null;
}

let abortcontrollerNode = debug(new Rollup(path.dirname(path.dirname(require.resolve('abortcontroller-polyfill'))), {
rollup: {
input: 'src/ponyfill.js',
output: {
file: 'node-abortcontroller.js',
name: 'NodeAbortcontroller',
format: 'amd',
amd: {
id: 'node-abortcontroller',
}
}
}
}), 'node-abortcontroller');

let fetchNode = debug(new Rollup(path.dirname(path.dirname(require.resolve('node-fetch'))), {
rollup: {
input: 'lib/index.js',
output: {
file: 'node-fetch.js',
name: 'NodeFetch',
format: 'iife'
}
}
}), 'node-fetch');

fetchNode = debug(map(fetchNode, (content) => `define('node-fetch', ['exports'], function(exports) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if there is a better way to do this?

var _exports = exports;
exports = {};
var module = {};
var global = window.self;
var Buffer = FastBoot.require('buffer').Buffer;
var require = FastBoot.require;
${content}
_exports.default = exports;
});`), 'node-fetch wrapped');

return this._super.treeForPublic.apply(this, [new MergeTrees([...arguments, abortcontrollerNode, fetchNode])]);
},

cacheKeyForTree(treeType) {
Expand All @@ -159,6 +201,8 @@ module.exports = {

// Add node version of fetch.js into fastboot package.json manifest vendorFiles array
updateFastBootManifest(manifest) {
manifest.vendorFiles.push('ember-fetch/node-abortcontroller.js');
manifest.vendorFiles.push('ember-fetch/node-fetch.js');
manifest.vendorFiles.push('ember-fetch/fetch-fastboot.js');
return manifest;
},
Expand Down
9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,12 @@
"ember-addon": {
"configPath": "tests/dummy/config",
"fastbootDependencies": [
"node-fetch",
"abortcontroller-polyfill",
"abortcontroller-polyfill/dist/cjs-ponyfill"
"buffer",
"http",
"https",
"stream",
"url",
"zlib"
]
}
}
8 changes: 3 additions & 5 deletions public/fetch-fastboot.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
/* globals define FastBoot */
/* globals define requirejs */
define('fetch', ['exports'], function(exports) {
var httpRegex = /^https?:\/\//;
var protocolRelativeRegex = /^\/\//;

var AbortControllerPolyfill = FastBoot.require(
'abortcontroller-polyfill/dist/cjs-ponyfill'
);
var nodeFetch = FastBoot.require('node-fetch');
var AbortControllerPolyfill = requirejs('node-abortcontroller');
var nodeFetch = requirejs('node-fetch')['default'];

function parseRequest(request) {
if (request === null) {
Expand Down