-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
118 lines (98 loc) · 3.54 KB
/
build.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
'use strict';
const getPath = require('path').resolve;
const copy = require('cpy');
const deleteFolders = require('del');
const fs = require('fs-extra');
const inlineResources = require('angular-inline-resources');
const ngc = require('@angular/compiler-cli/src/main').main;
const camelcase = require('camelcase');
const log = console.log;
const pkg = require('./src/lib/package.json');
const rollup = require('rollup');
const sourcemaps = require('rollup-plugin-sourcemaps');
const uglify = require('rollup-plugin-uglify');
return Promise.resolve()
// Delete output folders
.then(() => deleteFolders(['tmp', 'build', 'dist']))
// Copy sources to tmp folder
.then(() => fs.copy('src/lib', 'tmp/lib'))
// Inline angular templates and styleUrls for compilation
.then(() => inlineResources('tmp'))
.then(() => log('✓ Inline angular sources'))
// Compile to es2015
.then(() => ngc({project: `tmp/lib/tsconfig.json`}))
.then(exitCode => exitCode === 0 ? Promise.resolve() : Promise.reject())
.then(() => log('✓ Compile to es2015'))
// Compile to es5
.then(() => ngc({project: `tmp/lib/tsconfig.es5.json`}))
.then(exitCode => exitCode === 0 ? Promise.resolve() : Promise.reject())
.then(() => log('✓ Compile to es5'))
// Delete tmp
.then(() => deleteFolders(['tmp']))
// Copy resources
.then(() => Promise.resolve()
.then(() => copy('**/*.d.ts', getPath('dist'), { cwd: getPath('build/es2015'), parents: true }))
.then(() => copy('**/*.metadata.json', getPath('dist'), { cwd: getPath('build/es2015'), parents: true }))
.then(() => copy('LICENSE', getPath('dist'), { cwd: getPath('.') }))
.then(() => copy('README.md', getPath('dist'), { cwd: getPath('.') }))
.then(() => copy('package.json', getPath('dist'), { cwd: getPath('src/lib') }))
.then(() => console.log('✓ Copy typings, metadata, and other resources to dist folder'))
)
.then(() => {
// Generate bundles (using rollup)
const es5Entry = getPath(`build/es5/${pkg.name}.js`);
const es2015Entry = getPath(`build/es2015/${pkg.name}.js`);
const rollupBaseConfig = {
moduleName: camelcase(pkg.name),
sourceMap: true,
// Add below the peer dependencies
// of library's package.json
globals: {
'@angular/core': 'ng.core'
},
external: [
'@angular/core'
],
plugins: [
sourcemaps()
]
};
// fesm5 module
const fesm5Config = Object.assign({}, rollupBaseConfig, {
entry: es5Entry,
dest: getPath(`dist/${pkg.name}.es5.js`),
format: 'es'
});
// fesm2015
const fesm2015Config = Object.assign({}, rollupBaseConfig, {
entry: es2015Entry,
dest: getPath(`dist/${pkg.name}.js`),
format: 'es'
});
// umd
const umdConfig = Object.assign({}, rollupBaseConfig, {
entry: es5Entry,
dest: getPath(`dist/bundles/${pkg.name}.umd.js`),
format: 'umd',
});
// umd minified
const umdConfigMinified = Object.assign({}, rollupBaseConfig, {
entry: es5Entry,
dest: getPath(`dist/bundles/${pkg.name}.umd.min.js`),
format: 'umd',
plugins: rollupBaseConfig.plugins.concat([uglify({})])
});
const arrayConfig = [
fesm5Config,
fesm2015Config,
umdConfig,
umdConfigMinified,
].map(conf => rollup.rollup(conf).then(bundle => bundle.write(conf)));
return Promise.all(arrayConfig)
.then(() => console.log('✓ Generation of bundles'))
})
.catch(e => {
console.error(e);
console.error('\nBuild failed. Exiting process.');
process.exit(1);
});