Skip to content

Commit

Permalink
Better handling of root location and async loading in node-main
Browse files Browse the repository at this point in the history
  • Loading branch information
dpvc committed Aug 17, 2020
1 parent cd71659 commit 75c6cdd
Showing 1 changed file with 20 additions and 41 deletions.
61 changes: 20 additions & 41 deletions components/src/node-main/node-main.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const path = require('path');
const path = eval("require('path')"); // use actual node version, not webpack's version

/*
* Load the needed MathJax components
Expand All @@ -19,52 +19,31 @@ combineDefaults(MathJax.config.loader, 'dependencies', dependencies);
combineDefaults(MathJax.config.loader, 'paths', paths);
combineDefaults(MathJax.config.loader, 'provides', provides);

MathJax.config.loader.paths.mathjax = (function () {
//
// Convert a windows path to a unix path (when needed)
//
const convertWindows = (name) => (path.win32 || !name.match(/^[a-z]:\\/i)) ? name : name.replace(/\\/g, '/');
//
// Locate the directory for this file:
// Note that __dirname is not effective in webpacked files,
// but the complete path is listed in an error message's requireStack when require.resolve() fails.
//
try {
//
// Try to locate a non-existing file in order to throw an error
//
const dir = MathJax.config.loader.require.resolve('mathjax/es5/non-existing-file');
//
// (in case it ever exists, use its directory)
//
return path.dirname(convertWindows(dir));
} catch (err) {
//
// Find the directory containing this file from the error message
//
let dir = path.dirname(convertWindows(err.requireStack[0]));
if (path.basename(dir) == 'node-main') {
//
// This is components/src/node-main/node-main.js, so use
// components/src as the mathjax directory, and load the source array
//
dir = path.dirname(dir);
combineDefaults(MathJax.config.loader, 'source', require('../source.js').source);
}
return dir;
}
})();


/*
* Preload core and liteDOM adaptor (needed for node)
*/
Loader.preLoad('loader', 'startup', 'core', 'adaptors/liteDOM');
require('../core/core.js');
require('../adaptors/liteDOM/liteDOM.js');
const REQUIRE = MathJax.config.loader.require;
MathJax._.mathjax.mathjax.asyncLoad = function (name) {
return REQUIRE(name.charAt(0) === '.' ? path.resolve(root, name) : name);

/*
* Set the mathjax root path to the location where node-main.js was loaded from,
* using the actual node __dirname, not the webpack one, and removing
* the directory if we are loaded from components/src/node-main.
*/
const dir = CONFIG.paths.mathjax = eval('__dirname');
if (path.basename(dir) === 'node-main') {
CONFIG.paths.mathjax = path.dirname(dir);
combineDefaults(CONFIG, 'source', require('../source.js').source);
//
// Set the asynchronous loader to use the js directory, so we can load
// other files like entity definitions
//
const ROOT = path.resolve(dir, '../../../js');
const REQUIRE = MathJax.config.loader.require;
MathJax._.mathjax.mathjax.asyncLoad = function (name) {
return REQUIRE(name.charAt(0) === '.' ? path.resolve(ROOT, name) : name);
};
}


Expand Down

0 comments on commit 75c6cdd

Please sign in to comment.