From 487ee082da403020b4fe9eba966cdf9988116758 Mon Sep 17 00:00:00 2001 From: Wesley Wigham Date: Fri, 11 Sep 2015 10:10:27 -0700 Subject: [PATCH 01/14] commonjs bundling implementation --- src/compiler/commandLineParser.ts | 8 +- .../diagnosticInformationMap.generated.ts | 2 + src/compiler/diagnosticMessages.json | 8 ++ src/compiler/emitter.ts | 121 +++++++++++++++++- src/compiler/program.ts | 4 + src/compiler/types.ts | 3 +- 6 files changed, 138 insertions(+), 8 deletions(-) diff --git a/src/compiler/commandLineParser.ts b/src/compiler/commandLineParser.ts index d22bc98668840..1bef60783dba1 100644 --- a/src/compiler/commandLineParser.ts +++ b/src/compiler/commandLineParser.ts @@ -244,7 +244,13 @@ namespace ts { "classic": ModuleResolutionKind.Classic }, description: Diagnostics.Specifies_module_resolution_strategy_Colon_node_Node_or_classic_TypeScript_pre_1_6 - } + }, + { + name: "bundle", + type: "string", + isFilePath: true, + description: Diagnostics.Specifies_entrypoint_file_for_bundling + } ]; /* @internal */ diff --git a/src/compiler/diagnosticInformationMap.generated.ts b/src/compiler/diagnosticInformationMap.generated.ts index af48e67d964d7..2522d158fc84a 100644 --- a/src/compiler/diagnosticInformationMap.generated.ts +++ b/src/compiler/diagnosticInformationMap.generated.ts @@ -513,6 +513,7 @@ namespace ts { Option_0_cannot_be_specified_without_specifying_option_1: { code: 5052, category: DiagnosticCategory.Error, key: "Option '{0}' cannot be specified without specifying option '{1}'." }, Option_0_cannot_be_specified_with_option_1: { code: 5053, category: DiagnosticCategory.Error, key: "Option '{0}' cannot be specified with option '{1}'." }, A_tsconfig_json_file_is_already_defined_at_Colon_0: { code: 5053, category: DiagnosticCategory.Error, key: "A 'tsconfig.json' file is already defined at: '{0}'." }, + Option_0_requires_both_1_and_2: { code: 5055, category: DiagnosticCategory.Error, key: "Option '{0}' requires both '{1}' and '{2}'." }, Concatenate_and_emit_output_to_single_file: { code: 6001, category: DiagnosticCategory.Message, key: "Concatenate and emit output to single file." }, Generates_corresponding_d_ts_file: { code: 6002, category: DiagnosticCategory.Message, key: "Generates corresponding '.d.ts' file." }, Specifies_the_location_where_debugger_should_locate_map_files_instead_of_generated_locations: { code: 6003, category: DiagnosticCategory.Message, key: "Specifies the location where debugger should locate map files instead of generated locations." }, @@ -571,6 +572,7 @@ namespace ts { Initializes_a_TypeScript_project_and_creates_a_tsconfig_json_file: { code: 6070, category: DiagnosticCategory.Message, key: "Initializes a TypeScript project and creates a tsconfig.json file." }, Successfully_created_a_tsconfig_json_file: { code: 6071, category: DiagnosticCategory.Message, key: "Successfully created a tsconfig.json file." }, Suppress_excess_property_checks_for_object_literals: { code: 6072, category: DiagnosticCategory.Message, key: "Suppress excess property checks for object literals." }, + Specifies_entrypoint_file_for_bundling: { code: 6073, category: DiagnosticCategory.Message, key: "Specifies entrypoint file for bundling." }, Variable_0_implicitly_has_an_1_type: { code: 7005, category: DiagnosticCategory.Error, key: "Variable '{0}' implicitly has an '{1}' type." }, Parameter_0_implicitly_has_an_1_type: { code: 7006, category: DiagnosticCategory.Error, key: "Parameter '{0}' implicitly has an '{1}' type." }, Member_0_implicitly_has_an_1_type: { code: 7008, category: DiagnosticCategory.Error, key: "Member '{0}' implicitly has an '{1}' type." }, diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index 7abec7b7c43cf..91bbac4b23aef 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -2042,6 +2042,10 @@ "category": "Error", "code": 5053 }, + "Option '{0}' requires both '{1}' and '{2}'.": { + "category": "Error", + "code": 5055 + }, "Concatenate and emit output to single file.": { "category": "Message", @@ -2275,6 +2279,10 @@ "category": "Message", "code": 6072 }, + "Specifies entrypoint file for bundling.": { + "category": "Message", + "code": 6073 + }, "Variable '{0}' implicitly has an '{1}' type.": { "category": "Error", diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index 4fa14e775ac72..1fdedd23094ba 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -62,6 +62,67 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi }); };`; + const defineHelper = ` +var __define = (this && this.__define) || (function() { + var cache = {}, + id = 0, + idMap = {}, + builtinRequire = require || function() {}, + normalizeSlashes = function(path) { return path.replace("\\\\", "/"); }, + forEach = function(array, callback) { + if (array) { + for (var i = 0, len = array.length; i < len; i++) { + var result = callback(array[i], i); + if (result) { + return result; + } + } + } + return undefined; + }, + resolvePath = function(base, name) { + var result = normalizeSlashes(base).split("/"); + result.pop(); + forEach(normalizeSlashes(name).split("/"), function(part, index) { + switch(part) { + case "": + case ".": + break; + case "..": + result.pop(); + break; + default: + result.push(part); + break; + } + }); + return result.join("/"); + }, + resolveRequire = function(name) { + if (typeof cache[name] === "function") { + cache[name](); + } + return (cache[name] ? cache[name].exports : builtinRequire(name)); + }, + define = function (declaredName, factory) { + idMap[++id] = declaredName; + var module = { + id: id, + exports: {} + }; + var require = function(name) { + if (typeof name === "number") return resolveRequire(idMap[name]); + return resolveRequire(resolvePath(declaredName, name)); + }; + cache[declaredName] = function() { + cache[declaredName] = module; + factory(require, module.exports, module); + }; + }; + define.require = resolveRequire; + return define; +})();`; + let compilerOptions = host.getCompilerOptions(); let languageVersion = compilerOptions.target || ScriptTarget.ES3; let sourceMapDataList: SourceMapData[] = compilerOptions.sourceMap || compilerOptions.inlineSourceMap ? [] : undefined; @@ -71,12 +132,14 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi let shouldEmitJsx = (s: SourceFile) => (s.languageVariant === LanguageVariant.JSX && !jsxDesugaring); if (targetSourceFile === undefined) { - forEach(host.getSourceFiles(), sourceFile => { - if (shouldEmitToOwnFile(sourceFile, compilerOptions)) { - let jsFilePath = getOwnEmitOutputFilePath(sourceFile, host, shouldEmitJsx(sourceFile) ? ".jsx" : ".js"); - emitFile(jsFilePath, sourceFile); - } - }); + if (!compilerOptions.bundle) { + forEach(host.getSourceFiles(), sourceFile => { + if (shouldEmitToOwnFile(sourceFile, compilerOptions)) { + let jsFilePath = getOwnEmitOutputFilePath(sourceFile, host, shouldEmitJsx(sourceFile) ? ".jsx" : ".js"); + emitFile(jsFilePath, sourceFile); + } + }); + } if (compilerOptions.outFile || compilerOptions.out) { emitFile(compilerOptions.outFile || compilerOptions.out); @@ -198,11 +261,24 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi emitSourceFile(root); } else { + if (compilerOptions.bundle) { + writeLines("module.exports = (function() {"); + increaseIndent(); + writeLines(defineHelper); + writeLine(); + } forEach(host.getSourceFiles(), sourceFile => { if (!isExternalModuleOrDeclarationFile(sourceFile)) { emitSourceFile(sourceFile); + } else if (compilerOptions.bundle && isExternalModule(sourceFile)) { + emitBundledFile(sourceFile); } }); + if (compilerOptions.bundle) { + writeLines(`return __define.require("${removeFileExtension(compilerOptions.bundle)}");`); + decreaseIndent(); + writeLines("})();"); + } } writeLine(); @@ -214,6 +290,26 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi exportFunctionForFile = undefined; emit(sourceFile); } + + function emitBundledFile(sourceFile: SourceFile): void { + let canonicalName = removeFileExtension( + getRelativePathToDirectoryOrUrl( + host.getCurrentDirectory(), + host.getCanonicalFileName(sourceFile.fileName), + host.getCurrentDirectory(), + host.getCanonicalFileName, + /*isAbsolutePathAnUrl*/false + ) + ); + writeLine(); + write(`__define("${canonicalName}", function(require, exports, module){`); + writeLine(); + increaseIndent(); + emitBundledSourceFileNode(sourceFile); + decreaseIndent(); + writeLine(); + write("});"); + } function isUniqueName(name: string): boolean { return !resolver.hasGlobalName(name) && @@ -6716,6 +6812,19 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi emitLeadingComments(node.endOfFileToken); } + + function emitBundledSourceFileNode(node: SourceFile) { + currentSourceFile = node; + exportFunctionForFile = undefined; + writeLine(); + emitShebang(); + emitDetachedComments(node); + + // emit prologue directives prior to __extends + let startIndex = emitDirectivePrologues(node.statements, /*startWithNewLine*/ false); + emitCommonJSModule(node, startIndex); + emitLeadingComments(node.endOfFileToken); + } function emitNodeWithCommentsAndWithoutSourcemap(node: Node): void { emitNodeConsideringCommentsOption(node, emitNodeWithoutSourceMap); diff --git a/src/compiler/program.ts b/src/compiler/program.ts index d86a80a5387f5..796d383dd1367 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -1029,6 +1029,10 @@ namespace ts { if (options.module && languageVersion >= ScriptTarget.ES6) { programDiagnostics.add(createCompilerDiagnostic(Diagnostics.Cannot_compile_modules_into_commonjs_amd_system_or_umd_when_targeting_ES6_or_higher)); } + + if (options.bundle && !(options.module && outFile) ) { + programDiagnostics.add(createCompilerDiagnostic(Diagnostics.Option_0_requires_both_1_and_2, "bundle", "outFile", "module")); + } // there has to be common source directory if user specified --outdir || --sourceRoot // if user specified --mapRoot, there needs to be common source directory if there would be multiple files being emitted diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 265fa5c86fbcf..ed4f63ac66441 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -2058,7 +2058,8 @@ namespace ts { experimentalDecorators?: boolean; experimentalAsyncFunctions?: boolean; emitDecoratorMetadata?: boolean; - moduleResolution?: ModuleResolutionKind + moduleResolution?: ModuleResolutionKind, + bundle?: string; /* @internal */ stripInternal?: boolean; // Skip checking lib.d.ts to help speed up tests. From 9d340d94df2b8d3c9ff76d5c3f8bf628f770d697 Mon Sep 17 00:00:00 2001 From: Wesley Wigham Date: Fri, 11 Sep 2015 15:03:20 -0700 Subject: [PATCH 02/14] Emit for all module outputs, disable es6 module output when bundling --- .../diagnosticInformationMap.generated.ts | 2 +- src/compiler/diagnosticMessages.json | 2 +- src/compiler/emitter.ts | 207 ++++++++++++------ src/compiler/program.ts | 4 +- 4 files changed, 145 insertions(+), 70 deletions(-) diff --git a/src/compiler/diagnosticInformationMap.generated.ts b/src/compiler/diagnosticInformationMap.generated.ts index 2522d158fc84a..de4dcf755651e 100644 --- a/src/compiler/diagnosticInformationMap.generated.ts +++ b/src/compiler/diagnosticInformationMap.generated.ts @@ -160,7 +160,7 @@ namespace ts { Line_terminator_not_permitted_before_arrow: { code: 1200, category: DiagnosticCategory.Error, key: "Line terminator not permitted before arrow." }, Import_assignment_cannot_be_used_when_targeting_ECMAScript_6_or_higher_Consider_using_import_Asterisk_as_ns_from_mod_import_a_from_mod_or_import_d_from_mod_instead: { code: 1202, category: DiagnosticCategory.Error, key: "Import assignment cannot be used when targeting ECMAScript 6 or higher. Consider using 'import * as ns from \"mod\"', 'import {a} from \"mod\"' or 'import d from \"mod\"' instead." }, Export_assignment_cannot_be_used_when_targeting_ECMAScript_6_or_higher_Consider_using_export_default_instead: { code: 1203, category: DiagnosticCategory.Error, key: "Export assignment cannot be used when targeting ECMAScript 6 or higher. Consider using 'export default' instead." }, - Cannot_compile_modules_into_commonjs_amd_system_or_umd_when_targeting_ES6_or_higher: { code: 1204, category: DiagnosticCategory.Error, key: "Cannot compile modules into 'commonjs', 'amd', 'system' or 'umd' when targeting 'ES6' or higher." }, + Cannot_compile_modules_into_commonjs_amd_system_or_umd_when_targeting_ES6_or_higher_unless_bundle_is_specified: { code: 1204, category: DiagnosticCategory.Error, key: "Cannot compile modules into 'commonjs', 'amd', 'system' or 'umd' when targeting 'ES6' or higher unless --bundle is specified." }, Decorators_are_only_available_when_targeting_ECMAScript_5_and_higher: { code: 1205, category: DiagnosticCategory.Error, key: "Decorators are only available when targeting ECMAScript 5 and higher." }, Decorators_are_not_valid_here: { code: 1206, category: DiagnosticCategory.Error, key: "Decorators are not valid here." }, Decorators_cannot_be_applied_to_multiple_get_Slashset_accessors_of_the_same_name: { code: 1207, category: DiagnosticCategory.Error, key: "Decorators cannot be applied to multiple get/set accessors of the same name." }, diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index 91bbac4b23aef..d95cce91bb889 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -627,7 +627,7 @@ "category": "Error", "code": 1203 }, - "Cannot compile modules into 'commonjs', 'amd', 'system' or 'umd' when targeting 'ES6' or higher.": { + "Cannot compile modules into 'commonjs', 'amd', 'system' or 'umd' when targeting 'ES6' or higher unless --bundle is specified.": { "category": "Error", "code": 1204 }, diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index 1fdedd23094ba..77c3e71012a27 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -67,18 +67,12 @@ var __define = (this && this.__define) || (function() { var cache = {}, id = 0, idMap = {}, - builtinRequire = require || function() {}, + builtinRequire = typeof require === "function" ? require : function() {}, normalizeSlashes = function(path) { return path.replace("\\\\", "/"); }, forEach = function(array, callback) { - if (array) { - for (var i = 0, len = array.length; i < len; i++) { - var result = callback(array[i], i); - if (result) { - return result; - } - } + for (var i = 0, len = array.length; i < len; i++) { + callback(array[i], i); } - return undefined; }, resolvePath = function(base, name) { var result = normalizeSlashes(base).split("/"); @@ -89,6 +83,16 @@ var __define = (this && this.__define) || (function() { case ".": break; case "..": + if (result.length <= 1) { + if (result[0] === "..") { + result.push(".."); + break; + } + else if (result[0] === "." || result[0] === "") { + result[0] = ".."; + break; + } + } result.pop(); break; default: @@ -96,13 +100,15 @@ var __define = (this && this.__define) || (function() { break; } }); - return result.join("/"); + if (result[0] === "") result.shift(); + return "/"+result.join("/"); }, - resolveRequire = function(name) { - if (typeof cache[name] === "function") { - cache[name](); + resolveRequire = function(name, from) { + var resolved = resolvePath(from || "/", name); + if (typeof cache[resolved] === "function") { + cache[resolved](); } - return (cache[name] ? cache[name].exports : builtinRequire(name)); + return (cache[resolved] ? cache[resolved].exports : builtinRequire(name)); }, define = function (declaredName, factory) { idMap[++id] = declaredName; @@ -112,7 +118,7 @@ var __define = (this && this.__define) || (function() { }; var require = function(name) { if (typeof name === "number") return resolveRequire(idMap[name]); - return resolveRequire(resolvePath(declaredName, name)); + return resolveRequire(name, declaredName); }; cache[declaredName] = function() { cache[declaredName] = module; @@ -123,6 +129,15 @@ var __define = (this && this.__define) || (function() { return define; })();`; + const umdHelper = `(function (deps, factory) { + if (typeof module === 'object' && typeof module.exports === 'object') { + var v = factory(require, exports); if (v !== undefined) module.exports = v; + } + else if (typeof define === 'function' && define.amd) { + define(deps, factory); + } +})(`; + let compilerOptions = host.getCompilerOptions(); let languageVersion = compilerOptions.target || ScriptTarget.ES3; let sourceMapDataList: SourceMapData[] = compilerOptions.sourceMap || compilerOptions.inlineSourceMap ? [] : undefined; @@ -230,6 +245,64 @@ var __define = (this && this.__define) || (function() { /** Called once the emit of the node is done */ let emitEnd = function (node: Node) { }; + /** Called to build the opening part of a bundled module wrapper */ + let emitBundleWrapperStart: Map = { + [ModuleKind.CommonJS]: () => { + write("module.exports = "); + }, + [ModuleKind.AMD]: () => { //TODO: handle external deps with amd better + write("define([\"require\"], function(require){"); + writeLine(); + increaseIndent(); + write("return "); + }, + [ModuleKind.UMD]: () => { + writeLines(umdHelper); + write("[\"require\"], function(require){"); + writeLine(); + increaseIndent(); + write("return "); + }, + [ModuleKind.System]: () => { //TODO: handle external deps with system + currentSourceFile = {identifiers: []}; + exportFunctionForFile = makeUniqueName("exports"); + writeLine(); + write(`System.register([], function(${exportFunctionForFile}) {`); + writeLine(); + increaseIndent(); + let exportStarName = emitExportStarFunction(undefined); + exportFunctionForFile = undefined; + currentSourceFile = undefined; + writeLine(); + write(`${exportStarName}(`); + } + } + + /** Called to build the closing part of a bundled module wrapper */ + let emitBundleWrapperEnd: Map = { + [ModuleKind.CommonJS]: () => { + write(";"); + }, + [ModuleKind.AMD]: () => { + write(";"); + decreaseIndent(); + writeLine(); + write("});"); + }, + [ModuleKind.UMD]: () => { + write(";"); + decreaseIndent(); + writeLine(); + write("});"); + }, + [ModuleKind.System]: () => { + write(");"); + decreaseIndent(); + writeLine(); + write("});"); + } + } + /** Emit the text for the given token that comes after startPos * This by default writes the text provided with the given tokenKind * but if optional emitFn callback is provided the text is emitted using the callback instead of default text @@ -261,10 +334,16 @@ var __define = (this && this.__define) || (function() { emitSourceFile(root); } else { + let originalKind = compilerOptions.module; if (compilerOptions.bundle) { - writeLines("module.exports = (function() {"); + compilerOptions.module = ModuleKind.CommonJS; + emitBundleWrapperStart[originalKind](); + write("(function() {"); increaseIndent(); writeLines(defineHelper); + forEach(host.getSourceFiles(), file => { + emitEmitHelpers(file); + }); writeLine(); } forEach(host.getSourceFiles(), sourceFile => { @@ -277,7 +356,9 @@ var __define = (this && this.__define) || (function() { if (compilerOptions.bundle) { writeLines(`return __define.require("${removeFileExtension(compilerOptions.bundle)}");`); decreaseIndent(); - writeLines("})();"); + writeLines("})()"); + emitBundleWrapperEnd[originalKind](); + compilerOptions.module = originalKind; } } @@ -295,14 +376,14 @@ var __define = (this && this.__define) || (function() { let canonicalName = removeFileExtension( getRelativePathToDirectoryOrUrl( host.getCurrentDirectory(), - host.getCanonicalFileName(sourceFile.fileName), + sourceFile.fileName, host.getCurrentDirectory(), host.getCanonicalFileName, /*isAbsolutePathAnUrl*/false ) ); writeLine(); - write(`__define("${canonicalName}", function(require, exports, module){`); + write(`__define("/${canonicalName}", function(require, exports, module){`); writeLine(); increaseIndent(); emitBundledSourceFileNode(sourceFile); @@ -1589,7 +1670,7 @@ var __define = (this && this.__define) || (function() { if (container) { if (container.kind === SyntaxKind.SourceFile) { // Identifier references module export - if (languageVersion < ScriptTarget.ES6 && compilerOptions.module !== ModuleKind.System) { + if ((languageVersion < ScriptTarget.ES6 || compilerOptions.bundle) && compilerOptions.module !== ModuleKind.System) { write("exports."); } } @@ -1599,7 +1680,7 @@ var __define = (this && this.__define) || (function() { write("."); } } - else if (languageVersion < ScriptTarget.ES6) { + else if (languageVersion < ScriptTarget.ES6 || compilerOptions.bundle) { let declaration = resolver.getReferencedImportDeclaration(node); if (declaration) { if (declaration.kind === SyntaxKind.ImportClause) { @@ -3145,7 +3226,7 @@ var __define = (this && this.__define) || (function() { write(getGeneratedNameForNode(container)); write("."); } - else if (languageVersion < ScriptTarget.ES6 && compilerOptions.module !== ModuleKind.System) { + else if ((languageVersion < ScriptTarget.ES6 || compilerOptions.bundle) && compilerOptions.module !== ModuleKind.System) { write("exports."); } } @@ -3562,6 +3643,7 @@ var __define = (this && this.__define) || (function() { function isES6ExportedDeclaration(node: Node) { return !!(node.flags & NodeFlags.Export) && languageVersion >= ScriptTarget.ES6 && + !compilerOptions.bundle && node.parent.kind === SyntaxKind.SourceFile; } @@ -4661,7 +4743,7 @@ var __define = (this && this.__define) || (function() { emitEnd(node); write(";"); } - else if (isES6ExportedDeclaration(node) && (node.flags & NodeFlags.Default) && thisNodeIsDecorated) { + else if (isES6ExportedDeclaration(node) && (node.flags & NodeFlags.Default) && thisNodeIsDecorated && !compilerOptions.bundle) { // if this is a top level default export of decorated class, write the export after the declaration. writeLine(); write("export default "); @@ -5513,7 +5595,7 @@ var __define = (this && this.__define) || (function() { } function emitImportDeclaration(node: ImportDeclaration) { - if (languageVersion < ScriptTarget.ES6) { + if (languageVersion < ScriptTarget.ES6 || compilerOptions.bundle) { return emitExternalImportDeclaration(node); } @@ -5678,7 +5760,7 @@ var __define = (this && this.__define) || (function() { function emitExportDeclaration(node: ExportDeclaration) { Debug.assert(compilerOptions.module !== ModuleKind.System); - if (languageVersion < ScriptTarget.ES6) { + if (languageVersion < ScriptTarget.ES6 || compilerOptions.bundle) { if (node.moduleSpecifier && (!node.exportClause || resolver.isValueAliasDeclaration(node))) { emitStart(node); let generatedName = getGeneratedNameForNode(node); @@ -5764,7 +5846,7 @@ var __define = (this && this.__define) || (function() { function emitExportAssignment(node: ExportAssignment) { if (!node.isExportEquals && resolver.isValueAliasDeclaration(node)) { - if (languageVersion >= ScriptTarget.ES6) { + if (languageVersion >= ScriptTarget.ES6 && !compilerOptions.bundle) { writeLine(); emitStart(node); write("export default "); @@ -5995,37 +6077,6 @@ var __define = (this && this.__define) || (function() { return emitExportStarFunction(exportedNamesStorageRef); - function emitExportStarFunction(localNames: string): string { - const exportStarFunction = makeUniqueName("exportStar"); - - writeLine(); - - // define an export star helper function - write(`function ${exportStarFunction}(m) {`); - increaseIndent(); - writeLine(); - write(`var exports = {};`); - writeLine(); - write(`for(var n in m) {`); - increaseIndent(); - writeLine(); - write(`if (n !== "default"`); - if (localNames) { - write(`&& !${localNames}.hasOwnProperty(n)`); - } - write(`) exports[n] = m[n];`); - decreaseIndent(); - writeLine(); - write("}"); - writeLine(); - write(`${exportFunctionForFile}(exports);`); - decreaseIndent(); - writeLine(); - write("}"); - - return exportStarFunction; - } - function writeExportedName(node: Identifier | Declaration): void { // do not record default exports // they are local to module and never overwritten (explicitly skipped) by star export @@ -6410,6 +6461,37 @@ var __define = (this && this.__define) || (function() { write("}"); // execute } + function emitExportStarFunction(localNames: string): string { + const exportStarFunction = makeUniqueName("exportStar"); + + writeLine(); + + // define an export star helper function + write(`function ${exportStarFunction}(m) {`); + increaseIndent(); + writeLine(); + write(`var exports = {};`); + writeLine(); + write(`for(var n in m) {`); + increaseIndent(); + writeLine(); + write(`if (n !== "default"`); + if (localNames) { + write(`&& !${localNames}.hasOwnProperty(n)`); + } + write(`) exports[n] = m[n];`); + decreaseIndent(); + writeLine(); + write("}"); + writeLine(); + write(`${exportFunctionForFile}(exports);`); + decreaseIndent(); + writeLine(); + write("}"); + + return exportStarFunction; + } + type DependencyGroup = Array; function emitSystemModule(node: SourceFile, startIndex: number): void { @@ -6565,14 +6647,7 @@ var __define = (this && this.__define) || (function() { collectExternalModuleInfo(node); // Module is detected first to support Browserify users that load into a browser with an AMD loader - writeLines(`(function (deps, factory) { - if (typeof module === 'object' && typeof module.exports === 'object') { - var v = factory(require, exports); if (v !== undefined) module.exports = v; - } - else if (typeof define === 'function' && define.amd) { - define(deps, factory); - } -})(`); + writeLines(umdHelper); emitAMDDependencies(node, false); write(") {"); increaseIndent(); @@ -6783,7 +6858,7 @@ var __define = (this && this.__define) || (function() { let startIndex = emitDirectivePrologues(node.statements, /*startWithNewLine*/ false); if (isExternalModule(node) || compilerOptions.isolatedModules) { - if (languageVersion >= ScriptTarget.ES6) { + if (languageVersion >= ScriptTarget.ES6 && !compilerOptions.bundle) { emitES6Module(node, startIndex); } else if (compilerOptions.module === ModuleKind.AMD) { diff --git a/src/compiler/program.ts b/src/compiler/program.ts index 796d383dd1367..c09c2d9168371 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -1026,8 +1026,8 @@ namespace ts { } // Cannot specify module gen target when in es6 or above - if (options.module && languageVersion >= ScriptTarget.ES6) { - programDiagnostics.add(createCompilerDiagnostic(Diagnostics.Cannot_compile_modules_into_commonjs_amd_system_or_umd_when_targeting_ES6_or_higher)); + if (options.module && languageVersion >= ScriptTarget.ES6 && !(options.bundle)) { + programDiagnostics.add(createCompilerDiagnostic(Diagnostics.Cannot_compile_modules_into_commonjs_amd_system_or_umd_when_targeting_ES6_or_higher_unless_bundle_is_specified)); } if (options.bundle && !(options.module && outFile) ) { From 9840aa4501333ff55fbb97801859828e5f169dc0 Mon Sep 17 00:00:00 2001 From: Wesley Wigham Date: Fri, 11 Sep 2015 15:53:22 -0700 Subject: [PATCH 03/14] fix style nits --- src/compiler/emitter.ts | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index 77c3e71012a27..bea418455613f 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -246,24 +246,24 @@ var __define = (this && this.__define) || (function() { let emitEnd = function (node: Node) { }; /** Called to build the opening part of a bundled module wrapper */ - let emitBundleWrapperStart: Map = { - [ModuleKind.CommonJS]: () => { + let emitBundleWrapperStart: Map<() => void> = { + [ModuleKind.CommonJS]() { write("module.exports = "); }, - [ModuleKind.AMD]: () => { //TODO: handle external deps with amd better + [ModuleKind.AMD]() { //TODO: handle external deps with amd better write("define([\"require\"], function(require){"); writeLine(); increaseIndent(); write("return "); }, - [ModuleKind.UMD]: () => { + [ModuleKind.UMD]() { writeLines(umdHelper); write("[\"require\"], function(require){"); writeLine(); increaseIndent(); write("return "); }, - [ModuleKind.System]: () => { //TODO: handle external deps with system + [ModuleKind.System]() { //TODO: handle external deps with system currentSourceFile = {identifiers: []}; exportFunctionForFile = makeUniqueName("exports"); writeLine(); @@ -279,23 +279,23 @@ var __define = (this && this.__define) || (function() { } /** Called to build the closing part of a bundled module wrapper */ - let emitBundleWrapperEnd: Map = { - [ModuleKind.CommonJS]: () => { + let emitBundleWrapperEnd: Map<() => void> = { + [ModuleKind.CommonJS]() { write(";"); }, - [ModuleKind.AMD]: () => { + [ModuleKind.AMD]() { write(";"); decreaseIndent(); writeLine(); write("});"); }, - [ModuleKind.UMD]: () => { + [ModuleKind.UMD]() { write(";"); decreaseIndent(); writeLine(); write("});"); }, - [ModuleKind.System]: () => { + [ModuleKind.System]() { write(");"); decreaseIndent(); writeLine(); @@ -341,9 +341,7 @@ var __define = (this && this.__define) || (function() { write("(function() {"); increaseIndent(); writeLines(defineHelper); - forEach(host.getSourceFiles(), file => { - emitEmitHelpers(file); - }); + forEach(host.getSourceFiles(), emitEmitHelpers); writeLine(); } forEach(host.getSourceFiles(), sourceFile => { From 826e78b854cafe54a287244bdc4e59714642989e Mon Sep 17 00:00:00 2001 From: Wesley Wigham Date: Fri, 11 Sep 2015 16:02:26 -0700 Subject: [PATCH 04/14] fix more style --- src/compiler/emitter.ts | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index bea418455613f..cd9f938fce258 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -270,7 +270,7 @@ var __define = (this && this.__define) || (function() { write(`System.register([], function(${exportFunctionForFile}) {`); writeLine(); increaseIndent(); - let exportStarName = emitExportStarFunction(undefined); + let exportStarName = emitExportStarFunction(/*localNames*/undefined); exportFunctionForFile = undefined; currentSourceFile = undefined; writeLine(); @@ -344,13 +344,16 @@ var __define = (this && this.__define) || (function() { forEach(host.getSourceFiles(), emitEmitHelpers); writeLine(); } + forEach(host.getSourceFiles(), sourceFile => { if (!isExternalModuleOrDeclarationFile(sourceFile)) { emitSourceFile(sourceFile); - } else if (compilerOptions.bundle && isExternalModule(sourceFile)) { + } + else if (compilerOptions.bundle && isExternalModule(sourceFile)) { emitBundledFile(sourceFile); } }); + if (compilerOptions.bundle) { writeLines(`return __define.require("${removeFileExtension(compilerOptions.bundle)}");`); decreaseIndent(); @@ -371,14 +374,9 @@ var __define = (this && this.__define) || (function() { } function emitBundledFile(sourceFile: SourceFile): void { + let dir = host.getCurrentDirectory(); let canonicalName = removeFileExtension( - getRelativePathToDirectoryOrUrl( - host.getCurrentDirectory(), - sourceFile.fileName, - host.getCurrentDirectory(), - host.getCanonicalFileName, - /*isAbsolutePathAnUrl*/false - ) + getRelativePathToDirectoryOrUrl(dir, sourceFile.fileName, dir, f => host.getCanonicalFileName(f), /*isAbsolutePathAnUrl*/false) ); writeLine(); write(`__define("/${canonicalName}", function(require, exports, module){`); @@ -6470,7 +6468,7 @@ var __define = (this && this.__define) || (function() { writeLine(); write(`var exports = {};`); writeLine(); - write(`for(var n in m) {`); + write(`for (var n in m) {`); increaseIndent(); writeLine(); write(`if (n !== "default"`); From fcf33554175e6de666b7a8e5d355fe7199604895 Mon Sep 17 00:00:00 2001 From: Wesley Wigham Date: Fri, 11 Sep 2015 16:07:54 -0700 Subject: [PATCH 05/14] use for-of instead of forEach --- src/compiler/emitter.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index cd9f938fce258..e5f912b605980 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -148,12 +148,12 @@ var __define = (this && this.__define) || (function() { if (targetSourceFile === undefined) { if (!compilerOptions.bundle) { - forEach(host.getSourceFiles(), sourceFile => { + for (var sourceFile of host.getSourceFiles()) { if (shouldEmitToOwnFile(sourceFile, compilerOptions)) { let jsFilePath = getOwnEmitOutputFilePath(sourceFile, host, shouldEmitJsx(sourceFile) ? ".jsx" : ".js"); emitFile(jsFilePath, sourceFile); } - }); + } } if (compilerOptions.outFile || compilerOptions.out) { From 67c1facc8918eaeb6fcc1751344e2153df9d3fcc Mon Sep 17 00:00:00 2001 From: Wesley Wigham Date: Fri, 11 Sep 2015 17:11:48 -0700 Subject: [PATCH 06/14] accept baselines --- .../reference/constDeclarations-access5.errors.txt | 4 ++-- tests/baselines/reference/es6-amd.errors.txt | 4 ++-- tests/baselines/reference/es6-declaration-amd.errors.txt | 4 ++-- tests/baselines/reference/es6-sourcemap-amd.errors.txt | 4 ++-- tests/baselines/reference/es6-umd.errors.txt | 4 ++-- tests/baselines/reference/es6-umd2.errors.txt | 4 ++-- ...ImportDefaultBindingFollowedWithNamedImport.errors.txt | 4 ++-- .../reference/es6ImportNameSpaceImport.errors.txt | 4 ++-- tests/baselines/reference/es6ImportNamedImport.errors.txt | 4 ++-- .../es6ImportNamedImportInExportAssignment.errors.txt | 4 ++-- .../reference/es6ModuleWithModuleGenTargetAmd.errors.txt | 4 ++-- .../es6ModuleWithModuleGenTargetCommonjs.errors.txt | 4 ++-- tests/baselines/reference/systemModule1.errors.txt | 4 ++-- tests/baselines/reference/systemModule11.js | 8 ++++---- tests/baselines/reference/systemModule16.js | 2 +- tests/baselines/reference/systemModule9.js | 2 +- 16 files changed, 32 insertions(+), 32 deletions(-) diff --git a/tests/baselines/reference/constDeclarations-access5.errors.txt b/tests/baselines/reference/constDeclarations-access5.errors.txt index d58abfe6bd4a3..b378b2290cbce 100644 --- a/tests/baselines/reference/constDeclarations-access5.errors.txt +++ b/tests/baselines/reference/constDeclarations-access5.errors.txt @@ -1,4 +1,4 @@ -error TS1204: Cannot compile modules into 'commonjs', 'amd', 'system' or 'umd' when targeting 'ES6' or higher. +error TS1204: Cannot compile modules into 'commonjs', 'amd', 'system' or 'umd' when targeting 'ES6' or higher unless --bundle is specified. tests/cases/compiler/constDeclarations_access_2.ts(2,1): error TS1202: Import assignment cannot be used when targeting ECMAScript 6 or higher. Consider using 'import * as ns from "mod"', 'import {a} from "mod"' or 'import d from "mod"' instead. tests/cases/compiler/constDeclarations_access_2.ts(4,1): error TS2450: Left-hand side of assignment expression cannot be a constant. tests/cases/compiler/constDeclarations_access_2.ts(5,1): error TS2450: Left-hand side of assignment expression cannot be a constant. @@ -20,7 +20,7 @@ tests/cases/compiler/constDeclarations_access_2.ts(22,3): error TS2449: The oper tests/cases/compiler/constDeclarations_access_2.ts(24,1): error TS2450: Left-hand side of assignment expression cannot be a constant. -!!! error TS1204: Cannot compile modules into 'commonjs', 'amd', 'system' or 'umd' when targeting 'ES6' or higher. +!!! error TS1204: Cannot compile modules into 'commonjs', 'amd', 'system' or 'umd' when targeting 'ES6' or higher unless --bundle is specified. ==== tests/cases/compiler/constDeclarations_access_2.ts (19 errors) ==== /// import m = require('constDeclarations_access_1'); diff --git a/tests/baselines/reference/es6-amd.errors.txt b/tests/baselines/reference/es6-amd.errors.txt index b4a6442458b78..7ddd8e0b17a46 100644 --- a/tests/baselines/reference/es6-amd.errors.txt +++ b/tests/baselines/reference/es6-amd.errors.txt @@ -1,7 +1,7 @@ -error TS1204: Cannot compile modules into 'commonjs', 'amd', 'system' or 'umd' when targeting 'ES6' or higher. +error TS1204: Cannot compile modules into 'commonjs', 'amd', 'system' or 'umd' when targeting 'ES6' or higher unless --bundle is specified. -!!! error TS1204: Cannot compile modules into 'commonjs', 'amd', 'system' or 'umd' when targeting 'ES6' or higher. +!!! error TS1204: Cannot compile modules into 'commonjs', 'amd', 'system' or 'umd' when targeting 'ES6' or higher unless --bundle is specified. ==== tests/cases/compiler/es6-amd.ts (0 errors) ==== class A diff --git a/tests/baselines/reference/es6-declaration-amd.errors.txt b/tests/baselines/reference/es6-declaration-amd.errors.txt index fe5a254dd54a4..e7d8b045663da 100644 --- a/tests/baselines/reference/es6-declaration-amd.errors.txt +++ b/tests/baselines/reference/es6-declaration-amd.errors.txt @@ -1,7 +1,7 @@ -error TS1204: Cannot compile modules into 'commonjs', 'amd', 'system' or 'umd' when targeting 'ES6' or higher. +error TS1204: Cannot compile modules into 'commonjs', 'amd', 'system' or 'umd' when targeting 'ES6' or higher unless --bundle is specified. -!!! error TS1204: Cannot compile modules into 'commonjs', 'amd', 'system' or 'umd' when targeting 'ES6' or higher. +!!! error TS1204: Cannot compile modules into 'commonjs', 'amd', 'system' or 'umd' when targeting 'ES6' or higher unless --bundle is specified. ==== tests/cases/compiler/es6-declaration-amd.ts (0 errors) ==== class A diff --git a/tests/baselines/reference/es6-sourcemap-amd.errors.txt b/tests/baselines/reference/es6-sourcemap-amd.errors.txt index 8ffa16aaa2397..bc045f36c4217 100644 --- a/tests/baselines/reference/es6-sourcemap-amd.errors.txt +++ b/tests/baselines/reference/es6-sourcemap-amd.errors.txt @@ -1,7 +1,7 @@ -error TS1204: Cannot compile modules into 'commonjs', 'amd', 'system' or 'umd' when targeting 'ES6' or higher. +error TS1204: Cannot compile modules into 'commonjs', 'amd', 'system' or 'umd' when targeting 'ES6' or higher unless --bundle is specified. -!!! error TS1204: Cannot compile modules into 'commonjs', 'amd', 'system' or 'umd' when targeting 'ES6' or higher. +!!! error TS1204: Cannot compile modules into 'commonjs', 'amd', 'system' or 'umd' when targeting 'ES6' or higher unless --bundle is specified. ==== tests/cases/compiler/es6-sourcemap-amd.ts (0 errors) ==== class A diff --git a/tests/baselines/reference/es6-umd.errors.txt b/tests/baselines/reference/es6-umd.errors.txt index 72a58585073bc..5f3cb2c1482db 100644 --- a/tests/baselines/reference/es6-umd.errors.txt +++ b/tests/baselines/reference/es6-umd.errors.txt @@ -1,7 +1,7 @@ -error TS1204: Cannot compile modules into 'commonjs', 'amd', 'system' or 'umd' when targeting 'ES6' or higher. +error TS1204: Cannot compile modules into 'commonjs', 'amd', 'system' or 'umd' when targeting 'ES6' or higher unless --bundle is specified. -!!! error TS1204: Cannot compile modules into 'commonjs', 'amd', 'system' or 'umd' when targeting 'ES6' or higher. +!!! error TS1204: Cannot compile modules into 'commonjs', 'amd', 'system' or 'umd' when targeting 'ES6' or higher unless --bundle is specified. ==== tests/cases/compiler/es6-umd.ts (0 errors) ==== class A diff --git a/tests/baselines/reference/es6-umd2.errors.txt b/tests/baselines/reference/es6-umd2.errors.txt index 00becfb105fa1..334c97c2bbe2a 100644 --- a/tests/baselines/reference/es6-umd2.errors.txt +++ b/tests/baselines/reference/es6-umd2.errors.txt @@ -1,7 +1,7 @@ -error TS1204: Cannot compile modules into 'commonjs', 'amd', 'system' or 'umd' when targeting 'ES6' or higher. +error TS1204: Cannot compile modules into 'commonjs', 'amd', 'system' or 'umd' when targeting 'ES6' or higher unless --bundle is specified. -!!! error TS1204: Cannot compile modules into 'commonjs', 'amd', 'system' or 'umd' when targeting 'ES6' or higher. +!!! error TS1204: Cannot compile modules into 'commonjs', 'amd', 'system' or 'umd' when targeting 'ES6' or higher unless --bundle is specified. ==== tests/cases/compiler/es6-umd2.ts (0 errors) ==== export class A diff --git a/tests/baselines/reference/es6ImportDefaultBindingFollowedWithNamedImport.errors.txt b/tests/baselines/reference/es6ImportDefaultBindingFollowedWithNamedImport.errors.txt index 2688d4af06f1c..b7a96c7ebbced 100644 --- a/tests/baselines/reference/es6ImportDefaultBindingFollowedWithNamedImport.errors.txt +++ b/tests/baselines/reference/es6ImportDefaultBindingFollowedWithNamedImport.errors.txt @@ -1,7 +1,7 @@ -error TS1204: Cannot compile modules into 'commonjs', 'amd', 'system' or 'umd' when targeting 'ES6' or higher. +error TS1204: Cannot compile modules into 'commonjs', 'amd', 'system' or 'umd' when targeting 'ES6' or higher unless --bundle is specified. -!!! error TS1204: Cannot compile modules into 'commonjs', 'amd', 'system' or 'umd' when targeting 'ES6' or higher. +!!! error TS1204: Cannot compile modules into 'commonjs', 'amd', 'system' or 'umd' when targeting 'ES6' or higher unless --bundle is specified. ==== tests/cases/compiler/es6ImportDefaultBindingFollowedWithNamedImport_0.ts (0 errors) ==== export var a = 10; diff --git a/tests/baselines/reference/es6ImportNameSpaceImport.errors.txt b/tests/baselines/reference/es6ImportNameSpaceImport.errors.txt index 00a6dcb2cfddb..c2c6df6054012 100644 --- a/tests/baselines/reference/es6ImportNameSpaceImport.errors.txt +++ b/tests/baselines/reference/es6ImportNameSpaceImport.errors.txt @@ -1,7 +1,7 @@ -error TS1204: Cannot compile modules into 'commonjs', 'amd', 'system' or 'umd' when targeting 'ES6' or higher. +error TS1204: Cannot compile modules into 'commonjs', 'amd', 'system' or 'umd' when targeting 'ES6' or higher unless --bundle is specified. -!!! error TS1204: Cannot compile modules into 'commonjs', 'amd', 'system' or 'umd' when targeting 'ES6' or higher. +!!! error TS1204: Cannot compile modules into 'commonjs', 'amd', 'system' or 'umd' when targeting 'ES6' or higher unless --bundle is specified. ==== tests/cases/compiler/es6ImportNameSpaceImport_0.ts (0 errors) ==== export var a = 10; diff --git a/tests/baselines/reference/es6ImportNamedImport.errors.txt b/tests/baselines/reference/es6ImportNamedImport.errors.txt index 12dcf331967e6..2f3ec08134e65 100644 --- a/tests/baselines/reference/es6ImportNamedImport.errors.txt +++ b/tests/baselines/reference/es6ImportNamedImport.errors.txt @@ -1,7 +1,7 @@ -error TS1204: Cannot compile modules into 'commonjs', 'amd', 'system' or 'umd' when targeting 'ES6' or higher. +error TS1204: Cannot compile modules into 'commonjs', 'amd', 'system' or 'umd' when targeting 'ES6' or higher unless --bundle is specified. -!!! error TS1204: Cannot compile modules into 'commonjs', 'amd', 'system' or 'umd' when targeting 'ES6' or higher. +!!! error TS1204: Cannot compile modules into 'commonjs', 'amd', 'system' or 'umd' when targeting 'ES6' or higher unless --bundle is specified. ==== tests/cases/compiler/es6ImportNamedImport_0.ts (0 errors) ==== export var a = 10; diff --git a/tests/baselines/reference/es6ImportNamedImportInExportAssignment.errors.txt b/tests/baselines/reference/es6ImportNamedImportInExportAssignment.errors.txt index 970c51973b218..b3de68b3203fa 100644 --- a/tests/baselines/reference/es6ImportNamedImportInExportAssignment.errors.txt +++ b/tests/baselines/reference/es6ImportNamedImportInExportAssignment.errors.txt @@ -1,8 +1,8 @@ -error TS1204: Cannot compile modules into 'commonjs', 'amd', 'system' or 'umd' when targeting 'ES6' or higher. +error TS1204: Cannot compile modules into 'commonjs', 'amd', 'system' or 'umd' when targeting 'ES6' or higher unless --bundle is specified. tests/cases/compiler/es6ImportNamedImportInExportAssignment_1.ts(2,1): error TS1203: Export assignment cannot be used when targeting ECMAScript 6 or higher. Consider using 'export default' instead. -!!! error TS1204: Cannot compile modules into 'commonjs', 'amd', 'system' or 'umd' when targeting 'ES6' or higher. +!!! error TS1204: Cannot compile modules into 'commonjs', 'amd', 'system' or 'umd' when targeting 'ES6' or higher unless --bundle is specified. ==== tests/cases/compiler/es6ImportNamedImportInExportAssignment_0.ts (0 errors) ==== export var a = 10; diff --git a/tests/baselines/reference/es6ModuleWithModuleGenTargetAmd.errors.txt b/tests/baselines/reference/es6ModuleWithModuleGenTargetAmd.errors.txt index 9592a9f899e64..d54e26adcd9a0 100644 --- a/tests/baselines/reference/es6ModuleWithModuleGenTargetAmd.errors.txt +++ b/tests/baselines/reference/es6ModuleWithModuleGenTargetAmd.errors.txt @@ -1,7 +1,7 @@ -error TS1204: Cannot compile modules into 'commonjs', 'amd', 'system' or 'umd' when targeting 'ES6' or higher. +error TS1204: Cannot compile modules into 'commonjs', 'amd', 'system' or 'umd' when targeting 'ES6' or higher unless --bundle is specified. -!!! error TS1204: Cannot compile modules into 'commonjs', 'amd', 'system' or 'umd' when targeting 'ES6' or higher. +!!! error TS1204: Cannot compile modules into 'commonjs', 'amd', 'system' or 'umd' when targeting 'ES6' or higher unless --bundle is specified. ==== tests/cases/compiler/es6ModuleWithModuleGenTargetAmd.ts (0 errors) ==== export class A { diff --git a/tests/baselines/reference/es6ModuleWithModuleGenTargetCommonjs.errors.txt b/tests/baselines/reference/es6ModuleWithModuleGenTargetCommonjs.errors.txt index b740104009982..bba6c8323ad3e 100644 --- a/tests/baselines/reference/es6ModuleWithModuleGenTargetCommonjs.errors.txt +++ b/tests/baselines/reference/es6ModuleWithModuleGenTargetCommonjs.errors.txt @@ -1,7 +1,7 @@ -error TS1204: Cannot compile modules into 'commonjs', 'amd', 'system' or 'umd' when targeting 'ES6' or higher. +error TS1204: Cannot compile modules into 'commonjs', 'amd', 'system' or 'umd' when targeting 'ES6' or higher unless --bundle is specified. -!!! error TS1204: Cannot compile modules into 'commonjs', 'amd', 'system' or 'umd' when targeting 'ES6' or higher. +!!! error TS1204: Cannot compile modules into 'commonjs', 'amd', 'system' or 'umd' when targeting 'ES6' or higher unless --bundle is specified. ==== tests/cases/compiler/es6ModuleWithModuleGenTargetCommonjs.ts (0 errors) ==== export class A { diff --git a/tests/baselines/reference/systemModule1.errors.txt b/tests/baselines/reference/systemModule1.errors.txt index 18b64607455e3..92d0b0583057c 100644 --- a/tests/baselines/reference/systemModule1.errors.txt +++ b/tests/baselines/reference/systemModule1.errors.txt @@ -1,7 +1,7 @@ -error TS1204: Cannot compile modules into 'commonjs', 'amd', 'system' or 'umd' when targeting 'ES6' or higher. +error TS1204: Cannot compile modules into 'commonjs', 'amd', 'system' or 'umd' when targeting 'ES6' or higher unless --bundle is specified. -!!! error TS1204: Cannot compile modules into 'commonjs', 'amd', 'system' or 'umd' when targeting 'ES6' or higher. +!!! error TS1204: Cannot compile modules into 'commonjs', 'amd', 'system' or 'umd' when targeting 'ES6' or higher unless --bundle is specified. ==== tests/cases/compiler/systemModule1.ts (0 errors) ==== export var x = 1; \ No newline at end of file diff --git a/tests/baselines/reference/systemModule11.js b/tests/baselines/reference/systemModule11.js index bf9b57c511cc2..68392519f0fa3 100644 --- a/tests/baselines/reference/systemModule11.js +++ b/tests/baselines/reference/systemModule11.js @@ -52,7 +52,7 @@ System.register(['bar'], function(exports_1) { }; function exportStar_1(m) { var exports = {}; - for(var n in m) { + for (var n in m) { if (n !== "default"&& !exportedNames_1.hasOwnProperty(n)) exports[n] = m[n]; } exports_1(exports); @@ -75,7 +75,7 @@ System.register(['bar'], function(exports_1) { }; function exportStar_1(m) { var exports = {}; - for(var n in m) { + for (var n in m) { if (n !== "default"&& !exportedNames_1.hasOwnProperty(n)) exports[n] = m[n]; } exports_1(exports); @@ -101,7 +101,7 @@ System.register(['a', 'bar'], function(exports_1) { }; function exportStar_1(m) { var exports = {}; - for(var n in m) { + for (var n in m) { if (n !== "default"&& !exportedNames_1.hasOwnProperty(n)) exports[n] = m[n]; } exports_1(exports); @@ -147,7 +147,7 @@ System.register(['a'], function(exports_1) { function foo() { } function exportStar_1(m) { var exports = {}; - for(var n in m) { + for (var n in m) { if (n !== "default") exports[n] = m[n]; } exports_1(exports); diff --git a/tests/baselines/reference/systemModule16.js b/tests/baselines/reference/systemModule16.js index 851b941492a4e..bf37ed4e489d3 100644 --- a/tests/baselines/reference/systemModule16.js +++ b/tests/baselines/reference/systemModule16.js @@ -24,7 +24,7 @@ System.register(["foo", "bar"], function(exports_1) { }; function exportStar_1(m) { var exports = {}; - for(var n in m) { + for (var n in m) { if (n !== "default"&& !exportedNames_1.hasOwnProperty(n)) exports[n] = m[n]; } exports_1(exports); diff --git a/tests/baselines/reference/systemModule9.js b/tests/baselines/reference/systemModule9.js index d7913f2f8b73e..32f21e0596f3c 100644 --- a/tests/baselines/reference/systemModule9.js +++ b/tests/baselines/reference/systemModule9.js @@ -31,7 +31,7 @@ System.register(['file1', 'file2', 'file3', 'file4', 'file5', 'file6', 'file7'], }; function exportStar_1(m) { var exports = {}; - for(var n in m) { + for (var n in m) { if (n !== "default"&& !exportedNames_1.hasOwnProperty(n)) exports[n] = m[n]; } exports_1(exports); From b79d4c6e9fa6800bb0bdf372dccde32b7a278f64 Mon Sep 17 00:00:00 2001 From: Wesley Wigham Date: Mon, 14 Sep 2015 14:58:21 -0700 Subject: [PATCH 07/14] Fix system emit --- src/compiler/emitter.ts | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index e5f912b605980..eb2728659ef1c 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -274,6 +274,14 @@ var __define = (this && this.__define) || (function() { exportFunctionForFile = undefined; currentSourceFile = undefined; writeLine(); + write("return {") + writeLine(); + increaseIndent(); + write("setters: [],"); + writeLine(); + write("execute: function() {"); + writeLine(); + increaseIndent(); write(`${exportStarName}(`); } } @@ -297,6 +305,12 @@ var __define = (this && this.__define) || (function() { }, [ModuleKind.System]() { write(");"); + writeLine(); + decreaseIndent(); + write("}"); + writeLine(); + decreaseIndent(); + write("};"); decreaseIndent(); writeLine(); write("});"); From 2ea714d194d45e111f82fa370d2e02bda0a72484 Mon Sep 17 00:00:00 2001 From: Wesley Wigham Date: Wed, 16 Sep 2015 14:52:05 -0700 Subject: [PATCH 08/14] Handle external module references in amd/umd/system --- src/compiler/core.ts | 28 ++++++ src/compiler/emitter.ts | 192 ++++++++++++++++++++++++---------------- 2 files changed, 146 insertions(+), 74 deletions(-) diff --git a/src/compiler/core.ts b/src/compiler/core.ts index a1f6565ed1ffb..2a148d385d413 100644 --- a/src/compiler/core.ts +++ b/src/compiler/core.ts @@ -140,6 +140,34 @@ namespace ts { } return result; } + + export function pairwiseMap(map: Map, f: (key: string, value: T) => U): U[] { + let result: U[]; + if (map) { + result = []; + for (let k in map) { + result.push(f(k, map[k])); + } + } + return result; + } + + export function flatten(array: T[][]): T[] { + if (array) { + return [].concat(...array); + } + return; + } + + export function keys(map: Map): string[] { + let result: string[] = []; + if (map) { + for (let k in map) { + result.push(k); + } + } + return result; + } export function concatenate(array1: T[], array2: T[]): T[] { if (!array2 || !array2.length) return array1; diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index 2283ed7c2f6b2..08bed0a9e6309 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -62,73 +62,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi }); };`; - const defineHelper = ` -var __define = (this && this.__define) || (function() { - var cache = {}, - id = 0, - idMap = {}, - builtinRequire = typeof require === "function" ? require : function() {}, - normalizeSlashes = function(path) { return path.replace("\\\\", "/"); }, - forEach = function(array, callback) { - for (var i = 0, len = array.length; i < len; i++) { - callback(array[i], i); - } - }, - resolvePath = function(base, name) { - var result = normalizeSlashes(base).split("/"); - result.pop(); - forEach(normalizeSlashes(name).split("/"), function(part, index) { - switch(part) { - case "": - case ".": - break; - case "..": - if (result.length <= 1) { - if (result[0] === "..") { - result.push(".."); - break; - } - else if (result[0] === "." || result[0] === "") { - result[0] = ".."; - break; - } - } - result.pop(); - break; - default: - result.push(part); - break; - } - }); - if (result[0] === "") result.shift(); - return "/"+result.join("/"); - }, - resolveRequire = function(name, from) { - var resolved = resolvePath(from || "/", name); - if (typeof cache[resolved] === "function") { - cache[resolved](); - } - return (cache[resolved] ? cache[resolved].exports : builtinRequire(name)); - }, - define = function (declaredName, factory) { - idMap[++id] = declaredName; - var module = { - id: id, - exports: {} - }; - var require = function(name) { - if (typeof name === "number") return resolveRequire(idMap[name]); - return resolveRequire(name, declaredName); - }; - cache[declaredName] = function() { - cache[declaredName] = module; - factory(require, module.exports, module); - }; - }; - define.require = resolveRequire; - return define; -})();`; - const umdHelper = `(function (deps, factory) { if (typeof module === 'object' && typeof module.exports === 'object') { var v = factory(require, exports); if (v !== undefined) module.exports = v; @@ -244,32 +177,50 @@ var __define = (this && this.__define) || (function() { /** Called once the emit of the node is done */ let emitEnd = function (node: Node) { }; + + let bundleDependenciesMap: {[key: string]: string}; /** Called to build the opening part of a bundled module wrapper */ let emitBundleWrapperStart: Map<() => void> = { [ModuleKind.CommonJS]() { write("module.exports = "); }, - [ModuleKind.AMD]() { //TODO: handle external deps with amd better - write("define([\"require\"], function(require){"); + [ModuleKind.AMD]() { + write("define([\"require\""); + forEachKey(bundleDependenciesMap, name => write(`, "${name}"`)); + write("], function(require"); + forEachValue(bundleDependenciesMap, name => write(`, ${name}`)); + write("){"); writeLine(); increaseIndent(); write("return "); }, [ModuleKind.UMD]() { writeLines(umdHelper); - write("[\"require\"], function(require){"); + write("[\"require\""); + forEachKey(bundleDependenciesMap, name => write(`, "${name}"`)); + write("], function(require"); + forEachValue(bundleDependenciesMap, name => write(`, ${name}`)); + write("){"); writeLine(); increaseIndent(); write("return "); }, [ModuleKind.System]() { //TODO: handle external deps with system - currentSourceFile = {identifiers: []}; exportFunctionForFile = makeUniqueName("exports"); + let deps: string[] = []; + forEachKey(bundleDependenciesMap, name => {deps.push(`"${name}"`);}); + console.log(deps); writeLine(); - write(`System.register([], function(${exportFunctionForFile}) {`); + write(`System.register([${deps.join(", ")}], function(${exportFunctionForFile}) {`); writeLine(); increaseIndent(); + let idents: string[] = []; + forEachValue(bundleDependenciesMap, name => {idents.push(name);}); + forEach(idents, id => { + write(`var ${id};`); + writeLine(); + }); let exportStarName = emitExportStarFunction(/*localNames*/undefined); exportFunctionForFile = undefined; currentSourceFile = undefined; @@ -277,7 +228,11 @@ var __define = (this && this.__define) || (function() { write("return {") writeLine(); increaseIndent(); - write("setters: [],"); + let setters = map(idents, id => `function(m) { + ${id} = m; +}`); + writeLines("setters: ["+setters.join(","+newLine)); + write("],"); writeLine(); write("execute: function() {"); writeLine(); @@ -350,11 +305,12 @@ var __define = (this && this.__define) || (function() { else { let originalKind = compilerOptions.module; if (compilerOptions.bundle) { + buildBundleDependenciesMap(); compilerOptions.module = ModuleKind.CommonJS; emitBundleWrapperStart[originalKind](); write("(function() {"); increaseIndent(); - writeLines(defineHelper); + writeDefineHelper(); forEach(host.getSourceFiles(), emitEmitHelpers); writeLine(); } @@ -402,6 +358,94 @@ var __define = (this && this.__define) || (function() { write("});"); } + function buildBundleDependenciesMap(): void { + let identifiers = deduplicate(flatten(map(host.getSourceFiles(), file => keys(file.identifiers)))); + let identifiersMap: Map = {}; + forEach(identifiers, name => identifiersMap[name] = escapeIdentifier(name)); + currentSourceFile = {identifiers: identifiersMap}; // Enables bundlewide makeUniqueName + let deps = deduplicate(filter(flatten(map(host.getSourceFiles(), file => pairwiseMap(file.resolvedModules, (importName, resolvedModule) => (!resolvedModule || resolvedModule.isExternalLibraryImport) ? importName : undefined))), name => !!name)); + let depsIdentifiers = map(deps, name => makeUniqueName(escapeIdentifier(makeIdentifierFromModuleName(name)))); + bundleDependenciesMap = {}; + forEach(deps, (name, index) => { + bundleDependenciesMap[name] = depsIdentifiers[index]; + }); + } + + function writeDefineHelper(): void { + writeLines(` +var __define = (this && this.__define) || (function() { + var cache = {}, + id = 0, + idMap = {}, + builtinRequire = typeof require === "function" ? require : function() {}, + normalizeSlashes = function(path) { return path.replace("\\\\", "/"); }, + forEach = function(array, callback) { + for (var i = 0, len = array.length; i < len; i++) { + callback(array[i], i); + } + }, + resolvePath = function(base, name) { + var result = normalizeSlashes(base).split("/"); + result.pop(); + forEach(normalizeSlashes(name).split("/"), function(part, index) { + switch(part) { + case "": + case ".": + break; + case "..": + if (result.length <= 1) { + if (result[0] === "..") { + result.push(".."); + break; + } + else if (result[0] === "." || result[0] === "") { + result[0] = ".."; + break; + } + } + result.pop(); + break; + default: + result.push(part); + break; + } + }); + if (result[0] === "") result.shift(); + return "/"+result.join("/"); + }, + initializeAndGet = function(resolved) { + if (typeof cache[resolved] === "function") { + cache[resolved](); + } + return (cache[resolved] && cache[resolved].exports); + }, + resolveRequire = function(name, from) {`); + forEachKey(bundleDependenciesMap, name => { + writeLines(` if (name === "${name}") return ${bundleDependenciesMap[name]};`); + }); + writeLines(` var resolved = resolvePath(from || "/", name); + return (initializeAndGet(resolved) || builtinRequire(name)); + }, + define = function (declaredName, factory) { + idMap[++id] = declaredName; + var module = { + id: id, + exports: {} + }; + var require = function(name) { + if (typeof name === "number") return initializeAndGet(idMap[name]); + return resolveRequire(name, declaredName); + }; + cache[declaredName] = function() { + cache[declaredName] = module; + factory(require, module.exports, module); + }; + }; + define.require = resolveRequire; + return define; +})();`); + } + function isUniqueName(name: string): boolean { return !resolver.hasGlobalName(name) && !hasProperty(currentSourceFile.identifiers, name) && From 3b7ad598cc3f9231f8e20a2b51e23c834b545408 Mon Sep 17 00:00:00 2001 From: Wesley Wigham Date: Wed, 16 Sep 2015 15:00:33 -0700 Subject: [PATCH 09/14] Throw on module resolution failure --- src/compiler/emitter.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index 08bed0a9e6309..9fc399d7a565f 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -377,7 +377,7 @@ var __define = (this && this.__define) || (function() { var cache = {}, id = 0, idMap = {}, - builtinRequire = typeof require === "function" ? require : function() {}, + builtinRequire = typeof require === "function" ? require : function(name) { throw new Error("Could not find module named \""+name+"\"."); }, normalizeSlashes = function(path) { return path.replace("\\\\", "/"); }, forEach = function(array, callback) { for (var i = 0, len = array.length; i < len; i++) { From aa18b115622c51d9aed259bad10ed1082a29dbb2 Mon Sep 17 00:00:00 2001 From: Wesley Wigham Date: Wed, 16 Sep 2015 15:04:53 -0700 Subject: [PATCH 10/14] Indent case, other cleanup --- src/compiler/emitter.ts | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index 9fc399d7a565f..3422395047103 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -206,11 +206,10 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi increaseIndent(); write("return "); }, - [ModuleKind.System]() { //TODO: handle external deps with system + [ModuleKind.System]() { exportFunctionForFile = makeUniqueName("exports"); let deps: string[] = []; forEachKey(bundleDependenciesMap, name => {deps.push(`"${name}"`);}); - console.log(deps); writeLine(); write(`System.register([${deps.join(", ")}], function(${exportFunctionForFile}) {`); writeLine(); @@ -391,23 +390,23 @@ var __define = (this && this.__define) || (function() { switch(part) { case "": case ".": - break; + break; case "..": - if (result.length <= 1) { - if (result[0] === "..") { - result.push(".."); - break; - } - else if (result[0] === "." || result[0] === "") { - result[0] = ".."; - break; + if (result.length <= 1) { + if (result[0] === "..") { + result.push(".."); + break; + } + else if (result[0] === "." || result[0] === "") { + result[0] = ".."; + break; + } } - } - result.pop(); - break; + result.pop(); + break; default: - result.push(part); - break; + result.push(part); + break; } }); if (result[0] === "") result.shift(); From 55287c1a731e8d70deef694c4bd3f49862d60015 Mon Sep 17 00:00:00 2001 From: Wesley Wigham Date: Wed, 16 Sep 2015 15:36:01 -0700 Subject: [PATCH 11/14] bundle test --- tests/cases/compiler/bundleCommonjs.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 tests/cases/compiler/bundleCommonjs.ts diff --git a/tests/cases/compiler/bundleCommonjs.ts b/tests/cases/compiler/bundleCommonjs.ts new file mode 100644 index 0000000000000..d79f7d42d4a53 --- /dev/null +++ b/tests/cases/compiler/bundleCommonjs.ts @@ -0,0 +1,17 @@ +// @module: commonjs +// @outFile: bundleCommonjs_bundle.js +// @bundle: bundleCommonjs_file1 + +// @Filename: bundleCommonjs_file0.ts +export class Foo { + static CONSTANT = "Foo"; +} + +export default function assert(value: boolean) { + if (!value) throw new Error("Assertion failed!"); +} + + +// @Filename: bundleCommonjs_file1.ts +import {Foo, default as assert} from "./bundleCommonjs_file0"; +assert(Foo.CONSTANT === "Foo"); From 512e928eef906076e35777f4d1b8a9e430ba9306 Mon Sep 17 00:00:00 2001 From: Wesley Wigham Date: Thu, 17 Sep 2015 12:58:12 -0700 Subject: [PATCH 12/14] test cases galore --- tests/cases/compiler/bundleAMD.ts | 18 ++++++++++++++++++ tests/cases/compiler/bundleCommonjs.ts | 5 +++-- tests/cases/compiler/bundleES6.ts | 17 +++++++++++++++++ tests/cases/compiler/bundleSFX.ts | 18 ++++++++++++++++++ tests/cases/compiler/bundleSystem.ts | 18 ++++++++++++++++++ tests/cases/compiler/bundleToES3AMD.ts | 18 ++++++++++++++++++ tests/cases/compiler/bundleToES3Commonjs.ts | 18 ++++++++++++++++++ tests/cases/compiler/bundleToES3SFX.ts | 18 ++++++++++++++++++ tests/cases/compiler/bundleToES3System.ts | 18 ++++++++++++++++++ tests/cases/compiler/bundleToES3UMD.ts | 18 ++++++++++++++++++ tests/cases/compiler/bundleUMD.ts | 18 ++++++++++++++++++ 11 files changed, 182 insertions(+), 2 deletions(-) create mode 100644 tests/cases/compiler/bundleAMD.ts create mode 100644 tests/cases/compiler/bundleES6.ts create mode 100644 tests/cases/compiler/bundleSFX.ts create mode 100644 tests/cases/compiler/bundleSystem.ts create mode 100644 tests/cases/compiler/bundleToES3AMD.ts create mode 100644 tests/cases/compiler/bundleToES3Commonjs.ts create mode 100644 tests/cases/compiler/bundleToES3SFX.ts create mode 100644 tests/cases/compiler/bundleToES3System.ts create mode 100644 tests/cases/compiler/bundleToES3UMD.ts create mode 100644 tests/cases/compiler/bundleUMD.ts diff --git a/tests/cases/compiler/bundleAMD.ts b/tests/cases/compiler/bundleAMD.ts new file mode 100644 index 0000000000000..8618882432bf0 --- /dev/null +++ b/tests/cases/compiler/bundleAMD.ts @@ -0,0 +1,18 @@ +// @target: es6 +// @module: amd +// @outFile: bundleAMD_bundle.js +// @bundle: bundleAMD_file1 + +// @Filename: bundleAMD_file0.ts +export class Foo { + static CONSTANT = "Foo"; +} + +export function assert(value: boolean) { + if (!value) throw new Error("Assertion failed!"); +} + + +// @Filename: bundleAMD_file1.ts +import {Foo, assert} from "./bundleAMD_file0"; +assert(Foo.CONSTANT === "Foo"); diff --git a/tests/cases/compiler/bundleCommonjs.ts b/tests/cases/compiler/bundleCommonjs.ts index d79f7d42d4a53..10eb31f7e8397 100644 --- a/tests/cases/compiler/bundleCommonjs.ts +++ b/tests/cases/compiler/bundleCommonjs.ts @@ -1,3 +1,4 @@ +// @target: es6 // @module: commonjs // @outFile: bundleCommonjs_bundle.js // @bundle: bundleCommonjs_file1 @@ -7,11 +8,11 @@ export class Foo { static CONSTANT = "Foo"; } -export default function assert(value: boolean) { +export function assert(value: boolean) { if (!value) throw new Error("Assertion failed!"); } // @Filename: bundleCommonjs_file1.ts -import {Foo, default as assert} from "./bundleCommonjs_file0"; +import {Foo, assert} from "./bundleCommonjs_file0"; assert(Foo.CONSTANT === "Foo"); diff --git a/tests/cases/compiler/bundleES6.ts b/tests/cases/compiler/bundleES6.ts new file mode 100644 index 0000000000000..f2f48ba42f518 --- /dev/null +++ b/tests/cases/compiler/bundleES6.ts @@ -0,0 +1,17 @@ +// @target: es6 +// @outFile: bundleES6_bundle.js +// @bundle: bundleES6_file1 + +// @Filename: bundleES6_file0.ts +export class Foo { + static CONSTANT = "Foo"; +} + +export function assert(value: boolean) { + if (!value) throw new Error("Assertion failed!"); +} + + +// @Filename: bundleES6_file1.ts +import {Foo, assert} from "./bundleES6_file0"; +assert(Foo.CONSTANT === "Foo"); diff --git a/tests/cases/compiler/bundleSFX.ts b/tests/cases/compiler/bundleSFX.ts new file mode 100644 index 0000000000000..0f25cdd8cc2cd --- /dev/null +++ b/tests/cases/compiler/bundleSFX.ts @@ -0,0 +1,18 @@ +// @target: es6 +// @module: none +// @outFile: bundleSFX_bundle.js +// @bundle: bundleSFX_file1 + +// @Filename: bundleSFX_file0.ts +export class Foo { + static CONSTANT = "Foo"; +} + +export function assert(value: boolean) { + if (!value) throw new Error("Assertion failed!"); +} + + +// @Filename: bundleSFX_file1.ts +import {Foo, assert} from "./bundleSFX_file0"; +assert(Foo.CONSTANT === "Foo"); diff --git a/tests/cases/compiler/bundleSystem.ts b/tests/cases/compiler/bundleSystem.ts new file mode 100644 index 0000000000000..0e5e8dcf6c6c7 --- /dev/null +++ b/tests/cases/compiler/bundleSystem.ts @@ -0,0 +1,18 @@ +// @target: es6 +// @module: system +// @outFile: bundleSystem_bundle.js +// @bundle: bundleSystem_file1 + +// @Filename: bundleSystem_file0.ts +export class Foo { + static CONSTANT = "Foo"; +} + +export function assert(value: boolean) { + if (!value) throw new Error("Assertion failed!"); +} + + +// @Filename: bundleSystem_file1.ts +import {Foo, assert} from "./bundleSystem_file0"; +assert(Foo.CONSTANT === "Foo"); diff --git a/tests/cases/compiler/bundleToES3AMD.ts b/tests/cases/compiler/bundleToES3AMD.ts new file mode 100644 index 0000000000000..661cd7370b882 --- /dev/null +++ b/tests/cases/compiler/bundleToES3AMD.ts @@ -0,0 +1,18 @@ +// @target: es3 +// @module: amd +// @outFile: bundleToES3AMD_bundle.js +// @bundle: bundleToES3AMD_file1 + +// @Filename: bundleToES3AMD_file0.ts +export class Foo { + static CONSTANT = "Foo"; +} + +export function assert(value: boolean) { + if (!value) throw new Error("Assertion failed!"); +} + + +// @Filename: bundleToES3AMD_file1.ts +import {Foo, assert} from "./bundleToES3AMD_file0"; +assert(Foo.CONSTANT === "Foo"); diff --git a/tests/cases/compiler/bundleToES3Commonjs.ts b/tests/cases/compiler/bundleToES3Commonjs.ts new file mode 100644 index 0000000000000..a89f2714f1db0 --- /dev/null +++ b/tests/cases/compiler/bundleToES3Commonjs.ts @@ -0,0 +1,18 @@ +// @target: es3 +// @module: amd +// @outFile: bundleToES3Commonjs_bundle.js +// @bundle: bundleToES3Commonjs_file1 + +// @Filename: bundleToES3Commonjs_file0.ts +export class Foo { + static CONSTANT = "Foo"; +} + +export function assert(value: boolean) { + if (!value) throw new Error("Assertion failed!"); +} + + +// @Filename: bundleToES3Commonjs_file1.ts +import {Foo, assert} from "./bundleToES3Commonjs_file0"; +assert(Foo.CONSTANT === "Foo"); diff --git a/tests/cases/compiler/bundleToES3SFX.ts b/tests/cases/compiler/bundleToES3SFX.ts new file mode 100644 index 0000000000000..1b5b3c62f3ddf --- /dev/null +++ b/tests/cases/compiler/bundleToES3SFX.ts @@ -0,0 +1,18 @@ +// @target: es3 +// @module: none +// @outFile: bundleToES3SFX_bundle.js +// @bundle: bundleToES3SFX_file1 + +// @Filename: bundleToES3SFX_file0.ts +export class Foo { + static CONSTANT = "Foo"; +} + +export function assert(value: boolean) { + if (!value) throw new Error("Assertion failed!"); +} + + +// @Filename: bundleToES3SFX_file1.ts +import {Foo, assert} from "./bundleToES3SFX_file0"; +assert(Foo.CONSTANT === "Foo"); diff --git a/tests/cases/compiler/bundleToES3System.ts b/tests/cases/compiler/bundleToES3System.ts new file mode 100644 index 0000000000000..78752153119b5 --- /dev/null +++ b/tests/cases/compiler/bundleToES3System.ts @@ -0,0 +1,18 @@ +// @target: es3 +// @module: system +// @outFile: bundleToES3System_bundle.js +// @bundle: bundleToES3System_file1 + +// @Filename: bundleToES3System_file0.ts +export class Foo { + static CONSTANT = "Foo"; +} + +export function assert(value: boolean) { + if (!value) throw new Error("Assertion failed!"); +} + + +// @Filename: bundleToES3System_file1.ts +import {Foo, assert} from "./bundleToES3System_file0"; +assert(Foo.CONSTANT === "Foo"); diff --git a/tests/cases/compiler/bundleToES3UMD.ts b/tests/cases/compiler/bundleToES3UMD.ts new file mode 100644 index 0000000000000..69babde53c494 --- /dev/null +++ b/tests/cases/compiler/bundleToES3UMD.ts @@ -0,0 +1,18 @@ +// @target: es3 +// @module: umd +// @outFile: bundleToES3UMD_bundle.js +// @bundle: bundleToES3UMD_file1 + +// @Filename: bundleToES3UMD_file0.ts +export class Foo { + static CONSTANT = "Foo"; +} + +export function assert(value: boolean) { + if (!value) throw new Error("Assertion failed!"); +} + + +// @Filename: bundleToES3UMD_file1.ts +import {Foo, assert} from "./bundleToES3UMD_file0"; +assert(Foo.CONSTANT === "Foo"); diff --git a/tests/cases/compiler/bundleUMD.ts b/tests/cases/compiler/bundleUMD.ts new file mode 100644 index 0000000000000..daa279fe0182a --- /dev/null +++ b/tests/cases/compiler/bundleUMD.ts @@ -0,0 +1,18 @@ +// @target: es6 +// @module: umd +// @outFile: bundleUMD_bundle.js +// @bundle: bundleUMD_file1 + +// @Filename: bundleUMD_file0.ts +export class Foo { + static CONSTANT = "Foo"; +} + +export function assert(value: boolean) { + if (!value) throw new Error("Assertion failed!"); +} + + +// @Filename: bundleUMD_file1.ts +import {Foo, assert} from "./bundleUMD_file0"; +assert(Foo.CONSTANT === "Foo"); From 5d73d45492009c053d7f36f22cf5aefca8081a4d Mon Sep 17 00:00:00 2001 From: Wesley Wigham Date: Thu, 24 Sep 2015 18:56:35 -0700 Subject: [PATCH 13/14] Fix tests/system emit, working sfx bundle, none is valid module kind --- src/compiler/commandLineParser.ts | 3 +- src/compiler/diagnosticMessages.json | 6 +- src/compiler/emitter.ts | 14 ++- src/compiler/program.ts | 15 ++- src/harness/harness.ts | 1 - tests/baselines/reference/bundleAMD.js | 1 + tests/baselines/reference/bundleES6.js | 105 +++++++++++++++++ tests/baselines/reference/bundleES6.symbols | 30 +++++ tests/baselines/reference/bundleES6.types | 37 ++++++ tests/baselines/reference/bundleSFX.js | 105 +++++++++++++++++ tests/baselines/reference/bundleSFX.symbols | 30 +++++ tests/baselines/reference/bundleSFX.types | 37 ++++++ tests/baselines/reference/bundleSystem.js | 4 +- tests/baselines/reference/bundleToES3AMD.js | 1 + .../reference/bundleToES3Commonjs.js | 1 + tests/baselines/reference/bundleToES3SFX.js | 108 ++++++++++++++++++ .../reference/bundleToES3SFX.symbols | 30 +++++ .../baselines/reference/bundleToES3SFX.types | 37 ++++++ .../baselines/reference/bundleToES3System.js | 6 +- tests/cases/compiler/bundleES6.ts | 1 + 20 files changed, 556 insertions(+), 16 deletions(-) create mode 100644 tests/baselines/reference/bundleES6.js create mode 100644 tests/baselines/reference/bundleES6.symbols create mode 100644 tests/baselines/reference/bundleES6.types create mode 100644 tests/baselines/reference/bundleSFX.js create mode 100644 tests/baselines/reference/bundleSFX.symbols create mode 100644 tests/baselines/reference/bundleSFX.types create mode 100644 tests/baselines/reference/bundleToES3SFX.js create mode 100644 tests/baselines/reference/bundleToES3SFX.symbols create mode 100644 tests/baselines/reference/bundleToES3SFX.types diff --git a/src/compiler/commandLineParser.ts b/src/compiler/commandLineParser.ts index 368610466580d..5fd8c126d3fbd 100644 --- a/src/compiler/commandLineParser.ts +++ b/src/compiler/commandLineParser.ts @@ -72,13 +72,14 @@ namespace ts { name: "module", shortName: "m", type: { + "none": ModuleKind.None, "commonjs": ModuleKind.CommonJS, "amd": ModuleKind.AMD, "system": ModuleKind.System, "umd": ModuleKind.UMD, "es6": ModuleKind.ES6, }, - description: Diagnostics.Specify_module_code_generation_Colon_commonjs_amd_system_umd_or_es6, + description: Diagnostics.Specify_module_code_generation_Colon_commonjs_amd_system_umd_es6_or_none_only_valid_with_bundle, paramType: Diagnostics.KIND, error: Diagnostics.Argument_for_module_option_must_be_commonjs_amd_system_umd_or_es6 }, diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index cb90b94e91782..93b845efaef96 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -675,6 +675,10 @@ "category": "Error", "code": 1215 }, + "Cannot use option '--module none' without using '--bundle'.": { + "category": "Error", + "code": 1216 + }, "Export assignment is not supported when '--module' flag is 'system'.": { "category": "Error", "code": 1218 @@ -2105,7 +2109,7 @@ "category": "Message", "code": 6015 }, - "Specify module code generation: 'commonjs', 'amd', 'system', 'umd' or 'es6'": { + "Specify module code generation: 'commonjs', 'amd', 'system', 'umd', 'es6', or 'none' (only valid with --bundle)": { "category": "Message", "code": 6016 }, diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index 4b44bbed3f247..6a6c75cb14b57 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -183,6 +183,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi /** Called to build the opening part of a bundled module wrapper */ let emitBundleWrapperStart: Map<() => void> = { + [ModuleKind.None]() {}, [ModuleKind.CommonJS]() { write("module.exports = "); }, @@ -238,11 +239,13 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi writeLine(); increaseIndent(); write(`${exportStarName}(`); - } + }, + [ModuleKind.ES6]() {}, } /** Called to build the closing part of a bundled module wrapper */ let emitBundleWrapperEnd: Map<() => void> = { + [ModuleKind.None]() {}, [ModuleKind.CommonJS]() { write(";"); }, @@ -269,7 +272,8 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi decreaseIndent(); writeLine(); write("});"); - } + }, + [ModuleKind.ES6]() {}, } /** Emit the text for the given token that comes after startPos @@ -311,10 +315,10 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi emitSourceFile(root); } else { - let originalKind = compilerOptions.module; + let originalKind = modulekind; if (compilerOptions.bundle) { buildBundleDependenciesMap(); - compilerOptions.module = ModuleKind.CommonJS; + modulekind = ModuleKind.CommonJS; emitBundleWrapperStart[originalKind](); write("(function() {"); increaseIndent(); @@ -337,7 +341,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi decreaseIndent(); writeLines("})()"); emitBundleWrapperEnd[originalKind](); - compilerOptions.module = originalKind; + modulekind = originalKind; } } diff --git a/src/compiler/program.ts b/src/compiler/program.ts index 1b05db7917a69..589aa2624870e 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -947,6 +947,10 @@ namespace ts { return allFilesBelongToPath; } + + function moduleFormatProvided(): boolean { + return options.module !== undefined && options.module !== null; + } function verifyCompilerOptions() { if (options.isolatedModules) { @@ -1003,10 +1007,11 @@ namespace ts { let languageVersion = options.target || ScriptTarget.ES3; let outFile = options.outFile || options.out; + let moduleProvided = moduleFormatProvided(); let firstExternalModuleSourceFile = forEach(files, f => isExternalModule(f) ? f : undefined); if (options.isolatedModules) { - if (!options.module && languageVersion < ScriptTarget.ES6) { + if (!moduleProvided && languageVersion < ScriptTarget.ES6) { programDiagnostics.add(createCompilerDiagnostic(Diagnostics.Option_isolatedModules_can_only_be_used_when_either_option_module_is_provided_or_option_target_is_ES6_or_higher)); } @@ -1016,16 +1021,20 @@ namespace ts { programDiagnostics.add(createFileDiagnostic(firstNonExternalModuleSourceFile, span.start, span.length, Diagnostics.Cannot_compile_namespaces_when_the_isolatedModules_flag_is_provided)); } } - else if (firstExternalModuleSourceFile && languageVersion < ScriptTarget.ES6 && !options.module) { + else if (firstExternalModuleSourceFile && languageVersion < ScriptTarget.ES6 && !moduleProvided) { // We cannot use createDiagnosticFromNode because nodes do not have parents yet let span = getErrorSpanForNode(firstExternalModuleSourceFile, firstExternalModuleSourceFile.externalModuleIndicator); programDiagnostics.add(createFileDiagnostic(firstExternalModuleSourceFile, span.start, span.length, Diagnostics.Cannot_compile_modules_unless_the_module_flag_is_provided)); } - if (options.bundle && !(options.module && outFile) ) { + if (options.bundle && !(moduleProvided && outFile) ) { programDiagnostics.add(createCompilerDiagnostic(Diagnostics.Option_0_requires_both_1_and_2, "bundle", "outFile", "module")); } + if (options.module === ModuleKind.None && !options.bundle) { + programDiagnostics.add(createCompilerDiagnostic(Diagnostics.Cannot_use_option_module_none_without_using_bundle)); + } + // Cannot specify module gen target of es6 when below es6 if (options.module === ModuleKind.ES6 && languageVersion < ScriptTarget.ES6) { programDiagnostics.add(createCompilerDiagnostic(Diagnostics.Cannot_compile_modules_into_es6_when_targeting_ES5_or_lower)); diff --git a/src/harness/harness.ts b/src/harness/harness.ts index 57eb848ac23ae..b40419cdcf948 100644 --- a/src/harness/harness.ts +++ b/src/harness/harness.ts @@ -1079,7 +1079,6 @@ module Harness { options = options || { noResolve: false }; options.target = options.target || ts.ScriptTarget.ES3; - options.module = options.module || ts.ModuleKind.None; options.newLine = options.newLine || ts.NewLineKind.CarriageReturnLineFeed; options.noErrorTruncation = true; options.skipDefaultLibCheck = true; diff --git a/tests/baselines/reference/bundleAMD.js b/tests/baselines/reference/bundleAMD.js index 2bdc4193abbbe..4e597a2a99f64 100644 --- a/tests/baselines/reference/bundleAMD.js +++ b/tests/baselines/reference/bundleAMD.js @@ -99,6 +99,7 @@ define(["require"], function(require){ exports.assert = assert; }); __define("/tests/cases/compiler/bundleAMD_file1", function(require, exports, module){ + var bundleAMD_file0_1 = require("./bundleAMD_file0"); bundleAMD_file0_1.assert(bundleAMD_file0_1.Foo.CONSTANT === "Foo"); }); return __define.require("bundleAMD_file1"); diff --git a/tests/baselines/reference/bundleES6.js b/tests/baselines/reference/bundleES6.js new file mode 100644 index 0000000000000..6dd8d95779e52 --- /dev/null +++ b/tests/baselines/reference/bundleES6.js @@ -0,0 +1,105 @@ +//// [tests/cases/compiler/bundleES6.ts] //// + +//// [bundleES6_file0.ts] + +export class Foo { + static CONSTANT = "Foo"; +} + +export function assert(value: boolean) { + if (!value) throw new Error("Assertion failed!"); +} + + +//// [bundleES6_file1.ts] +import {Foo, assert} from "./bundleES6_file0"; +assert(Foo.CONSTANT === "Foo"); + + +//// [bundleES6_bundle.js] +(function() { + var __define = (this && this.__define) || (function() { + var cache = {}, + id = 0, + idMap = {}, + builtinRequire = typeof require === "function" ? require : function(name) { throw new Error("Could not find module named ""+name+""."); }, + normalizeSlashes = function(path) { return path.replace("\\", "/"); }, + forEach = function(array, callback) { + for (var i = 0, len = array.length; i < len; i++) { + callback(array[i], i); + } + }, + resolvePath = function(base, name) { + var result = normalizeSlashes(base).split("/"); + result.pop(); + forEach(normalizeSlashes(name).split("/"), function(part, index) { + switch(part) { + case "": + case ".": + break; + case "..": + if (result.length <= 1) { + if (result[0] === "..") { + result.push(".."); + break; + } + else if (result[0] === "." || result[0] === "") { + result[0] = ".."; + break; + } + } + result.pop(); + break; + default: + result.push(part); + break; + } + }); + if (result[0] === "") result.shift(); + return "/"+result.join("/"); + }, + initializeAndGet = function(resolved) { + if (typeof cache[resolved] === "function") { + cache[resolved](); + } + return (cache[resolved] && cache[resolved].exports); + }, + resolveRequire = function(name, from) { + var resolved = resolvePath(from || "/", name); + return (initializeAndGet(resolved) || builtinRequire(name)); + }, + define = function (declaredName, factory) { + idMap[++id] = declaredName; + var module = { + id: id, + exports: {} + }; + var require = function(name) { + if (typeof name === "number") return initializeAndGet(idMap[name]); + return resolveRequire(name, declaredName); + }; + cache[declaredName] = function() { + cache[declaredName] = module; + factory(require, module.exports, module); + }; + }; + define.require = resolveRequire; + return define; + })(); + __define("/tests/cases/compiler/bundleES6_file0", function(require, exports, module){ + class Foo { + } + Foo.CONSTANT = "Foo"; + exports.Foo = Foo; + function assert(value) { + if (!value) + throw new Error("Assertion failed!"); + } + exports.assert = assert; + }); + __define("/tests/cases/compiler/bundleES6_file1", function(require, exports, module){ + var bundleES6_file0_1 = require("./bundleES6_file0"); + bundleES6_file0_1.assert(bundleES6_file0_1.Foo.CONSTANT === "Foo"); + }); + return __define.require("bundleES6_file1"); +})() diff --git a/tests/baselines/reference/bundleES6.symbols b/tests/baselines/reference/bundleES6.symbols new file mode 100644 index 0000000000000..92d0eba40b2f9 --- /dev/null +++ b/tests/baselines/reference/bundleES6.symbols @@ -0,0 +1,30 @@ +=== tests/cases/compiler/bundleES6_file0.ts === + +export class Foo { +>Foo : Symbol(Foo, Decl(bundleES6_file0.ts, 0, 0)) + + static CONSTANT = "Foo"; +>CONSTANT : Symbol(Foo.CONSTANT, Decl(bundleES6_file0.ts, 1, 18)) +} + +export function assert(value: boolean) { +>assert : Symbol(assert, Decl(bundleES6_file0.ts, 3, 1)) +>value : Symbol(value, Decl(bundleES6_file0.ts, 5, 23)) + + if (!value) throw new Error("Assertion failed!"); +>value : Symbol(value, Decl(bundleES6_file0.ts, 5, 23)) +>Error : Symbol(Error, Decl(lib.d.ts, 876, 38), Decl(lib.d.ts, 889, 11)) +} + + +=== tests/cases/compiler/bundleES6_file1.ts === +import {Foo, assert} from "./bundleES6_file0"; +>Foo : Symbol(Foo, Decl(bundleES6_file1.ts, 0, 8)) +>assert : Symbol(assert, Decl(bundleES6_file1.ts, 0, 12)) + +assert(Foo.CONSTANT === "Foo"); +>assert : Symbol(assert, Decl(bundleES6_file1.ts, 0, 12)) +>Foo.CONSTANT : Symbol(Foo.CONSTANT, Decl(bundleES6_file0.ts, 1, 18)) +>Foo : Symbol(Foo, Decl(bundleES6_file1.ts, 0, 8)) +>CONSTANT : Symbol(Foo.CONSTANT, Decl(bundleES6_file0.ts, 1, 18)) + diff --git a/tests/baselines/reference/bundleES6.types b/tests/baselines/reference/bundleES6.types new file mode 100644 index 0000000000000..b6b8d909a5ae2 --- /dev/null +++ b/tests/baselines/reference/bundleES6.types @@ -0,0 +1,37 @@ +=== tests/cases/compiler/bundleES6_file0.ts === + +export class Foo { +>Foo : Foo + + static CONSTANT = "Foo"; +>CONSTANT : string +>"Foo" : string +} + +export function assert(value: boolean) { +>assert : (value: boolean) => void +>value : boolean + + if (!value) throw new Error("Assertion failed!"); +>!value : boolean +>value : boolean +>new Error("Assertion failed!") : Error +>Error : ErrorConstructor +>"Assertion failed!" : string +} + + +=== tests/cases/compiler/bundleES6_file1.ts === +import {Foo, assert} from "./bundleES6_file0"; +>Foo : typeof Foo +>assert : (value: boolean) => void + +assert(Foo.CONSTANT === "Foo"); +>assert(Foo.CONSTANT === "Foo") : void +>assert : (value: boolean) => void +>Foo.CONSTANT === "Foo" : boolean +>Foo.CONSTANT : string +>Foo : typeof Foo +>CONSTANT : string +>"Foo" : string + diff --git a/tests/baselines/reference/bundleSFX.js b/tests/baselines/reference/bundleSFX.js new file mode 100644 index 0000000000000..52f7ef1be2c21 --- /dev/null +++ b/tests/baselines/reference/bundleSFX.js @@ -0,0 +1,105 @@ +//// [tests/cases/compiler/bundleSFX.ts] //// + +//// [bundleSFX_file0.ts] + +export class Foo { + static CONSTANT = "Foo"; +} + +export function assert(value: boolean) { + if (!value) throw new Error("Assertion failed!"); +} + + +//// [bundleSFX_file1.ts] +import {Foo, assert} from "./bundleSFX_file0"; +assert(Foo.CONSTANT === "Foo"); + + +//// [bundleSFX_bundle.js] +(function() { + var __define = (this && this.__define) || (function() { + var cache = {}, + id = 0, + idMap = {}, + builtinRequire = typeof require === "function" ? require : function(name) { throw new Error("Could not find module named ""+name+""."); }, + normalizeSlashes = function(path) { return path.replace("\\", "/"); }, + forEach = function(array, callback) { + for (var i = 0, len = array.length; i < len; i++) { + callback(array[i], i); + } + }, + resolvePath = function(base, name) { + var result = normalizeSlashes(base).split("/"); + result.pop(); + forEach(normalizeSlashes(name).split("/"), function(part, index) { + switch(part) { + case "": + case ".": + break; + case "..": + if (result.length <= 1) { + if (result[0] === "..") { + result.push(".."); + break; + } + else if (result[0] === "." || result[0] === "") { + result[0] = ".."; + break; + } + } + result.pop(); + break; + default: + result.push(part); + break; + } + }); + if (result[0] === "") result.shift(); + return "/"+result.join("/"); + }, + initializeAndGet = function(resolved) { + if (typeof cache[resolved] === "function") { + cache[resolved](); + } + return (cache[resolved] && cache[resolved].exports); + }, + resolveRequire = function(name, from) { + var resolved = resolvePath(from || "/", name); + return (initializeAndGet(resolved) || builtinRequire(name)); + }, + define = function (declaredName, factory) { + idMap[++id] = declaredName; + var module = { + id: id, + exports: {} + }; + var require = function(name) { + if (typeof name === "number") return initializeAndGet(idMap[name]); + return resolveRequire(name, declaredName); + }; + cache[declaredName] = function() { + cache[declaredName] = module; + factory(require, module.exports, module); + }; + }; + define.require = resolveRequire; + return define; + })(); + __define("/tests/cases/compiler/bundleSFX_file0", function(require, exports, module){ + class Foo { + } + Foo.CONSTANT = "Foo"; + exports.Foo = Foo; + function assert(value) { + if (!value) + throw new Error("Assertion failed!"); + } + exports.assert = assert; + }); + __define("/tests/cases/compiler/bundleSFX_file1", function(require, exports, module){ + var bundleSFX_file0_1 = require("./bundleSFX_file0"); + bundleSFX_file0_1.assert(bundleSFX_file0_1.Foo.CONSTANT === "Foo"); + }); + return __define.require("bundleSFX_file1"); +})() diff --git a/tests/baselines/reference/bundleSFX.symbols b/tests/baselines/reference/bundleSFX.symbols new file mode 100644 index 0000000000000..f47acb343b9ab --- /dev/null +++ b/tests/baselines/reference/bundleSFX.symbols @@ -0,0 +1,30 @@ +=== tests/cases/compiler/bundleSFX_file0.ts === + +export class Foo { +>Foo : Symbol(Foo, Decl(bundleSFX_file0.ts, 0, 0)) + + static CONSTANT = "Foo"; +>CONSTANT : Symbol(Foo.CONSTANT, Decl(bundleSFX_file0.ts, 1, 18)) +} + +export function assert(value: boolean) { +>assert : Symbol(assert, Decl(bundleSFX_file0.ts, 3, 1)) +>value : Symbol(value, Decl(bundleSFX_file0.ts, 5, 23)) + + if (!value) throw new Error("Assertion failed!"); +>value : Symbol(value, Decl(bundleSFX_file0.ts, 5, 23)) +>Error : Symbol(Error, Decl(lib.d.ts, 876, 38), Decl(lib.d.ts, 889, 11)) +} + + +=== tests/cases/compiler/bundleSFX_file1.ts === +import {Foo, assert} from "./bundleSFX_file0"; +>Foo : Symbol(Foo, Decl(bundleSFX_file1.ts, 0, 8)) +>assert : Symbol(assert, Decl(bundleSFX_file1.ts, 0, 12)) + +assert(Foo.CONSTANT === "Foo"); +>assert : Symbol(assert, Decl(bundleSFX_file1.ts, 0, 12)) +>Foo.CONSTANT : Symbol(Foo.CONSTANT, Decl(bundleSFX_file0.ts, 1, 18)) +>Foo : Symbol(Foo, Decl(bundleSFX_file1.ts, 0, 8)) +>CONSTANT : Symbol(Foo.CONSTANT, Decl(bundleSFX_file0.ts, 1, 18)) + diff --git a/tests/baselines/reference/bundleSFX.types b/tests/baselines/reference/bundleSFX.types new file mode 100644 index 0000000000000..784e557751f9d --- /dev/null +++ b/tests/baselines/reference/bundleSFX.types @@ -0,0 +1,37 @@ +=== tests/cases/compiler/bundleSFX_file0.ts === + +export class Foo { +>Foo : Foo + + static CONSTANT = "Foo"; +>CONSTANT : string +>"Foo" : string +} + +export function assert(value: boolean) { +>assert : (value: boolean) => void +>value : boolean + + if (!value) throw new Error("Assertion failed!"); +>!value : boolean +>value : boolean +>new Error("Assertion failed!") : Error +>Error : ErrorConstructor +>"Assertion failed!" : string +} + + +=== tests/cases/compiler/bundleSFX_file1.ts === +import {Foo, assert} from "./bundleSFX_file0"; +>Foo : typeof Foo +>assert : (value: boolean) => void + +assert(Foo.CONSTANT === "Foo"); +>assert(Foo.CONSTANT === "Foo") : void +>assert : (value: boolean) => void +>Foo.CONSTANT === "Foo" : boolean +>Foo.CONSTANT : string +>Foo : typeof Foo +>CONSTANT : string +>"Foo" : string + diff --git a/tests/baselines/reference/bundleSystem.js b/tests/baselines/reference/bundleSystem.js index e62f979d48094..06d4b8149271d 100644 --- a/tests/baselines/reference/bundleSystem.js +++ b/tests/baselines/reference/bundleSystem.js @@ -101,12 +101,12 @@ System.register([], function(exports_1) { class Foo { } Foo.CONSTANT = "Foo"; - Foo = Foo; + exports.Foo = Foo; function assert(value) { if (!value) throw new Error("Assertion failed!"); } - undefined("assert", assert); + exports.assert = assert; }); __define("/tests/cases/compiler/bundleSystem_file1", function(require, exports, module){ var bundleSystem_file0_1 = require("./bundleSystem_file0"); diff --git a/tests/baselines/reference/bundleToES3AMD.js b/tests/baselines/reference/bundleToES3AMD.js index 5da080d87ae5a..d27dd7d0f47ba 100644 --- a/tests/baselines/reference/bundleToES3AMD.js +++ b/tests/baselines/reference/bundleToES3AMD.js @@ -102,6 +102,7 @@ define(["require"], function(require){ exports.assert = assert; }); __define("/tests/cases/compiler/bundleToES3AMD_file1", function(require, exports, module){ + var bundleToES3AMD_file0_1 = require("./bundleToES3AMD_file0"); bundleToES3AMD_file0_1.assert(bundleToES3AMD_file0_1.Foo.CONSTANT === "Foo"); }); return __define.require("bundleToES3AMD_file1"); diff --git a/tests/baselines/reference/bundleToES3Commonjs.js b/tests/baselines/reference/bundleToES3Commonjs.js index 65d9cfa87b4c7..3d0967cf111a0 100644 --- a/tests/baselines/reference/bundleToES3Commonjs.js +++ b/tests/baselines/reference/bundleToES3Commonjs.js @@ -102,6 +102,7 @@ define(["require"], function(require){ exports.assert = assert; }); __define("/tests/cases/compiler/bundleToES3Commonjs_file1", function(require, exports, module){ + var bundleToES3Commonjs_file0_1 = require("./bundleToES3Commonjs_file0"); bundleToES3Commonjs_file0_1.assert(bundleToES3Commonjs_file0_1.Foo.CONSTANT === "Foo"); }); return __define.require("bundleToES3Commonjs_file1"); diff --git a/tests/baselines/reference/bundleToES3SFX.js b/tests/baselines/reference/bundleToES3SFX.js new file mode 100644 index 0000000000000..5d148aa480529 --- /dev/null +++ b/tests/baselines/reference/bundleToES3SFX.js @@ -0,0 +1,108 @@ +//// [tests/cases/compiler/bundleToES3SFX.ts] //// + +//// [bundleToES3SFX_file0.ts] + +export class Foo { + static CONSTANT = "Foo"; +} + +export function assert(value: boolean) { + if (!value) throw new Error("Assertion failed!"); +} + + +//// [bundleToES3SFX_file1.ts] +import {Foo, assert} from "./bundleToES3SFX_file0"; +assert(Foo.CONSTANT === "Foo"); + + +//// [bundleToES3SFX_bundle.js] +(function() { + var __define = (this && this.__define) || (function() { + var cache = {}, + id = 0, + idMap = {}, + builtinRequire = typeof require === "function" ? require : function(name) { throw new Error("Could not find module named ""+name+""."); }, + normalizeSlashes = function(path) { return path.replace("\\", "/"); }, + forEach = function(array, callback) { + for (var i = 0, len = array.length; i < len; i++) { + callback(array[i], i); + } + }, + resolvePath = function(base, name) { + var result = normalizeSlashes(base).split("/"); + result.pop(); + forEach(normalizeSlashes(name).split("/"), function(part, index) { + switch(part) { + case "": + case ".": + break; + case "..": + if (result.length <= 1) { + if (result[0] === "..") { + result.push(".."); + break; + } + else if (result[0] === "." || result[0] === "") { + result[0] = ".."; + break; + } + } + result.pop(); + break; + default: + result.push(part); + break; + } + }); + if (result[0] === "") result.shift(); + return "/"+result.join("/"); + }, + initializeAndGet = function(resolved) { + if (typeof cache[resolved] === "function") { + cache[resolved](); + } + return (cache[resolved] && cache[resolved].exports); + }, + resolveRequire = function(name, from) { + var resolved = resolvePath(from || "/", name); + return (initializeAndGet(resolved) || builtinRequire(name)); + }, + define = function (declaredName, factory) { + idMap[++id] = declaredName; + var module = { + id: id, + exports: {} + }; + var require = function(name) { + if (typeof name === "number") return initializeAndGet(idMap[name]); + return resolveRequire(name, declaredName); + }; + cache[declaredName] = function() { + cache[declaredName] = module; + factory(require, module.exports, module); + }; + }; + define.require = resolveRequire; + return define; + })(); + __define("/tests/cases/compiler/bundleToES3SFX_file0", function(require, exports, module){ + var Foo = (function () { + function Foo() { + } + Foo.CONSTANT = "Foo"; + return Foo; + })(); + exports.Foo = Foo; + function assert(value) { + if (!value) + throw new Error("Assertion failed!"); + } + exports.assert = assert; + }); + __define("/tests/cases/compiler/bundleToES3SFX_file1", function(require, exports, module){ + var bundleToES3SFX_file0_1 = require("./bundleToES3SFX_file0"); + bundleToES3SFX_file0_1.assert(bundleToES3SFX_file0_1.Foo.CONSTANT === "Foo"); + }); + return __define.require("bundleToES3SFX_file1"); +})() diff --git a/tests/baselines/reference/bundleToES3SFX.symbols b/tests/baselines/reference/bundleToES3SFX.symbols new file mode 100644 index 0000000000000..51fc49953e96f --- /dev/null +++ b/tests/baselines/reference/bundleToES3SFX.symbols @@ -0,0 +1,30 @@ +=== tests/cases/compiler/bundleToES3SFX_file0.ts === + +export class Foo { +>Foo : Symbol(Foo, Decl(bundleToES3SFX_file0.ts, 0, 0)) + + static CONSTANT = "Foo"; +>CONSTANT : Symbol(Foo.CONSTANT, Decl(bundleToES3SFX_file0.ts, 1, 18)) +} + +export function assert(value: boolean) { +>assert : Symbol(assert, Decl(bundleToES3SFX_file0.ts, 3, 1)) +>value : Symbol(value, Decl(bundleToES3SFX_file0.ts, 5, 23)) + + if (!value) throw new Error("Assertion failed!"); +>value : Symbol(value, Decl(bundleToES3SFX_file0.ts, 5, 23)) +>Error : Symbol(Error, Decl(lib.d.ts, 876, 38), Decl(lib.d.ts, 889, 11)) +} + + +=== tests/cases/compiler/bundleToES3SFX_file1.ts === +import {Foo, assert} from "./bundleToES3SFX_file0"; +>Foo : Symbol(Foo, Decl(bundleToES3SFX_file1.ts, 0, 8)) +>assert : Symbol(assert, Decl(bundleToES3SFX_file1.ts, 0, 12)) + +assert(Foo.CONSTANT === "Foo"); +>assert : Symbol(assert, Decl(bundleToES3SFX_file1.ts, 0, 12)) +>Foo.CONSTANT : Symbol(Foo.CONSTANT, Decl(bundleToES3SFX_file0.ts, 1, 18)) +>Foo : Symbol(Foo, Decl(bundleToES3SFX_file1.ts, 0, 8)) +>CONSTANT : Symbol(Foo.CONSTANT, Decl(bundleToES3SFX_file0.ts, 1, 18)) + diff --git a/tests/baselines/reference/bundleToES3SFX.types b/tests/baselines/reference/bundleToES3SFX.types new file mode 100644 index 0000000000000..65c191c998895 --- /dev/null +++ b/tests/baselines/reference/bundleToES3SFX.types @@ -0,0 +1,37 @@ +=== tests/cases/compiler/bundleToES3SFX_file0.ts === + +export class Foo { +>Foo : Foo + + static CONSTANT = "Foo"; +>CONSTANT : string +>"Foo" : string +} + +export function assert(value: boolean) { +>assert : (value: boolean) => void +>value : boolean + + if (!value) throw new Error("Assertion failed!"); +>!value : boolean +>value : boolean +>new Error("Assertion failed!") : Error +>Error : ErrorConstructor +>"Assertion failed!" : string +} + + +=== tests/cases/compiler/bundleToES3SFX_file1.ts === +import {Foo, assert} from "./bundleToES3SFX_file0"; +>Foo : typeof Foo +>assert : (value: boolean) => void + +assert(Foo.CONSTANT === "Foo"); +>assert(Foo.CONSTANT === "Foo") : void +>assert : (value: boolean) => void +>Foo.CONSTANT === "Foo" : boolean +>Foo.CONSTANT : string +>Foo : typeof Foo +>CONSTANT : string +>"Foo" : string + diff --git a/tests/baselines/reference/bundleToES3System.js b/tests/baselines/reference/bundleToES3System.js index 5dc700b566d5f..f52ff588eb8d4 100644 --- a/tests/baselines/reference/bundleToES3System.js +++ b/tests/baselines/reference/bundleToES3System.js @@ -98,18 +98,18 @@ System.register([], function(exports_1) { return define; })(); __define("/tests/cases/compiler/bundleToES3System_file0", function(require, exports, module){ - Foo = (function () { + var Foo = (function () { function Foo() { } Foo.CONSTANT = "Foo"; return Foo; })(); - undefined("Foo", Foo); + exports.Foo = Foo; function assert(value) { if (!value) throw new Error("Assertion failed!"); } - undefined("assert", assert); + exports.assert = assert; }); __define("/tests/cases/compiler/bundleToES3System_file1", function(require, exports, module){ var bundleToES3System_file0_1 = require("./bundleToES3System_file0"); diff --git a/tests/cases/compiler/bundleES6.ts b/tests/cases/compiler/bundleES6.ts index f2f48ba42f518..3abaf00f985e0 100644 --- a/tests/cases/compiler/bundleES6.ts +++ b/tests/cases/compiler/bundleES6.ts @@ -1,4 +1,5 @@ // @target: es6 +// @module: es6 // @outFile: bundleES6_bundle.js // @bundle: bundleES6_file1 From 529e48dc62dea17e9db263cd05a058494bf362ca Mon Sep 17 00:00:00 2001 From: Wesley Wigham Date: Mon, 28 Sep 2015 17:55:04 -0700 Subject: [PATCH 14/14] ES6 emit, fix compiler tests current directory --- src/compiler/checker.ts | 2 +- src/compiler/emitter.ts | 23 ++++++++++++++++++--- src/compiler/program.ts | 1 + src/compiler/utilities.ts | 2 +- src/harness/compilerRunner.ts | 2 +- src/harness/harness.ts | 8 +++---- tests/cases/compiler/bundleAMD.ts | 4 +++- tests/cases/compiler/bundleCommonjs.ts | 4 +++- tests/cases/compiler/bundleES6.ts | 4 +++- tests/cases/compiler/bundleSFX.ts | 4 +++- tests/cases/compiler/bundleSystem.ts | 4 +++- tests/cases/compiler/bundleToES3AMD.ts | 4 +++- tests/cases/compiler/bundleToES3Commonjs.ts | 4 +++- tests/cases/compiler/bundleToES3SFX.ts | 4 +++- tests/cases/compiler/bundleToES3System.ts | 4 +++- tests/cases/compiler/bundleToES3UMD.ts | 4 +++- tests/cases/compiler/bundleUMD.ts | 4 +++- 17 files changed, 61 insertions(+), 21 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 582687530fc65..0b50c18911ebe 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -44,7 +44,7 @@ namespace ts { let compilerOptions = host.getCompilerOptions(); let languageVersion = compilerOptions.target || ScriptTarget.ES3; - let modulekind = compilerOptions.module ? compilerOptions.module : languageVersion === ScriptTarget.ES6 ? ModuleKind.ES6 : ModuleKind.None; + let modulekind = (compilerOptions.module !== undefined && compilerOptions.module !== null) ? compilerOptions.module : languageVersion === ScriptTarget.ES6 ? ModuleKind.ES6 : ModuleKind.None; let emitResolver = createResolver(); diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index 6a6c75cb14b57..1e5809a848c81 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -73,7 +73,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi let compilerOptions = host.getCompilerOptions(); let languageVersion = compilerOptions.target || ScriptTarget.ES3; - let modulekind = compilerOptions.module ? compilerOptions.module : languageVersion === ScriptTarget.ES6 ? ModuleKind.ES6 : ModuleKind.None; + let modulekind = (compilerOptions.module !== undefined && compilerOptions.module !== null) ? compilerOptions.module : languageVersion === ScriptTarget.ES6 ? ModuleKind.ES6 : ModuleKind.None; let sourceMapDataList: SourceMapData[] = compilerOptions.sourceMap || compilerOptions.inlineSourceMap ? [] : undefined; let diagnostics: Diagnostic[] = []; let newLine = host.getNewLine(); @@ -240,7 +240,9 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi increaseIndent(); write(`${exportStarName}(`); }, - [ModuleKind.ES6]() {}, + [ModuleKind.ES6]() { + write(`var __exports = `); + }, } /** Called to build the closing part of a bundled module wrapper */ @@ -273,7 +275,22 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi writeLine(); write("});"); }, - [ModuleKind.ES6]() {}, + [ModuleKind.ES6]() { + let module = host.resolveModuleName(compilerOptions.bundle); + if (!module) { + throw new Error('Bundle entrypoint not resolvable'); + } + let file = forEach(host.getSourceFiles(), file => file.fileName === module.resolvedFileName ? file : undefined); + forEachValue(file.symbol.exports, symbol => { + writeLine(); + if (symbol.name === "default") { + write("export default __exports[\"default\"];"); + } + else { + write(`export var ${symbol.name} = __exports.${symbol.name};`); + } + }); + }, } /** Emit the text for the given token that comes after startPos diff --git a/src/compiler/program.ts b/src/compiler/program.ts index 589aa2624870e..e67eb5c2720e3 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -521,6 +521,7 @@ namespace ts { getNewLine: () => host.getNewLine(), getSourceFile: program.getSourceFile, getSourceFiles: program.getSourceFiles, + resolveModuleName: (moduleName: string) => resolveModuleNamesWorker([moduleName], ts.combinePaths(host.getCurrentDirectory(), 'dummy.ts'))[0], writeFile: writeFileCallback || ( (fileName, data, writeByteOrderMark, onError) => host.writeFile(fileName, data, writeByteOrderMark, onError)), }; diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index 3397cd242b1d6..2e7626e9aedd7 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -34,7 +34,7 @@ namespace ts { export interface EmitHost extends ScriptReferenceHost { getSourceFiles(): SourceFile[]; - + resolveModuleName(moduleName: string): ResolvedModule; getCommonSourceDirectory(): string; getCanonicalFileName(fileName: string): string; getNewLine(): string; diff --git a/src/harness/compilerRunner.ts b/src/harness/compilerRunner.ts index a24ed30ae143a..0a108a43872fe 100644 --- a/src/harness/compilerRunner.ts +++ b/src/harness/compilerRunner.ts @@ -94,7 +94,7 @@ class CompilerBaselineRunner extends RunnerBase { program = _program; }, function (settings) { harnessCompiler.setCompilerSettings(tcSettings); - }); + }, undefined, rootDir); }); after(() => { diff --git a/src/harness/harness.ts b/src/harness/harness.ts index b40419cdcf948..96b288244f9cb 100644 --- a/src/harness/harness.ts +++ b/src/harness/harness.ts @@ -918,7 +918,10 @@ module Harness { function getSourceFile(fn: string, languageVersion: ts.ScriptTarget) { fn = ts.normalizePath(fn); - if (Object.prototype.hasOwnProperty.call(filemap, getCanonicalFileName(fn))) { + if (fn === defaultLibFileName) { + return languageVersion === ts.ScriptTarget.ES6 ? defaultES6LibSourceFile : defaultLibSourceFile; + } + else if (Object.prototype.hasOwnProperty.call(filemap, getCanonicalFileName(fn))) { return filemap[getCanonicalFileName(fn)]; } else if (currentDirectory) { @@ -931,9 +934,6 @@ module Harness { return fourslashSourceFile; } else { - if (fn === defaultLibFileName) { - return languageVersion === ts.ScriptTarget.ES6 ? defaultES6LibSourceFile : defaultLibSourceFile; - } // Don't throw here -- the compiler might be looking for a test that actually doesn't exist as part of the TC return undefined; } diff --git a/tests/cases/compiler/bundleAMD.ts b/tests/cases/compiler/bundleAMD.ts index 8618882432bf0..f273651537a56 100644 --- a/tests/cases/compiler/bundleAMD.ts +++ b/tests/cases/compiler/bundleAMD.ts @@ -1,7 +1,7 @@ // @target: es6 // @module: amd // @outFile: bundleAMD_bundle.js -// @bundle: bundleAMD_file1 +// @bundle: ./bundleAMD_file1 // @Filename: bundleAMD_file0.ts export class Foo { @@ -16,3 +16,5 @@ export function assert(value: boolean) { // @Filename: bundleAMD_file1.ts import {Foo, assert} from "./bundleAMD_file0"; assert(Foo.CONSTANT === "Foo"); + +export {assert} from "./bundleAMD_file0"; diff --git a/tests/cases/compiler/bundleCommonjs.ts b/tests/cases/compiler/bundleCommonjs.ts index 10eb31f7e8397..10370fc466e13 100644 --- a/tests/cases/compiler/bundleCommonjs.ts +++ b/tests/cases/compiler/bundleCommonjs.ts @@ -1,7 +1,7 @@ // @target: es6 // @module: commonjs // @outFile: bundleCommonjs_bundle.js -// @bundle: bundleCommonjs_file1 +// @bundle: ./bundleCommonjs_file1 // @Filename: bundleCommonjs_file0.ts export class Foo { @@ -16,3 +16,5 @@ export function assert(value: boolean) { // @Filename: bundleCommonjs_file1.ts import {Foo, assert} from "./bundleCommonjs_file0"; assert(Foo.CONSTANT === "Foo"); + +export {assert} from "./bundleCommonjs_file0"; diff --git a/tests/cases/compiler/bundleES6.ts b/tests/cases/compiler/bundleES6.ts index 3abaf00f985e0..ec15bee18071c 100644 --- a/tests/cases/compiler/bundleES6.ts +++ b/tests/cases/compiler/bundleES6.ts @@ -1,7 +1,7 @@ // @target: es6 // @module: es6 // @outFile: bundleES6_bundle.js -// @bundle: bundleES6_file1 +// @bundle: ./bundleES6_file1 // @Filename: bundleES6_file0.ts export class Foo { @@ -16,3 +16,5 @@ export function assert(value: boolean) { // @Filename: bundleES6_file1.ts import {Foo, assert} from "./bundleES6_file0"; assert(Foo.CONSTANT === "Foo"); + +export {assert} from "./bundleES6_file0"; diff --git a/tests/cases/compiler/bundleSFX.ts b/tests/cases/compiler/bundleSFX.ts index 0f25cdd8cc2cd..700e51acad47d 100644 --- a/tests/cases/compiler/bundleSFX.ts +++ b/tests/cases/compiler/bundleSFX.ts @@ -1,7 +1,7 @@ // @target: es6 // @module: none // @outFile: bundleSFX_bundle.js -// @bundle: bundleSFX_file1 +// @bundle: ./bundleSFX_file1 // @Filename: bundleSFX_file0.ts export class Foo { @@ -16,3 +16,5 @@ export function assert(value: boolean) { // @Filename: bundleSFX_file1.ts import {Foo, assert} from "./bundleSFX_file0"; assert(Foo.CONSTANT === "Foo"); + +export {assert} from "./bundleSFX_file0"; diff --git a/tests/cases/compiler/bundleSystem.ts b/tests/cases/compiler/bundleSystem.ts index 0e5e8dcf6c6c7..2dd949bdf54ea 100644 --- a/tests/cases/compiler/bundleSystem.ts +++ b/tests/cases/compiler/bundleSystem.ts @@ -1,7 +1,7 @@ // @target: es6 // @module: system // @outFile: bundleSystem_bundle.js -// @bundle: bundleSystem_file1 +// @bundle: ./bundleSystem_file1 // @Filename: bundleSystem_file0.ts export class Foo { @@ -16,3 +16,5 @@ export function assert(value: boolean) { // @Filename: bundleSystem_file1.ts import {Foo, assert} from "./bundleSystem_file0"; assert(Foo.CONSTANT === "Foo"); + +export {assert} from "./bundleSystem_file0"; diff --git a/tests/cases/compiler/bundleToES3AMD.ts b/tests/cases/compiler/bundleToES3AMD.ts index 661cd7370b882..84fd3d9275b0d 100644 --- a/tests/cases/compiler/bundleToES3AMD.ts +++ b/tests/cases/compiler/bundleToES3AMD.ts @@ -1,7 +1,7 @@ // @target: es3 // @module: amd // @outFile: bundleToES3AMD_bundle.js -// @bundle: bundleToES3AMD_file1 +// @bundle: ./bundleToES3AMD_file1 // @Filename: bundleToES3AMD_file0.ts export class Foo { @@ -16,3 +16,5 @@ export function assert(value: boolean) { // @Filename: bundleToES3AMD_file1.ts import {Foo, assert} from "./bundleToES3AMD_file0"; assert(Foo.CONSTANT === "Foo"); + +export {assert} from "./bundleToES3AMD_file0"; diff --git a/tests/cases/compiler/bundleToES3Commonjs.ts b/tests/cases/compiler/bundleToES3Commonjs.ts index a89f2714f1db0..37e1b642837c7 100644 --- a/tests/cases/compiler/bundleToES3Commonjs.ts +++ b/tests/cases/compiler/bundleToES3Commonjs.ts @@ -1,7 +1,7 @@ // @target: es3 // @module: amd // @outFile: bundleToES3Commonjs_bundle.js -// @bundle: bundleToES3Commonjs_file1 +// @bundle: ./bundleToES3Commonjs_file1 // @Filename: bundleToES3Commonjs_file0.ts export class Foo { @@ -16,3 +16,5 @@ export function assert(value: boolean) { // @Filename: bundleToES3Commonjs_file1.ts import {Foo, assert} from "./bundleToES3Commonjs_file0"; assert(Foo.CONSTANT === "Foo"); + +export {assert} from "./bundleToES3Commonjs_file0"; diff --git a/tests/cases/compiler/bundleToES3SFX.ts b/tests/cases/compiler/bundleToES3SFX.ts index 1b5b3c62f3ddf..dd5fba7808e99 100644 --- a/tests/cases/compiler/bundleToES3SFX.ts +++ b/tests/cases/compiler/bundleToES3SFX.ts @@ -1,7 +1,7 @@ // @target: es3 // @module: none // @outFile: bundleToES3SFX_bundle.js -// @bundle: bundleToES3SFX_file1 +// @bundle: ./bundleToES3SFX_file1 // @Filename: bundleToES3SFX_file0.ts export class Foo { @@ -16,3 +16,5 @@ export function assert(value: boolean) { // @Filename: bundleToES3SFX_file1.ts import {Foo, assert} from "./bundleToES3SFX_file0"; assert(Foo.CONSTANT === "Foo"); + +export {assert} from "./bundleToES3SFX_file0"; diff --git a/tests/cases/compiler/bundleToES3System.ts b/tests/cases/compiler/bundleToES3System.ts index 78752153119b5..5605c60e38fd6 100644 --- a/tests/cases/compiler/bundleToES3System.ts +++ b/tests/cases/compiler/bundleToES3System.ts @@ -1,7 +1,7 @@ // @target: es3 // @module: system // @outFile: bundleToES3System_bundle.js -// @bundle: bundleToES3System_file1 +// @bundle: ./bundleToES3System_file1 // @Filename: bundleToES3System_file0.ts export class Foo { @@ -16,3 +16,5 @@ export function assert(value: boolean) { // @Filename: bundleToES3System_file1.ts import {Foo, assert} from "./bundleToES3System_file0"; assert(Foo.CONSTANT === "Foo"); + +export {assert} from "./bundleToES3System_file0"; diff --git a/tests/cases/compiler/bundleToES3UMD.ts b/tests/cases/compiler/bundleToES3UMD.ts index 69babde53c494..f404d392f01fb 100644 --- a/tests/cases/compiler/bundleToES3UMD.ts +++ b/tests/cases/compiler/bundleToES3UMD.ts @@ -1,7 +1,7 @@ // @target: es3 // @module: umd // @outFile: bundleToES3UMD_bundle.js -// @bundle: bundleToES3UMD_file1 +// @bundle: ./bundleToES3UMD_file1 // @Filename: bundleToES3UMD_file0.ts export class Foo { @@ -16,3 +16,5 @@ export function assert(value: boolean) { // @Filename: bundleToES3UMD_file1.ts import {Foo, assert} from "./bundleToES3UMD_file0"; assert(Foo.CONSTANT === "Foo"); + +export {assert} from "./bundleToES3UMD_file0"; diff --git a/tests/cases/compiler/bundleUMD.ts b/tests/cases/compiler/bundleUMD.ts index daa279fe0182a..41ec57bcdc9ec 100644 --- a/tests/cases/compiler/bundleUMD.ts +++ b/tests/cases/compiler/bundleUMD.ts @@ -1,7 +1,7 @@ // @target: es6 // @module: umd // @outFile: bundleUMD_bundle.js -// @bundle: bundleUMD_file1 +// @bundle: ./bundleUMD_file1 // @Filename: bundleUMD_file0.ts export class Foo { @@ -16,3 +16,5 @@ export function assert(value: boolean) { // @Filename: bundleUMD_file1.ts import {Foo, assert} from "./bundleUMD_file0"; assert(Foo.CONSTANT === "Foo"); + +export {assert} from "./bundleUMD_file0";