If outDir is not specified, dont need to include json files explicitly in the config since they will not be emitted (#55389)

This commit is contained in:
Sheetal Nandi
2023-09-08 12:23:13 -07:00
committed by GitHub
parent eb374c28d6
commit dd18dc1dac
39 changed files with 1852 additions and 832 deletions

View File

@@ -1,92 +1,228 @@
import * as vfs from "../../_namespaces/vfs";
import {
CompilerOptions,
} from "../../_namespaces/ts";
import {
dedent,
} from "../../_namespaces/Utils";
import {
noChangeOnlyRuns,
verifyTsc,
VerifyTscWithEditsInput,
} from "../helpers/tsc";
import {
loadProjectFromDisk,
loadProjectFromFiles,
replaceText,
} from "../helpers/vfs";
describe("unittests:: tsbuild:: with resolveJsonModule option on project resolveJsonModuleAndComposite", () => {
let projFs: vfs.FileSystem;
before(() => {
projFs = loadProjectFromDisk("tests/projects/resolveJsonModuleAndComposite");
function getProjFs(tsconfigFiles: object, additionalCompilerOptions?: CompilerOptions) {
return loadProjectFromFiles({
"/src/src/hello.json": JSON.stringify(
{
hello: "world",
},
undefined,
" ",
),
"/src/src/index.ts": dedent`
import hello from "./hello.json"
export default hello.hello
`,
"/src/tsconfig.json": JSON.stringify(
{
compilerOptions: {
composite: true,
moduleResolution: "node",
module: "commonjs",
resolveJsonModule: true,
esModuleInterop: true,
allowSyntheticDefaultImports: true,
outDir: "dist",
skipDefaultLibCheck: true,
...additionalCompilerOptions,
},
...tsconfigFiles,
},
undefined,
" ",
),
});
}
function verfiyJson(
input: Pick<VerifyTscWithEditsInput, "subScenario" | "modifyFs" | "edits"> | string,
tsconfigFiles: object,
additionalCompilerOptions?: CompilerOptions,
) {
if (typeof input === "string") input = { subScenario: input };
verifyTsc({
scenario: "resolveJsonModule",
fs: () => getProjFs(tsconfigFiles, additionalCompilerOptions),
commandLineArgs: ["--b", "/src/tsconfig.json", "--v", "--explainFiles", "--listEmittedFiles"],
...input,
});
verifyTsc({
scenario: "resolveJsonModule",
fs: () => getProjFs(tsconfigFiles, { composite: undefined, ...additionalCompilerOptions }),
commandLineArgs: ["--b", "/src/tsconfig.json", "--v", "--explainFiles", "--listEmittedFiles"],
...input,
subScenario: `${input.subScenario} non-composite`,
});
}
verfiyJson("include only", {
include: [
"src/**/*",
],
});
after(() => {
projFs = undefined!; // Release the contents
});
verfiyJson("include only without outDir", {
include: [
"src/**/*",
],
}, { outDir: undefined });
verifyTsc({
scenario: "resolveJsonModule",
subScenario: "include only",
fs: () => projFs,
commandLineArgs: ["--b", "/src/tsconfig_withInclude.json", "--v", "--explainFiles"],
});
verifyTsc({
scenario: "resolveJsonModule",
subScenario: "include of json along with other include",
fs: () => projFs,
commandLineArgs: ["--b", "/src/tsconfig_withIncludeOfJson.json", "--v", "--explainFiles"],
});
verifyTsc({
scenario: "resolveJsonModule",
subScenario: "include of json along with other include and file name matches ts file",
fs: () => projFs,
commandLineArgs: ["--b", "/src/tsconfig_withIncludeOfJson.json", "--v", "--explainFiles"],
verfiyJson({
subScenario: "include only with json not in rootDir",
modifyFs: fs => {
fs.rimrafSync("/src/src/hello.json");
fs.writeFileSync("/src/src/index.json", JSON.stringify({ hello: "world" }));
fs.writeFileSync(
"/src/src/index.ts",
`import hello from "./index.json"
export default hello.hello`,
);
fs.renameSync("/src/src/hello.json", "/src/hello.json");
replaceText(fs, "/src/src/index.ts", "./hello.json", "../hello.json");
},
}, {
include: [
"src/**/*",
],
}, { rootDir: "src" });
verfiyJson({
subScenario: "include only with json without rootDir but outside configDirectory",
modifyFs: fs => {
fs.renameSync("/src/src/hello.json", "/hello.json");
replaceText(fs, "/src/src/index.ts", "./hello.json", "../../hello.json");
},
}, {
include: [
"src/**/*",
],
});
verifyTsc({
scenario: "resolveJsonModule",
subScenario: "files containing json file",
fs: () => projFs,
commandLineArgs: ["--b", "/src/tsconfig_withFiles.json", "--v", "--explainFiles"],
verfiyJson("include of json along with other include", {
include: [
"src/**/*",
"src/**/*.json",
],
});
verifyTsc({
scenario: "resolveJsonModule",
subScenario: "include and files",
fs: () => projFs,
commandLineArgs: ["--b", "/src/tsconfig_withIncludeAndFiles.json", "--v", "--explainFiles"],
verfiyJson({
subScenario: "include of json along with other include and file name matches ts file",
modifyFs: fs => {
fs.renameSync("/src/src/hello.json", "/src/src/index.json");
replaceText(fs, "/src/src/index.ts", "hello.json", "index.json");
},
}, {
include: [
"src/**/*",
"src/**/*.json",
],
});
verifyTsc({
scenario: "resolveJsonModule",
verfiyJson("files containing json file", {
files: [
"src/index.ts",
"src/hello.json",
],
});
verfiyJson("include and files", {
files: [
"src/hello.json",
],
include: [
"src/**/*",
],
});
verfiyJson({
subScenario: "sourcemap",
fs: () => projFs,
commandLineArgs: ["--b", "src/tsconfig_withFiles.json", "--verbose", "--explainFiles"],
modifyFs: fs => replaceText(fs, "src/tsconfig_withFiles.json", `"composite": true,`, `"composite": true, "sourceMap": true,`),
edits: noChangeOnlyRuns,
});
}, {
files: [
"src/index.ts",
"src/hello.json",
],
}, { sourceMap: true });
verifyTsc({
scenario: "resolveJsonModule",
verfiyJson({
subScenario: "without outDir",
fs: () => projFs,
commandLineArgs: ["--b", "src/tsconfig_withFiles.json", "--verbose"],
modifyFs: fs => replaceText(fs, "src/tsconfig_withFiles.json", `"outDir": "dist",`, ""),
edits: noChangeOnlyRuns,
});
}, {
files: [
"src/index.ts",
"src/hello.json",
],
}, { outDir: undefined });
});
describe("unittests:: tsbuild:: with resolveJsonModule option on project importJsonFromProjectReference", () => {
verifyTsc({
scenario: "resolveJsonModule",
subScenario: "importing json module from project reference",
fs: () => loadProjectFromDisk("tests/projects/importJsonFromProjectReference"),
fs: () =>
loadProjectFromFiles({
"/src/strings/foo.json": JSON.stringify(
{
foo: "bar baz",
},
undefined,
" ",
),
"/src/strings/tsconfig.json": JSON.stringify(
{
extends: "../tsconfig.json",
include: ["foo.json"],
references: [],
},
undefined,
" ",
),
"/src/main/index.ts": dedent`
import { foo } from '../strings/foo.json';
console.log(foo);
`,
"/src/main/tsconfig.json": JSON.stringify(
{
extends: "../tsconfig.json",
include: [
"./**/*.ts",
],
references: [{
path: "../strings/tsconfig.json",
}],
},
undefined,
" ",
),
"/src/tsconfig.json": JSON.stringify(
{
compilerOptions: {
target: "es5",
module: "commonjs",
rootDir: "./",
composite: true,
resolveJsonModule: true,
strict: true,
esModuleInterop: true,
},
references: [
{ path: "./strings/tsconfig.json" },
{ path: "./main/tsconfig.json" },
],
files: [],
},
undefined,
" ",
),
}),
commandLineArgs: ["--b", "src/tsconfig.json", "--verbose", "--explainFiles"],
edits: noChangeOnlyRuns,
});

View File

@@ -710,6 +710,7 @@ console.log(blabla);`,
compilerOptions: {
resolveJsonModule: true,
composite: true,
outDir: "dist",
},
include,
}),