-
Notifications
You must be signed in to change notification settings - Fork 37
/
index.js
173 lines (146 loc) · 5.28 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
/* eslint-disable */
const path = require('path')
const fs = require('fs')
const mergeTrees = require('broccoli-merge-trees')
const Funnel = require('broccoli-funnel')
const inclusionFilter = require('./lib/inclusion-filter')
const exclusionFilter = require('./lib/exclusion-filter')
const UnwatchedDir = require('broccoli-source').UnwatchedDir
const existsSync = require('exists-sync')
module.exports = {
name: 'ember-d3',
allD3Modules(target) {
let deps = Object.values(target.project.packageInfoCache.entries).map(obj => obj.name || '')
let d3ModuleNames = deps.filter(dep => dep.startsWith('d3'))
let ui = this.ui
if (d3ModuleNames.indexOf('d3') === -1) {
ui.writeError(
"ERROR: Package d3 is not installed, please add it to your project's dependencies"
)
return []
} else {
d3ModuleNames = [...d3ModuleNames, ...dependenciesForPackage('d3')]
}
let paths = d3ModuleNames.map(name => {
let modulePath = resolveSync(name, true)
return { name, path: modulePath, basename: path.basename(modulePath) }
})
return paths
function dependenciesForPackage(depName) {
try {
let pkgPath = resolveSync(path.join(depName, 'package.json'))
return Object.keys(target.project.require(pkgPath).dependencies).filter(dep =>
dep.startsWith('d3-')
)
} catch (err) {
ui.writeError(
`ERROR: Package d3 is not installed (required by "${depName}"), please add it to your project's dependencies`
)
return []
}
}
function resolveSync(name, isBrowserTarget) {
if (isBrowserTarget === undefined) isBrowserTarget = false
return require('resolve').sync(name, {
basedir: target.project.root,
packageFilter(pkg, packageFileDir) {
if (isBrowserTarget) {
if (pkg.hasOwnProperty('unpkg')) {
// D3 publishes browser build at `unpkg`
pkg.main = undefined
pkg.main = pkg.unpkg
} else if (pkg.hasOwnProperty('jsdelivr')) {
// Some older d3 packages may only contain jsdelivr, which is browser
pkg.main = undefined
pkg.main = pkg.jsdelivr
} else if (pkg.hasOwnProperty('browser')) {
// Some older d3 packages (d3-request) may only contain browser directive
pkg.main = undefined
pkg.main = pkg.browser
}
// if an unminified version exists alongside the minified one, use that instead
let unminifiedPath = pkg.main.replace('.min.js', '.js')
if (pkg.main.endsWith('.min.js') && fs.existsSync(path.join(packageFileDir, unminifiedPath))) {
pkg.main = unminifiedPath
}
}
return pkg
}
})
}
},
filterD3Modules(modules, config) {
/**
* config.bundle will import all d3 packages as d3, this is identical to d3.js
*
* Cannot be used with any other options.
*/
if (config.bundle) {
return modules.filter(({ name }) => name === 'd3')
} else {
modules = modules.filter(({ name }) => name !== 'd3')
/**
* config.only accepts a list of packages to include from all included dependencies of d3.
*/
var onlyModules = config.only || []
/**
* config.except accepts a list of package name to exclude from the build,
* this is the opposite of only.
*/
var exceptModules = config.except || []
return modules.filter(inclusionFilter(onlyModules)).filter(exclusionFilter(exceptModules))
}
},
included(app) {
this._super.included.apply(this, arguments)
// 1. Find target app to locate d3 dependencies from
let target = findTargetHost(this, app)
// 2. Load app config for our addon
let config = app.project.config(app.env) || {}
let addonConfig = config[this.name] || {}
this.d3Modules = this.filterD3Modules(this.allD3Modules(target), addonConfig)
this.d3Modules.forEach(({ name, basename, path: modulePath }) => {
target.import(path.join('vendor', 'd3', basename), {
using: [{ transformation: 'amd', as: name }]
})
})
},
afterInstall() {
return this.addPackageToProject('d3')
},
treeForVendor() {
let trees = []
let modules = this.d3Modules
if (!modules) {
return mergeTrees(trees)
}
modules.forEach(({ name, path: modulePath, basename }) => {
let dir = path.dirname(modulePath)
trees.push(
Funnel(new UnwatchedDir(dir), {
files: [basename],
annotation: `basename`,
destDir: '/d3'
})
)
})
return mergeTrees(trees, { overwrite: true })
}
}
function findTargetHost(addon, app) {
let target = app
// If the addon has the _findHost() method (in ember-cli >= 2.7.0), we'll just
// use that.
if (typeof addon._findHost === 'function') {
target = addon._findHost()
}
// Otherwise, we'll use this implementation borrowed from the _findHost()
// method in ember-cli.
// Keep iterating upward until we don't have a grandparent.
// Has to do this grandparent check because at some point we hit the project.
let current = addon
do {
target = current.app || app
} while (current.parent.parent && (current = current.parent))
return target
}