Comparing version 0.0.44 to 0.0.45
{ | ||
"name": "traceur", | ||
"version": "0.0.44", | ||
"version": "0.0.45", | ||
"description": "ES6 to ES5 compiler", | ||
@@ -48,3 +48,3 @@ "keywords": [ | ||
"semver": "2.2.1", | ||
"traceur": "0.0.43", | ||
"traceur": "0.0.44", | ||
"promises-aplus-tests": "~2.0.4" | ||
@@ -51,0 +51,0 @@ }, |
@@ -24,102 +24,41 @@ // Copyright 2013 Traceur Authors. | ||
var traceur = require('./traceur.js'); | ||
var AttachModuleNameTransformer = | ||
traceur.codegeneration.module.AttachModuleNameTransformer; | ||
var ErrorReporter = traceur.util.CollectingErrorReporter; | ||
var FromOptionsTransformer = traceur.codegeneration.FromOptionsTransformer; | ||
var PureES6Transformer = traceur.codegeneration.PureES6Transformer; | ||
var Parser = traceur.syntax.Parser; | ||
var SourceFile = traceur.syntax.SourceFile; | ||
var SourceMapGenerator = traceur.outputgeneration.SourceMapGenerator; | ||
var TreeWriter = traceur.outputgeneration.TreeWriter; | ||
var traceurOptions = traceur.options; | ||
function merge(dest) { | ||
var src, i; | ||
for (i = 1; i < arguments.length; i++) { | ||
src = arguments[i]; | ||
Object.keys(src).forEach(function(key) { | ||
dest[key] = src[key]; | ||
}); | ||
} | ||
var ModuleToCommonJSCompiler = traceur.ModuleToCommonJSCompiler; | ||
return dest; | ||
function NodeCompiler() { | ||
ModuleToCommonJSCompiler.call(this); | ||
this.cwd = process.cwd(); | ||
} | ||
// The absolute path to traceur-runtime.js -- the file that should be executed | ||
// if you want to run Traceur-compiled scripts when the compiler isn't present. | ||
var RUNTIME_PATH = path.join(__dirname, '../../bin/traceur-runtime.js'); | ||
NodeCompiler.prototype = { | ||
__proto__: ModuleToCommonJSCompiler.prototype, | ||
resolveModuleName: function(filename) { | ||
var moduleName = filename.replace(/\.js$/, ''); | ||
return path.relative(this.cwd, moduleName).replace(/\\/g,'/'); | ||
}, | ||
sourceRootForFilename: function(filename) { | ||
return path.relative(path.dirname(filename), '.'); | ||
} | ||
}; | ||
/** | ||
* Compile ES6 source code with Traceur. | ||
* Use Traceur to Compile ES6 module source code to commonjs format. | ||
* | ||
* TODO(vojta): Support source maps. | ||
* | ||
* @param {string} content ES6 source code. | ||
* @param {Object=} options Traceur options. | ||
* @return {string} Transpiled ES5 code. | ||
* @return {{js: string, errors: Array, sourceMap: string} Transpiled code. | ||
*/ | ||
function compile(content, options) { | ||
options = merge({ | ||
outputLanguage: 'es5', | ||
modules: 'commonjs', | ||
filename: '<unknown file>', | ||
sourceMap: false, | ||
cwd: process.cwd(), | ||
moduleName: false | ||
}, options || {}); | ||
function moduleToCommonJS(content, options) { | ||
return new NodeCompiler().compile(content, options); | ||
} | ||
var moduleName = options.moduleName; | ||
// The absolute path to traceur-runtime.js -- the file that should be executed | ||
// if you want to run Traceur-compiled scripts when the compiler isn't present. | ||
var RUNTIME_PATH = path.join(__dirname, '../../bin/traceur-runtime.js'); | ||
traceurOptions.reset(); | ||
merge(traceurOptions, options); | ||
var errorReporter = new ErrorReporter(); | ||
var sourceFile = new SourceFile(options.filename, content); | ||
var parser = new Parser(sourceFile, errorReporter); | ||
var tree = parser.parseModule(); | ||
var transformer; | ||
if (moduleName === true || options.modules == 'register' || options.modules == 'inline') { | ||
moduleName = options.filename.replace(/\.js$/, ''); | ||
moduleName = path.relative(options.cwd, moduleName).replace(/\\/g,'/'); | ||
} | ||
if (moduleName) { | ||
transformer = new AttachModuleNameTransformer(moduleName); | ||
tree = transformer.transformAny(tree); | ||
} | ||
if (options.outputLanguage.toLowerCase() === 'es6') { | ||
transformer = new PureES6Transformer(errorReporter); | ||
} else { | ||
transformer = new FromOptionsTransformer(errorReporter); | ||
} | ||
var transformedTree = transformer.transform(tree); | ||
if (errorReporter.hadError()) { | ||
return { | ||
js: null, | ||
errors: errorReporter.errors, | ||
sourceMap: null | ||
}; | ||
} | ||
var treeWriterOptions = {}; | ||
if (options.sourceMap) { | ||
treeWriterOptions.sourceMapGenerator = new SourceMapGenerator({ | ||
file: options.filename, | ||
sourceRoot: null | ||
}); | ||
} | ||
return { | ||
js: TreeWriter.write(transformedTree, treeWriterOptions), | ||
errors: errorReporter.errors, | ||
sourceMap: treeWriterOptions.sourceMap || null | ||
}; | ||
// extend traceur module | ||
module.exports = { | ||
__proto__: traceur, | ||
compile: moduleToCommonJS, | ||
RUNTIME_PATH: RUNTIME_PATH | ||
}; | ||
// extend traceur module | ||
module.exports = Object.create(traceur); | ||
module.exports.compile = compile; | ||
module.exports.RUNTIME_PATH = RUNTIME_PATH; |
@@ -133,5 +133,7 @@ // Copyright 2013 Traceur Authors. | ||
throw new Error('Compile all in directory requires exactly one input filename'); | ||
var compiler = new traceur.Compiler(traceur.options); | ||
var compileAllJsFilesInDir = | ||
require('./compile-single-file.js').compileAllJsFilesInDir; | ||
compileAllJsFilesInDir(dir, rootSources[0].name, true); | ||
compileAllJsFilesInDir(dir, rootSources[0].name, | ||
compiler.compile.bind(compiler)); | ||
} else { | ||
@@ -138,0 +140,0 @@ rootSources.forEach(function(obj) { |
@@ -19,33 +19,13 @@ // Copyright 2013 Traceur Authors. | ||
var path = require('path'); | ||
var writeTreeToFile = require('./compiler.js').writeTreeToFile; | ||
var writeCompiledCodeToFile = require('./compiler.js').writeCompiledCodeToFile; | ||
var traceur = require('./traceur.js'); | ||
var ErrorReporter = traceur.util.ErrorReporter; | ||
var AttachModuleNameTransformer = | ||
traceur.codegeneration.module.AttachModuleNameTransformer; | ||
var FromOptionsTransformer = traceur.codegeneration.FromOptionsTransformer; | ||
var Parser = traceur.syntax.Parser; | ||
var SourceFile = traceur.syntax.SourceFile; | ||
function compileSingleFile(inputFilePath, outputFilePath, anonymousModules) { | ||
function compileSingleFile(inputFilePath, outputFilePath, compile) { | ||
return fs.read(inputFilePath).then(function(contents) { | ||
var reporter = new ErrorReporter(); | ||
var sourceFile = new SourceFile(inputFilePath, contents); | ||
var parser = new Parser(sourceFile, reporter); | ||
var tree = parser.parseModule(); | ||
var moduleName, transformer; | ||
if (!anonymousModules) { | ||
moduleName = inputFilePath.replace(/\.js$/, '').replace(/\\/g,'/'); | ||
// Module naming uses ./ to signal relative names. | ||
if (moduleName[0] !== '/') | ||
moduleName = './' + moduleName; | ||
transformer = new AttachModuleNameTransformer(moduleName); | ||
tree = transformer.transformAny(tree); | ||
} | ||
transformer = new FromOptionsTransformer(reporter); | ||
var transformed = transformer.transform(tree); | ||
var result = compile(contents); | ||
if (!reporter.hadError()) { | ||
writeTreeToFile(transformed, outputFilePath); | ||
} | ||
if (result.error) | ||
throw new Error(result.errors.join('\n')); | ||
writeCompiledCodeToFile(result.js, outputFilePath, result.sourceMap); | ||
}); | ||
@@ -58,3 +38,6 @@ } | ||
function compileAllJsFilesInDir(inputDir, outputDir, anonymousModules) { | ||
function compileAllJsFilesInDir(inputDir, outputDir, compile) { | ||
if (typeof compile !== 'function') | ||
throw new Error('Missing required function(string) -> result'); | ||
inputDir = path.normalize(inputDir); | ||
@@ -65,3 +48,3 @@ outputDir = path.normalize(outputDir); | ||
var outputFilePath = inputFilePath.replace(inputDir, outputDir); | ||
compileSingleFile(inputFilePath, outputFilePath, anonymousModules).done(); | ||
compileSingleFile(inputFilePath, outputFilePath, compile).done(); | ||
}); | ||
@@ -68,0 +51,0 @@ }).done(); |
@@ -27,4 +27,2 @@ // Copyright 2013 Traceur Authors. | ||
var normalizePath = util.normalizePath; | ||
var ErrorReporter = traceur.util.ErrorReporter; | ||
var TreeWriter = traceur.outputgeneration.TreeWriter; | ||
@@ -40,3 +38,2 @@ var SourceMapGenerator = traceur.outputgeneration.SourceMapGenerator; | ||
if (useSourceMaps) { | ||
var sourceMapFilePath = getSourceMapFileName(filename); | ||
var config = { | ||
@@ -51,3 +48,10 @@ file: path.basename(filename), | ||
var compiledCode = TreeWriter.write(tree, options); | ||
if (useSourceMaps) { | ||
var sourcemap = useSourceMaps ? options.sourcemap : null; | ||
writeCompiledCodeToFile(compiledCode, filename, sourcemap); | ||
} | ||
function writeCompiledCodeToFile(compiledCode, filename, sourcemap) { | ||
var sourceMapFilePath | ||
if (sourcemap) { | ||
sourceMapFilePath = getSourceMapFileName(filename); | ||
compiledCode += '\n//# sourceMappingURL=' + | ||
@@ -57,8 +61,7 @@ path.basename(sourceMapFilePath) + '\n'; | ||
writeFile(filename, compiledCode); | ||
if (useSourceMaps) | ||
writeFile(sourceMapFilePath, options.sourceMap); | ||
if (sourcemap) | ||
writeFile(sourceMapFilePath, sourcemap); | ||
} | ||
function compileToSingleFile(outputFile, includes, useSourceMaps) { | ||
var reporter = new ErrorReporter(); | ||
var resolvedOutputFile = path.resolve(outputFile); | ||
@@ -82,3 +85,3 @@ var outputDir = path.dirname(resolvedOutputFile); | ||
inlineAndCompile(resolvedIncludes, traceur.options, reporter, function(tree) { | ||
inlineAndCompile(resolvedIncludes, traceur.options, function(tree) { | ||
writeTreeToFile(tree, resolvedOutputFile, useSourceMaps); | ||
@@ -93,3 +96,2 @@ process.exit(0); | ||
function compileToDirectory(outputDir, includes, useSourceMaps) { | ||
var reporter = new ErrorReporter(); | ||
var outputDir = path.resolve(outputDir); | ||
@@ -104,3 +106,2 @@ | ||
inlineAndCompile(includes.slice(current, current + 1), traceur.options, | ||
reporter, | ||
function(tree) { | ||
@@ -123,2 +124,2 @@ var outputFile = path.join(outputDir, includes[current].name); | ||
exports.compileToDirectory = compileToDirectory; | ||
exports.writeTreeToFile = writeTreeToFile; | ||
exports.writeCompiledCodeToFile = writeCompiledCodeToFile; |
@@ -29,3 +29,2 @@ // Copyright 2012 Traceur Authors. | ||
/** | ||
* @param {ErrorReporter} reporter | ||
* @param {Array.<ParseTree>} elements | ||
@@ -35,4 +34,4 @@ * @param {string|undefined} depTarget A valid depTarget means dependency | ||
*/ | ||
function InlineLoaderHooks(reporter, url, elements, depTarget) { | ||
LoaderHooks.call(this, reporter, url, | ||
function InlineLoaderHooks(url, elements, depTarget) { | ||
LoaderHooks.call(this, null, url, | ||
nodeLoader, // Load modules using node fs. | ||
@@ -75,3 +74,2 @@ moduleStore); // Look up modules in our static module store | ||
* 'fileNamesAndTypes' being printed to stdout, with 'depTarget' as the target. | ||
* @param {ErrorReporter} reporter | ||
* @param {Function} callback Callback used to return the result. A null result | ||
@@ -82,3 +80,3 @@ * indicates that inlineAndCompile has returned successfully from a | ||
*/ | ||
function inlineAndCompile(fileNamesAndTypes, options, reporter, callback, errback) { | ||
function inlineAndCompile(fileNamesAndTypes, options, callback, errback) { | ||
@@ -106,3 +104,3 @@ var depTarget = options && options.depTarget; | ||
var elements = []; | ||
var hooks = new InlineLoaderHooks(reporter, basePath, elements, depTarget); | ||
var hooks = new InlineLoaderHooks(basePath, elements, depTarget); | ||
var loader = new TraceurLoader(hooks); | ||
@@ -109,0 +107,0 @@ |
@@ -21,8 +21,2 @@ // Copyright 2013 Traceur Authors. | ||
var ErrorReporter = traceur.util.ErrorReporter; | ||
var FromOptionsTransformer = traceur.codegeneration.FromOptionsTransformer; | ||
var Parser = traceur.syntax.Parser; | ||
var SourceFile = traceur.syntax.SourceFile; | ||
var TreeWriter = traceur.outputgeneration.TreeWriter; | ||
var ext = '.traceur-compiled'; | ||
@@ -36,14 +30,9 @@ | ||
function compile(filename) { | ||
traceur.options.modules = 'commonjs'; | ||
var contents = fs.readFileSync(filename, 'utf-8'); | ||
var compiler = new traceur.ModuleToCommonJSCompiler(); | ||
var results = compiler.compile(contents); | ||
if (!results.js) | ||
console.error(results.errors); | ||
var contents = fs.readFileSync(filename, 'utf-8'); | ||
var sourceFile = new SourceFile(filename, contents); | ||
var parser = new Parser(sourceFile); | ||
var tree = parser.parseModule(); | ||
var reporter = new ErrorReporter(); | ||
var transformer = new FromOptionsTransformer(reporter); | ||
tree = transformer.transform(tree); | ||
if (reporter.hadError()) | ||
throw new Error('Error transforming ' + filename); | ||
return TreeWriter.write(tree); | ||
return results.js; | ||
} | ||
@@ -50,0 +39,0 @@ |
@@ -27,8 +27,5 @@ // Copyright 2013 Traceur Authors. | ||
// Nasty, we should rather pass the options to FromOptionsTransformer | ||
traceur.options.modules = 'amd'; | ||
var inputDir = process.argv[2]; | ||
var outputDir = process.argv[3]; | ||
compileAllJsFilesInDir(inputDir, outputDir, true); | ||
compileAllJsFilesInDir(inputDir, outputDir, traceur.moduleToAmd); |
@@ -27,8 +27,5 @@ // Copyright 2013 Traceur Authors. | ||
// Nasty, we should rather pass the options to FromOptionsTransformer | ||
traceur.options.modules = 'commonjs'; | ||
var inputDir = process.argv[2]; | ||
var outputDir = process.argv[3]; | ||
compileAllJsFilesInDir(inputDir, outputDir, true); | ||
compileAllJsFilesInDir(inputDir, outputDir, traceur.moduleToCommonJS); |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1038555
26467