system_external.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import { Variables } from './variables.js';
  2. export class SystemExternal {
  3. static nodeRequire() {
  4. return eval('require');
  5. }
  6. static extRequire(library) {
  7. if (typeof process !== 'undefined' && typeof require !== 'undefined') {
  8. return SystemExternal.nodeRequire()(library);
  9. }
  10. return null;
  11. }
  12. }
  13. SystemExternal.windowSupported = (() => !(typeof window === 'undefined'))();
  14. SystemExternal.documentSupported = (() => SystemExternal.windowSupported &&
  15. !(typeof window.document === 'undefined'))();
  16. SystemExternal.xmldom = SystemExternal.documentSupported
  17. ? window
  18. : SystemExternal.extRequire('@xmldom/xmldom');
  19. SystemExternal.document = SystemExternal.documentSupported
  20. ? window.document
  21. : new SystemExternal.xmldom.DOMImplementation().createDocument('', '', 0);
  22. SystemExternal.xpath = SystemExternal.documentSupported
  23. ? document
  24. : (function () {
  25. const window = { document: {}, XPathResult: {} };
  26. const wgx = SystemExternal.extRequire('wicked-good-xpath');
  27. wgx.install(window);
  28. window.document.XPathResult = window.XPathResult;
  29. return window.document;
  30. })();
  31. SystemExternal.mathmapsIePath = 'https://cdn.jsdelivr.net/npm/sre-mathmaps-ie@' +
  32. Variables.VERSION +
  33. 'mathmaps_ie.js';
  34. SystemExternal.fs = SystemExternal.documentSupported
  35. ? null
  36. : SystemExternal.extRequire('fs');
  37. SystemExternal.url = Variables.url;
  38. SystemExternal.jsonPath = (function () {
  39. if (SystemExternal.documentSupported) {
  40. return SystemExternal.url;
  41. }
  42. if (process.env.SRE_JSON_PATH || global.SRE_JSON_PATH) {
  43. return process.env.SRE_JSON_PATH || global.SRE_JSON_PATH;
  44. }
  45. try {
  46. const path = SystemExternal.nodeRequire().resolve('speech-rule-engine');
  47. return path.replace(/sre\.js$/, '') + 'mathmaps';
  48. }
  49. catch (_err) {
  50. }
  51. try {
  52. const path = SystemExternal.nodeRequire().resolve('.');
  53. return path.replace(/sre\.js$/, '') + 'mathmaps';
  54. }
  55. catch (_err) {
  56. }
  57. return typeof __dirname !== 'undefined'
  58. ? __dirname + (__dirname.match(/lib?$/) ? '/mathmaps' : '/lib/mathmaps')
  59. : process.cwd() + '/lib/mathmaps';
  60. })();
  61. SystemExternal.WGXpath = Variables.WGXpath;
  62. SystemExternal.wgxpath = null;
  63. export default SystemExternal;