Skip to content

Commit 49edada

Browse files
authored
Merge pull request #1995 from maartenbreddels/custom_file_for_module
if moduleName contains a slash, use a different module/path
2 parents 45f2c3b + 9679b3c commit 49edada

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

packages/html-manager/src/libembed-amd.ts

+21-2
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,32 @@ let requirePromise = function(pkg: string | string[]): Promise<any> {
1919
});
2020
}
2121

22+
function moduleNameToCDNUrl(moduleName: string, moduleVersion: string) {
23+
let packageName = moduleName;
24+
let fileName = 'index'; // default filename
25+
// if a '/' is present, like 'foo/bar', packageName is changed to 'foo', and path to 'bar'
26+
// We first find the first '/'
27+
let index = moduleName.indexOf('/');
28+
if ((index != -1) && (moduleName[0] == '@')) {
29+
// if we have a namespace, it's a different story
30+
// @foo/bar/baz should translate to @foo/bar and baz
31+
// so we find the 2nd '/'
32+
index = moduleName.indexOf('/', index+1);
33+
}
34+
if (index != -1) {
35+
fileName = moduleName.substr(index+1);
36+
packageName = moduleName.substr(0, index);
37+
}
38+
return `https://unpkg.com/${packageName}@${moduleVersion}/dist/${fileName}.js`;
39+
}
40+
2241
function requireLoader(moduleName: string, moduleVersion: string) {
2342
return requirePromise([`${moduleName}`]).catch((err) => {
2443
let failedId = err.requireModules && err.requireModules[0];
2544
if (failedId) {
2645
console.log(`Falling back to unpkg.com for ${moduleName}@${moduleVersion}`);
27-
return requirePromise([`https://unpkg.com/${moduleName}@${moduleVersion}/dist/index.js`]);
28-
}
46+
return requirePromise([moduleNameToCDNUrl(moduleName, moduleVersion)]);
47+
}
2948
});
3049
}
3150

0 commit comments

Comments
 (0)