Skip to content

Commit

Permalink
Consolidate Configuration (#68)
Browse files Browse the repository at this point in the history
In order to have a consistent configuration experience I've moved the
options that were in `ember-cli-build.js` into `environment.js`. Since
we need some configuration at both build and run time this is the best
place for it.

To provide for a smooth upgrade path existing configuration in
`ember-cli-build.js` will be respected, but a warning will be printed
during the build.  Any configuration in `environment.js` will supersede
anything in `ember-cli.build.js`
  • Loading branch information
jrjohnson authored and mlwilkerson committed Aug 21, 2018
1 parent 3505edc commit a8cda96
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 50 deletions.
32 changes: 18 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,25 +115,29 @@ Using the Pro packages requires [additional configuration](https://fontawesome.c

If you want to include only a subset of icons from an icon pack, add a
`fontawesome` configuration object to your applications options in
`ember-cli-build.js`. The following example declares that all icons in
`environment.js`. The following example declares that all icons in
`free-solid-svg-icons` should be included in the `vendor.js` bundle add
added to the library, and for `pro-light-svg-icons`, only `adjust`,
`ambulance`, and `pencil-alt` are to be included in the bundle and added to the library.

```
// ...
let app = new EmberApp(defaults, {
// Add options here
fontawesome: {
icons: {
'free-solid-svg-icons': 'all'
'pro-light-svg-icons': [
'adjust',
'ambulance',
'pencil-alt'
]
```js
module.exports = function(environment) {
let ENV = {
// Add options here
fontawesome: {
icons: {
'free-solid-svg-icons': 'all'
'pro-light-svg-icons': [
'adjust',
'ambulance',
'pencil-alt'
]
}
}
})
};
// ...
return ENV;
};
```

## Usage
Expand Down
21 changes: 0 additions & 21 deletions ember-cli-build.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,6 @@ const EmberAddon = require('ember-cli/lib/broccoli/ember-addon')
module.exports = function(defaults) {
let app = new EmberAddon(defaults, {
// Add options here
fontawesome: {
enableExperimentalBuildTimeTransform: true,
icons: {
'free-solid-svg-icons': [
'coffee',
'magic',
'circle',
'square',
'home',
'info',
'book',
'pencil-alt',
'cog',
'spinner',
'checkSquare',
'fax',
'sync'
],
'free-regular-svg-icons': 'all',
}
}
})

/*
Expand Down
51 changes: 36 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,27 +83,49 @@ module.exports = {
]);
},

buildConfig() {
// 1. start with the host app configuration
// 2. If no icons are defined, automatically configure whatever is there under node_modules
// @TODO: look for any addons contributing config. maybe enumerated in this.app.options.addons
let addonOptions = (this.parent && this.parent.options) || (this.app && this.app.options) || {};
let fontawesomeConfig;
fontawesomeConfig = addonOptions['fontawesome'] || {
/**
* Read the configuration from `ember-cli-build` and `environment.js`
* and combine them with defaults to arrive at a complete configuration
*
* This work can't be done in the ember-cli `config` hook because it is run
* after `included` in some cases.
*/
readConfig() {
const config = this.app.project.config();
const appConfig = config['fontawesome'] || {};
const configDefaults = {
enableExperimentalBuildTimeTransform: false,
icons: {}
icons: {},
defaultPrefix: 'fas',
};
let buildConfig = {};

if (Object.keys(fontawesomeConfig.icons).length === 0) {
let addonOptions = (this.parent && this.parent.options) || (this.app && this.app.options) || {};
if ('fontawesome' in addonOptions) {
this.ui.writeWarnLine(`fontawesome is no longer configured in 'ember-cli-build.js'.
All configuration should be moved to 'environment.js'.
See https://github.com/FortAwesome/ember-fontawesome#subsetting-icons for details.
`);
buildConfig = addonOptions.fontawesome;
}

this.fontawesomeConfig = Object.assign(configDefaults, buildConfig, appConfig);
},

includeIconPackages() {
// 1. start with the host app configuration
// 2. If no icons are defined, automatically configure whatever is there under node_modules
// @TODO: look for any addons contributing config. maybe enumerated in this.app.options.addons
if (Object.keys(this.fontawesomeConfig.icons).length === 0) {
glob.sync('node_modules/@fortawesome/@(free|pro)-*-svg-icons')
.map(i => i.split('/').pop())
.reduce((acc, cur) => {
acc.icons[cur] = 'all'
return acc
}, fontawesomeConfig);
}, this.fontawesomeConfig);
}

if(Object.keys(fontawesomeConfig.icons).length === 0) {
if(Object.keys(this.fontawesomeConfig.icons).length === 0) {
this.ui.writeWarnLine(
'No icons are included in your build configuration.\n'+
'Any icon packs you install under node_modules will be bundled into vendor.js\n'+
Expand All @@ -129,7 +151,6 @@ module.exports = {
'});'
)
}
return fontawesomeConfig
},
included(app) {
this._super.included.apply(this, arguments)
Expand All @@ -141,9 +162,9 @@ module.exports = {
app = current.app || app;
} while (current.parent.parent && (current = current.parent));

this.app = app

this.fontawesomeConfig = this.buildConfig()
this.app = app;
this.readConfig();
this.includeIconPackages();

this.setupPreprocessorRegistryAfterConfiguration('parent', app.registry);

Expand Down
22 changes: 22 additions & 0 deletions tests/dummy/config/environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,28 @@ module.exports = function(environment) {
Date: false
}
},
fontawesome: {
defaultPrefix: 'fas',
enableExperimentalBuildTimeTransform: true,
icons: {
'free-solid-svg-icons': [
'coffee',
'magic',
'circle',
'square',
'home',
'info',
'book',
'pencil-alt',
'cog',
'spinner',
'checkSquare',
'fax',
'sync'
],
'free-regular-svg-icons': 'all',
}
},

APP: {
// Here you can pass flags/options to your application instance
Expand Down

0 comments on commit a8cda96

Please sign in to comment.