]>
git.proxmox.com Git - mirror_novnc.git/blob - utils/use_require.js
3 var path
= require('path');
4 var program
= require('commander');
5 var fs
= require('fs');
6 var fse
= require('fs-extra');
7 var browserify
= require('browserify');
9 var make_modules_transform
= require('./make-module-transform');
10 var babelify
= require("babelify");
14 .option('-b, --browserify', 'create a browserify bundled app')
15 .option('--as-require', 'output files using "require" instead of ES6 import and export')
18 // the various important paths
19 var core_path
= path
.resolve(__dirname
, '..', 'core');
20 var app_path
= path
.resolve(__dirname
, '..', 'app');
21 var out_dir_base
= path
.resolve(__dirname
, '..', 'build');
22 var lib_dir_base
= path
.resolve(__dirname
, '..', 'lib');
24 var make_browserify = function (src_files
, opts
) {
25 // change to the root noVNC directory
26 process
.chdir(path
.resolve(__dirname
, '..'));
28 var b
= browserify(src_files
, opts
);
30 // register the transforms
31 b
.transform(make_modules_transform
);
33 { plugins
: ["add-module-exports", "transform-es2015-modules-commonjs"] });
38 var make_full_app = function () {
39 // make sure the output directory exists
40 fse
.ensureDir(out_dir_base
);
42 // actually bundle the files into a browserified bundled
43 var ui_file
= path
.join(app_path
, 'ui.js');
44 var b
= make_browserify(ui_file
, {});
45 var app_file
= path
.join(out_dir_base
, 'app.js');
46 b
.bundle().pipe(fs
.createWriteStream(app_file
));
48 // copy over app-related resources (images, styles, etc)
49 var src_dir_app
= path
.join(__dirname
, '..', 'app');
50 fs
.readdir(src_dir_app
, function (err
, files
) {
51 if (err
) { throw err
; }
53 files
.forEach(function (src_file
) {
54 var src_file_path
= path
.resolve(src_dir_app
, src_file
);
55 var out_file_path
= path
.resolve(out_dir_base
, src_file
);
56 var ext
= path
.extname(src_file
);
57 if (ext
=== '.js' || ext
=== '.html') return;
58 fse
.copy(src_file_path
, out_file_path
, function (err
) {
59 if (err
) { throw err
; }
60 console
.log("Copied file(s) from " + src_file_path
+ " to " + out_file_path
);
65 // write out the modified vnc.html file that works with the bundle
66 var src_html_path
= path
.resolve(__dirname
, '..', 'vnc.html');
67 var out_html_path
= path
.resolve(out_dir_base
, 'vnc.html');
68 fs
.readFile(src_html_path
, function (err
, contents_raw
) {
69 if (err
) { throw err
; }
71 var contents
= contents_raw
.toString();
72 contents
= contents
.replace(/="app\//g, '="');
74 var start_marker
= '<!-- begin scripts -->\n';
75 var end_marker
= '<!-- end scripts -->';
76 var start_ind
= contents
.indexOf(start_marker
) + start_marker
.length
;
77 var end_ind
= contents
.indexOf(end_marker
, start_ind
);
79 contents
= contents
.slice(0, start_ind
) + '<script src="app.js"></script>\n' + contents
.slice(end_ind
);
81 fs
.writeFile(out_html_path
, contents
, function (err
) {
82 if (err
) { throw err
; }
83 console
.log("Wrote " + out_html_path
);
88 var make_lib_files = function (use_require
) {
89 // make sure the output directory exists
90 fse
.ensureDir(lib_dir_base
);
92 var through
= require('through2');
95 var rfb_file
= path
.join(core_path
, 'rfb.js');
96 var b
= make_browserify(rfb_file
, {});
97 b
.on('transform', function (tr
, file
) {
98 if (tr
._is_make_module
) {
99 var new_path
= path
.join(lib_dir_base
, path
.relative(core_path
, file
));
100 fse
.ensureDir(path
.dirname(new_path
));
101 console
.log("Writing " + new_path
)
102 var fileStream
= fs
.createWriteStream(new_path
);
105 var babelificate
= babelify(file
,
106 { plugins
: ["add-module-exports", "transform-es2015-modules-commonjs"] });
107 tr
.pipe(babelificate
);
117 if (program
.browserify
) {
120 make_lib_files(program
.asRequire
);