|
109 | 109 | },
|
110 | 110 | isWebWorker = typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope,
|
111 | 111 | isNode = typeof process === "object" && {}.toString.call(process) === "[object process]",
|
112 |
| - isBrowserWithWindow = !isNode && typeof window !== "undefined"; |
| 112 | + isBrowserWithWindow = !isNode && typeof window !== "undefined", |
| 113 | + warn = typeof console === "object" ? console.warn.bind(console) : function(){}; |
113 | 114 |
|
114 | 115 | var filename = function(uri){
|
115 | 116 | var lastSlash = uri.lastIndexOf("/");
|
@@ -698,6 +699,93 @@ if(typeof System !== "undefined") {
|
698 | 699 | applyTraceExtension(System);
|
699 | 700 | }
|
700 | 701 |
|
| 702 | +/* |
| 703 | + SystemJS JSON Format |
| 704 | + Provides the JSON module format definition. |
| 705 | +*/ |
| 706 | +function _SYSTEM_addJSON(loader) { |
| 707 | + var jsonTest = /^[\s\n\r]*[\{\[]/; |
| 708 | + var jsonExt = /\.json$/i; |
| 709 | + var jsExt = /\.js$/i; |
| 710 | + |
| 711 | + // Add the extension to _extensions so that it can be cloned. |
| 712 | + loader._extensions.push(_SYSTEM_addJSON); |
| 713 | + |
| 714 | + // if someone has a moduleName that is .json, make sure it loads a json file |
| 715 | + // no matter what paths might do |
| 716 | + var loaderLocate = loader.locate; |
| 717 | + loader.locate = function(load){ |
| 718 | + return loaderLocate.apply(this, arguments).then(function(address){ |
| 719 | + if(jsonExt.test(load.name)) { |
| 720 | + return address.replace(jsExt, ""); |
| 721 | + } |
| 722 | + |
| 723 | + return address; |
| 724 | + }); |
| 725 | + }; |
| 726 | + |
| 727 | + var transform = function(loader, load, data){ |
| 728 | + var fn = loader.jsonOptions && loader.jsonOptions.transform; |
| 729 | + if(!fn) return data; |
| 730 | + return fn.call(loader, load, data); |
| 731 | + }; |
| 732 | + |
| 733 | + // If we are in a build we should convert to CommonJS instead. |
| 734 | + if(isNode) { |
| 735 | + var loaderTranslate = loader.translate; |
| 736 | + loader.translate = function(load){ |
| 737 | + if(jsonExt.test(load.name)) { |
| 738 | + var parsed = parse(load); |
| 739 | + if(parsed) { |
| 740 | + parsed = transform(this, load, parsed); |
| 741 | + return "def" + "ine([], function(){\n" + |
| 742 | + "\treturn " + JSON.stringify(parsed) + "\n});"; |
| 743 | + } |
| 744 | + } |
| 745 | + |
| 746 | + return loaderTranslate.call(this, load); |
| 747 | + }; |
| 748 | + return; |
| 749 | + } |
| 750 | + |
| 751 | + var loaderInstantiate = loader.instantiate; |
| 752 | + loader.instantiate = function(load) { |
| 753 | + var loader = this, |
| 754 | + parsed; |
| 755 | + |
| 756 | + parsed = parse(load); |
| 757 | + if(parsed) { |
| 758 | + parsed = transform(loader, load, parsed); |
| 759 | + load.metadata.format = 'json'; |
| 760 | + |
| 761 | + load.metadata.execute = function(){ |
| 762 | + return parsed; |
| 763 | + }; |
| 764 | + } |
| 765 | + |
| 766 | + return loaderInstantiate.call(loader, load); |
| 767 | + }; |
| 768 | + |
| 769 | + return loader; |
| 770 | + |
| 771 | + // Attempt to parse a load as json. |
| 772 | + function parse(load){ |
| 773 | + if ( (load.metadata.format === 'json' || !load.metadata.format) && jsonTest.test(load.source) ) { |
| 774 | + try { |
| 775 | + return JSON.parse(load.source); |
| 776 | + } catch(e) { |
| 777 | + warn("Error parsing " + load.address + ":", e); |
| 778 | + return {}; |
| 779 | + } |
| 780 | + } |
| 781 | + |
| 782 | + } |
| 783 | +} |
| 784 | + |
| 785 | +if (typeof System !== "undefined") { |
| 786 | + _SYSTEM_addJSON(System); |
| 787 | +} |
| 788 | + |
701 | 789 | // Overwrites System.config with setter hooks
|
702 | 790 | var setterConfig = function(loader, configOrder, configSpecial){
|
703 | 791 | var oldConfig = loader.config;
|
@@ -1448,7 +1536,6 @@ if (typeof System !== "undefined") {
|
1448 | 1536 | steal.clone = cloneSteal;
|
1449 | 1537 | module.exports = global.steal;
|
1450 | 1538 | global.steal.addSteal = addSteal;
|
1451 |
| - require("system-json"); |
1452 | 1539 |
|
1453 | 1540 | } else {
|
1454 | 1541 | var oldSteal = global.steal;
|
|
0 commit comments