diff options
author | Florian Dold <florian.dold@gmail.com> | 2016-10-10 03:50:11 +0200 |
---|---|---|
committer | Florian Dold <florian.dold@gmail.com> | 2016-10-10 03:50:11 +0200 |
commit | 9af485a584e47fd503ed5c62b9f6482574715f1e (patch) | |
tree | 687abbcff412bd4ce5c51d51de030a06e3963cf5 /lib/legacy-transpiler.js |
Squashed 'thirdparty/systemjs/' content from commit 5ed69b6
git-subtree-dir: thirdparty/systemjs
git-subtree-split: 5ed69b6344e8fc1cd43bf758350b2236f57e1499
Diffstat (limited to 'lib/legacy-transpiler.js')
-rw-r--r-- | lib/legacy-transpiler.js | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/lib/legacy-transpiler.js b/lib/legacy-transpiler.js new file mode 100644 index 000000000..79c3d9697 --- /dev/null +++ b/lib/legacy-transpiler.js @@ -0,0 +1,86 @@ +/* + * Traceur, Babel and TypeScript transpile hook for Loader + */ +var transpile = (function() { + + // use Traceur by default + Loader.prototype.transpiler = 'traceur'; + + function transpile(load) { + var self = this; + + return Promise.resolve(__global[self.transpiler == 'typescript' ? 'ts' : self.transpiler] + || (self.pluginLoader || self)['import'](self.transpiler)) + .then(function(transpiler) { + if (transpiler.__useDefault) + transpiler = transpiler['default']; + + var transpileFunction; + if (transpiler.Compiler) + transpileFunction = traceurTranspile; + else if (transpiler.createLanguageService) + transpileFunction = typescriptTranspile; + else + transpileFunction = babelTranspile; + + // note __moduleName will be part of the transformer meta in future when we have the spec for this + return '(function(__moduleName){' + transpileFunction.call(self, load, transpiler) + '\n})("' + load.name + '");\n//# sourceURL=' + load.address + '!transpiled'; + }); + }; + + function traceurTranspile(load, traceur) { + var options = this.traceurOptions || {}; + options.modules = 'instantiate'; + options.script = false; + if (options.sourceMaps === undefined) + options.sourceMaps = 'inline'; + options.filename = load.address; + options.inputSourceMap = load.metadata.sourceMap; + options.moduleName = false; + + var compiler = new traceur.Compiler(options); + + return doTraceurCompile(load.source, compiler, options.filename); + } + function doTraceurCompile(source, compiler, filename) { + try { + return compiler.compile(source, filename); + } + catch(e) { + // on older versions of traceur (<0.9.3), an array of errors is thrown + // rather than a single error. + if (e.length) { + throw e[0]; + } + throw e; + } + } + + function babelTranspile(load, babel) { + var options = this.babelOptions || {}; + options.modules = 'system'; + if (options.sourceMap === undefined) + options.sourceMap = 'inline'; + options.inputSourceMap = load.metadata.sourceMap; + options.filename = load.address; + options.code = true; + options.ast = false; + + return babel.transform(load.source, options).code; + } + + function typescriptTranspile(load, ts) { + var options = this.typescriptOptions || {}; + options.target = options.target || ts.ScriptTarget.ES5; + if (options.sourceMap === undefined) + options.sourceMap = true; + if (options.sourceMap && options.inlineSourceMap !== false) + options.inlineSourceMap = true; + + options.module = ts.ModuleKind.System; + + return ts.transpile(load.source, options, load.address); + } + + return transpile; +})(); |