]>
git.proxmox.com Git - mirror_novnc.git/blob - utils/use_require.js
3 const path
= require ( 'path' );
4 const program
= require ( 'commander' );
5 const fs
= require ( 'fs' );
6 const fse
= require ( 'fs-extra' );
7 const babel
= require ( '@babel/core' );
10 . option ( '-m, --with-source-maps [type]' , 'output source maps when not generating a bundled app (type may be empty for external source maps, inline for inline source maps, or both) ' )
11 . option ( '--clean' , 'clear the lib folder before building' )
14 // the various important paths
16 main
: path
. resolve ( __dirname
, '..' ),
17 core
: path
. resolve ( __dirname
, '..' , 'core' ),
18 vendor
: path
. resolve ( __dirname
, '..' , 'vendor' ),
19 libDirBase
: path
. resolve ( __dirname
, '..' , 'lib' ),
22 // util.promisify requires Node.js 8.x, so we have our own
23 function promisify ( original
) {
24 return function promiseWrap () {
25 const args
= Array
. prototype . slice
. call ( arguments
);
26 return new Promise (( resolve
, reject
) => {
27 original
. apply ( this , args
. concat (( err
, value
) => {
28 if ( err
) return reject ( err
);
35 const writeFile
= promisify ( fs
. writeFile
);
37 const readdir
= promisify ( fs
. readdir
);
38 const lstat
= promisify ( fs
. lstat
);
40 const ensureDir
= promisify ( fse
. ensureDir
);
42 const babelTransformFile
= promisify ( babel
. transformFile
);
44 // walkDir *recursively* walks directories trees,
45 // calling the callback for all normal files found.
46 function walkDir ( basePath
, cb
, filter
) {
47 return readdir ( basePath
)
49 const paths
= files
. map ( filename
=> path
. join ( basePath
, filename
));
50 return Promise
. all ( paths
. map ( filepath
=> lstat ( filepath
)
52 if ( filter
!== undefined && ! filter ( filepath
, stats
)) return ;
54 if ( stats
. isSymbolicLink ()) return ;
55 if ( stats
. isFile ()) return cb ( filepath
);
56 if ( stats
. isDirectory ()) return walkDir ( filepath
, cb
, filter
);
61 function makeLibFiles ( sourceMaps
) {
62 // NB: we need to make a copy of babelOpts, since babel sets some defaults on it
63 const babelOpts
= () => ({
66 [ '@babel/preset-env' ,
67 { modules
: 'commonjs' } ]
70 sourceMaps
: sourceMaps
,
73 fse
. ensureDirSync ( paths
. libDirBase
);
77 const handleDir
= ( vendorRewrite
, inPathBase
, filename
) => Promise
. resolve ()
79 const outPath
= path
. join ( paths
. libDirBase
, path
. relative ( inPathBase
, filename
));
81 if ( path
. extname ( filename
) !== '.js' ) {
82 return ; // skip non-javascript files
84 return Promise
. resolve ()
85 . then (() => ensureDir ( path
. dirname ( outPath
)))
87 const opts
= babelOpts ();
88 // Adjust for the fact that we move the core files relative
89 // to the vendor directory
91 opts
. plugins
. push ([ "import-redirect" ,
92 { "root" : paths
. libDirBase
,
93 "redirect" : { "vendor/(.+)" : "./vendor/$1" }}]);
96 return babelTransformFile ( filename
, opts
)
98 console
. log ( `Writing ${outPath} ` );
101 if ( sourceMaps
=== true ) {
102 // append URL for external source map
103 code
+= ` \n //# sourceMappingURL= ${path.basename(outPath)} .map \n ` ;
105 outFiles
. push ( ` ${outPath} ` );
106 return writeFile ( outPath
, code
)
108 if ( sourceMaps
=== true || sourceMaps
=== 'both' ) {
109 console
. log ( ` and ${outPath} .map` );
110 outFiles
. push ( ` ${outPath} .map` );
111 return writeFile ( ` ${outPath} .map` , JSON
. stringify ( map
));
120 const handler
= handleDir
. bind ( null , false , paths
. main
);
121 return walkDir ( paths
. vendor
, handler
);
124 const handler
= handleDir
. bind ( null , true , paths
. core
);
125 return walkDir ( paths
. core
, handler
);
128 console
. error ( `Failure converting modules: ${err} ` );
134 console
. log ( `Removing ${paths.libDirBase} ` );
135 fse
. removeSync ( paths
. libDirBase
);
138 makeLibFiles ( program
. withSourceMaps
);