Skip to content
This repository was archived by the owner on Jul 13, 2020. It is now read-only.

Commit 23fd5ba

Browse files
committed
eval consolidation
1 parent 9e48717 commit 23fd5ba

File tree

7 files changed

+86
-93
lines changed

7 files changed

+86
-93
lines changed

Gruntfile.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,23 @@ module.exports = function (grunt) {
2222
files: {
2323
'dist/<%= pkg.name %>-prod.src.js': [
2424
'node_modules/when/es6-shim/Promise.js',
25-
'src/polyfill-wrapper-start.js',
25+
'src/wrapper-start.js',
2626
'src/loader.js',
2727
'src/dynamic-only.js',
2828
'src/url.js',
2929
'src/system.js',
30-
'src/polyfill-wrapper-end.js'
30+
'src/wrapper-end.js'
3131
],
3232
'dist/<%= pkg.name %>.src.js': [
3333
'node_modules/when/es6-shim/Promise.js',
34-
'src/polyfill-wrapper-start.js',
34+
'src/wrapper-start.js',
3535
'src/loader.js',
3636
'src/declarative.js',
3737
'src/transpiler.js',
3838
'src/url.js',
3939
'src/system.js',
4040
'src/module-tag.js',
41-
'src/polyfill-wrapper-end.js'
41+
'src/wrapper-end.js'
4242
]
4343
}
4444
}

src/loader.js

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,8 @@ function logloads(loads) {
244244
load.declare = declare;
245245
load.depsList = deps;
246246
}
247-
__eval(transpiled, __global, load);
247+
// empty {} context is closest to undefined 'this' we can get
248+
__eval(transpiled, load.address, {});
248249
curSystem.register = curRegister;
249250
});
250251
}
@@ -571,14 +572,6 @@ function logloads(loads) {
571572
return module;
572573
}
573574

574-
function addToError(err, msg) {
575-
if (err instanceof Error)
576-
err.message = msg + err.message;
577-
else
578-
err = msg + err;
579-
return err;
580-
}
581-
582575
// 26.3 Loader
583576

584577
// 26.3.1.1

src/polyfill-wrapper-end.js

Lines changed: 0 additions & 32 deletions
This file was deleted.

src/polyfill-wrapper-start.js

Lines changed: 0 additions & 35 deletions
This file was deleted.

src/transpiler.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,16 @@
2626
return self['import'](self.transpiler).then(function(transpiler) {
2727
if (transpiler.__useDefault)
2828
transpiler = transpiler['default'];
29-
return 'var __moduleAddress = "' + load.address + '";' + (transpiler.Compiler ? traceurTranspile : babelTranspile).call(self, load, transpiler);
29+
30+
return 'var __moduleName = "' + load.name + '", __moduleAddress = "' + load.address + '";'
31+
+ (transpiler.Compiler ? traceurTranspile : babelTranspile).call(self, load, transpiler)
32+
+ '\n//# sourceURL=' + load.address + '!eval';
33+
34+
// sourceURL and sourceMappingURL:
35+
// Ideally we wouldn't need a sourceURL and would just use the sourceMap.
36+
// But without the sourceURL as well, line-by-line debugging doesn't work.
37+
// We thus need to ensure the sourceURL is a different name to the original
38+
// source, and hence the !eval suffix.
3039
});
3140
};
3241

@@ -42,7 +51,7 @@
4251
var curSystem = __global.System;
4352
var curLoader = __global.Reflect.Loader;
4453
// ensure not detected as CommonJS
45-
__eval('(function(require,exports,module){' + load.source + '})();', __global, load);
54+
__eval('(function(require,exports,module){' + load.source + '})();', load.address, __global);
4655
__global.System = curSystem;
4756
__global.Reflect.Loader = curLoader;
4857
return getTranspilerModule(self, load.name);
@@ -62,13 +71,8 @@
6271
options.moduleName = false;
6372

6473
var compiler = new traceur.Compiler(options);
65-
var source = doTraceurCompile(load.source, compiler, options.filename);
6674

67-
// add "!eval" to end of Traceur sourceURL
68-
// I believe this does something?
69-
source += '!eval';
70-
71-
return source;
75+
return doTraceurCompile(load.source, compiler, options.filename);
7276
}
7377
function doTraceurCompile(source, compiler, filename) {
7478
try {
@@ -91,9 +95,5 @@
9195
if (!options.blacklist)
9296
options.blacklist = ['react'];
9397

94-
var source = babel.transform(load.source, options).code;
95-
96-
// add "!eval" to end of Babel sourceURL
97-
// I believe this does something?
98-
return source + '\n//# sourceURL=' + load.address + '!eval';
98+
return babel.transform(load.source, options).code;
9999
}

src/wrapper-end.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// -- exporting --
2+
3+
if (typeof exports === 'object')
4+
module.exports = Loader;
5+
6+
__global.Reflect = __global.Reflect || {};
7+
__global.Reflect.Loader = __global.Reflect.Loader || Loader;
8+
__global.Reflect.global = __global.Reflect.global || __global;
9+
__global.LoaderPolyfill = Loader;
10+
11+
if (typeof exports === 'object')
12+
module.exports = System;
13+
14+
__global.System = System;
15+
16+
})(typeof window != 'undefined' ? window : (typeof WorkerGlobalScope != 'undefined' ? self : global));

src/wrapper-start.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
(function(__global) {
2+
3+
var isWorker = typeof self !== 'undefined' && typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope;
4+
var isBrowser = typeof window != 'undefined' && !isWorker;
5+
var isWindows = typeof process != 'undefined' && !!process.platform.match(/^win/);
6+
7+
if (__global.console)
8+
console.assert = console.assert || function() {};
9+
10+
// IE8 support
11+
var indexOf = Array.prototype.indexOf || function(item) {
12+
for (var i = 0, thisLen = this.length; i < thisLen; i++) {
13+
if (this[i] === item) {
14+
return i;
15+
}
16+
}
17+
return -1;
18+
};
19+
20+
var defineProperty;
21+
(function () {
22+
try {
23+
if (!!Object.defineProperty({}, 'a', {}))
24+
defineProperty = Object.defineProperty;
25+
}
26+
catch (e) {
27+
defineProperty = function(obj, prop, opt) {
28+
try {
29+
obj[prop] = opt.value || opt.get.call(obj);
30+
}
31+
catch(e) {}
32+
}
33+
}
34+
})();
35+
36+
function addToError(err, msg) {
37+
if (err instanceof Error)
38+
err.message = msg + err.message;
39+
else
40+
err = msg + err;
41+
return err;
42+
}
43+
44+
function __eval(source, debugName, context) {
45+
try {
46+
new Function(source).call(context);
47+
}
48+
catch(e) {
49+
throw addToError(e, 'Evaluating ' + debugName);
50+
}
51+
}

0 commit comments

Comments
 (0)