]> git.proxmox.com Git - mirror_novnc.git/blobdiff - utils/use_require_helpers.js
Always include Promise polyfill for legacy browsers
[mirror_novnc.git] / utils / use_require_helpers.js
index 990fb4df2481331e5998c7b91c39a377bba3dd40..ec5da0d1924b16de8070544c23c55982074780d5 100644 (file)
@@ -1,46 +1,68 @@
 // writes helpers require for vnc.html (they should output app.js)
-var fs = require('fs');
-var fse = require('fs-extra');
-var path = require('path');
+const fs = require('fs');
+const path = require('path');
+
+// util.promisify requires Node.js 8.x, so we have our own
+function promisify(original) {
+    return function promise_wrap() {
+        const args = Array.prototype.slice.call(arguments);
+        return new Promise((resolve, reject) => {
+            original.apply(this, args.concat((err, value) => {
+                if (err) return reject(err);
+                resolve(value);
+            }));
+        });
+    };
+}
+
+const writeFile = promisify(fs.writeFile);
 
 module.exports = {
     'amd': {
-        appWriter: (base_out_path, out_path) => {
+        appWriter: (base_out_path, script_base_path, out_path) => {
             // setup for requirejs
-            fs.writeFile(out_path, 'requirejs(["app/ui"], function (ui) {});', (err) => { if (err) throw err; });
-            console.log(`Please place RequireJS in ${path.join(base_out_path, 'require.js')}`);
-            return `<script src="require.js" data-main="${path.relative(base_out_path, out_path)}"></script>`;
+            const ui_path = path.relative(base_out_path,
+                                          path.join(script_base_path, 'app', 'ui'));
+            return writeFile(out_path, `requirejs(["${ui_path}"], (ui) => {});`)
+                .then(() => {
+                    console.log(`Please place RequireJS in ${path.join(script_base_path, 'require.js')}`);
+                    const require_path = path.relative(base_out_path,
+                                                       path.join(script_base_path, 'require.js'));
+                    return [ require_path ];
+                });
         },
-        noCopyOverride: () => {},
     },
     'commonjs': {
-        optionsOverride: (opts) => {   
+        optionsOverride: (opts) => {
             // CommonJS supports properly shifting the default export to work as normal
             opts.plugins.unshift("add-module-exports");
         },
-        appWriter: (base_out_path, out_path) => {
-            var browserify = require('browserify');
-            var b = browserify(path.join(base_out_path, 'app/ui.js'), {});
-            b.bundle().pipe(fs.createWriteStream(out_path));
-            return `<script src="${path.relative(base_out_path, out_path)}"></script>`;
+        appWriter: (base_out_path, script_base_path, out_path) => {
+            const browserify = require('browserify');
+            const b = browserify(path.join(script_base_path, 'app/ui.js'), {});
+            return promisify(b.bundle).call(b)
+                .then(buf => writeFile(out_path, buf))
+                .then(() => []);
         },
-        noCopyOverride: () => {},
+        removeModules: true,
     },
     'systemjs': {
-        appWriter: (base_out_path, out_path) => {
-            fs.writeFile(out_path, 'SystemJS.import("./app/ui.js");', (err) => { if (err) throw err; });
-            console.log(`Please place SystemJS in ${path.join(base_out_path, 'system-production.js')}`);
-            return `<script src="vendor/promise.js"></script>
-<script src="system-production.js"></script>\n<script src="${path.relative(base_out_path, out_path)}"></script>`;
-        },
-        noCopyOverride: (paths, no_copy_files) => {
-            no_copy_files.delete(path.join(paths.vendor, 'promise.js'));
+        appWriter: (base_out_path, script_base_path, out_path) => {
+            const ui_path = path.relative(base_out_path,
+                                          path.join(script_base_path, 'app', 'ui.js'));
+            return writeFile(out_path, `SystemJS.import("${ui_path}");`)
+                .then(() => {
+                    console.log(`Please place SystemJS in ${path.join(script_base_path, 'system-production.js')}`);
+                    const systemjs_path = path.relative(base_out_path,
+                                                        path.join(script_base_path, 'system-production.js'));
+                    return [ systemjs_path ];
+                });
         },
     },
     'umd': {
-        optionsOverride: (opts) => {   
+        optionsOverride: (opts) => {
             // umd supports properly shifting the default export to work as normal
             opts.plugins.unshift("add-module-exports");
         },
     },
-}
+};