forked from FortAwesome/ember-fontawesome
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
177 lines (159 loc) · 5.76 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
'use strict';
var broccoliSource = require('broccoli-source')
var UnwatchedDir = broccoliSource.UnwatchedDir
var MergeTrees = require('broccoli-merge-trees')
var Rollup = require('broccoli-rollup')
var resolve = require('rollup-plugin-node-resolve')
var FontAwesomePack = require('./vendor/broccoli-fontawesome-pack')
var FontAwesomeAutoLibrary = require('./vendor/broccoli-fontawesome-auto-library')
var glob = require('glob')
var buildAstTransform = require('./lib/ast-transform');
var writeFile = require('broccoli-file-creator');
const { config, dom } = require('@fortawesome/fontawesome-svg-core');
module.exports = {
name: '@fortawesome/ember-fontawesome',
fontawesomeConfig: null,
treeForVendor(vendorTree) {
const iconRollups = []
Object.keys(this.fontawesomeConfig.icons).forEach(pack => {
const iconExportsFile = `exports-${pack}.js`
const iconPackTree = new FontAwesomePack(
[new UnwatchedDir('node_modules/@fortawesome/fontawesome-svg-core')],
{
pack,
icons: this.fontawesomeConfig.icons[pack],
output: iconExportsFile
}
)
const rolledIconPackFile = `${pack}.js`
const rollupNode = new Rollup(iconPackTree, {
rollup: {
input: iconExportsFile,
output: {
file: rolledIconPackFile,
format: 'amd',
interop: false,
amd: {
id:`@fortawesome/${pack}`
}
},
plugins: [
resolve()
]
},
name: `${pack}-rollup`
})
iconRollups.push(rollupNode)
})
const fontawesomeRollup = new Rollup(new UnwatchedDir('node_modules/@fortawesome/fontawesome-svg-core'), {
rollup: {
input: 'index.es.js',
output: {
file: 'fontawesome.js',
exports: 'named',
format: 'amd',
amd: {
id:'@fortawesome/fontawesome-svg-core'
}
},
plugins: [
resolve()
]
},
name: 'fontawesome-svg-core'
})
const autoLibraryNode = new FontAwesomeAutoLibrary({
icons: this.fontawesomeConfig.icons,
output: 'autoLibrary.js'
})
const fontawesomeStyles = writeFile('fontawesome.css', dom.css());
return new MergeTrees([
vendorTree,
fontawesomeRollup,
autoLibraryNode,
...iconRollups,
fontawesomeStyles
]);
},
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'] || {
enableExperimentalBuildTimeTransform: false,
icons: {}
};
if (Object.keys(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);
}
if(Object.keys(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'+
'and added to the icon library by default.\n\n'+
"For example, 'npm install --save-dev @fortawesome/free-solid-svg-icons' would add all of the icons in that pack.\n\n"+
'To declare a subset of icons, after adding some icon packs as shown above,\n'+
'modify your ember-cli-build.js and add a fontawesome config object.\n'+
'The following example declares that all icons in free-solid-svg-icons should be\n'+
'included in the vendor.js bundle add added to the library,\n'+
'and for pro-light-svg-icons, only faAdjust and faAmbulance are to be included in the\n'+
'bundle and added to the library.\n'+
'// ...\n'+
'let app = new EmberApp(defaults, {\n'+
' // Add options here\n'+
' fontawesome: {\n'+
' icons: {\n'+
" 'free-solid-svg-icons': 'all'\n"+
" 'pro-light-svg-icons': [\n"+
" 'adjust',\n"+
" 'ambulance'\n"+
' ]\n'+
' }\n'+
'});'
)
}
return fontawesomeConfig
},
included(app) {
this._super.included.apply(this, arguments)
let current = this;
// Keep iterating upward until we don't have a grandparent.
// Has to do this grandparent check because at some point we hit the project.
do {
app = current.app || app;
} while (current.parent.parent && (current = current.parent));
this.app = app
this.fontawesomeConfig = this.buildConfig()
this.setupPreprocessorRegistryAfterConfiguration('parent', app.registry);
app.import('vendor/fontawesome.js')
Object.keys(this.fontawesomeConfig.icons).forEach(pack => {
app.import(`vendor/${pack}.js`)
})
app.import('vendor/autoLibrary.js')
config.autoAddCss = false;
app.import('vendor/fontawesome.css');
},
/**
* setupPreprocessorRegistry is called before included
* see https://github.com/ember-cli/ember-cli/issues/3701
* as a workaround we ignore that hook and call this method from included
*/
setupPreprocessorRegistryAfterConfiguration(type, registry) {
if (this.fontawesomeConfig.enableExperimentalBuildTimeTransform) {
registry.add('htmlbars-ast-plugin', {
name: 'font-awesome-static-transform',
plugin: buildAstTransform(this),
baseDir() {
return __dirname;
},
});
}
},
}