UNPKG

1.72 kBJavaScriptView Raw
1/*
2 require( 'require' )
3 -----------------------------------------------------------------------
4 @example
5
6 var Path = require("node://path"); // Only in NodeJS/NW.js environment.
7 var Button = require("tfw.button");
8
9 */
10"use strict";
11
12// window.require =
13( function ( workspace ) {
14 const
15 NODE_PREFIX = "node://",
16 NODE_PREFIX_SIZE = NODE_PREFIX.length,
17 modules = {},
18 definitions = {},
19 nodejsRequire = typeof window.require === 'function' ? window.require : null;
20 const f = function ( id, body ) {
21 if ( id.substr( 0, NODE_PREFIX_SIZE ) === NODE_PREFIX ) {
22 // Calling for a NodeJS module.
23 if ( !nodejsRequire ) {
24 throw Error( "[require] NodeJS is not available to load module " + id.substr( NODE_PREFIX_SIZE ) );
25 }
26 return nodejsRequire( id.substr( NODE_PREFIX_SIZE ) );
27 }
28
29 if ( typeof body === 'function' ) {
30 definitions[ id ] = body;
31 return body;
32 }
33 const moduleInit = definitions[ id ];
34 if ( typeof moduleInit === 'undefined' ) {
35 const err = new Error( "Required module is missing: " + id );
36 console.error( err.stack );
37 throw err;
38 }
39 var mod = modules[ id ];
40 if ( typeof mod === 'undefined' ) {
41 mod = { exports: {} };
42 const exports = mod.exports;
43 moduleInit.call( workspace, f, mod, exports );
44 modules[ id ] = mod.exports;
45 mod = mod.exports;
46 }
47 return mod;
48 };
49
50 workspace.APP = {};
51 workspace.require = f;
52 return f;
53}( this ) );
\No newline at end of file