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,74 +2,131 @@
* *
* *
* *
* 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_cfg = { var _requirejs = typeof(requirejs) != 'undefined' && requirejs
// NOTE: this is really odd: running electron as a packed binary breaks
// requirejs' paths...
baseUrl: typeof(process) != 'undefined'
&& process.versions.electron ?
(require.main ?
require.main.filename.split(/[\\\/]/g).slice(0, -1).join('/')
: document.baseURI
.replace(/^[a-zA-Z]+:\/\/\/?/, '')
.split(/[#&]/)[0].split(/[\\\/]/g).slice(0, -1).join('/'))
: '.',
// XXX this does not work on direct filesystem access...
//urlArgs: 'bust='+Date.now(), var setup = function(require){
var res = {}
paths: { var requirejs = _requirejs
text: 'node_modules/requirejs-plugins/lib/text',
json: 'node_modules/requirejs-plugins/src/json', var requirejs_cfg = {
// NOTE: this is really odd: running electron as a packed binary breaks
// requirejs' paths...
baseUrl: typeof(process) != 'undefined'
&& process.versions.electron ?
(require.main ?
require.main.filename.split(/[\\\/]/g).slice(0, -1).join('/')
: document.baseURI
.replace(/^[a-zA-Z]+:\/\/\/?/, '')
.split(/[#&]/)[0].split(/[\\\/]/g).slice(0, -1).join('/'))
: '.',
// XXX this does not work on direct filesystem access...
//urlArgs: 'bust='+Date.now(),
//react: 'node_modules/react/dist/react-with-addons.min.js', paths: {
//'react-dom': 'node_modules/react-dom/dist/react-dom.min.js', text: 'node_modules/requirejs-plugins/lib/text',
//'ext-lib/preact': './node_modules/preact/dist/preact.dev', json: 'node_modules/requirejs-plugins/src/json',
//react: 'node_modules/react/dist/react-with-addons.min.js',
//'react-dom': 'node_modules/react-dom/dist/react-dom.min.js',
//'ext-lib/preact': './node_modules/preact/dist/preact.dev',
'lib/object': 'node_modules/ig-object/object', 'lib/object': 'node_modules/ig-object/object',
'lib/types': 'node_modules/ig-types/', 'lib/types': 'node_modules/ig-types/',
'lib/actions': 'node_modules/ig-actions/actions', 'lib/actions': 'node_modules/ig-actions/actions',
'lib/features': 'node_modules/ig-features/features', 'lib/features': 'node_modules/ig-features/features',
//'lib/keyboard': './node_modules/ig-keyboard/keyboard', //'lib/keyboard': './node_modules/ig-keyboard/keyboard',
'object-run': 'node_modules/object-run/run', 'object-run': 'node_modules/object-run/run',
'lib/argv': 'node_modules/ig-argv/argv', 'lib/argv': 'node_modules/ig-argv/argv',
'lib/walk': 'node_modules/generic-walk/walk', 'lib/walk': 'node_modules/generic-walk/walk',
}, },
map: { map: {
'*': { '*': {
// back-refs // back-refs
// ...these enable the npm modules reference each other in // ...these enable the npm modules reference each other in
// a cross-platform manner.... // a cross-platform manner....
'ig-object': 'lib/object', 'ig-object': 'lib/object',
'ig-types': 'lib/types', 'ig-types': 'lib/types',
'ig-actions': 'lib/actions', 'ig-actions': 'lib/actions',
'ig-features': 'lib/features', 'ig-features': 'lib/features',
//'ig-keyboard': 'lib/keyboard', //'ig-keyboard': 'lib/keyboard',
'ig-argv': 'lib/argv', 'ig-argv': 'lib/argv',
'generic-walk': 'lib/walk', 'generic-walk': 'lib/walk',
},
}, },
}, packages: [
packages: [ 'lib/types',
'lib/types', ],
], }
}
if(typeof(require) != 'undefined'){ // node contexts...
requirejs_cfg.nodeRequire = require } 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... // browser contexts...
if(typeof(require) != 'undefined' && typeof(global) != 'undefined'){ if(typeof(window) != 'undefined'){
global.nodeRequire = require window.nodeRequire =
global.requirejs = global.requirejs || require('requirejs') } window.nodeRequire
|| (typeof(require) != 'undefined'
&& require !== requirejs
&& require)
window.requirejs = requirejs }
requirejs.config(requirejs_cfg)
return res }
requirejs.config(requirejs_cfg)
//---------------------------------------------------------------------
// 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 Entry points
============ ============
The current state of things is far from optimal -- we have "three" We have two entry points here for a reason, e.js is tuned to be as
"different" entry points: 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 - index.html - Browser
- e.js - electron - electron e.js - electron
-> electron.html - electron-specific html <-> ig.js - parse args if given and optionally
return control back to e.js...
- ig.js - node - ig.js - node
-> e.js - node can spawn an electron app -> 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') //require('v8-compile-cache')
var electron = require('electron') var electron = require('electron')
var path = require('path') var path = require('path')
var url = require('url') var url = require('url')
@ -106,9 +105,8 @@ function createWindow(){
WIN.setMenu(null) WIN.setMenu(null)
WIN.loadURL(url.format({ WIN.loadURL(url.format({
pathname: path.join(__dirname, 'index.html'), pathname: path.join(__dirname, 'index.html'),
//pathname: path.join(__dirname, 'electron.html'),
protocol: 'file:', protocol: 'file:',
slashes: true slashes: true,
})) }))
// XXX HACK: pass this in a formal way... (???) // XXX HACK: pass this in a formal way... (???)
WIN.once('ready-to-show', WIN.once('ready-to-show',
@ -164,7 +162,6 @@ var argv1 = process.argv[1]
;(process.argv.length > 2 ;(process.argv.length > 2
|| (argv1 && argv1 != require.main.filename)) ? || (argv1 && argv1 != require.main.filename)) ?
// got some arguments -- trigger ig.js... // got some arguments -- trigger ig.js...
// XXX BUG: when running in a built app this will break with a require error...
(require('./ig') (require('./ig')
&& global.START_GUI && global.START_GUI
&& start()) && start())

View File

@ -474,6 +474,7 @@ module.CLI = core.ImageGridFeatures.Feature({
argv.Parser({ argv.Parser({
context: this, context: this,
script: nodeRequire.main.filename,
// XXX argv.js is not picking these up because // XXX argv.js is not picking these up because
// of the require(..) mixup... // 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... // NOTE: this fixes several issues with lib/util conflicting with stuff...
require('repl') require('repl')
require('./cfg/requirejs') // setup module loaders...
require = require('./cfg/requirejs')(require).requirejs
nodeRequire = require.main = {filename: (nodeRequire.main || {}).filename}
global.nodeRequire =
require
require =
requirejs =
global.requirejs =
require('requirejs')

View File

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

View File

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

View File

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

View File

@ -5,38 +5,14 @@
**********************************************************************/ **********************************************************************/
// Pre-setup... // 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 // This keeps the large node module set outside the zip thus speeding
// up the loading process significantly... // up the loading process significantly...
if((typeof(process) != 'undefined' ? process : {}).__nwjs){ if((typeof(process) != 'undefined' ? process : {}).__nwjs){
var path = require('path') var path = require('path')
require('app-module-path') require('app-module-path')
.addPath(path.dirname(process.execPath) + '/node_modules/') .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
}