Skip to content

Commit e9156ba

Browse files
authored
Merge pull request #1109 from mathjax/better-asyncLoad
Update the asyncLoad implementations to be more consistent, and update tests.
2 parents dedbde8 + 48d57d6 commit e9156ba

File tree

10 files changed

+53
-23
lines changed

10 files changed

+53
-23
lines changed

testsuite/tests/util/asyncLoad/esm.test.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { describe, test, expect } from '@jest/globals';
2+
import {mathjax} from '#js/mathjax.js';
23
import {asyncLoad} from '#js/util/AsyncLoad.js';
34
import {setBaseURL} from '#js/util/asyncLoad/esm.js';
45
import * as path from 'path';
56

6-
const root = new URL(path.resolve('..', 'mjs'), 'file://').href;
7-
const lib = new URL(path.resolve('lib'), 'file://').href;
7+
const root = path.resolve('..', 'mjs');
8+
const lib = path.resolve('lib');
89

910
describe('asyncLoad() for esm', () => {
1011

@@ -20,10 +21,16 @@ describe('asyncLoad() for esm', () => {
2021
await expect(asyncLoad(absFile)).resolves.toEqual({loaded: true}); // absolute file found
2122
await expect(asyncLoad(absUnknown).catch(() => true)).resolves.toBe(true); // absolute file not found
2223

24+
await expect(asyncLoad('#js/components/version.js') // load using package exports
25+
.then((result: any) => result.VERSION)).resolves.toBe(mathjax.version);
26+
await expect(asyncLoad('mathjax-full/js/components/version.js') // load from module
27+
.then((result: any) => result.VERSION)).resolves.toBe(mathjax.version);
28+
2329
await expect(asyncLoad(mjsFile).then((result: any) => result.loaded)).resolves.toBe(true); // mjs file loads
30+
expect(mathjax.asyncIsSynchronous).toBe(false); // esm.js is asynchronous
2431
});
2532

26-
test('serBaseURL() for esm', async () => {
33+
test('setBaseURL() for esm', async () => {
2734
setBaseURL(lib);
2835
const relFile = './AsyncLoad.child.cjs';
2936
const relUnknown = './AsyncLoad.unknown.cjs';

testsuite/tests/util/asyncLoad/node.test.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { describe, test, expect } from '@jest/globals';
22
import './dirname.mjs';
33
import '#source/../require.mjs';
4+
import {mathjax} from '#js/mathjax.js';
45
import {asyncLoad} from '#js/util/AsyncLoad.js';
56
import {setBaseURL} from '#js/util/asyncLoad/node.js';
67
import * as path from 'path';
@@ -22,10 +23,16 @@ describe('asyncLoad() for node', () => {
2223
await expect(asyncLoad(absFile)).resolves.toEqual({loaded: true}); // absolute file found
2324
await expect(asyncLoad(absUnknown).catch(() => true)).resolves.toBe(true); // absolute file not found
2425

26+
await expect(asyncLoad('#js/../cjs/components/version.js') // load using package exports
27+
.then((result: any) => result.VERSION)).resolves.toBe(mathjax.version);
28+
await expect(asyncLoad('mathjax-full/js/components/version.js') // load from module
29+
.then((result: any) => result.VERSION)).resolves.toBe(mathjax.version);
30+
2531
await expect(asyncLoad(mjsFile).catch(() => true)).resolves.toBe(true); // mjs file fails
32+
expect(mathjax.asyncIsSynchronous).toBe(true); // node.js is synchronous
2633
});
2734

28-
test('serBaseURL() for node', async () => {
35+
test('setBaseURL() for node', async () => {
2936
setBaseURL(lib);
3037
const relFile = './AsyncLoad.child.cjs';
3138
const relUnknown = './AsyncLoad.unknown.cjs';

testsuite/tests/util/asyncLoad/system.test.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { describe, test, expect } from '@jest/globals';
22
import './dirname.mjs';
33
import './system.cjs';
4+
import {mathjax} from '#js/mathjax.js';
45
import {asyncLoad} from '#js/util/AsyncLoad.js';
56
import {setBaseURL} from '#js/util/asyncLoad/system.js';
67
import * as path from 'path';
78

8-
const root = new URL(path.resolve('..', 'mjs'), 'file://').href;
9-
const lib = new URL(path.resolve('lib'), 'file://').href;
9+
const root = path.resolve('..', 'mjs');
10+
const lib = path.resolve('lib');
1011

1112
describe('asyncLoad() for node', () => {
1213

@@ -22,10 +23,16 @@ describe('asyncLoad() for node', () => {
2223
await expect(asyncLoad(absFile)).resolves.toEqual({loaded: true}); // absolute file found
2324
await expect(asyncLoad(absUnknown).catch(() => true)).resolves.toBe(true); // absolute file not found
2425

26+
await expect(asyncLoad('#js/components/version.js') // can't load using package exports
27+
.catch(() => true)).resolves.toBe(true);
28+
await expect(asyncLoad('mathjax-full/js/components/version.js') // can't load from module
29+
.catch(() => true)).resolves.toBe(true);
30+
2531
await expect(asyncLoad(mjsFile).then((result: any) => result.loaded)).resolves.toBe(true); // mjs file loads
32+
expect(mathjax.asyncIsSynchronous).toBe(false); // system.js is asynchronous
2633
});
2734

28-
test('serBaseURL() for node', async () => {
35+
test('setBaseURL() for node', async () => {
2936
setBaseURL(lib);
3037
const relFile = './AsyncLoad.child.cjs';
3138
const relUnknown = './AsyncLoad.unknown.cjs';

ts/mathjax.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,8 @@ export const mathjax = {
6464
*/
6565
asyncLoad: null as ((file: string) => any),
6666

67+
/**
68+
* When asyncLoad uses require(), it actually operates synchronously and this is true
69+
*/
70+
asyncIsSynchronous: false,
6771
};

ts/output/common/FontData.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@
2222
* @author [email protected] (Davide Cervone)
2323
*/
2424

25+
import {mathjax} from '../../mathjax.js';
2526
import {OptionList, defaultOptions, userOptions} from '../../util/Options.js';
2627
import {StyleList} from '../../util/StyleList.js';
2728
import {asyncLoad} from '../../util/AsyncLoad.js';
2829
import {retryAfter} from '../../util/Retries.js';
29-
import {mathjax} from '../../mathjax.js';
3030
import {DIRECTION} from './Direction.js';
3131
export {DIRECTION} from './Direction.js';
3232

@@ -1101,7 +1101,7 @@ export class FontData<C extends CharOptions, V extends VariantData<C>, D extends
11011101
const prefix = (!dynamic.extension ? this.options.dynamicPrefix :
11021102
this.CLASS.dynamicExtensions.get(dynamic.extension).prefix);
11031103
return (dynamic.file.match(/^(?:[\/\[]|[a-z]+:\/\/|[a-z]:)/i) ? dynamic.file :
1104-
prefix + '/' + dynamic.file.replace(/\.js$/, ''));
1104+
prefix + '/' + dynamic.file.replace(/(?<!\.js)$/, '.js'));
11051105
}
11061106

11071107
/**
@@ -1141,8 +1141,9 @@ export class FontData<C extends CharOptions, V extends VariantData<C>, D extends
11411141
* synchronous loading function, as in util/asyncLoad/node.ts.
11421142
*/
11431143
public loadDynamicFilesSync() {
1144-
if (!mathjax.asyncLoad) {
1145-
throw Error('MathJax(loadDynamicFilesSync): mathjax.asyncLoad must be specified and synchronous');
1144+
if (!mathjax.asyncIsSynchronous) {
1145+
throw Error('MathJax(loadDynamicFilesSync): mathjax.asyncLoad must be specified and synchronous\n' +
1146+
' Try importing #js/../components/require.mjs and #js/util/asyncLoad/node.js');
11461147
}
11471148
const dynamicFiles = this.CLASS.dynamicFiles;
11481149
Object.keys(dynamicFiles).forEach(name => this.loadDynamicFileSync(dynamicFiles[name]));

ts/util/AsyncLoad.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import {mathjax} from '../mathjax.js';
3131
*/
3232
export function asyncLoad(name: string): Promise<any> {
3333
if (!mathjax.asyncLoad) {
34-
return Promise.reject(`Can't load '${name}': No asyncLoad method specified`);
34+
return Promise.reject(`Can't load '${name}': No mathjax.asyncLoad method specified`);
3535
}
3636
return new Promise((ok, fail) => {
3737
const result = mathjax.asyncLoad(name);

ts/util/asyncLoad/esm.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,16 @@ let root = new URL(import.meta.url).href.replace(/\/util\/asyncLoad\/esm.js$/, '
2727

2828
if (!mathjax.asyncLoad) {
2929
mathjax.asyncLoad = async (name: string) => {
30-
return import(new URL(name, root).href).then((result) => result?.default || result);
30+
const file = (name.charAt(0) === '.' ? new URL(name, root).pathname : name);
31+
return import(file).then((result) => result?.default || result);
3132
};
3233
}
3334

3435
/**
35-
* @param {string} URL the base URL to use for loading relative paths
36+
* @param {string} url the base URL to use for loading relative paths
3637
*/
37-
export function setBaseURL(URL: string) {
38-
root = URL;
38+
export function setBaseURL(url: string) {
39+
root = new URL(url, 'file://').href;
3940
if (!root.match(/\/$/)) {
4041
root += '/';
4142
}

ts/util/asyncLoad/node.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,20 @@
2323

2424
import {mathjax} from '../../mathjax.js';
2525
import * as path from 'path';
26+
import {src} from '#source/source.cjs';
2627

2728
declare var require: (name: string) => any;
28-
declare var __dirname: string;
2929

30-
let root = path.dirname(path.dirname(__dirname));
30+
let root = path.resolve(src, '..', '..', 'cjs');
3131

3232
if (!mathjax.asyncLoad && typeof require !== 'undefined') {
3333
mathjax.asyncLoad = (name: string) => {
3434
return require(name.charAt(0) === '.' ? path.resolve(root, name) : name);
3535
};
36+
mathjax.asyncIsSynchronous = true;
3637
}
3738

39+
3840
/**
3941
* @param {string} URL the base URL to use for loading relative paths
4042
*/

ts/util/asyncLoad/path.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
declare module 'path' {
22
export function dirname(dir: string): string;
3-
export function resolve(root: string, name: string): string;
3+
export function resolve(root: string, ...name: string[]): string;
44
}

ts/util/asyncLoad/system.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,16 @@ let root = 'file://' + __dirname.replace(/\/[^\/]*\/[^\/]*$/, '/');
3030

3131
if (!mathjax.asyncLoad && typeof System !== 'undefined' && System.import) {
3232
mathjax.asyncLoad = (name: string) => {
33-
return System.import(name, root).then((result: any) => result?.default || result);
33+
const file = (name.charAt(0) === '.' ? new URL(name, root) : new URL(name, 'file://')).href;
34+
return System.import(file).then((result: any) => result?.default || result);
3435
};
3536
}
3637

3738
/**
38-
* @param {string} URL the base URL to use for loading relative paths
39+
* @param {string} url the base URL to use for loading relative paths
3940
*/
40-
export function setBaseURL(URL: string) {
41-
root = URL;
41+
export function setBaseURL(url: string) {
42+
root = new URL(url, 'file://').href;
4243
if (!root.match(/\/$/)) {
4344
root += '/';
4445
}

0 commit comments

Comments
 (0)