reworked module import...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2020-12-12 22:41:49 +03:00
parent 6659614212
commit aaecdfa1e3
9 changed files with 129 additions and 105 deletions

View File

@ -2,8 +2,30 @@
*
*
*
* This can be loaded from two contexts:
*
* - <script src=../>
* Needs the requirejs module already loaded...
* Example:
* <script src="js/require.min.js"/>
* <script src="cfg/requirejs.js"/>
*
* - require(..)
* This needs the root require(..) function...
* Example:
* // in the root module...
* require('./cfg/requirejs.js')(require)
*
*
**********************************************************************/
var _requirejs = typeof(requirejs) != 'undefined' && requirejs
var setup = function(require){
var res = {}
var requirejs = _requirejs
var requirejs_cfg = {
// NOTE: this is really odd: running electron as a packed binary breaks
// requirejs' paths...
@ -59,18 +81,53 @@ var requirejs_cfg = {
}
if(typeof(require) != 'undefined'){
requirejs_cfg.nodeRequire = require }
// node contexts...
if(typeof(process) != 'undefined'){
var nodeRequire =
requirejs_cfg.nodeRequire =
global.nodeRequire
|| global.require
|| require
require('app-module-path')
.addPath('.')
requirejs =
global.requirejs =
res.requirejs =
global.requirejs
|| require('requirejs')
global.nodeRequire =
res.nodeRequire =
nodeRequire }
// XXX revise...
if(typeof(require) != 'undefined' && typeof(global) != 'undefined'){
global.nodeRequire = require
global.requirejs = global.requirejs || require('requirejs') }
// browser contexts...
if(typeof(window) != 'undefined'){
window.nodeRequire =
window.nodeRequire
|| (typeof(require) != 'undefined'
&& require !== requirejs
&& require)
window.requirejs = requirejs }
requirejs.config(requirejs_cfg)
return res }
//---------------------------------------------------------------------
// Run/export the setup...
//
// we can get here from two contexts...
typeof(process) == 'undefined' ?
// browser's <script src="..">...
setup(require)
// node's require(..)
: (module.exports = setup)

View File

@ -40,18 +40,18 @@ revised.
Entry points
============
The current state of things is far from optimal -- we have "three"
"different" entry points:
We have two entry points here for a reason, e.js is tuned to be as
light/fast as possible for the general case, i.e. when run without
arguments combining it with ig.js would make things much-much slower...
- index.html - Browser
- e.js - electron
-> electron.html - electron-specific html
- electron e.js - electron
<-> ig.js - parse args if given and optionally
return control back to e.js...
- ig.js - node
-> e.js - node can spawn an electron app
The goal is to simplify this by merging the electron/node entry points
and merging the html files into one.

View File

@ -11,7 +11,6 @@
//require('v8-compile-cache')
var electron = require('electron')
var path = require('path')
var url = require('url')
@ -106,9 +105,8 @@ function createWindow(){
WIN.setMenu(null)
WIN.loadURL(url.format({
pathname: path.join(__dirname, 'index.html'),
//pathname: path.join(__dirname, 'electron.html'),
protocol: 'file:',
slashes: true
slashes: true,
}))
// XXX HACK: pass this in a formal way... (???)
WIN.once('ready-to-show',
@ -164,7 +162,6 @@ var argv1 = process.argv[1]
;(process.argv.length > 2
|| (argv1 && argv1 != require.main.filename)) ?
// got some arguments -- trigger ig.js...
// XXX BUG: when running in a built app this will break with a require error...
(require('./ig')
&& global.START_GUI
&& start())

View File

@ -474,6 +474,7 @@ module.CLI = core.ImageGridFeatures.Feature({
argv.Parser({
context: this,
script: nodeRequire.main.filename,
// XXX argv.js is not picking these up because
// of the require(..) mixup...

View File

@ -12,16 +12,9 @@ require('v8-compile-cache')
// NOTE: this fixes several issues with lib/util conflicting with stuff...
require('repl')
require('./cfg/requirejs')
nodeRequire =
global.nodeRequire =
require
require =
requirejs =
global.requirejs =
require('requirejs')
// setup module loaders...
require = require('./cfg/requirejs')(require).requirejs
require.main = {filename: (nodeRequire.main || {}).filename}

View File

@ -79,7 +79,7 @@ if(window.require){
// NOTE: we need to load the electron way first because the other way
// around requirejs messes things up...
if(typeof(process) != 'undefined'){
require('./cfg/requirejs')
require('./cfg/requirejs')(require)
requirejs('ui') }
</script>
<script src="node_modules/requirejs/require.js"></script>

View File

@ -1127,9 +1127,9 @@
}
},
"ig-argv": {
"version": "2.16.1",
"resolved": "https://registry.npmjs.org/ig-argv/-/ig-argv-2.16.1.tgz",
"integrity": "sha512-fSgX86zTSRuMFaxGvbHK97/ASWgrYAnqLK/q32skXuZ8eDM2ZyM84kHbdgMqd3FrZS4DoyY5fA2TXD9AfWcE/w==",
"version": "2.16.2",
"resolved": "https://registry.npmjs.org/ig-argv/-/ig-argv-2.16.2.tgz",
"integrity": "sha512-bSOslxGN9XqJ+IVba/XC0h9p1r/z/25wmJ2ik2xCNlMU07WI/K2ensIFR93I0aVfLHxsV37G5CdlP8xmiRIcLg==",
"requires": {
"ig-object": "^5.2.6"
}

View File

@ -31,7 +31,7 @@
"glob": "^7.1.6",
"guarantee-events": "^1.0.0",
"ig-actions": "^3.24.22",
"ig-argv": "^2.16.1",
"ig-argv": "^2.16.2",
"ig-features": "^3.4.2",
"ig-object": "^5.4.12",
"ig-types": "^5.0.40",

View File

@ -5,38 +5,14 @@
**********************************************************************/
// Pre-setup...
// Add node_modules path outside of the packed nwjs code...
// nw.js: add node_modules path outside of the packed nwjs code...
//
// This keeps the large node module set outside the zip thus speeding
// up the loading process significantly...
if((typeof(process) != 'undefined' ? process : {}).__nwjs){
var path = require('path')
require('app-module-path')
.addPath(path.dirname(process.execPath) + '/node_modules/')
}
// Setup requirejs if we are in node/nw...
//
// NOTE: no need to do this in browser...
if(typeof(process) != 'undefined'){
//require('v8-compile-cache')
requirejs =
global.requirejs =
window.requirejs =
// XXX for some reason we can't just use the browser requirejs
// even if we pass it nodeRequire, it still can't pass the
// node stuff to node...
require('requirejs')
nodeRequire =
global.nodeRequire =
window.nodeRequire =
require
}
.addPath(path.dirname(process.execPath) + '/node_modules/') }