-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbuild.lib.js
More file actions
156 lines (146 loc) · 5.44 KB
/
build.lib.js
File metadata and controls
156 lines (146 loc) · 5.44 KB
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
'use strict';
const libName = require('./package.json').name;
const fs = require('fs');
const path = require('path');
const glob = require('glob');
const ngc = require('@angular/compiler-cli/src/main').main;
const rollup = require('rollup');
const uglify = require('rollup-plugin-uglify');
const babel = require('rollup-plugin-babel');
const sass = require('npm-sass');
const inlineStyles = require('./build.inline');
const rollupConfig = require('./build.rollup');
const rootFolder = path.join(__dirname);
const tscFolder = path.join(rootFolder, 'out-tsc');
const srcFolder = path.join(rootFolder, 'src');
const distFolder = path.join(rootFolder, 'dist');
const tempFolder = tscFolder; // path.join(tscFolder, 'lib');
const themeFolder = tempFolder;
const es2015Folder = path.join(tscFolder, 'es2015');
const es2015Entry = path.join(es2015Folder, 'index.js');
return Promise.resolve()
// Copy library to temporary folder and inline html/css.
.then(() => console.log(`copy: ${srcFolder}`))
.then(() => relativeCopy(`**/*`, srcFolder, tempFolder))
.then(() => console.log(`copy: succeeded`))
.then(() => console.log(`scss: ${themeFolder}`))
.then(() => {
const files = glob.sync('**/*.scss', { cwd: themeFolder });
return Promise.all(files.map(name =>
new Promise((resolve, reject) => {
const filePath = path.join(themeFolder, name);
sass(filePath, (err, result) => {
if (err) {
reject(err);
} else {
fs.writeFileSync(filePath.replace('.scss', '.css'), result.css);
resolve(result);
}
});
})
))
})
.then(() => console.log(`scss: success`))
.then(() => console.log(`inline: ${tempFolder}`))
.then(() => inlineStyles(tempFolder))
.then(() => console.log('inline: succeeded'))
// Compile to ES2015.
.then(() => console.log('ngc: tsconfig.es2015.json'))
.then(() => ngc(['--project', 'tsconfig.es2015.json']))
.then(code => code === 0 ? Promise.resolve() : Promise.reject())
.then(() => console.log('ngc es2015: succeeded'))
// Copy typings and metadata to `dist/` folder.
.then(() => console.log(`copy metadata: ${distFolder}`))
.then(() => relativeCopy('**/*.d.ts', es2015Folder, distFolder))
.then(() => relativeCopy('**/*.metadata.json', es2015Folder, distFolder))
.then(() => console.log('copy metadata: succeeded'))
// Bundle lib.
.then(() => console.log(`bundle es2015: ${libName}`))
.then(() => {
const cfg = Object.assign({}, rollupConfig, {
input: es2015Entry,
output: Object.assign({}, rollupConfig.output, {
file: path.join(distFolder, `bundles`, `${libName}.js`),
format: 'es'
})
});
return rollup.rollup(cfg).then(bundle => bundle.write(cfg.output));
})
.then(() => console.log('bundle es2015: succeeded'))
.then(() => console.log(`bundle umd: ${libName}`))
.then(() => {
const cfg = Object.assign({}, rollupConfig, {
input: es2015Entry,
output: Object.assign({}, rollupConfig.output, {
file: path.join(distFolder, `bundles`, `${libName}.umd.js`),
format: 'umd'
}),
plugins: rollupConfig.plugins.concat([babel({})])
});
return rollup.rollup(cfg).then(bundle => bundle.write(cfg.output));
})
.then(() => console.log('bundle umd: succeeded'))
.then(() => console.log(`bundle umd.min: ${libName}`))
.then(() => {
const cfg = Object.assign({}, rollupConfig, {
input: es2015Entry,
output: Object.assign({}, rollupConfig.output, {
file: path.join(distFolder, `bundles`, `${libName}.umd.min.js`),
format: 'umd'
}),
plugins: rollupConfig.plugins.concat([babel({}), uglify({})])
});
return rollup.rollup(cfg).then(bundle => bundle.write(cfg.output));
})
.then(() => console.log('bundle umd.min: succeeded'))
.then(() => console.log(`bundle es5: ${libName}`))
.then(() => {
const cfg = Object.assign({}, rollupConfig, {
input: es2015Entry,
output: Object.assign({}, rollupConfig.output, {
file: path.join(distFolder, `bundles`, `${libName}.es5.js`),
format: 'es'
}),
plugins: rollupConfig.plugins.concat([babel({})])
});
return rollup.rollup(cfg).then(bundle => bundle.write(cfg.output));
})
.then(() => console.log('bundle es5: succeeded'))
.then(() => console.log('bundle: successed'))
// Copy package files
.then(() => console.log('copy package: start'))
.then(() => relativeCopy('LICENSE', rootFolder, distFolder))
.then(() => relativeCopy('package.json', tscFolder, distFolder))
.then(() => relativeCopy('README.md', rootFolder, distFolder))
.then(() => console.log('copy package: success'))
.catch(ex => {
console.error('\nBuild failed. See below for errors.\n');
console.error(ex);
process.exit(1);
});
// Copy files maintaining relative paths.
function relativeCopy(fileGlob, from, to) {
return new Promise((resolve, reject) => {
glob(fileGlob, { cwd: from, nodir: true }, (err, files) => {
if (err) {
reject(err);
}
files.forEach(file => {
const origin = path.join(from, file);
const dest = path.join(to, file);
const data = fs.readFileSync(origin, 'utf-8');
makeFolderTree(path.dirname(dest));
fs.writeFileSync(dest, data);
console.log(`copy: ${file}`);
});
resolve();
})
});
}
// Recursively create a dir.
function makeFolderTree(dir) {
if (!fs.existsSync(dir)) {
makeFolderTree(path.dirname(dir));
fs.mkdirSync(dir);
}
}