Skip to content

Commit

Permalink
🪨: fix +esm url handling when fetching via esm.run
Browse files Browse the repository at this point in the history
  • Loading branch information
merryman authored and linusha committed Jul 29, 2024
1 parent 8cba057 commit cf188b5
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions lively.resources/src/esm-resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export class ESMResource extends Resource {
// jspm servers both the entry point into a package as well as subcontent from package@version/
// differentiate these cases by introducing an index.js which will automatically be served by systemJS
if (pathStructure.length === 1 ||
!pathStructure[pathStructure.length - 1].endsWith('+esm') &&
!pathStructure[pathStructure.length - 1].endsWith('js') &&
!pathStructure[pathStructure.length - 1].endsWith('!cjs')) {
let fileName = 'index.js';
Expand All @@ -24,6 +25,11 @@ export class ESMResource extends Resource {
pathStructure.push(fileName);
}


if (pathStructure[pathStructure.length - 1].endsWith('+esm')) {
pathStructure[pathStructure.length - 1] = pathStructure[pathStructure.length - 1].replace('+esm', 'esm.js');
}

if (pathStructure[pathStructure.length - 1].endsWith('.js!cjs')) {
pathStructure[pathStructure.length - 1] = pathStructure[pathStructure.length - 1].replace('.js!cjs', '.cjs');
}
Expand All @@ -40,7 +46,8 @@ export class ESMResource extends Resource {

const id = this.url.replace(/esm:\/\/(run|cache)\//g, '');
let baseUrl = 'https://jspm.dev/';
if (this.url.startsWith('esm://run/')) baseUrl = 'https://esm.run/';
if (this.url.startsWith('esm://run/npm/')) baseUrl = 'https://cdn.jsdelivr.net/';
else if (this.url.startsWith('esm://run/')) baseUrl = 'https://esm.run/';

let pathStructure = ESMResource.normalize(id);

Expand Down Expand Up @@ -69,7 +76,9 @@ export class ESMResource extends Resource {

// pathElements can together either describe a directory or a file
// in the case that it is not a file (will be either js or !cjs) we need to fixup the last part of the path
if (!pathElements[pathElements.length - 1].endsWith('js') && !pathElements[pathElements.length - 1].endsWith('!cjs')) {
if (!pathElements[pathElements.length - 1].endsWith('js') &&
!pathElements[pathElements.length - 1].endsWith('!cjs') &&
!pathElements[pathElements.length - 1].endsWith('+esm')) {
pathElements[pathElements.length - 1] = pathElements[pathElements.length - 1] + '/';
}

Expand Down

0 comments on commit cf188b5

Please sign in to comment.