summaryrefslogtreecommitdiff
path: root/utils/use_require_helpers.js
blob: 90342cb1dfef99ff9ef4a9ccb0f00dcfea4b4dbd (plain)
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
// writes helpers require for vnc.html (they should output app.js)
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, script_base_path, out_path) => {
            // setup for requirejs
            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) => {   
            // CommonJS supports properly shifting the default export to work as normal
            opts.plugins.unshift("add-module-exports");
        },
        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, 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')}`);
                // FIXME: Should probably be in the legacy directory
                    const promise_path = path.relative(base_out_path,
                                                       path.join(base_out_path, 'vendor', 'promise.js'))
                    const systemjs_path = path.relative(base_out_path,
                                                        path.join(script_base_path, 'system-production.js'))
                    return [ promise_path, systemjs_path ];
                });
        },
        noCopyOverride: (paths, no_copy_files) => {
            no_copy_files.delete(path.join(paths.vendor, 'promise.js'));
        },
    },
    'umd': {
        optionsOverride: (opts) => {   
            // umd supports properly shifting the default export to work as normal
            opts.plugins.unshift("add-module-exports");
        },
    },
}