Skip to content

Instantly share code, notes, and snippets.

@jamesarosen
Created August 25, 2018 23:42
Show Gist options
  • Save jamesarosen/77a83ddd6c314d21136047a2ca035714 to your computer and use it in GitHub Desktop.
Save jamesarosen/77a83ddd6c314d21136047a2ca035714 to your computer and use it in GitHub Desktop.
addon-mirage-supprot

The following code will let an ember addon provide an addon-test-support/ folder whose contents get merged into app/mirage/. It excludes addon-test-support/ from the build if mirage is disabled.

See Better Addon Support on the Ember CLI Mirage feature-request board.

'use strict'
const Funnel = require('broccoli-funnel')
const mergeTrees = require('broccoli-merge-trees')
const path = require('path')
module.exports = {
name: '@my/addon',
included(app) {
this._super.included.apply(this, arguments)
this.mirageConfig = app.project.config(app.env)['ember-cli-mirage'] || {}
},
treeForApp(appTree) {
const trees = [appTree]
if (this._shouldIncludeMirageFiles()) {
const mirageDir = path.join(__dirname, 'addon-mirage-support')
const mirageTree = new Funnel(mirageDir, { destDir: 'mirage' })
trees.push(mirageTree)
}
return mergeTrees(trees)
},
// Copied from ember-cli-mirage. It would be really nice if mirage could export this information
// somehow. A helper function would work, but perhaps better would be to have ember-cli-mirage
// modify app.env['ember-cli-mirage'].enabled.
_shouldIncludeMirageFiles() {
if (process.env.EMBER_CLI_FASTBOOT) {
return false
}
let environment = this.app.env
let enabledInProd = environment === 'production' && this.mirageConfig.enabled
let explicitExcludeFiles = this.mirageConfig.excludeFilesFromBuild
if (enabledInProd && explicitExcludeFiles) {
throw new Error(
'Mirage was explicitly enabled in production, but its files were excluded ' +
"from the build. Please, use only ENV['ember-cli-mirage'].enabled in " +
'production environment.'
)
}
return (
enabledInProd ||
(environment && environment !== 'production' && explicitExcludeFiles !== true)
)
},
}
import { hasMany, Model } from 'ember-cli-mirage'
export default Model.extend({
comments: hasMany(),
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment