Skip to content

Commit 3ecefb8

Browse files
author
blois
authored
Add support for requirejs 'module' module. (#26)
#25
1 parent 294824f commit 3ecefb8

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

src/amd.spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,11 @@ test('getHostedModuleUrl', () => {
3131
.pathname
3232
).toBe('/npm/@jupyter-widgets/[email protected]/css/index.css');
3333
});
34+
35+
test('requirejs magic modules', async () => {
36+
const loader = new Loader();
37+
loader.define('foo', ['module'], (module) => {
38+
return `module: ${module.id}`;
39+
});
40+
expect(await loader.load('foo')).toBe('module: foo');
41+
});

src/amd.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,14 @@ export class Loader {
9191
const requirements = await Promise.all(
9292
module.dependencies.map((dependency) => {
9393
const definition = this.definitions.get(dependency);
94+
// Support requirejs magic modules:
95+
// https://github.com/requirejs/requirejs/wiki/Differences-between-the-simplified-CommonJS-wrapper-and-standard-AMD-define#magic
96+
if (dependency === 'module') {
97+
return {
98+
id: module.id,
99+
url: this.resolveModule(module.id),
100+
};
101+
}
94102
if (!definition) {
95103
throw new Error(`Unknown dependency ${dependency}`);
96104
}
@@ -121,6 +129,7 @@ export class Loader {
121129
const module = {
122130
factory: factory as () => unknown,
123131
dependencies,
132+
id: moduleId,
124133
};
125134
if (!definition) {
126135
this.definitions.set(moduleId, {
@@ -158,6 +167,7 @@ interface Definition {
158167
}
159168

160169
interface Module {
170+
id: string;
161171
dependencies: string[];
162172
factory: (...args: unknown[]) => unknown;
163173
exports?: Promise<unknown>;

0 commit comments

Comments
 (0)