|
1 |
| -import * as vfs from "../../_namespaces/vfs"; |
| 1 | +import { |
| 2 | + CompilerOptions, |
| 3 | +} from "../../_namespaces/ts"; |
| 4 | +import { |
| 5 | + dedent, |
| 6 | +} from "../../_namespaces/Utils"; |
2 | 7 | import {
|
3 | 8 | noChangeOnlyRuns,
|
4 | 9 | verifyTsc,
|
| 10 | + VerifyTscWithEditsInput, |
5 | 11 | } from "../helpers/tsc";
|
6 | 12 | import {
|
7 |
| - loadProjectFromDisk, |
| 13 | + loadProjectFromFiles, |
8 | 14 | replaceText,
|
9 | 15 | } from "../helpers/vfs";
|
10 | 16 |
|
11 | 17 | describe("unittests:: tsbuild:: with resolveJsonModule option on project resolveJsonModuleAndComposite", () => {
|
12 |
| - let projFs: vfs.FileSystem; |
13 |
| - before(() => { |
14 |
| - projFs = loadProjectFromDisk("tests/projects/resolveJsonModuleAndComposite"); |
15 |
| - }); |
| 18 | + function getProjFs(tsconfigFiles: object, additionalCompilerOptions?: CompilerOptions) { |
| 19 | + return loadProjectFromFiles({ |
| 20 | + "/src/src/hello.json": JSON.stringify( |
| 21 | + { |
| 22 | + hello: "world", |
| 23 | + }, |
| 24 | + undefined, |
| 25 | + " ", |
| 26 | + ), |
| 27 | + "/src/src/index.ts": dedent` |
| 28 | + import hello from "./hello.json" |
| 29 | + export default hello.hello |
| 30 | + `, |
| 31 | + "/src/tsconfig.json": JSON.stringify( |
| 32 | + { |
| 33 | + compilerOptions: { |
| 34 | + composite: true, |
| 35 | + moduleResolution: "node", |
| 36 | + module: "commonjs", |
| 37 | + resolveJsonModule: true, |
| 38 | + esModuleInterop: true, |
| 39 | + allowSyntheticDefaultImports: true, |
| 40 | + outDir: "dist", |
| 41 | + skipDefaultLibCheck: true, |
| 42 | + ...additionalCompilerOptions, |
| 43 | + }, |
| 44 | + ...tsconfigFiles, |
| 45 | + }, |
| 46 | + undefined, |
| 47 | + " ", |
| 48 | + ), |
| 49 | + }); |
| 50 | + } |
| 51 | + |
| 52 | + function verfiyJson( |
| 53 | + input: Pick<VerifyTscWithEditsInput, "subScenario" | "modifyFs" | "edits"> | string, |
| 54 | + tsconfigFiles: object, |
| 55 | + additionalCompilerOptions?: CompilerOptions, |
| 56 | + ) { |
| 57 | + if (typeof input === "string") input = { subScenario: input }; |
| 58 | + verifyTsc({ |
| 59 | + scenario: "resolveJsonModule", |
| 60 | + fs: () => getProjFs(tsconfigFiles, additionalCompilerOptions), |
| 61 | + commandLineArgs: ["--b", "/src/tsconfig.json", "--v", "--explainFiles", "--listEmittedFiles"], |
| 62 | + ...input, |
| 63 | + }); |
| 64 | + verifyTsc({ |
| 65 | + scenario: "resolveJsonModule", |
| 66 | + fs: () => getProjFs(tsconfigFiles, { composite: undefined, ...additionalCompilerOptions }), |
| 67 | + commandLineArgs: ["--b", "/src/tsconfig.json", "--v", "--explainFiles", "--listEmittedFiles"], |
| 68 | + ...input, |
| 69 | + subScenario: `${input.subScenario} non-composite`, |
| 70 | + }); |
| 71 | + } |
16 | 72 |
|
17 |
| - after(() => { |
18 |
| - projFs = undefined!; // Release the contents |
| 73 | + verfiyJson("include only", { |
| 74 | + include: [ |
| 75 | + "src/**/*", |
| 76 | + ], |
19 | 77 | });
|
20 | 78 |
|
21 |
| - verifyTsc({ |
22 |
| - scenario: "resolveJsonModule", |
23 |
| - subScenario: "include only", |
24 |
| - fs: () => projFs, |
25 |
| - commandLineArgs: ["--b", "/src/tsconfig_withInclude.json", "--v", "--explainFiles"], |
| 79 | + verfiyJson("include only without outDir", { |
| 80 | + include: [ |
| 81 | + "src/**/*", |
| 82 | + ], |
| 83 | + }, { outDir: undefined }); |
| 84 | + |
| 85 | + verfiyJson({ |
| 86 | + subScenario: "include only with json not in rootDir", |
| 87 | + modifyFs: fs => { |
| 88 | + fs.renameSync("/src/src/hello.json", "/src/hello.json"); |
| 89 | + replaceText(fs, "/src/src/index.ts", "./hello.json", "../hello.json"); |
| 90 | + }, |
| 91 | + }, { |
| 92 | + include: [ |
| 93 | + "src/**/*", |
| 94 | + ], |
| 95 | + }, { rootDir: "src" }); |
| 96 | + |
| 97 | + verfiyJson({ |
| 98 | + subScenario: "include only with json without rootDir but outside configDirectory", |
| 99 | + modifyFs: fs => { |
| 100 | + fs.renameSync("/src/src/hello.json", "/hello.json"); |
| 101 | + replaceText(fs, "/src/src/index.ts", "./hello.json", "../../hello.json"); |
| 102 | + }, |
| 103 | + }, { |
| 104 | + include: [ |
| 105 | + "src/**/*", |
| 106 | + ], |
26 | 107 | });
|
27 | 108 |
|
28 |
| - verifyTsc({ |
29 |
| - scenario: "resolveJsonModule", |
30 |
| - subScenario: "include of json along with other include", |
31 |
| - fs: () => projFs, |
32 |
| - commandLineArgs: ["--b", "/src/tsconfig_withIncludeOfJson.json", "--v", "--explainFiles"], |
| 109 | + verfiyJson("include of json along with other include", { |
| 110 | + include: [ |
| 111 | + "src/**/*", |
| 112 | + "src/**/*.json", |
| 113 | + ], |
33 | 114 | });
|
34 | 115 |
|
35 |
| - verifyTsc({ |
36 |
| - scenario: "resolveJsonModule", |
| 116 | + verfiyJson({ |
37 | 117 | subScenario: "include of json along with other include and file name matches ts file",
|
38 |
| - fs: () => projFs, |
39 |
| - commandLineArgs: ["--b", "/src/tsconfig_withIncludeOfJson.json", "--v", "--explainFiles"], |
40 | 118 | modifyFs: fs => {
|
41 |
| - fs.rimrafSync("/src/src/hello.json"); |
42 |
| - fs.writeFileSync("/src/src/index.json", JSON.stringify({ hello: "world" })); |
43 |
| - fs.writeFileSync( |
44 |
| - "/src/src/index.ts", |
45 |
| - `import hello from "./index.json" |
46 |
| -
|
47 |
| -export default hello.hello`, |
48 |
| - ); |
| 119 | + fs.renameSync("/src/src/hello.json", "/src/src/index.json"); |
| 120 | + replaceText(fs, "/src/src/index.ts", "hello.json", "index.json"); |
49 | 121 | },
|
| 122 | + }, { |
| 123 | + include: [ |
| 124 | + "src/**/*", |
| 125 | + "src/**/*.json", |
| 126 | + ], |
50 | 127 | });
|
51 | 128 |
|
52 |
| - verifyTsc({ |
53 |
| - scenario: "resolveJsonModule", |
54 |
| - subScenario: "files containing json file", |
55 |
| - fs: () => projFs, |
56 |
| - commandLineArgs: ["--b", "/src/tsconfig_withFiles.json", "--v", "--explainFiles"], |
| 129 | + verfiyJson("files containing json file", { |
| 130 | + files: [ |
| 131 | + "src/index.ts", |
| 132 | + "src/hello.json", |
| 133 | + ], |
57 | 134 | });
|
58 | 135 |
|
59 |
| - verifyTsc({ |
60 |
| - scenario: "resolveJsonModule", |
61 |
| - subScenario: "include and files", |
62 |
| - fs: () => projFs, |
63 |
| - commandLineArgs: ["--b", "/src/tsconfig_withIncludeAndFiles.json", "--v", "--explainFiles"], |
| 136 | + verfiyJson("include and files", { |
| 137 | + files: [ |
| 138 | + "src/hello.json", |
| 139 | + ], |
| 140 | + include: [ |
| 141 | + "src/**/*", |
| 142 | + ], |
64 | 143 | });
|
65 | 144 |
|
66 |
| - verifyTsc({ |
67 |
| - scenario: "resolveJsonModule", |
| 145 | + verfiyJson({ |
68 | 146 | subScenario: "sourcemap",
|
69 |
| - fs: () => projFs, |
70 |
| - commandLineArgs: ["--b", "src/tsconfig_withFiles.json", "--verbose", "--explainFiles"], |
71 |
| - modifyFs: fs => replaceText(fs, "src/tsconfig_withFiles.json", `"composite": true,`, `"composite": true, "sourceMap": true,`), |
72 | 147 | edits: noChangeOnlyRuns,
|
73 |
| - }); |
| 148 | + }, { |
| 149 | + files: [ |
| 150 | + "src/index.ts", |
| 151 | + "src/hello.json", |
| 152 | + ], |
| 153 | + }, { sourceMap: true }); |
74 | 154 |
|
75 |
| - verifyTsc({ |
76 |
| - scenario: "resolveJsonModule", |
| 155 | + verfiyJson({ |
77 | 156 | subScenario: "without outDir",
|
78 |
| - fs: () => projFs, |
79 |
| - commandLineArgs: ["--b", "src/tsconfig_withFiles.json", "--verbose"], |
80 |
| - modifyFs: fs => replaceText(fs, "src/tsconfig_withFiles.json", `"outDir": "dist",`, ""), |
81 | 157 | edits: noChangeOnlyRuns,
|
82 |
| - }); |
| 158 | + }, { |
| 159 | + files: [ |
| 160 | + "src/index.ts", |
| 161 | + "src/hello.json", |
| 162 | + ], |
| 163 | + }, { outDir: undefined }); |
83 | 164 | });
|
84 | 165 |
|
85 | 166 | describe("unittests:: tsbuild:: with resolveJsonModule option on project importJsonFromProjectReference", () => {
|
86 | 167 | verifyTsc({
|
87 | 168 | scenario: "resolveJsonModule",
|
88 | 169 | subScenario: "importing json module from project reference",
|
89 |
| - fs: () => loadProjectFromDisk("tests/projects/importJsonFromProjectReference"), |
| 170 | + fs: () => |
| 171 | + loadProjectFromFiles({ |
| 172 | + "/src/strings/foo.json": JSON.stringify( |
| 173 | + { |
| 174 | + foo: "bar baz", |
| 175 | + }, |
| 176 | + undefined, |
| 177 | + " ", |
| 178 | + ), |
| 179 | + "/src/strings/tsconfig.json": JSON.stringify( |
| 180 | + { |
| 181 | + extends: "../tsconfig.json", |
| 182 | + include: ["foo.json"], |
| 183 | + references: [], |
| 184 | + }, |
| 185 | + undefined, |
| 186 | + " ", |
| 187 | + ), |
| 188 | + "/src/main/index.ts": dedent` |
| 189 | + import { foo } from '../strings/foo.json'; |
| 190 | + console.log(foo); |
| 191 | + `, |
| 192 | + "/src/main/tsconfig.json": JSON.stringify( |
| 193 | + { |
| 194 | + extends: "../tsconfig.json", |
| 195 | + include: [ |
| 196 | + "./**/*.ts", |
| 197 | + ], |
| 198 | + references: [{ |
| 199 | + path: "../strings/tsconfig.json", |
| 200 | + }], |
| 201 | + }, |
| 202 | + undefined, |
| 203 | + " ", |
| 204 | + ), |
| 205 | + "/src/tsconfig.json": JSON.stringify( |
| 206 | + { |
| 207 | + compilerOptions: { |
| 208 | + target: "es5", |
| 209 | + module: "commonjs", |
| 210 | + rootDir: "./", |
| 211 | + composite: true, |
| 212 | + resolveJsonModule: true, |
| 213 | + strict: true, |
| 214 | + esModuleInterop: true, |
| 215 | + }, |
| 216 | + references: [ |
| 217 | + { path: "./strings/tsconfig.json" }, |
| 218 | + { path: "./main/tsconfig.json" }, |
| 219 | + ], |
| 220 | + files: [], |
| 221 | + }, |
| 222 | + undefined, |
| 223 | + " ", |
| 224 | + ), |
| 225 | + }), |
90 | 226 | commandLineArgs: ["--b", "src/tsconfig.json", "--verbose", "--explainFiles"],
|
91 | 227 | edits: noChangeOnlyRuns,
|
92 | 228 | });
|
|
0 commit comments