]> git.proxmox.com Git - mirror_novnc.git/blame - utils/use_require_helpers.js
Use fat arrow functions `const foo = () => { ... };` for callbacks
[mirror_novnc.git] / utils / use_require_helpers.js
CommitLineData
6cae7b58 1// writes helpers require for vnc.html (they should output app.js)
2b5f94fa
JD
2const fs = require('fs');
3const path = require('path');
6cae7b58 4
21633268
PO
5// util.promisify requires Node.js 8.x, so we have our own
6function promisify(original) {
7 return function () {
2b5f94fa 8 const args = Array.prototype.slice.call(arguments);
21633268 9 return new Promise((resolve, reject) => {
651c23ec 10 original.apply(this, args.concat((err, value) => {
21633268
PO
11 if (err) return reject(err);
12 resolve(value);
13 }));
14 });
15 }
16}
17
18const writeFile = promisify(fs.writeFile);
19
6cae7b58
SR
20module.exports = {
21 'amd': {
4a65d50d 22 appWriter: (base_out_path, script_base_path, out_path) => {
6cae7b58 23 // setup for requirejs
2b5f94fa 24 const ui_path = path.relative(base_out_path,
4a65d50d 25 path.join(script_base_path, 'app', 'ui'));
651c23ec 26 return writeFile(out_path, `requirejs(["${ui_path}"], (ui) => {});`)
21633268 27 .then(() => {
4a65d50d 28 console.log(`Please place RequireJS in ${path.join(script_base_path, 'require.js')}`);
2b5f94fa 29 const require_path = path.relative(base_out_path,
4a65d50d
PO
30 path.join(script_base_path, 'require.js'))
31 return [ require_path ];
21633268 32 });
6cae7b58 33 },
152c3995 34 noCopyOverride: () => {},
6cae7b58
SR
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 },
4a65d50d 41 appWriter: (base_out_path, script_base_path, out_path) => {
2b5f94fa
JD
42 const browserify = require('browserify');
43 const b = browserify(path.join(script_base_path, 'app/ui.js'), {});
21633268 44 return promisify(b.bundle).call(b)
651c23ec 45 .then(buf => writeFile(out_path, buf))
4a65d50d 46 .then(() => []);
6cae7b58 47 },
152c3995 48 noCopyOverride: () => {},
be7b4e88 49 removeModules: true,
6cae7b58
SR
50 },
51 'systemjs': {
4a65d50d 52 appWriter: (base_out_path, script_base_path, out_path) => {
2b5f94fa 53 const ui_path = path.relative(base_out_path,
4a65d50d
PO
54 path.join(script_base_path, 'app', 'ui.js'));
55 return writeFile(out_path, `SystemJS.import("${ui_path}");`)
21633268 56 .then(() => {
4a65d50d
PO
57 console.log(`Please place SystemJS in ${path.join(script_base_path, 'system-production.js')}`);
58 // FIXME: Should probably be in the legacy directory
2b5f94fa 59 const promise_path = path.relative(base_out_path,
4a65d50d 60 path.join(base_out_path, 'vendor', 'promise.js'))
2b5f94fa 61 const systemjs_path = path.relative(base_out_path,
4a65d50d
PO
62 path.join(script_base_path, 'system-production.js'))
63 return [ promise_path, systemjs_path ];
21633268 64 });
152c3995
SR
65 },
66 noCopyOverride: (paths, no_copy_files) => {
67 no_copy_files.delete(path.join(paths.vendor, 'promise.js'));
6cae7b58
SR
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}