UNPKG

3.67 kBJavaScriptView Raw
1var assign = require("lodash/assign");
2var merge = require("lodash/merge");
3
4var trace = function(System, BuildSystem, startupCalledPromise, onFulfilled){
5
6 System.pluginLoader = BuildSystem;
7 BuildSystem.localLoader = System;
8
9 // override System.config to keep track of the configuration properties set
10 var systemConfig = System.config;
11 System.config = function(cfg) {
12 System.__loaderConfig = System.__loaderConfig || {};
13 assign(System.__loaderConfig, cfg);
14 systemConfig.apply(System, arguments);
15 };
16
17 var systemLocate = System.locate;
18 System.locate = function(load) {
19 if(load.metadata.parsedModuleName && !load.metadata.packageJson) {
20 var pmn = load.metadata.parsedModuleName;
21 var packages = this.npmContext.packages, pkg;
22 for(var i = 0; i < packages.length; i++) {
23 pkg = packages[i];
24 if(pkg.name === pmn.packageName && pkg.version === pmn.version) {
25 load.metadata.packageJson = pkg;
26 break;
27 }
28 }
29 }
30 return systemLocate.apply(this, arguments);
31 };
32
33 // The BuildSystem loader will execute modules, but wait for the value to come through
34 var buildInstantiate = BuildSystem.instantiate;
35 BuildSystem.instantiate = function(load){
36 var res = buildInstantiate.apply(this, arguments);
37
38 // turn on this flag if you need the module to include its local dependencies in the
39 // generated bundle but might want to load different dependencies during build time.
40 // E.g: during build, steal-less needs to load the NodeJS version of the less engine but
41 // the development bundles need to include the browser version to work correctly.
42 if (load.metadata.useLocalDeps) {
43 return startupCalledPromise
44 .then(function() {
45 return System.import(System.configMain);
46 })
47 .then(function() {
48 return System.import(load.name);
49 })
50 .then(function () {
51 return res;
52 });
53 }
54 else {
55 // Get the value of the plugin (this will let us check for includeInBuild)
56 BuildSystem.import(load.name).then(function(pluginValue){
57 //var deps = BuildSystem._traceData.deps[load.name];
58 //var dependencies = BuildSystem.getDependencies(load.name);
59 var deps = load.metadata.deps || [];
60 var dependencies = load.metadata.dependencies || [];
61 onFulfilled(load, deps, dependencies, pluginValue);
62 });
63
64 return res;
65 }
66 };
67
68 var buildConfig = BuildSystem.config;
69 BuildSystem.config = function(cfg){
70 // Merge meta configuration
71 if(cfg && cfg.meta && this.meta) {
72 cfg = assign({}, cfg, {
73 meta: merge({}, this.meta, cfg.meta)
74 });
75 }
76 buildConfig.call(this, cfg);
77 };
78
79 System.preventModuleExecution = true;
80 System.allowModuleExecution(System.configMain);
81
82 // Override instantiate
83 var systemInstantiate = System.instantiate;
84 System.instantiate = function(load){
85 // Figure out if there's a plugin
86 var pluginName = load.metadata.pluginName;
87
88 var res = systemInstantiate.apply(this, arguments);
89
90 return Promise.resolve(res).then(function fullfill(instantiateResult){
91 var deps = load.metadata.deps || [];
92 var dependencies = load.metadata.dependencies || [];
93 if(pluginName) {
94 deps = deps.concat([pluginName]);
95 dependencies = dependencies.concat([pluginName]);
96 }
97
98 // If the config is a global mark it as cjs so that it will be converted
99 // to AMD by transpile. Needed because of this bug:
100 // https://github.com/ModuleLoader/es6-module-loader/issues/231
101 if(load.name === System.configMain && load.metadata.format === "global") {
102 load.metadata.format = "cjs";
103 }
104
105 if(load.metadata.includeInDependencyGraph !== false) {
106 onFulfilled(load, deps, dependencies);
107 }
108
109 return instantiateResult;
110 });
111 };
112};
113
114module.exports = trace;