@lumjs/core 1.13.0 → 1.14.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/context.js +70 -11
- package/package.json +1 -1
package/lib/context.js
CHANGED
|
@@ -6,18 +6,42 @@
|
|
|
6
6
|
* describing the current JS environment and execution context.
|
|
7
7
|
*
|
|
8
8
|
* @module @lumjs/core/context
|
|
9
|
-
* @property {boolean}
|
|
10
|
-
* @property {boolean} CJS - CommonJS environment detected.
|
|
9
|
+
* @property {boolean} hasDefine - A global `define()` function was found.
|
|
11
10
|
* @property {boolean} hasRequire - A global `require()` function was found.
|
|
12
|
-
* @property {boolean} hasExports - A global `exports`
|
|
11
|
+
* @property {boolean} hasExports - A global `exports` variable was found.
|
|
13
12
|
* @property {boolean} hasModule - A global `module` object was found.
|
|
14
|
-
* @property {boolean}
|
|
13
|
+
* @property {boolean} hasModuleExports - A `module.exports` exists.
|
|
14
|
+
* @property {boolean} hasSyncedExports - `exports === module.exports`
|
|
15
|
+
* @property {boolean} isNode - An alias to `Node.ok`.
|
|
15
16
|
* @property {boolean} isBrowser - A web-browser environment detected.
|
|
16
17
|
* @property {boolean} isWindow - Is a browser `Window` context.
|
|
17
18
|
* @property {boolean} isWorker - Is a browser `Worker` (sub-types below.)
|
|
18
19
|
* @property {boolean} isServiceWorker - Is a `ServiceWorker` context.
|
|
19
20
|
* @property {boolean} isDedicatedWorker - Is a `DedicatedWorker` context.
|
|
20
21
|
* @property {boolean} isSharedWorker - Is a `SharedWorker` context.
|
|
22
|
+
* @property {object} AMD - Asynchronous Module Definition detection.
|
|
23
|
+
* @property {boolean} AMD.isSupported - The `define.amd` property is set.
|
|
24
|
+
* @property {boolean} AMD.isRequireJS - A global `requirejs` was found.
|
|
25
|
+
* @property {object} CJS - CommonJS detection.
|
|
26
|
+
* @property {boolean} CJS.standard - `hasDefine && hasRequire`
|
|
27
|
+
* @property {boolean} CJS.nodeLike - `CJS.standard && hasExports`
|
|
28
|
+
* @property {boolean} CJS.isLumV5 - Lum.js browser bundler detected.
|
|
29
|
+
* @property {boolean} CJS.isWebpack - Webpack bundler detected.
|
|
30
|
+
* @property {boolean} CJS.inBrowser - `CJS.isLumV5 || CJS.isWebpack`
|
|
31
|
+
* @property {object} Node - Node.js detection.
|
|
32
|
+
* @property {boolean} Node.isSane - `CJS.nodeLike && !CJS.inBrowser`
|
|
33
|
+
* @property {boolean} Node.hasProcess - A global `process` object exists.
|
|
34
|
+
* @property {boolean} Node.ok - `Node.isSane && Node.hasProcess`
|
|
35
|
+
* @property {?string} Node.ver - `process.versions.node ?? null`
|
|
36
|
+
* @property {?object} Node.versions - `process.versions ?? null`
|
|
37
|
+
* @property {Array} Node.args - Command line arguments.
|
|
38
|
+
* @property {string} Node.script - Command line script name.
|
|
39
|
+
* @property {object} Electron - Electron detection.
|
|
40
|
+
* @property {boolean} ok - Electron environment detected.
|
|
41
|
+
* @property {boolean} isDefault - Is this the default app?
|
|
42
|
+
* @property {boolean} isBundled - Is this a bundled app?
|
|
43
|
+
* @property {boolean} isMain - Is this the main renderer frame?
|
|
44
|
+
* @property {?string} type - `process.type`
|
|
21
45
|
* @property {object} root - See {@link module:@lumjs/core/types.root}
|
|
22
46
|
*/
|
|
23
47
|
|
|
@@ -28,37 +52,72 @@ const rootHas = what => typeof root[what] !== U;
|
|
|
28
52
|
const cd = def(ctx, true);
|
|
29
53
|
const CJS = {};
|
|
30
54
|
const AMD = {};
|
|
55
|
+
const Node = {};
|
|
56
|
+
const Elec = {};
|
|
31
57
|
const cjs = def(CJS, true);
|
|
32
58
|
const amd = def(AMD, true);
|
|
59
|
+
const node = def(Node, true);
|
|
60
|
+
const elec = def(Elec, true);
|
|
33
61
|
const isLumV5 = typeof module.$lib === O && module.$lib.$cached === module;
|
|
34
62
|
|
|
35
63
|
cd('hasDefine', typeof define === F)
|
|
36
64
|
('hasRequire', typeof require === F)
|
|
37
|
-
('hasExports', typeof exports !== U)
|
|
65
|
+
('hasExports', typeof exports !== U && isComplex(exports))
|
|
38
66
|
('hasModule', typeof module === O && module !== null)
|
|
39
|
-
('hasModuleExports', isComplex(module.exports))
|
|
40
|
-
|
|
67
|
+
('hasModuleExports', ctx.hasModule && isComplex(module.exports))
|
|
68
|
+
('hasSyncedExports', ctx.hasExports && ctx.hasModuleExports
|
|
69
|
+
&& exports === module.exports)
|
|
41
70
|
|
|
42
71
|
cjs('standard', ctx.hasModule && ctx.hasRequire)
|
|
43
72
|
('nodeLike', CJS.standard && ctx.hasExports)
|
|
44
73
|
('isLumV5', CJS.nodeLike && isLumV5)
|
|
45
74
|
('isWebpack', CJS.nodeLike && require.resolve === require)
|
|
46
75
|
('inBrowser', CJS.isLumV5 || CJS.isWebpack)
|
|
47
|
-
|
|
76
|
+
|
|
77
|
+
node('isSane', CJS.nodeLike && !CJS.inBrowser)
|
|
78
|
+
('hasProcess', typeof process === O && process !== null)
|
|
79
|
+
('ok', Node.isSane && Node.hasProcess)
|
|
80
|
+
('versions', Node.hasProcess ? process.versions : null)
|
|
81
|
+
('ver', Node.hasProcess ? process.versions.node : null)
|
|
82
|
+
('args', {get: function()
|
|
83
|
+
{ // Inspired by yargs hideBin() function.
|
|
84
|
+
if (Node.ok)
|
|
85
|
+
{
|
|
86
|
+
const o = (Elec.isBundled) ? 1 : 2;
|
|
87
|
+
return process.argv.slice(o);
|
|
88
|
+
}
|
|
89
|
+
return [];
|
|
90
|
+
}})
|
|
91
|
+
('script', {get: function()
|
|
92
|
+
{ // Inspired by yargs getProcessArgvBin() function.
|
|
93
|
+
if (Node.ok)
|
|
94
|
+
{
|
|
95
|
+
const i = (Elec.isBundled) ? 0 : 1;
|
|
96
|
+
return process.argv[i];
|
|
97
|
+
}
|
|
98
|
+
return '';
|
|
99
|
+
}})
|
|
100
|
+
|
|
101
|
+
elec('ok', Node.ok && !!Node.versions.electron)
|
|
102
|
+
('isDefault', Elec.ok && !!process.defaultApp)
|
|
103
|
+
('isBundled', Elec.ok && !process.defaultApp)
|
|
104
|
+
('isMain', Elec.ok && !!process.isMainFrame)
|
|
105
|
+
('type', Elec.ok && process.type)
|
|
48
106
|
|
|
49
107
|
amd('isSupported', ctx.hasDefine && isObj(define.amd))
|
|
50
108
|
('isRequireJS', AMD.isSupported && typeof requirejs === F)
|
|
51
109
|
|
|
52
110
|
cd('CJS', CJS)
|
|
53
111
|
('AMD', AMD)
|
|
54
|
-
('
|
|
55
|
-
('
|
|
112
|
+
('Node', Node)
|
|
113
|
+
('Electron', Elec)
|
|
114
|
+
('isNode', Node.ok)
|
|
115
|
+
('isWindow', typeof Window === F && rootHas(window))
|
|
56
116
|
('isWorker', rootHas('WorkerGlobalScope'))
|
|
57
117
|
('isServiceWorker', rootHas('ServiceWorkerGlobalScope'))
|
|
58
118
|
('isDedicatedWorker', rootHas('DedicatedWorkerGlobalScope'))
|
|
59
119
|
('isSharedWorker', rootHas('SharedWorkerGlobalScope'))
|
|
60
120
|
('isBrowser', !ctx.isNode && (ctx.isWindow || ctx.isWorker))
|
|
61
|
-
;
|
|
62
121
|
|
|
63
122
|
/**
|
|
64
123
|
* See if a root-level name is defined.
|