Skip to content

Commit d023db6

Browse files
authored
Merge pull request #541 from mathjax/better-path
Better handling of root location and async loading in node-main
2 parents cd71659 + 75c6cdd commit d023db6

File tree

1 file changed

+20
-41
lines changed

1 file changed

+20
-41
lines changed

components/src/node-main/node-main.js

Lines changed: 20 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const path = require('path');
1+
const path = eval("require('path')"); // use actual node version, not webpack's version
22

33
/*
44
* Load the needed MathJax components
@@ -19,52 +19,31 @@ combineDefaults(MathJax.config.loader, 'dependencies', dependencies);
1919
combineDefaults(MathJax.config.loader, 'paths', paths);
2020
combineDefaults(MathJax.config.loader, 'provides', provides);
2121

22-
MathJax.config.loader.paths.mathjax = (function () {
23-
//
24-
// Convert a windows path to a unix path (when needed)
25-
//
26-
const convertWindows = (name) => (path.win32 || !name.match(/^[a-z]:\\/i)) ? name : name.replace(/\\/g, '/');
27-
//
28-
// Locate the directory for this file:
29-
// Note that __dirname is not effective in webpacked files,
30-
// but the complete path is listed in an error message's requireStack when require.resolve() fails.
31-
//
32-
try {
33-
//
34-
// Try to locate a non-existing file in order to throw an error
35-
//
36-
const dir = MathJax.config.loader.require.resolve('mathjax/es5/non-existing-file');
37-
//
38-
// (in case it ever exists, use its directory)
39-
//
40-
return path.dirname(convertWindows(dir));
41-
} catch (err) {
42-
//
43-
// Find the directory containing this file from the error message
44-
//
45-
let dir = path.dirname(convertWindows(err.requireStack[0]));
46-
if (path.basename(dir) == 'node-main') {
47-
//
48-
// This is components/src/node-main/node-main.js, so use
49-
// components/src as the mathjax directory, and load the source array
50-
//
51-
dir = path.dirname(dir);
52-
combineDefaults(MathJax.config.loader, 'source', require('../source.js').source);
53-
}
54-
return dir;
55-
}
56-
})();
57-
58-
5922
/*
6023
* Preload core and liteDOM adaptor (needed for node)
6124
*/
6225
Loader.preLoad('loader', 'startup', 'core', 'adaptors/liteDOM');
6326
require('../core/core.js');
6427
require('../adaptors/liteDOM/liteDOM.js');
65-
const REQUIRE = MathJax.config.loader.require;
66-
MathJax._.mathjax.mathjax.asyncLoad = function (name) {
67-
return REQUIRE(name.charAt(0) === '.' ? path.resolve(root, name) : name);
28+
29+
/*
30+
* Set the mathjax root path to the location where node-main.js was loaded from,
31+
* using the actual node __dirname, not the webpack one, and removing
32+
* the directory if we are loaded from components/src/node-main.
33+
*/
34+
const dir = CONFIG.paths.mathjax = eval('__dirname');
35+
if (path.basename(dir) === 'node-main') {
36+
CONFIG.paths.mathjax = path.dirname(dir);
37+
combineDefaults(CONFIG, 'source', require('../source.js').source);
38+
//
39+
// Set the asynchronous loader to use the js directory, so we can load
40+
// other files like entity definitions
41+
//
42+
const ROOT = path.resolve(dir, '../../../js');
43+
const REQUIRE = MathJax.config.loader.require;
44+
MathJax._.mathjax.mathjax.asyncLoad = function (name) {
45+
return REQUIRE(name.charAt(0) === '.' ? path.resolve(ROOT, name) : name);
46+
};
6847
}
6948

7049

0 commit comments

Comments
 (0)