-
Notifications
You must be signed in to change notification settings - Fork 200
/
Copy pathbuild.js
127 lines (119 loc) · 3.29 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
119
120
121
122
123
124
125
126
127
const fs = require('fs');
const del = require('del');
const rollup = require('rollup');
const babel = require('rollup-plugin-babel');
const uglify = require('rollup-plugin-uglify');
const pkg = require('./package.json');
const typescript = require('rollup-plugin-typescript2');
var sass = require('node-sass');
var autoprefixer = require('autoprefixer');
var cssnano = require('cssnano');
var postcss = require('postcss');
const bundles = [
{
format: 'es',
plugins: [],
babelPresets: ['stage-1'],
file: pkg.module,
input: 'src/index.ts',
},
{
format: 'cjs',
plugins: [],
babelPresets: ['stage-1'],
file: pkg.main,
input: 'src/index.ts',
},
{
format: 'iife',
plugins: [uglify.uglify()],
babelPresets: ['es2015-rollup', 'stage-1'],
babelPlugins: [],
name: 'Notyf',
minify: true,
file: 'notyf.min.js',
input: 'src/notyf.ts',
},
{
format: 'umd',
babelPresets: ['es2015-rollup', 'stage-1'],
babelPlugins: [],
name: 'Notyf',
file: 'notyf.umd.js',
input: 'src/notyf.ts',
},
];
let promise = Promise.resolve();
// Clean up the output directory
promise = promise.then(() => del(['dist/*']));
// Compile source code into a distributable format with Babel and Rollup
for (const config of bundles) {
promise = promise.then(() =>
rollup
.rollup({
input: config.input,
external: [...Object.keys(pkg.dependencies || {}), ...Object.keys(pkg.peerDependencies || {})],
plugins: [
babel({
babelrc: false,
exclude: 'node_modules/**',
presets: config.babelPresets,
plugins: config.babelPlugins,
}),
typescript({
typescript: require('typescript'),
}),
].concat(config.plugins),
})
.then((bundle) => {
compileSass();
return bundle.write({
file: 'dist/' + config.file,
format: config.format,
sourceMap: !config.minify,
...(config.name && { name: config.name }),
});
}),
);
}
// Copy package.json and LICENSE.md
promise = promise.then(() => {
delete pkg.private;
delete pkg.prettier;
delete pkg.devDependencies;
delete pkg.scripts;
fs.writeFileSync('dist/package.json', JSON.stringify(pkg, null, ' '), 'utf-8');
fs.writeFileSync('dist/LICENSE.md', fs.readFileSync('LICENSE.md', 'utf-8'), 'utf-8');
fs.writeFileSync('dist/README.md', fs.readFileSync('README.md', 'utf-8'), 'utf-8');
});
promise.catch((err) => console.error(err.stack));
const stylesSrc = 'src/notyf.scss';
const stylesMin = 'dist/notyf.min.css';
function compileSass() {
sass.render(
{
file: stylesSrc,
outFile: stylesMin,
},
function (error, result) {
if (!error) {
postcss([autoprefixer, cssnano])
.process(result.css, {
from: stylesSrc,
to: stylesMin,
})
.then(function (result) {
result.warnings().forEach(function (warn) {
console.warn(warn.toString());
});
// No errors during the compilation, write this result on the disk
fs.writeFile(stylesMin, result.css, function (err) {
if (err) {
console.warn(err);
}
});
});
}
},
);
}