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

fixes addon on apps with engines #86

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
60 changes: 39 additions & 21 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,60 @@

const path = require('path');
const Funnel = require('broccoli-funnel');
const MergeTrees = require('broccoli-merge-trees');
const map = require('broccoli-stew').map;
const BroccoliMergeTrees = require('broccoli-merge-trees');
const fastbootTransform = require('fastboot-transform');
const resolve = require('resolve');

module.exports = {
name: require('./package').name,

included(app) {
included() {
this._super.included.apply(this, arguments);
let app;

// see: https://github.com/ember-cli/ember-cli/issues/3718
while (typeof app.import !== 'function' && app.app) {
app = app.app;
// If the addon has the _findHost() method (in ember-cli >= 2.7.0), we'll just
// use that.
if (typeof this._findHost === 'function') {
app = this._findHost();
} else {
// Otherwise, we'll use this implementation borrowed from the _findHost()
// method in ember-cli.
let current = this;
do {
app = current.app || app;
} while (current.parent.parent && (current = current.parent));
}

if (typeof app.import !== 'function') {
throw new Error('ember-hammertime is being used within another addon or engine '
+ 'and is having trouble registering itself to the parent application.');
}

app.import('vendor/hammer-time.js');
app.import('vendor/hammer-timejs/hammer-time.js');
},

treeForVendor(vendorTree) {
treeForVendor(tree) {
let trees = [];
let hammertimeTree = new Funnel(path.dirname(require.resolve('hammer-timejs/hammer-time.js')), {
files: ['hammer-time.js']
});
hammertimeTree = map(hammertimeTree, (content) => `if (typeof FastBoot === 'undefined') { ${content} }`);

if (vendorTree !== undefined) {
trees.push(vendorTree);
let hammerTimeJs = fastbootTransform(new Funnel(this.pathBase('hammer-timejs'), {
files: ['hammer-time.js'],
destDir: 'hammer-timejs'
}));

trees.push(hammerTimeJs);

if (tree) {
trees.push(tree);
}

trees.push(hammertimeTree);
return new BroccoliMergeTrees(trees);
},

return new MergeTrees(trees);
/*
Rely on the `resolve` package to mimic node's resolve algorithm.
It finds the modules in a manner that works for npm 2.x,
3.x, and yarn in both the addon itself and projects that depend on this addon.
This is an edge case b/c some modules do not have a main
module we can require.resolve through node itself and similarily ember-cli
does not have such a hack for the same reason.
*/
pathBase(packageName) {
return path.dirname(resolve.sync(`${packageName}/package.json`, { basedir: __dirname }));
},

isDevelopingAddon() {
Expand Down
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@
"test:all": "ember try:each"
},
"dependencies": {
"broccoli-funnel": "^2.0.1",
"broccoli-merge-trees": "^3.0.1",
"broccoli-funnel": "^2.0.2",
"broccoli-merge-trees": "^3.0.2",
"broccoli-stew": "^2.0.0",
"ember-cli-babel": "^6.16.0",
"ember-get-config": "^0.2.4",
"hammer-timejs": "^1.1.0"
"fastboot-transform": "^0.1.3",
"hammer-timejs": "^1.1.0",
"resolve": "^1.10.0"
},
"devDependencies": {
"@ember/optional-features": "^0.6.3",
Expand Down