fix(build): support transpile to commonjs
This commit is contained in:
@ -87,6 +87,7 @@ function reloadCompiler() {
|
||||
convertTypesToExpressionsInEs6Patch();
|
||||
removeNonStaticFieldDeclarationsInEs6Patch();
|
||||
disableGetterSetterAssertionPatch();
|
||||
patchCommonJSModuleTransformerToSupportExportStar();
|
||||
}
|
||||
|
||||
function loadModule(filepath, transpile) {
|
||||
@ -221,3 +222,29 @@ function useRttsAssertModuleForConvertingTypesToExpressions() {
|
||||
// NYI
|
||||
}
|
||||
}
|
||||
|
||||
// TODO(tbosch): patch exports for CommonJS to support `export * from ...`
|
||||
// see https://github.com/google/traceur-compiler/issues/1042
|
||||
function patchCommonJSModuleTransformerToSupportExportStar() {
|
||||
var traceurVersion = System.map['traceur'];
|
||||
var CommonJsModuleTransformer = System.get(traceurVersion+'/src/codegeneration/CommonJsModuleTransformer').CommonJsModuleTransformer;
|
||||
var parseStatement = System.get(traceurVersion+'/src/codegeneration/PlaceholderParser.js').parseStatement;
|
||||
var prependStatements = System.get(traceurVersion+"/src/codegeneration/PrependStatements.js").prependStatements;
|
||||
|
||||
var _wrapModule = CommonJsModuleTransformer.prototype.wrapModule;
|
||||
CommonJsModuleTransformer.prototype.wrapModule = function(statements) {
|
||||
if (this.hasStarExports()) {
|
||||
var last = statements[statements.length - 1];
|
||||
statements = statements.slice(0, -1);
|
||||
var exportObject = last.expression;
|
||||
if (exportObject.propertyNameAndValues) {
|
||||
throw new Error('Don\'t support export * with named exports right now...');
|
||||
}
|
||||
statements.push(parseStatement(['module.exports = ', ';'], exportObject));
|
||||
return statements;
|
||||
} else {
|
||||
return _wrapModule.apply(this, arguments);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,4 +1,8 @@
|
||||
var compiler = require('../index');
|
||||
var temp = require('temp');
|
||||
var fs = require('fs');
|
||||
|
||||
temp.track();
|
||||
|
||||
var DEFAULT_OPTIONS = {
|
||||
sourceMaps: false,
|
||||
@ -157,6 +161,34 @@ describe('transpile to es6', function() {
|
||||
|
||||
});
|
||||
|
||||
describe('transpile to cjs', function() {
|
||||
var options;
|
||||
|
||||
beforeEach(function() {
|
||||
options = merge(DEFAULT_OPTIONS, {modules: 'commonjs'});
|
||||
});
|
||||
|
||||
function compileAndWrite(input) {
|
||||
var transpiledCode = compiler.compile(options, "test.js", input).js;
|
||||
var tempFile = temp.openSync('ng2transpiler');
|
||||
fs.writeSync(tempFile.fd, transpiledCode);
|
||||
return tempFile.path;
|
||||
}
|
||||
|
||||
it('should transpile export *', function() {
|
||||
var file1 = compileAndWrite('export var a = 1');
|
||||
var file2 = compileAndWrite('export * from "' + file1 + '"');
|
||||
expect(require(file2).a).toBe(1);
|
||||
});
|
||||
|
||||
it('should transpile export {name}', function() {
|
||||
var file1 = compileAndWrite('export var a = 1');
|
||||
var file2 = compileAndWrite('export {a} from "' + file1 + '"');
|
||||
expect(require(file2).a).toBe(1);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
|
||||
function merge(a, b) {
|
||||
var result = {};
|
||||
|
Reference in New Issue
Block a user