This repository was archived by the owner on Apr 17, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrollup.ts
204 lines (181 loc) · 6.23 KB
/
rollup.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
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'fs';
import * as path from 'path';
import * as glob from 'glob';
import * as camelCase from 'camelcase';
import { main as ngc } from '@angular/compiler-cli/src/main';
import * as rollup from 'rollup';
import * as commonJs from 'rollup-plugin-commonjs';
import * as sourceMaps from 'rollup-plugin-sourcemaps';
import * as resolve from 'rollup-plugin-node-resolve';
import * as uglify from 'rollup-plugin-uglify';
import { NODE_MODULES, root } from './helpers';
import { inlineResources } from './inline-resources';
const compilationFolder = root('.temp');
let globals = {
tslib: 'tslib',
'@angular/core': 'ng.core'
};
const relativeCopy = (fileGlob: string, from: string, to: string) => {
return new Promise((res, reject) => {
glob(fileGlob, {
cwd: from,
nodir: true
}, (err: string, files: Array<any>) => {
if (err)
reject(err);
for (const file of files) {
if (file.indexOf(NODE_MODULES) >= 0)
continue;
const origin = path.join(from, file);
const destination = path.join(to, file);
const data = readFileSync(origin, 'utf-8');
recursiveMkDir(path.dirname(destination));
writeFileSync(destination, data);
}
res();
});
});
};
const recursiveMkDir = (dir: string) => {
if (!existsSync(dir)) {
recursiveMkDir(path.dirname(dir));
mkdirSync(dir);
}
};
import * as buildConfig from './build-config.json';
const build = (group: string, item: string, settings: any) => {
const paths = {
src: root(`packages/${group}/${item}`),
temp: path.join(compilationFolder, `packages/${group}/${item}`),
es2015: path.join(compilationFolder, `packages/${group}/${item}-es2015`),
es5: path.join(compilationFolder, `packages/${group}/${item}-es5`),
dist: root(`dist/${group}/${item}`)
};
globals = {
...globals,
...settings.bundle.globals
};
const external = settings.bundle.external
? settings.bundle.external.concat(Object.keys(globals))
: Object.keys(globals);
return Promise.resolve()
.then(() => relativeCopy('**/*', paths.src, paths.temp)
.then(() => inlineResources(paths.temp))
// tslint:disable-next-line
.then(() => console.log(`>>> ${group}/${item}: Inlining succeeded`))
)
.then(() => ngc(['--project', `${paths.temp}/tsconfig.es2015.json`]))
.then(exitCode => new Promise((res, reject) => {
exitCode === 0
? res()
: reject();
}))
// tslint:disable-next-line
.then(() => console.log(`>>> ${group}/${item}: ES2015 compilation succeeded`))
.then(() => ngc(['--project', `${paths.temp}/tsconfig.es5.json`]))
.then(exitCode => new Promise((res, reject) => {
exitCode === 0
? res()
: reject();
}))
// tslint:disable-next-line
.then(() => console.log(`>>> ${group}/${item}: ES5 compilation succeeded`))
.then(() => Promise.resolve()
.then(() => relativeCopy('**/*.d.ts', paths.es2015, paths.dist))
.then(() => relativeCopy('**/*.metadata.json', paths.es2015, paths.dist))
// tslint:disable-next-line
.then(() => console.log(`>>> ${group}/${item}: Typings and metadata copy succeeded`))
)
.then(() => {
const es5Entry = path.join(paths.es5, `${settings.angularVersion > 2 ? item : 'index'}.js`);
const es2015Entry = path.join(paths.es2015, `${settings.angularVersion > 2 ? item : 'index'}.js`);
const rollupBaseConfig = {
moduleName: `${camelCase(group.replace(/@/g, ''))}.${camelCase(item)}`,
sourceMap: true,
globals,
external,
plugins: [
resolve({
module: true,
jsnext: true
}),
commonJs({
include: ['node_modules/rxjs/**']
}),
sourceMaps()
]
};
const umdConfig = {
...rollupBaseConfig,
entry: es5Entry,
dest: path.join(paths.dist, 'bundles', `${item}.umd.js`),
format: 'umd'
};
const minUmdConfig = {
...rollupBaseConfig,
entry: es5Entry,
dest: path.join(paths.dist, 'bundles', `${item}.umd.min.js`),
format: 'umd',
plugins: rollupBaseConfig.plugins.concat([uglify({})])
};
const es5config = {
...rollupBaseConfig,
entry: es5Entry,
dest: path.join(paths.dist, `${group}/${item}.es5.js`),
format: 'es'
};
const es2015config = {
...rollupBaseConfig,
entry: es2015Entry,
dest: path.join(paths.dist, `${group}/${item}.js`),
format: 'es'
};
const bundles = [
umdConfig,
minUmdConfig,
es5config,
es2015config
]
.map(options => rollup.rollup(options)
.then((bundle: any) => bundle.write(options)));
return Promise.all(bundles)
// tslint:disable-next-line
.then(() => console.log(`>>> ${group}/${item}: All bundles generated successfully`));
})
.then(() => Promise.resolve()
.then(() => relativeCopy('LICENSE', root(), paths.dist))
.then(() => relativeCopy('package.json', paths.src, paths.dist))
.then(() => relativeCopy('README.md', paths.src, paths.dist))
// tslint:disable-next-line
.then(() => console.log(`>>> ${group}/${item}: Package files copy succeeded`))
// tslint:disable-next-line
.then(() => console.log(`\n`))
)
.catch(e => {
console.error(`>>> ${group}/${item}: Build failed, see below for errors\n`);
console.error(e);
process.exit(1);
});
};
const libs = [];
for (const group of Object.keys(buildConfig))
if (group !== '__once__')
for (const item of Object.keys(buildConfig[group]))
libs.push({
group,
item,
settings: buildConfig[group][item]
});
else
libs.push({
group: '',
item: Object.keys(buildConfig[group])[0],
settings: buildConfig[group][Object.keys(buildConfig[group])[0]]
});
const toSeries = (series: any) => series
.reduce((promise: Promise<any>, fn: Function) => promise
.then((res: any) => fn()
.then(Array.prototype.concat.bind(res))), Promise.resolve([]));
const builds = libs
.map((lib: any) => () => build(lib.group, lib.item, lib.settings));
toSeries(builds);