]> git.proxmox.com Git - mirror_novnc.git/blob - utils/use_require_helpers.js
Use fat arrow functions `const foo = () => { ... };` for callbacks
[mirror_novnc.git] / utils / use_require_helpers.js
1 // writes helpers require for vnc.html (they should output app.js)
2 const fs = require('fs');
3 const path = require('path');
4
5 // util.promisify requires Node.js 8.x, so we have our own
6 function promisify(original) {
7 return function () {
8 const args = Array.prototype.slice.call(arguments);
9 return new Promise((resolve, reject) => {
10 original.apply(this, args.concat((err, value) => {
11 if (err) return reject(err);
12 resolve(value);
13 }));
14 });
15 }
16 }
17
18 const writeFile = promisify(fs.writeFile);
19
20 module.exports = {
21 'amd': {
22 appWriter: (base_out_path, script_base_path, out_path) => {
23 // setup for requirejs
24 const ui_path = path.relative(base_out_path,
25 path.join(script_base_path, 'app', 'ui'));
26 return writeFile(out_path, `requirejs(["${ui_path}"], (ui) => {});`)
27 .then(() => {
28 console.log(`Please place RequireJS in ${path.join(script_base_path, 'require.js')}`);
29 const require_path = path.relative(base_out_path,
30 path.join(script_base_path, 'require.js'))
31 return [ require_path ];
32 });
33 },
34 noCopyOverride: () => {},
35 },
36 'commonjs': {
37 optionsOverride: (opts) => {
38 // CommonJS supports properly shifting the default export to work as normal
39 opts.plugins.unshift("add-module-exports");
40 },
41 appWriter: (base_out_path, script_base_path, out_path) => {
42 const browserify = require('browserify');
43 const b = browserify(path.join(script_base_path, 'app/ui.js'), {});
44 return promisify(b.bundle).call(b)
45 .then(buf => writeFile(out_path, buf))
46 .then(() => []);
47 },
48 noCopyOverride: () => {},
49 removeModules: true,
50 },
51 'systemjs': {
52 appWriter: (base_out_path, script_base_path, out_path) => {
53 const ui_path = path.relative(base_out_path,
54 path.join(script_base_path, 'app', 'ui.js'));
55 return writeFile(out_path, `SystemJS.import("${ui_path}");`)
56 .then(() => {
57 console.log(`Please place SystemJS in ${path.join(script_base_path, 'system-production.js')}`);
58 // FIXME: Should probably be in the legacy directory
59 const promise_path = path.relative(base_out_path,
60 path.join(base_out_path, 'vendor', 'promise.js'))
61 const systemjs_path = path.relative(base_out_path,
62 path.join(script_base_path, 'system-production.js'))
63 return [ promise_path, systemjs_path ];
64 });
65 },
66 noCopyOverride: (paths, no_copy_files) => {
67 no_copy_files.delete(path.join(paths.vendor, 'promise.js'));
68 },
69 },
70 'umd': {
71 optionsOverride: (opts) => {
72 // umd supports properly shifting the default export to work as normal
73 opts.plugins.unshift("add-module-exports");
74 },
75 },
76 }