Tobias Bosch c79f0c3472 refactor: simplify and make tests work in JS and Dart
* remove `wraps` syntax enhancements for imports
  and support new `import * as module from ...` syntax

  - default imports are the wrong construct for importing
    everything from a module

* moved tests from transpiler to jasmine and karma

  - transpiler tests are included when running karma in main project folder
  - transpiler is reloaded after every test run in karma,
    so no need to restart karma when the transpiler has been changed.
  - removed own gulp build for transpiler and `postinstall.sh`
    as they are no more needed.
  - transpiler tests are now executed in Dart AND JavaScript (used to be executed
    only in Dart), which allowed to catch some bugs (see the bug with the
    import specification above).

* made tests work in dart as well by using the following hack:

  - dependencies are loaded from the `build` folder, which makes
    running `gulp build` necessary before running karma for dart
  - for this to work,
    the dependencies are included in main `pubspec.yaml` of project
  - reason for the hack: `karma-dart` loads all `packages` urls
    directly from disc (should rather use the karma file list)

* added explicit annotations `FIELD`, `ABSTRACT`, ... to `facade/lang.*`

  - needed for now that we can run tests and don't get errors for undefined
    annotations.

* added `README.md` with details about the build and tests
2014-09-28 21:50:38 -07:00

84 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 = /transpiler\/src/;
var SELF_COMPILE_OPTIONS = {
modules: 'register',
moduleName: true,
script: false // parse as a module
};
var needsReload = true;
exports.reloadSources = 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('transpiler/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, 'transpiler')
.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;
}