angular/tools/js2dart/index.js
Vojta Jina c3b442ea53 chore: karma with JS, Dart
Note: karma with dart is still not working
because of how `karma-dart` loads `package:…` dependencies.

Usage:
```
karma start karma-js.conf.js
karma start karma-dart.conf.js
```

Make sure to set `DARTIUM_BIN` env variable.

Refactors `js2dart`:
- live outside of the traceur module (`tools/js2dart/index.js`)
  so it can be reused by gulp and karma
- automatically build the sources in memory,
  so that `js2dart` can be used without running `gulp build` first
- provide a way to specify the moduleName of a compilation run
  independently of the input filename. This helps error messages
  and source maps (not yet enabled) to report the correct file name

Changes project setup:
- add module `test_lib` that contains the primitives for tests
  (e.g. `describe`, `it`, …)
- clean up some sources that had errors in them
- module names in transpiled js and dart files don’t contain
  `lib`, `test` nor `src` any more (e.g. `di/di`).
2014-09-26 16:53:54 -07:00

85 lines
2.1 KiB
JavaScript

// Entry point for Node.
var fs = require('fs');
var glob = require('glob');
var path = require('path');
var traceur = require('traceur');
exports.RUNTIME_PATH = traceur.RUNTIME_PATH;
var TRACEUR_PATH = traceur.RUNTIME_PATH.replace('traceur-runtime.js', 'traceur.js');
var SELF_SOURCE_REGEX = /js2dart\/src/;
var SELF_COMPILE_OPTIONS = {
modules: 'register',
moduleName: true,
script: false // parse as a module
};
var needsReload = true;
// TODO(vojta): call this if sources changed
exports.sourcesChanged = function() {
needsReload = true;
};
exports.compile = function compile(options, paths, source) {
if (needsReload) {
reloadCompiler();
needsReload = false;
}
var inputPath, outputPath, moduleName;
if (typeof paths === 'string') {
inputPath = outputPath = paths;
} else {
inputPath = paths.inputPath;
outputPath = paths.inputPath;
moduleName = paths.moduleName;
}
outputPath = outputPath || inputPath;
moduleName = moduleName || inputPath;
moduleName = moduleName.replace(/\.\w*$/, '');
var localOptions = extend(options, {
moduleName: moduleName
});
var CompilerCls = System.get('js2dart/src/compiler').Compiler;
return (new CompilerCls(localOptions)).compile(source, inputPath, outputPath);
};
// Transpile and evaluate the code in `src`.
// Use existing traceur to compile our sources.
function reloadCompiler() {
loadModule(TRACEUR_PATH, false);
glob.sync(__dirname + '/src/**/*.js').forEach(function(fileName) {
loadModule(fileName, true);
});
}
function loadModule(filepath, transpile) {
var data = fs.readFileSync(filepath, 'utf8');
if (!data) {
throw new Error('Failed to import ' + filepath);
}
if (transpile) {
var moduleName = filepath
.replace(__dirname, 'js2dart')
.replace(/\.\w*$/, '');
data = (new traceur.NodeCompiler(
extend(SELF_COMPILE_OPTIONS, { moduleName: moduleName } )
)).compile(data, filepath, filepath);
}
('global', eval)(data);
}
function extend(source, props) {
var res = {};
for (var prop in source) {
res[prop] = source[prop];
}
for (var prop in props) {
res[prop] = props[prop];
}
return res;
}