-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcbuild.ts
279 lines (215 loc) · 7.46 KB
/
cbuild.ts
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
// This file is part of cbuild, copyright (c) 2016 BusFaster Ltd.
// Released under the MIT license, see LICENSE.
import * as fs from 'fs';
import * as path from 'path';
import * as Promise from 'bluebird';
import * as Builder from 'systemjs-builder';
import * as resolve from 'browser-resolve';
export {BuildResult, BuildItem} from 'systemjs-builder';
/** Options object for the build function. */
export interface BuildOptions {
/** If true, set NODE_ENV to development. */
debug?: boolean;
/** If true, create static (sfx) bundle. */
sfx?: boolean;
/** Bundled file to output. */
bundlePath?: string;
/** Main source file to bundle. */
sourcePath?: string;
/** Output config mapping other package names to their main source files. */
outConfigPath?: string;
/** Map additional packages in output config. */
mapPackages?: string[];
}
function writeConfig(
configPath: string,
pathTbl: { [name: string]: string },
fixTbl: { [path: string]: string },
repoList: string[],
shimPath: string
) {
var sectionList: string[] = [];
var fixList = Object.keys(fixTbl);
// Output table mapping npm package names to their main entry points.
sectionList.push(
'\tmap: {\n' +
Object.keys(pathTbl).map((name: string) =>
'\t\t"' + name + '": "' + pathTbl[name] + '"'
).join(',\n') + '\n' +
'\t}'
);
// Output meta command to inject a global process variable to all files
// under all encountered node_modules trees.
sectionList.push(
'\tmeta: {\n' +
repoList.map((path: string) =>
'\t\t"' + path + '/*": { globals: { process: "' + shimPath + '" } }'
).join(',\n') + '\n' +
'\t}'
);
// Output a list of fixes to file paths, mainly to append index.js
// where a directory is being imported.
if(fixList.length) {
sectionList.push(
'\tpackages: {\n' +
'\t\t".": {\n' +
'\t\t\tmap: {\n' +
fixList.map((path: string) =>
'\t\t\t\t"' + path + '": "' + fixTbl[path] + '"'
).join(',\n') + '\n' +
'\t\t\t}\n' +
'\t\t}\n' +
'\t}'
);
}
var output = (
'System.config({\n' +
sectionList.join(',\n') + '\n' +
'});\n'
);
return(fs.writeFileSync(configPath, output, { encoding: 'utf-8' }));
}
function url2path(pathName: string) {
return(pathName.replace(/^file:\/\//, ''));
}
var resolveAsync = Promise.promisify(resolve);
/** Bundle files from package in basePath according to options. */
export function build(basePath: string, options?: BuildOptions) {
var builder = new Builder(basePath, 'config.js');
var pathTbl: { [name: string]: string } = {};
var fixTbl: { [path: string]: string } = {};
var repoTbl: { [path: string]: boolean } = {};
/** Find the main entry point to an npm package (considering package.json
* browser fields of the required and requiring packages). */
function findPackage(name: string, parentName: string) {
return(resolveAsync(name, { filename: parentName }).then((pathName: string) => {
if(pathName == name) throw(new Error('Internal module'));
pathName = path.relative(basePath, pathName);
// Store entry point path for this package name.
pathTbl[name] = pathName;
// Store path of top node_modules directory.
repoTbl[pathName.replace(/((\/|^)node_modules)\/.*/i, '$1')] = true;
return(pathName);
}));
}
options = options || {};
var bundlePath = options.bundlePath;
var sourcePath = options.sourcePath;
// If no entry point for bundling was given, use the browser or main field
// in package.json under the base directory.
if(!sourcePath) {
var packageJson = require(path.resolve(basePath, 'package.json'));
var browser = packageJson.browser;
if(typeof(browser) == 'string') sourcePath = browser;
else sourcePath = packageJson.main;
}
/** Old systemjs-builder normalize function which doesn't look for npm packages.
* See https://github.com/ModuleLoader/es6-module-loader/wiki/Extending-the-ES6-Loader */
var oldNormalize = builder.loader.normalize;
// Replace systemjs-builder normalize function adding support for
// npm packages and gathering information about paths needed for
// generating a SystemJS configuration file.
builder.loader.normalize = function(name, parentName, parentAddress) {
var pathName: string;
var indexName: string;
return(oldNormalize.call(this, name, parentName, parentAddress).then((result: string) => {
pathName = result;
return(Promise.promisify(fs.stat)(url2path(pathName)));
}).then((stats: fs.Stats) =>
pathName
).catch((err: NodeJS.ErrnoException) => {
indexName = pathName.replace(/.js$/, '/index.js');
return(
Promise.promisify(fs.stat)(
url2path(indexName)
).then((stats: fs.Stats) => {
fixTbl['./' + path.relative(basePath, url2path(pathName))] = './' + path.relative(basePath, url2path(indexName));
return(indexName);
}).catch((err: NodeJS.ErrnoException) =>
findPackage(name, url2path(parentName))
).catch((err: any) =>
pathName
)
);
}));
}
var built: Promise<Builder.BuildResult>;
// Run systemjs-builder.
if(options.sfx) {
if(bundlePath) built = builder.buildStatic(sourcePath, bundlePath, {});
else built = builder.buildStatic(sourcePath, {});
} else {
if(bundlePath) built = builder.bundle(sourcePath, bundlePath, {});
else built = builder.bundle(sourcePath, {});
}
return(built.then(() =>
// Add mappings to any extra packages listed in command line options.
Promise.map(options.mapPackages || [], (name: string) =>
findPackage(name, path.resolve(basePath, 'package.json'))
)
).then(() => {
// Restore original systemjs-builder normalize function.
builder.loader.normalize = oldNormalize;
if(options.outConfigPath) {
// Output SystemJS configuration file.
return(
resolveAsync(
options.debug ? 'cbuild/process-dev.js' : 'cbuild/process.js',
{ filename: path.resolve(basePath, 'package.json') }
).then((shimPath: string) =>
writeConfig(
options.outConfigPath,
pathTbl,
fixTbl,
Object.keys(repoTbl),
path.relative(basePath, shimPath)
)
)
);
}
}).then(() => built.value()));
}
/** Dependency tree branch, used for makeTree() output. */
export interface Branch extends Array<string | Branch> {
/** File name. */
0?: string
}
/** Extract a dependency tree from the build function result object.
* Returns a nameless root item.
* Each item is a list of a file name and its child items.
* Uses Breadth-First Search to print shortest import chain to each file. */
export function makeTree(result: Builder.BuildResult) {
var output: Branch = [''];
var queue: string[] = [];
var found: { [name: string]: Branch } = {};
function report(name: string, branch: Branch) {
if(!found[name]) {
var leaf: Branch = [name];
found[name] = leaf;
branch.push(leaf);
queue.push(name);
}
}
var entryPoints = result.entryPoints;
if(!entryPoints) {
// Bundling reported no entry points (maybe it's an sfx bundle).
// Create a table of all modules that were imported somehow.
var importedTbl: { [name: string]: boolean } = {};
for(var name of Object.keys(result.tree)) {
var item = result.tree[name];
for(var dep of item.deps) {
importedTbl[item.depMap[dep]] = true;
}
}
// Assume modules not imported by others are entry points.
entryPoints = Object.keys(result.tree).filter((name: string) => !importedTbl[name]);
}
for(var name of entryPoints) report(name, output);
while(queue.length) {
var name = queue.shift();
var branch = found[name];
var item = result.tree[name];
for(var dep of item.deps) report(item.depMap[dep], branch);
}
return(output);
}