/
single.js
68 lines (56 loc) · 1.88 KB
/
single.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import * as rcu from 'rcu';
import get from '../utils/get';
import load from '../load';
var promises = {},
global = ( typeof window !== 'undefined' ? window : {} );
export default loadSingle;
// Load a single component:
//
// Ractive.load( 'path/to/foo' ).then( function ( Foo ) {
// var foo = new Foo(...);
// });
function loadSingle ( path, parentUrl, baseUrl, cache ) {
var promise, url;
url = rcu.resolve( path, path[0] === '.' ? parentUrl : baseUrl );
// if this component has already been requested, don't
// request it again
if ( !cache || !promises[ url ] ) {
promise = get( url ).then( function ( template ) {
return new Promise( function ( fulfil, reject ) {
rcu.make( template, {
url: url,
loadImport: function ( name, path, parentUrl, callback ) {
// if import has a relative URL, it should resolve
// relative to this (parent). Otherwise, relative
// to load.baseUrl
loadSingle( path, parentUrl, baseUrl, cache ).then( callback, reject );
},
require: ractiveRequire
}, fulfil, reject );
});
});
promises[ url ] = promise;
}
return promises[ url ];
}
function ractiveRequire ( name ) {
var dependency, qualified;
dependency = load.modules.hasOwnProperty( name ) ? load.modules[ name ] :
global.hasOwnProperty( name ) ? global[ name ] : null;
if ( !dependency && typeof require === 'function' ) {
try {
dependency = require( name );
} catch (e) {
if (typeof process !== 'undefined') {
dependency = require( process.cwd() + '/' + name );
} else {
throw e;
}
}
}
if ( !dependency ) {
qualified = !/^[$_a-zA-Z][$_a-zA-Z0-9]*$/.test( name ) ? '["' + name + '"]' : '.' + name;
throw new Error( 'Ractive.load() error: Could not find dependency "' + name + '". It should be exposed as Ractive.load.modules' + qualified + ' or window' + qualified );
}
return dependency;
}