-
Notifications
You must be signed in to change notification settings - Fork 2
/
jsbundle.js
176 lines (157 loc) · 6.43 KB
/
jsbundle.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
//
// Copyright (c) ZeroC, Inc. All rights reserved.
//
const PluginError = require('plugin-error');
const rollup = require("rollup");
const through = require("through2");
const path = require("path");
const Vinyl = require('vinyl');
const {PLUGIN_NAME, copyright} = require("./util");
function jsbundle(options)
{
const inputs = [];
let modules;
const format = options.jsbundleFormat || "es";
const sourcemap = options.jsbundleSourcemap === undefined ? true : options.jsbundleSourcemap;
return through.obj(
function(file, enc, cb)
{
if(file.path == "module.json")
{
modules = JSON.parse(file.contents.toString().replace(/\\/g, "\\\\"));
}
if(!file.path.endsWith(".js"))
{
//
// Let non JavaScript files pass-through
//
cb(null, file);
}
else
{
inputs.push(file);
cb();
}
},
function(cb)
{
if(inputs.length == 0 || modules === undefined)
{
cb();
}
else
{
//
// Create a bundle for each js:module and a default bundle for files doesn't
// belong to any module
//
const all = [];
const names = modules.map(m => m.module).filter((v, i, a) => a.indexOf(v) == i);
for(const key of names)
{
const files = modules.filter(m => m.module == key && m.file.endsWith(".js")).map(m => m.file);
let data = "";
for(const f of files)
{
data += `\nexport * from "./${f}";`;
}
const minputs = inputs.filter(f1 => files.find(f2 => path.resolve(f1.path) == path.resolve(f2)));
const input = key == "" ? "generated.js" : `${key}.js`;
let file = new Vinyl({cwd: "./", path: input});
file.contents = Buffer.from(data);
minputs.push(file);
const globals = {};
//
// Allow to resolve JavaScript files in the gulp stream
//
function resolve()
{
return {
name: "ice-bundle",
resolveId: function(id, importer)
{
//
// IDs not resolved in the bundle imputs are considered
// externals returning false from resolveId make rollup
// treat them as externals
//
let target = path.resolve(id);
if(!path.basename(target).endsWith(".js"))
{
target += ".js";
}
if(minputs.find(f => path.resolve(f.path) === target))
{
return target;
}
//
// Add external module imports to the bundle globals.
//
if(!id.startsWith(".") && !id.endsWith(".js"))
{
globals[id] = id;
}
return false;
},
load: function(id)
{
let target = path.resolve(id);
if(!path.basename(target).endsWith(".js"))
{
target += ".js";
}
const file = minputs.find(f => path.resolve(f.path) === target);
if(file)
{
return file.contents.toString();
}
}
};
}
let p = rollup.rollup(
{
input: input,
plugins: [resolve(minputs)],
onwarn: warn => {
if(warn.code == 'NAMESPACE_CONFLICT')
{
return;
}
console.error(warn.message);
}
});
p = p.then(bundle => bundle.generate(
{
format: format,
sourcemap: sourcemap,
globals: globals,
name: path.basename(input, ".js")
}));
p = p.then(bundle =>
{
const {code, map} = bundle.output[0];
file = new Vinyl({cwd: "./", path: input});
file.contents = Buffer.from(`${copyright}\n${code}`);
this.push(file);
if(sourcemap)
{
file = new Vinyl({cwd: "./", path: `${input}.map`});
file.contents = Buffer.from(JSON.stringify(map, null, 4));
this.push(file);
}
});
all.push(p);
}
Promise.all(all).then(
() =>
{
cb();
},
err =>
{
cb(new PluginError(PLUGIN_NAME, `error generating JavaScript bundle: ${err}`));
});
}
});
}
module.exports = jsbundle;