diff --git a/js/compiler.ts b/js/compiler.ts index 88a383e4b60c48..20ddbae9fc3bab 100644 --- a/js/compiler.ts +++ b/js/compiler.ts @@ -79,7 +79,7 @@ export interface Ts { */ export class ModuleMetaData implements ts.IScriptSnapshot { public deps?: ModuleFileName[]; - public exports = {}; + public readonly exports = {}; public factory?: AmdFactory; public gatheringDeps = false; public hasRun = false; @@ -129,15 +129,6 @@ function getExtension( } } -/** Generate output code for a provided JSON string along with its source. */ -export function jsonAmdTemplate( - jsonString: string, - sourceFileName: string -): OutputCode { - // tslint:disable-next-line:max-line-length - return `define([], function() { return JSON.parse(\`${jsonString}\`); });\n//# sourceURL=${sourceFileName}`; -} - /** A singleton class that combines the TypeScript Language Service host API * with Deno specific APIs to provide an interface for compiling and running * TypeScript and JavaScript modules. @@ -162,12 +153,11 @@ export class DenoCompiler >(); // TODO ideally this are not static and can be influenced by command line // arguments - private readonly _options: ts.CompilerOptions = { + private readonly _options: Readonly = { allowJs: true, checkJs: true, module: ts.ModuleKind.AMD, outDir: "$deno$", - resolveJsonModule: true, sourceMap: true, stripComments: true, target: ts.ScriptTarget.ESNext @@ -208,15 +198,7 @@ export class DenoCompiler ); assert(moduleMetaData.hasRun === false, "Module has already been run."); // asserts not tracked by TypeScripts, so using not null operator - const exports = moduleMetaData.factory!( - ...this._getFactoryArguments(moduleMetaData) - ); - // For JSON module support and potential future features. - // TypeScript always imports `exports` and mutates it directly, but the - // AMD specification allows values to be returned from the factory. - if (exports != null) { - moduleMetaData.exports = exports; - } + moduleMetaData.factory!(...this._getFactoryArguments(moduleMetaData)); moduleMetaData.hasRun = true; } } @@ -439,74 +421,50 @@ export class DenoCompiler if (!recompile && moduleMetaData.outputCode) { return moduleMetaData.outputCode; } - const { fileName, sourceCode, mediaType, moduleId } = moduleMetaData; + const { fileName, sourceCode, moduleId } = moduleMetaData; console.warn("Compiling", moduleId); const service = this._service; - // Instead of using TypeScript to transpile JSON modules, we will just do - // it directly. - if (mediaType === MediaType.Json) { - moduleMetaData.outputCode = jsonAmdTemplate(sourceCode, fileName); - } else { - assert( - mediaType === MediaType.TypeScript || mediaType === MediaType.JavaScript - ); - // TypeScript is overly opinionated that only CommonJS modules kinds can - // support JSON imports. Allegedly this was fixed in - // Microsoft/TypeScript#26825 but that doesn't seem to be working here, - // so we will trick the TypeScript compiler. - this._options.module = ts.ModuleKind.AMD; - const output = service.getEmitOutput(fileName); - this._options.module = ts.ModuleKind.CommonJS; - - // Get the relevant diagnostics - this is 3x faster than - // `getPreEmitDiagnostics`. - const diagnostics = [ - ...service.getCompilerOptionsDiagnostics(), - ...service.getSyntacticDiagnostics(fileName), - ...service.getSemanticDiagnostics(fileName) - ]; - if (diagnostics.length > 0) { - const errMsg = this._ts.formatDiagnosticsWithColorAndContext( - diagnostics, - this - ); - console.log(errMsg); - // All TypeScript errors are terminal for deno - this._os.exit(1); - } - - assert( - !output.emitSkipped, - "The emit was skipped for an unknown reason." + const output = service.getEmitOutput(fileName); + + // Get the relevant diagnostics - this is 3x faster than + // `getPreEmitDiagnostics`. + const diagnostics = [ + ...service.getCompilerOptionsDiagnostics(), + ...service.getSyntacticDiagnostics(fileName), + ...service.getSemanticDiagnostics(fileName) + ]; + if (diagnostics.length > 0) { + const errMsg = this._ts.formatDiagnosticsWithColorAndContext( + diagnostics, + this ); + console.log(errMsg); + // All TypeScript errors are terminal for deno + this._os.exit(1); + } - assert( - output.outputFiles.length === 2, - `Expected 2 files to be emitted, got ${output.outputFiles.length}.` - ); + assert(!output.emitSkipped, "The emit was skipped for an unknown reason."); - const [sourceMapFile, outputFile] = output.outputFiles; - assert( - sourceMapFile.name.endsWith(".map"), - "Expected first emitted file to be a source map" - ); - assert( - outputFile.name.endsWith(".js"), - "Expected second emitted file to be JavaScript" - ); - moduleMetaData.outputCode = `${ - outputFile.text - }\n//# sourceURL=${fileName}`; - moduleMetaData.sourceMap = sourceMapFile.text; - } + assert( + output.outputFiles.length === 2, + `Expected 2 files to be emitted, got ${output.outputFiles.length}.` + ); - moduleMetaData.scriptVersion = "1"; - this._os.codeCache( - fileName, - sourceCode, - moduleMetaData.outputCode, - moduleMetaData.sourceMap + const [sourceMapFile, outputFile] = output.outputFiles; + assert( + sourceMapFile.name.endsWith(".map"), + "Expected first emitted file to be a source map" ); + assert( + outputFile.name.endsWith(".js"), + "Expected second emitted file to be JavaScript" + ); + const outputCode = (moduleMetaData.outputCode = `${ + outputFile.text + }\n//# sourceURL=${fileName}`); + const sourceMap = (moduleMetaData.sourceMap = sourceMapFile.text); + moduleMetaData.scriptVersion = "1"; + this._os.codeCache(fileName, sourceCode, outputCode, sourceMap); return moduleMetaData.outputCode; } diff --git a/js/compiler_test.ts b/js/compiler_test.ts index 729d6b4a71e787..d7a5c877c425b8 100644 --- a/js/compiler_test.ts +++ b/js/compiler_test.ts @@ -6,7 +6,7 @@ import * as ts from "typescript"; // We use a silly amount of `any` in these tests... // tslint:disable:no-any -const { DenoCompiler, jsonAmdTemplate } = (deno as any)._compiler; +const { DenoCompiler } = (deno as any)._compiler; interface ModuleInfo { moduleName: string | undefined; @@ -118,11 +118,6 @@ const fooBazTsOutput = `define(["require", "exports", "./bar.ts"], function (req // This is not a valid map, just mock data const fooBazTsSourcemap = `{"version":3,"file":"baz.js","sourceRoot":"","sources":["file:///root/project/foo/baz.ts"],"names":[],"mappings":""}`; - -const loadConfigSource = `import * as config from "./config.json"; -console.log(config.foo.baz); -`; -const configJsonSource = `{"foo":{"bar": true,"baz": ["qat", 1]}}`; // tslint:enable:max-line-length const moduleMap: { @@ -153,14 +148,6 @@ const moduleMap: { "console.log();", null, null - ), - "loadConfig.ts": mockModuleInfo( - "/root/project/loadConfig.ts", - "/root/project/loadConfig.ts", - MediaType.TypeScript, - loadConfigSource, - null, - null ) }, "/root/project/foo/baz.ts": { @@ -179,16 +166,6 @@ const moduleMap: { "/root/project/modB.ts": { "./modA.ts": modAModuleInfo }, - "/root/project/loadConfig.ts": { - "./config.json": mockModuleInfo( - "/root/project/config.json", - "/root/project/config.json", - MediaType.Json, - configJsonSource, - null, - null - ) - }, "/moduleKinds": { "foo.ts": mockModuleInfo( "foo", @@ -303,7 +280,7 @@ const osMock = { return mockModuleInfo(null, null, null, null, null, null); }, exit(code: number): never { - throw new Error(`Unexpected call to os.exit(${code})`); + throw new Error(`os.exit(${code})`); } }; const tsMock = { @@ -312,9 +289,9 @@ const tsMock = { }, formatDiagnosticsWithColorAndContext( diagnostics: ReadonlyArray, - _host: ts.FormatDiagnosticsHost + host: ts.FormatDiagnosticsHost ): string { - return JSON.stringify(diagnostics.map(({ messageText }) => messageText)); + return ""; } }; @@ -397,23 +374,6 @@ function teardown() { Object.assign(compilerInstance, originals); } -test(function testJsonAmdTemplate() { - let deps: string[]; - let factory: Function; - function define(d: string[], f: Function) { - deps = d; - factory = f; - } - - const code = jsonAmdTemplate(`{ "hello": "world", "foo": "bar" }`); - const result = eval(code); - assert(result == null); - assertEqual(deps && deps.length, 0); - assert(factory != null); - const factoryResult = factory(); - assertEqual(factoryResult, { hello: "world", foo: "bar" }); -}); - test(function compilerInstance() { assert(DenoCompiler != null); assert(DenoCompiler.instance() != null); @@ -519,29 +479,6 @@ test(function compilerRunCircularDependency() { teardown(); }); -test(function compilerLoadJsonModule() { - setup(); - const factoryStack: string[] = []; - const configJsonDeps: string[] = []; - const configJsonFactory = () => { - factoryStack.push("configJson"); - return JSON.parse(configJsonSource); - }; - const loadConfigDeps = ["require", "exports", "./config.json"]; - const loadConfigFactory = (_require, _exports, _config) => { - factoryStack.push("loadConfig"); - assertEqual(_config, JSON.parse(configJsonSource)); - }; - - mockDepsStack.push(configJsonDeps); - mockFactoryStack.push(configJsonFactory); - mockDepsStack.push(loadConfigDeps); - mockFactoryStack.push(loadConfigFactory); - compilerInstance.run("loadConfig.ts", "/root/project"); - assertEqual(factoryStack, ["configJson", "loadConfig"]); - teardown(); -}); - test(function compilerResolveModule() { setup(); const moduleMetaData = compilerInstance.resolveModule( @@ -607,7 +544,6 @@ test(function compilerGetCompilationSettings() { "checkJs", "module", "outDir", - "resolveJsonModule", "sourceMap", "stripComments", "target" diff --git a/tests/020_json_modules.ts b/tests/020_json_modules.ts deleted file mode 100644 index 89963751c034fc..00000000000000 --- a/tests/020_json_modules.ts +++ /dev/null @@ -1,3 +0,0 @@ -import * as config from "./subdir/config.json"; - -console.log(JSON.stringify(config)); diff --git a/tests/020_json_modules.ts.out b/tests/020_json_modules.ts.out deleted file mode 100644 index 5d1623e6b61d13..00000000000000 --- a/tests/020_json_modules.ts.out +++ /dev/null @@ -1 +0,0 @@ -{"foo":{"bar":true,"baz":["qat",1]}} diff --git a/tests/subdir/config.json b/tests/subdir/config.json deleted file mode 100644 index 01c3b5e79adf86..00000000000000 --- a/tests/subdir/config.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "foo": { - "bar": true, - "baz": ["qat", 1] - } -} diff --git a/tsconfig.json b/tsconfig.json index c67223f5303dfd..806143644c930d 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -16,7 +16,6 @@ "preserveConstEnums": true, "pretty": true, "removeComments": true, - "resolveJsonModule": true, "sourceMap": true, "strict": true, "target": "esnext",