| Index: lib/module.ts
|
| diff --git a/lib/module.ts b/lib/module.ts
|
| index a985fcdf3b4225aaff03a1acff9a3a664d5131a8..18b20f59a87b92398707a0163fd36f6abf8068a7 100644
|
| --- a/lib/module.ts
|
| +++ b/lib/module.ts
|
| @@ -5,7 +5,7 @@ import {FacadeConverter} from './facade_converter';
|
| import {OutputContext, Transpiler} from './main';
|
|
|
| export default class ModuleTranspiler extends base.TranspilerBase {
|
| - constructor(tr: Transpiler, private fc: FacadeConverter, private generateLibraryName: boolean) {
|
| + constructor(tr: Transpiler, private fc: FacadeConverter, private moduleName: string) {
|
| super(tr);
|
| }
|
|
|
| @@ -13,15 +13,30 @@ export default class ModuleTranspiler extends base.TranspilerBase {
|
| switch (node.kind) {
|
| case ts.SyntaxKind.SourceFile:
|
| this.pushContext(OutputContext.Import);
|
| - this.emit('@JS()');
|
| + let sourceFile = node as ts.SourceFile;
|
| + let moduleName = this.moduleName;
|
| + if (sourceFile.moduleName) {
|
| + moduleName = sourceFile.moduleName;
|
| + }
|
| + sourceFile.statements.forEach((n: ts.Node) => {
|
| + if (n.kind === ts.SyntaxKind.GlobalModuleExportDeclaration) {
|
| + let decl = n as ts.GlobalModuleExportDeclaration;
|
| + moduleName = base.ident(decl.name);
|
| + }
|
| + });
|
| + if (moduleName) {
|
| + this.emit('@JS("' + moduleName + '")');
|
| + } else {
|
| + this.emit('@JS()');
|
| + }
|
| this.emit('library');
|
| this.emit(this.getLibraryName());
|
| this.emit(';');
|
| this.popContext();
|
|
|
| this.emitImport('package:js/js.dart');
|
| - ts.forEachChild(node, this.visit.bind(this));
|
| - break;
|
| + // The declaration transpiler is responsible for emitting the contents of the source file.
|
| + return false;
|
| case ts.SyntaxKind.EndOfFileToken:
|
| ts.forEachChild(node, this.visit.bind(this));
|
| break;
|
| @@ -71,6 +86,10 @@ export default class ModuleTranspiler extends base.TranspilerBase {
|
| }
|
| this.fc.visitTypeName(spec.name);
|
| break;
|
| + case ts.SyntaxKind.GlobalModuleExportDeclaration:
|
| + // We handle this globally exporting all files in the packge with the specified global
|
| + // module export location.
|
| + break;
|
| case ts.SyntaxKind.ExportDeclaration:
|
| let exportDecl = <ts.ExportDeclaration>node;
|
| this.emit('export');
|
| @@ -80,7 +99,7 @@ export default class ModuleTranspiler extends base.TranspilerBase {
|
| this.reportError(node, 're-exports must have a module URL (export x from "./y").');
|
| }
|
| if (exportDecl.exportClause) this.visit(exportDecl.exportClause);
|
| - this.emit(';');
|
| + this.emit(';\n');
|
| break;
|
| case ts.SyntaxKind.ImportEqualsDeclaration:
|
| let importEqDecl = <ts.ImportEqualsDeclaration>node;
|
| @@ -89,7 +108,7 @@ export default class ModuleTranspiler extends base.TranspilerBase {
|
| this.visit(importEqDecl.moduleReference);
|
| this.emit('as');
|
| this.fc.visitTypeName(importEqDecl.name);
|
| - this.emit(';');
|
| + this.emit(';\n');
|
| this.popContext();
|
| break;
|
| case ts.SyntaxKind.ExternalModuleReference:
|
| @@ -114,7 +133,13 @@ export default class ModuleTranspiler extends base.TranspilerBase {
|
| } else if (!text.match(/^\.\.\//)) {
|
| // Unprefixed imports are package imports.
|
| text = 'package:' + text;
|
| + } else {
|
| + // TODO(jacobr): actually handle imports in different directories. We assume for now that all
|
| + // files in a library will be output to a single directory for codegen simplicity.
|
| + let parts = text.split('/');
|
| + text = parts[parts.length - 1];
|
| }
|
| +
|
| this.emit(JSON.stringify(text + '.dart'));
|
| }
|
|
|
| @@ -130,14 +155,12 @@ export default class ModuleTranspiler extends base.TranspilerBase {
|
| return ns.filter((e) => !ModuleTranspiler.isIgnoredImport(e));
|
| }
|
|
|
| - getLibraryName(nameForTest?: string) {
|
| - let fileName = this.getRelativeFileName(nameForTest);
|
| + getLibraryName(jsFileName?: string) {
|
| + let fileName = this.getDartFileName(jsFileName);
|
| let parts = fileName.split('/');
|
| - return parts.filter((p) => p.length > 0)
|
| + return parts.filter((p) => p.length > 0 && p !== '..')
|
| .map((p) => p.replace(/[^\w.]/g, '_'))
|
| - .map((p) => p.replace(/\.d\.ts$/g, ''))
|
| - .map((p) => p.replace(/\.[jt]s$/g, ''))
|
| - .map((p) => p.replace(/\./g, ''))
|
| + .map((p) => p.replace(/\.dart$/, ''))
|
| .map((p) => FacadeConverter.DART_RESERVED_WORDS.indexOf(p) !== -1 ? '_' + p : p)
|
| .filter((p) => p.length > 0)
|
| .join('.');
|
|
|