forked from angular/angular
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheval_module.ts
More file actions
38 lines (34 loc) · 1.54 KB
/
Copy patheval_module.ts
File metadata and controls
38 lines (34 loc) · 1.54 KB
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
import {Promise, PromiseWrapper} from 'angular2/src/core/facade/async';
import {isPresent, global} from 'angular2/src/core/facade/lang';
var evalCounter = 0;
function nextModuleName() {
return `evalScript${evalCounter++}`;
}
export function evalModule(moduleSource: string, moduleImports: string[][], args: any[]):
Promise<any> {
var moduleName = nextModuleName();
var moduleSourceWithImports = [];
var importModuleNames = [];
moduleImports.forEach(sourceImport => {
var modName = sourceImport[0];
var modAlias = sourceImport[1];
importModuleNames.push(modName);
// Note: After transpilation to commonJS and loading this file in a browser
// using SystemJS, the loader might get confused by the presence of require,
// and attempt to load "+ modName +.js" !?!
// A simple string concat manages to prevent that, but that is one compiler
// optimaztion away from breaking again. Proceed with caution!
moduleSourceWithImports.push(`var ${modAlias} = require` + `('${modName}');`);
});
moduleSourceWithImports.push(moduleSource);
var moduleBody = new Function('require', 'exports', 'module', moduleSourceWithImports.join('\n'));
var System = global['System'];
if (isPresent(System) && isPresent(System.registerDynamic)) {
System.registerDynamic(moduleName, importModuleNames, false, moduleBody);
return <Promise<any>>System.import(moduleName).then((module) => module.run(args));
} else {
var exports = {};
moduleBody(require, exports, {});
return PromiseWrapper.resolve(exports['run'](args));
}
}