@@ -19,13 +19,32 @@ let requirePromise = function(pkg: string | string[]): Promise<any> {
19
19
} ) ;
20
20
}
21
21
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
+
22
41
function requireLoader ( moduleName : string , moduleVersion : string ) {
23
42
return requirePromise ( [ `${ moduleName } ` ] ) . catch ( ( err ) => {
24
43
let failedId = err . requireModules && err . requireModules [ 0 ] ;
25
44
if ( failedId ) {
26
45
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
+ }
29
48
} ) ;
30
49
}
31
50
0 commit comments