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