forked from sciactive/pnotify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
make.js
264 lines (227 loc) · 7 KB
/
make.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
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#!/user/bin/env nodejs
'use strict';
/* eslint-env shelljs */
const fs = require('fs');
require('shelljs/make');
let pnotifySrc = {
// Main code.
'core': 'PNotify.html',
'animate': 'PNotifyAnimate.html',
'buttons': 'PNotifyButtons.html',
'callbacks': 'PNotifyCallbacks.html',
'nonblock': 'PNotifyNonBlock.html',
'mobile': 'PNotifyMobile.html',
'history': 'PNotifyHistory.html',
'desktop': 'PNotifyDesktop.html',
'confirm': 'PNotifyConfirm.html',
// Compat module.
'compat': 'PNotifyCompat.js',
// Styles.
'stylematerial': 'PNotifyStyleMaterial.html',
// Reference module.
'reference': 'PNotifyReference.html'
};
let pnotifyJs = {
// Main code.
'core': 'PNotify.js',
'animate': 'PNotifyAnimate.js',
'buttons': 'PNotifyButtons.js',
'callbacks': 'PNotifyCallbacks.js',
'nonblock': 'PNotifyNonBlock.js',
'mobile': 'PNotifyMobile.js',
'history': 'PNotifyHistory.js',
'desktop': 'PNotifyDesktop.js',
'confirm': 'PNotifyConfirm.js',
// Compat module.
'compat': 'PNotifyCompat.js',
// Styles.
'stylematerial': 'PNotifyStyleMaterial.js',
// Reference module.
'reference': 'PNotifyReference.js'
};
let pnotifyCss = {
'brighttheme': 'PNotifyBrightTheme.css'
};
for (let module in pnotifySrc) {
target[module + '_lib'] = (args) => compileJs(module, pnotifySrc[module], args);
((target) => {
const existing = target[module];
target[module] = (args) => {
existing && existing(args);
target[module + '_lib'](args);
};
})(target);
}
for (let module in pnotifyJs) {
target[module + '_js'] = (args) => compressJs(module, pnotifyJs[module], args);
((target) => {
const existing = target[module];
target[module] = (args) => {
existing && existing(args);
target[module + '_js'](args);
};
})(target);
}
for (let module in pnotifyCss) {
target[module + '_css'] = () => compressCss(module, pnotifyCss[module]);
((target) => {
const existing = target[module];
target[module] = (args) => {
existing && existing(args);
target[module + '_css'](args);
};
})(target);
}
target.dist = (args) => {
for (let module in pnotifySrc) {
target[module + '_lib'](args);
}
for (let module in pnotifyJs) {
target[module + '_js'](args);
}
for (let module in pnotifyCss) {
target[module + '_css'](args);
}
};
// Functions
let compileJs = (module, filename, args) => {
let format = setup(args);
const srcFilename = 'src/' + filename;
const dstFilename = 'lib/' + format + '/' + filename.replace(/\.html$/, '.js');
console.log('Compiling JavaScript ' + module + ' from ' + srcFilename + ' to ' + dstFilename);
console.log('Generating source map for ' + dstFilename + ' in ' + dstFilename + '.map');
// Gather code.
let code;
let map;
let inputCode;
let inputMap;
let isSvelte = filename.slice(-4) === 'html';
inputCode = code = fs.readFileSync(srcFilename, 'utf8');
inputMap = map = null;
// Pre-compile transforms.
if (module === 'compat' && format === 'iife') {
inputCode = code = code.replace(/import PNotify(\w*) from ["']\.\/PNotify(\w*)\.html["'];/g, 'var PNotify$1 = window.PNotify$2;');
inputCode = code = code.replace(/export default PNotifyCompat;/g, 'window.PNotifyCompat = PNotifyCompat;');
}
// Compile.
if (isSvelte) {
// Use Svelte to compile the code first.
const svelte = require('svelte');
const { js } = svelte.compile(code, {
format: format,
filename: srcFilename,
name: filename.replace(/\.html$/, ''),
amd: {
id: filename.replace(/\.html$/, '')
},
globals: {
'./PNotify.html': 'PNotify'
},
onerror: err => {
console.error(err);
},
onwarn: warning => {
console.warn(warning);
},
css: true,
cascade: false
});
({ code, map } = js);
[inputCode, inputMap] = [code, map];
inputMap.file = filename.replace(/\.html$/, '.js');
inputCode += '\n//# sourceMappingURL=' + filename.replace(/\.html$/, '.js') + '.map';
}
if (format !== 'es') {
const babel = require('babel-core');
const babelOptions = {
moduleId: filename.replace(/\.(html|js)$/, ''),
filename: filename.replace(/\.html$/, '.js'),
filenameRelative: srcFilename,
sourceMapTarget: srcFilename,
moduleRoot: '',
sourceMaps: 'both',
sourceRoot: '../',
plugins: [
'transform-object-assign'
],
sourceType: (format !== 'es' && isSvelte) ? 'script' : 'module'
};
if (inputMap) {
babelOptions.inputSourceMap = inputMap;
}
if (format === 'umd' && !isSvelte) {
babelOptions.passPerPreset = true;
babelOptions.presets = [
['env', {
modules: 'umd'
}],
'stage-3'
];
babelOptions.plugins.push('add-module-exports');
} else {
babelOptions.presets = [
['env', {
modules: false
}],
'stage-3'
];
}
({ code, map } = babel.transform(inputCode, babelOptions));
}
// Post-compile transforms.
if (format === 'es') {
code = code.replace(/import PNotify(\w*) from ["']\.\/PNotify(\w*)\.html["'];/g, 'import PNotify$1 from "./PNotify$2.js";');
}
if (format === 'umd') {
code = code.replace(/require\(["']\.\/PNotify(\w*)?\.html["']\)/g, 'require(\'./PNotify$1\')');
code = code.replace(/, ["']\.\/PNotify(\w*)?\.html["']/g, ', \'./PNotify$1\'');
}
fs.writeFileSync(dstFilename, code);
if (map) {
fs.writeFileSync(dstFilename + '.map', JSON.stringify(map));
}
};
let compressJs = (module, filename, args) => {
let format = setup(args);
const srcFilename = 'lib/' + format + '/' + filename;
const dstFilename = 'dist/' + format + '/' + filename;
console.log('Compressing JavaScript ' + module + ' from ' + srcFilename + ' to ' + dstFilename);
console.log('Generating source map for ' + dstFilename + ' in ' + dstFilename + '.map');
const UglifyJS = format === 'es' ? require('uglify-es') : require('uglify-js');
const options = {
sourceMap: {
root: '../',
filename: filename,
url: filename + '.map'
}
};
const { code, map, error } = UglifyJS.minify({
[filename]: fs.readFileSync(srcFilename, 'utf8')
}, options);
if (!code) {
console.log('error:', error);
}
fs.writeFileSync(dstFilename, code);
if (map) {
fs.writeFileSync(dstFilename + '.map', map);
}
};
let compressCss = (module, filename) => {
setup();
const srcFilename = 'src/' + filename;
const dstFilename = 'dist/' + filename;
console.log('Compressing CSS ' + module + ' from ' + srcFilename + ' to ' + dstFilename);
const CleanCSS = require('clean-css');
const options = {
rebase: false
};
fs.writeFileSync(dstFilename, (new CleanCSS(options).minify(fs.readFileSync(srcFilename, 'utf8'))).styles);
};
let setup = (args) => {
let format = 'iife';
(args || []).filter(arg => arg.match(/^--format=/)).map(arg => (format = arg.slice(9)));
cd(__dirname);
mkdir('-p', 'lib/' + (args ? format : ''));
mkdir('-p', 'dist/' + (args ? format : ''));
return format;
};