Skip to content

Commit

Permalink
Public API to query if files are included in build
Browse files Browse the repository at this point in the history
Implement a public API (using the environment config) for addons to determine if mirage's files are included in the build, so they can decide whether to include any mirage-dependent files.

Fixes miragejs#2166
  • Loading branch information
bendemboski committed Apr 18, 2021
1 parent a9b38e7 commit d653001
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 4 deletions.
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

0 comments on commit d653001

Please sign in to comment.