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

Public API to query if files are included in build #2167

Open
wants to merge 1 commit 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
17 changes: 13 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,20 @@ module.exports = {
}

this.app = app;
this.addonConfig = this.app.project.config(app.env)['ember-cli-mirage'] || {};

// Make sure the mirage config exists, and set the `includedInBuild` key so
// addons that provide code that relies on mirage can decide whether to
// include their code or not based on whether or not mirage is included in
// the build.
let config = this.app.project.config(app.env);
config['ember-cli-mirage'] = config['ember-cli-mirage'] || {};
this.addonConfig = config['ember-cli-mirage'];
this.addonConfig.includedInBuild = this._shouldIncludeFiles();

this.addonBuildConfig = this.app.options['ember-cli-mirage'] || {};

// Call super after initializing config so we can use _shouldIncludeFiles for the node assets
// Call super after initializing config so we can check the `includeInBuild`
// flag for the node assets
this._super.included.apply(this, arguments);

if (this.addonBuildConfig.directory) {
Expand All @@ -50,8 +60,7 @@ module.exports = {
},

treeFor(name) {
let shouldIncludeFiles = this._shouldIncludeFiles();
if (shouldIncludeFiles || name === 'vendor') {
if (this.addonConfig.includedInBuild || name === 'vendor') {
return this._super.treeFor.apply(this, arguments);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Supplying helpers from addons

Some addons supply Mirage models, route functions, or other code to help applications set up their Mirage configuration. The build's target environment and the [environment options](#environment-options) together determine whether Mirage's code will be included in the build or not, so addons that supply code that imports modules from mirage have to include or exclude that code accordingly.

To support this, Mirage writes an `includedInBuild` value to `ENV['ember-cli-mirage']` that other addons can read. To take advantage of this in your addon, you need to first make sure that your addon's hooks are called _after_ `ember-cli-mirage`'s by putting the following in your addon's `package.json`:

```diff
"ember-addon": {
"configPath": "tests/dummy/config",
+ "after": [
+ "ember-cli-mirage"
+ ],
}
```

Then you can look for the `includedInBuild` property of `ENV['ember-cli-mirage']` (being careful to not assume that `ENV['ember-cli-mirage']` is present, since it won't be if `ember-cli-mirage` isn't installed in the consuming application):

```js
included(app) {
this.mirageConfig = config['ember-cli-mirage'] || {};
this._super.included.apply(this, arguments);
},

treeForAddon() {
let tree = this._super(...arguments);
if (!mirageConfig.includedInBuild) {
// Exclude mirage-dependent files, e.g. use broccol-funnel to exclude
// files in `addon/mirage/`:
//
// const removeFile = require('broccoli-funnel');
// tree = funnel(tree, {
// exclude: 'mirage/**/*.js',
// });
}
return tree;
}
```
1 change: 1 addition & 0 deletions tests/dummy/app/pods/docs/template.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
{{nav.item "Mocking GUIDs" "docs.advanced.mocking-guids"}}
{{nav.item "Customizing the inflector" "docs.advanced.customizing-the-inflector"}}
{{nav.item "Switching between scenarios" "docs.advanced.switching-between-scenarios"}}
{{nav.item "Supplying helpers from addons" "docs.advanced.supplying-helpers-from-addons"}}

{{/viewer.nav}}

Expand Down
1 change: 1 addition & 0 deletions tests/dummy/app/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ Router.map(function() {
this.route('mocking-guids');
this.route('customizing-the-inflector');
this.route('switching-between-scenarios');
this.route('supplying-helpers-from-addons');
});

this.route('api', function() {
Expand Down