forked from DataTables/DataTablesSrc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wrapper.js
197 lines (160 loc) · 3.56 KB
/
wrapper.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
/**
* Create a loader wrapper around Javascript files
*/
const fs = require('fs');
function usage() {
console.log(`
JS wrapper script - to create a UMD or ES loader for DataTables and its extensions
Use:
node wrapper.js inputFile es|umd outputFile dependency1 dependency2 ...
`);
}
function main(args) {
if (args.length < 5) {
usage();
return;
}
let file = '';
let script = '';
let deps = [];
let modType = '';
let extn = '';
try {
script = fs.readFileSync(args[2], 'utf8');
} catch (e) {
console.error('File ' + args[2] + ' does not exist');
return;
}
for (let i=5 ; i<args.length ; i++) {
deps.push(args[i]);
}
let exp = exportFromPath(args[2]);
let scriptParts = breakScript(script);
if (args[3] === 'es') {
file = es(scriptParts, deps, exp);
modType = 'ES module';
extn = '.mjs';
}
else if (args[3] === 'umd') {
file = umd(scriptParts, deps, exp);
modType = 'UMD';
extn = '.js';
}
else {
console.error('Output module must be `es` or `umd`');
return;
}
// arg4 write to file
// console.log(file);
fs.writeFileSync(args[4] + extn, file);
console.log('Written ' + modType + ' loader to file ' + args[4] + extn);
}
function es(script, deps, exp) {
let imports = [];
for (let dep of deps) {
let alias = nameFromDependency(dep);
imports.push(`import ${alias} from '${dep}';`);
}
// For testing uncomment to see the basic structure of the generated file
// script.main = '';
return `
${script.header}
${imports.join('\n')}
${script.main}
export default ${exp};
`
}
function umd(script, deps, exp) {
let amd = deps.map(l => `'${l}'`).join(', ');
let commonjs = [];
let defineDataTable = '';
commonjs.push(` if ( ! root ) {
// CommonJS environments without a window global must pass a
// root. This will give an error otherwise
root = window;
}
`);
for (let dep of deps) {
if (dep === 'jquery') {
commonjs.push(`
if ( ! $ ) {
$ = typeof window !== 'undefined' ? // jQuery's factory checks for a global window
require('jquery') :
require('jquery')( root );
}
`);
}
else if (nameFromDependency(dep) === 'DataTable') {
commonjs.push(`
if ( ! $.fn.dataTable ) {
require('${dep}')(root, $);
}
`);
}
else {
let name = nameFromDependency(dep);
defineDataTable = '\nvar DataTable = $.fn.dataTable;';
commonjs.push(`
if ( ! $.fn.dataTable.${name} ) {
require('${dep}')(root, $);
}
`);
}
}
return `
${script.header}
(function( factory ){
if ( typeof define === 'function' && define.amd ) {
// AMD
define( [${amd}], function ( $ ) {
return factory( $, window, document );
} );
}
else if ( typeof exports === 'object' ) {
// CommonJS
module.exports = function (root, $) {
${commonjs.join('')}
return factory( $, root, root.document );
};
}
else {
// Browser
factory( jQuery, window, document );
}
}(function( $, window, document, undefined ) {
'use strict';${defineDataTable}
${script.main}
return ${exp};
}));
`;
}
function nameFromDependency(dep) {
let name = dep.toLowerCase();
if (name.includes('autofill')) {
return 'AutoFill';
}
else if (name.includes('jquery')) {
return '$';
}
else {
return 'DataTable';
}
}
function exportFromPath(path) {
let parts = path.split('/');
let name = parts[parts.length - 1].toLowerCase();
if (name.includes('autofill')) {
return 'AutoFill';
}
else {
return 'DataTable';
}
}
function breakScript(script) {
let match = script.match(/\/\*![\s\S]+?\*\//);
return {
header: match ? match[0] : '',
main: match ? script.replace(match[0], '') : script
};
}
main(process.argv);