Skip to content

Commit

Permalink
Add support for requirejs 'module' module. (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
blois authored Jul 11, 2022
1 parent 294824f commit 3ecefb8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/amd.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,11 @@ test('getHostedModuleUrl', () => {
.pathname
).toBe('/npm/@jupyter-widgets/[email protected]/css/index.css');
});

test('requirejs magic modules', async () => {
const loader = new Loader();
loader.define('foo', ['module'], (module) => {
return `module: ${module.id}`;
});
expect(await loader.load('foo')).toBe('module: foo');
});
10 changes: 10 additions & 0 deletions src/amd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,14 @@ export class Loader {
const requirements = await Promise.all(
module.dependencies.map((dependency) => {
const definition = this.definitions.get(dependency);
// Support requirejs magic modules:
// https://github.com/requirejs/requirejs/wiki/Differences-between-the-simplified-CommonJS-wrapper-and-standard-AMD-define#magic
if (dependency === 'module') {
return {
id: module.id,
url: this.resolveModule(module.id),
};
}
if (!definition) {
throw new Error(`Unknown dependency ${dependency}`);
}
Expand Down Expand Up @@ -121,6 +129,7 @@ export class Loader {
const module = {
factory: factory as () => unknown,
dependencies,
id: moduleId,
};
if (!definition) {
this.definitions.set(moduleId, {
Expand Down Expand Up @@ -158,6 +167,7 @@ interface Definition {
}

interface Module {
id: string;
dependencies: string[];
factory: (...args: unknown[]) => unknown;
exports?: Promise<unknown>;
Expand Down

0 comments on commit 3ecefb8

Please sign in to comment.