-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
128 lines (102 loc) · 3.15 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
var path = require('path');
var fs = require('fs');
/** Map kebab-case module names to camelCase exported variable names. */
function camelize(name) {
return(name.replace(/-([a-z])/g, function(match) {
return(match[1].toUpperCase());
}));
}
/** Generate internal npm package descriptor. */
function newPackage(name, root, json) {
return({
pkgName: name,
varName: camelize(name),
// Package root path containing package.json.
root: root,
json: json || require(path.resolve(root, 'package.json')),
bundle: false
});
}
/** Generate rollup config object. */
function configure(config) {
config = config || {};
var root = config.root || '.';
var json = require(path.resolve(root, 'package.json'));
var packages = [];
// Add all external packages as possible dependencies.
var registry = {};
var names = {};
[].concat(
Object.keys(json.dependencies || {}),
Object.keys(json.peerDependencies || {})
).map(function(name) {
if((config.map || {})[name]) return;
try {
var spec = newPackage(name, path.resolve(root, 'node_modules', name));
registry[name] = spec;
names[name] = spec.varName;
} catch(err) {}
});
if(config.alle) {
// Find all packages under packages/node_modules or similar.
fs.readdirSync(path.resolve(config.alle, 'node_modules')).map(function(name) {
try {
var spec = newPackage(name, path.resolve(config.alle, 'node_modules', name));
registry[name] = spec;
names[name] = spec.varName;
packages.push(spec);
} catch(err) {}
});
} else {
// Add this package.
packages.push(newPackage(json.name, root, json));
}
// Note packages configured for inclusion in bundles.
(config.include || []).map(function(name) {
(registry[name] || {}).bundle = true;
});
// Resolve a package name for rollup.
function resolve(name, parent) {
var result = (config.map || {})[name];
if(result) return(path.resolve(result));
var parts = name.split('/');
var spec = registry[parts[0]];
if(spec) {
var json = spec.json;
return(path.resolve(spec.root, parts.slice(1).join('/') || json.module || json.main));
}
}
// Generate rollup config to bundle each internal package separately.
var rollupConfig = packages.map(function(spec) {
// Read internal package config.
var json = spec.json;
return({
// Start from ES module or main entry point.
input: path.resolve(spec.root, json.module || json.main),
// Treat all dependencies as external, not bundled in.
external: Object.keys(registry).filter(function(name) { return(!registry[name].bundle); }),
output: [
{
// Generate browser entry point script.
file: path.resolve(spec.root, json.browser),
// Use module name in camelCase as the variable to export.
name: camelize(json.name),
// Guess variable names exported by other packages.
globals: names,
format: 'umd'
}
],
plugins: [
{
resolveId: resolve
}
]
});
});
if((!module.parent || !module.parent.parent) && !config.silent) {
// If rollup config script was called directly, print generated config.
process.stdout.write(JSON.stringify(rollupConfig, null, ' '));
}
return(rollupConfig);
};
module.exports = configure;