mirror of
https://github.com/denoland/deno.git
synced 2025-07-29 08:04:00 +00:00
Separate source map from output code.
This commit is contained in:
parent
a68403d090
commit
8b39d2c99e
6 changed files with 161 additions and 63 deletions
|
@ -9,11 +9,12 @@ import * as ts from "typescript";
|
|||
const { DenoCompiler } = (deno as any)._compiler;
|
||||
|
||||
interface ModuleInfo {
|
||||
moduleName: string | null;
|
||||
filename: string | null;
|
||||
mediaType: MediaType | null;
|
||||
sourceCode: string | null;
|
||||
outputCode: string | null;
|
||||
moduleName: string | undefined;
|
||||
filename: string | undefined;
|
||||
mediaType: MediaType | undefined;
|
||||
sourceCode: string | undefined;
|
||||
outputCode: string | undefined;
|
||||
sourceMap: string | undefined;
|
||||
}
|
||||
|
||||
const compilerInstance = DenoCompiler.instance();
|
||||
|
@ -36,18 +37,20 @@ enum MediaType {
|
|||
}
|
||||
|
||||
function mockModuleInfo(
|
||||
moduleName: string | null,
|
||||
filename: string | null,
|
||||
mediaType: MediaType | null,
|
||||
sourceCode: string | null,
|
||||
outputCode: string | null
|
||||
moduleName: string | undefined,
|
||||
filename: string | undefined,
|
||||
mediaType: MediaType | undefined,
|
||||
sourceCode: string | undefined,
|
||||
outputCode: string | undefined,
|
||||
sourceMap: string | undefined
|
||||
): ModuleInfo {
|
||||
return {
|
||||
moduleName,
|
||||
filename,
|
||||
mediaType,
|
||||
sourceCode,
|
||||
outputCode
|
||||
outputCode,
|
||||
sourceMap
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -73,6 +76,7 @@ const modAModuleInfo = mockModuleInfo(
|
|||
"/root/project/modA.ts",
|
||||
MediaType.TypeScript,
|
||||
modASource,
|
||||
undefined,
|
||||
undefined
|
||||
);
|
||||
|
||||
|
@ -88,10 +92,10 @@ const modBModuleInfo = mockModuleInfo(
|
|||
"/root/project/modB.ts",
|
||||
MediaType.TypeScript,
|
||||
modBSource,
|
||||
undefined,
|
||||
undefined
|
||||
);
|
||||
|
||||
// TODO(#23) Remove source map strings from fooBarTsOutput.
|
||||
// tslint:disable:max-line-length
|
||||
const fooBarTsOutput = `define(["require", "exports", "deno"], function (require, exports, deno) {
|
||||
"use strict";
|
||||
|
@ -99,17 +103,21 @@ const fooBarTsOutput = `define(["require", "exports", "deno"], function (require
|
|||
console.log(deno);
|
||||
exports.foo = "bar";
|
||||
});
|
||||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiYmFyLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiZmlsZTovLy9yb290L3Byb2plY3QvZm9vL2Jhci50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7SUFDQSxPQUFPLENBQUMsR0FBRyxDQUFDLElBQUksQ0FBQyxDQUFDO0lBQ0wsUUFBQSxHQUFHLEdBQUcsS0FBSyxDQUFDIiwic291cmNlc0NvbnRlbnQiOlsiaW1wb3J0ICogYXMgZGVubyBmcm9tIFwiZGVub1wiO1xuY29uc29sZS5sb2coZGVubyk7XG5leHBvcnQgY29uc3QgZm9vID0gXCJiYXJcIjtcbiJdfQ==
|
||||
//# sourceMappingURL=bar.js.map
|
||||
//# sourceURL=/root/project/foo/bar.ts`;
|
||||
|
||||
// TODO(#23) Remove source map strings from fooBazTsOutput.
|
||||
const fooBarTsSourcemap = `{"version":3,"file":"bar.js","sourceRoot":"","sources":["file:///root/project/foo/bar.ts"],"names":[],"mappings":";;;IACA,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;IACL,QAAA,GAAG,GAAG,KAAK,CAAC"}`;
|
||||
|
||||
const fooBazTsOutput = `define(["require", "exports", "./bar.ts"], function (require, exports, bar_ts_1) {
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
console.log(bar_ts_1.foo);
|
||||
});
|
||||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiYmF6LmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiZmlsZTovLy9yb290L3Byb2plY3QvZm9vL2Jhei50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7SUFDQSxPQUFPLENBQUMsR0FBRyxDQUFDLFlBQUcsQ0FBQyxDQUFDIiwic291cmNlc0NvbnRlbnQiOlsiaW1wb3J0IHsgZm9vIH0gZnJvbSBcIi4vYmFyLnRzXCI7XG5jb25zb2xlLmxvZyhmb28pO1xuIl19
|
||||
//# sourceMappingURL=baz.js.map
|
||||
//# sourceURL=/root/project/foo/baz.ts`;
|
||||
|
||||
// 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":""}`;
|
||||
// tslint:enable:max-line-length
|
||||
|
||||
const moduleMap: {
|
||||
|
@ -121,6 +129,7 @@ const moduleMap: {
|
|||
"/root/project/foo/bar.ts",
|
||||
MediaType.TypeScript,
|
||||
fooBarTsSource,
|
||||
null,
|
||||
null
|
||||
),
|
||||
"foo/baz.ts": mockModuleInfo(
|
||||
|
@ -128,7 +137,8 @@ const moduleMap: {
|
|||
"/root/project/foo/baz.ts",
|
||||
MediaType.TypeScript,
|
||||
fooBazTsSource,
|
||||
fooBazTsOutput
|
||||
fooBazTsOutput,
|
||||
fooBazTsSourcemap
|
||||
),
|
||||
"modA.ts": modAModuleInfo,
|
||||
"some.txt": mockModuleInfo(
|
||||
|
@ -136,6 +146,7 @@ const moduleMap: {
|
|||
"/root/project/some.text",
|
||||
MediaType.Unknown,
|
||||
"console.log();",
|
||||
null,
|
||||
null
|
||||
)
|
||||
},
|
||||
|
@ -145,7 +156,8 @@ const moduleMap: {
|
|||
"/root/project/foo/bar.ts",
|
||||
MediaType.TypeScript,
|
||||
fooBarTsSource,
|
||||
fooBarTsOutput
|
||||
fooBarTsOutput,
|
||||
fooBarTsSourcemap
|
||||
)
|
||||
},
|
||||
"/root/project/modA.ts": {
|
||||
|
@ -160,6 +172,7 @@ const moduleMap: {
|
|||
"/moduleKinds/foo.ts",
|
||||
MediaType.TypeScript,
|
||||
"console.log('foo');",
|
||||
undefined,
|
||||
undefined
|
||||
),
|
||||
"foo.d.ts": mockModuleInfo(
|
||||
|
@ -167,6 +180,7 @@ const moduleMap: {
|
|||
"/moduleKinds/foo.d.ts",
|
||||
MediaType.TypeScript,
|
||||
"console.log('foo');",
|
||||
undefined,
|
||||
undefined
|
||||
),
|
||||
"foo.js": mockModuleInfo(
|
||||
|
@ -174,6 +188,7 @@ const moduleMap: {
|
|||
"/moduleKinds/foo.js",
|
||||
MediaType.JavaScript,
|
||||
"console.log('foo');",
|
||||
undefined,
|
||||
undefined
|
||||
),
|
||||
"foo.json": mockModuleInfo(
|
||||
|
@ -181,6 +196,7 @@ const moduleMap: {
|
|||
"/moduleKinds/foo.json",
|
||||
MediaType.Json,
|
||||
"console.log('foo');",
|
||||
undefined,
|
||||
undefined
|
||||
),
|
||||
"foo.txt": mockModuleInfo(
|
||||
|
@ -188,6 +204,7 @@ const moduleMap: {
|
|||
"/moduleKinds/foo.txt",
|
||||
MediaType.JavaScript,
|
||||
"console.log('foo');",
|
||||
undefined,
|
||||
undefined
|
||||
)
|
||||
}
|
||||
|
@ -211,6 +228,7 @@ let codeCacheStack: Array<{
|
|||
fileName: string;
|
||||
sourceCode: string;
|
||||
outputCode: string;
|
||||
sourceMap: string;
|
||||
}> = [];
|
||||
let codeFetchStack: Array<{
|
||||
moduleSpecifier: string;
|
||||
|
@ -230,18 +248,25 @@ function logMock(...args: any[]): void {
|
|||
logStack.push(args);
|
||||
}
|
||||
const osMock = {
|
||||
codeCache(fileName: string, sourceCode: string, outputCode: string): void {
|
||||
codeCacheStack.push({ fileName, sourceCode, outputCode });
|
||||
codeCache(
|
||||
fileName: string,
|
||||
sourceCode: string,
|
||||
outputCode: string,
|
||||
sourceMap: string
|
||||
): void {
|
||||
codeCacheStack.push({ fileName, sourceCode, outputCode, sourceMap });
|
||||
if (fileName in moduleCache) {
|
||||
moduleCache[fileName].sourceCode = sourceCode;
|
||||
moduleCache[fileName].outputCode = outputCode;
|
||||
moduleCache[fileName].sourceMap = sourceMap;
|
||||
} else {
|
||||
moduleCache[fileName] = mockModuleInfo(
|
||||
fileName,
|
||||
fileName,
|
||||
MediaType.TypeScript,
|
||||
sourceCode,
|
||||
outputCode
|
||||
outputCode,
|
||||
sourceMap
|
||||
);
|
||||
}
|
||||
},
|
||||
|
@ -252,7 +277,7 @@ const osMock = {
|
|||
return moduleMap[containingFile][moduleSpecifier];
|
||||
}
|
||||
}
|
||||
return mockModuleInfo(null, null, null, null, null);
|
||||
return mockModuleInfo(null, null, null, null, null, null);
|
||||
},
|
||||
exit(code: number): never {
|
||||
throw new Error(`os.exit(${code})`);
|
||||
|
@ -373,6 +398,7 @@ test(function compilerRun() {
|
|||
assert(moduleMetaData.hasRun);
|
||||
assertEqual(moduleMetaData.sourceCode, fooBarTsSource);
|
||||
assertEqual(moduleMetaData.outputCode, fooBarTsOutput);
|
||||
assertEqual(moduleMetaData.sourceMap, fooBarTsSourcemap);
|
||||
assertEqual(moduleMetaData.exports, { foo: "bar" });
|
||||
|
||||
assertEqual(
|
||||
|
@ -385,6 +411,11 @@ test(function compilerRun() {
|
|||
1,
|
||||
"Compiled code should have only been cached once."
|
||||
);
|
||||
const [codeCacheCall] = codeCacheStack;
|
||||
assertEqual(codeCacheCall.fileName, "/root/project/foo/bar.ts");
|
||||
assertEqual(codeCacheCall.sourceCode, fooBarTsSource);
|
||||
assertEqual(codeCacheCall.outputCode, fooBarTsOutput);
|
||||
assertEqual(codeCacheCall.sourceMap, fooBarTsSourcemap);
|
||||
teardown();
|
||||
});
|
||||
|
||||
|
@ -456,6 +487,7 @@ test(function compilerResolveModule() {
|
|||
);
|
||||
assertEqual(moduleMetaData.sourceCode, fooBazTsSource);
|
||||
assertEqual(moduleMetaData.outputCode, fooBazTsOutput);
|
||||
assertEqual(moduleMetaData.sourceMap, fooBazTsSourcemap);
|
||||
assert(!moduleMetaData.hasRun);
|
||||
assert(!moduleMetaData.deps);
|
||||
assertEqual(moduleMetaData.exports, {});
|
||||
|
@ -507,18 +539,20 @@ test(function compilerGetModuleDependencies() {
|
|||
// TypeScript LanguageServiceHost APIs
|
||||
|
||||
test(function compilerGetCompilationSettings() {
|
||||
const result = compilerInstance.getCompilationSettings();
|
||||
for (const key of [
|
||||
const expectedKeys = [
|
||||
"allowJs",
|
||||
"checkJs",
|
||||
"module",
|
||||
"outDir",
|
||||
"inlineSourceMap",
|
||||
"inlineSources",
|
||||
"sourceMap",
|
||||
"stripComments",
|
||||
"target"
|
||||
]) {
|
||||
];
|
||||
const result = compilerInstance.getCompilationSettings();
|
||||
for (const key of expectedKeys) {
|
||||
assert(key in result, `Expected "${key}" in compiler options.`);
|
||||
}
|
||||
assertEqual(Object.keys(result).length, expectedKeys.length);
|
||||
});
|
||||
|
||||
test(function compilerGetNewLine() {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue