Skip to content

Commit 49ac60f

Browse files
committed
Support resolveJsonModule option when files contain the json file
Fixes #25636
1 parent 47e513e commit 49ac60f

File tree

7 files changed

+89
-2
lines changed

7 files changed

+89
-2
lines changed

src/compiler/program.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,7 @@ namespace ts {
592592
const programDiagnostics = createDiagnosticCollection();
593593
const currentDirectory = host.getCurrentDirectory();
594594
const supportedExtensions = getSupportedExtensions(options);
595+
const supportedExtensionsWithJsonIfResolveJsonModule = options.resolveJsonModule ? [...supportedExtensions, Extension.Json] : undefined;
595596

596597
// Map storing if there is emit blocking diagnostics for given input
597598
const hasEmitBlockingDiagnostics = createMap<boolean>();
@@ -1925,7 +1926,7 @@ namespace ts {
19251926
refFile?: SourceFile): SourceFile | undefined {
19261927

19271928
if (hasExtension(fileName)) {
1928-
if (!options.allowNonTsExtensions && !forEach(supportedExtensions, extension => fileExtensionIs(host.getCanonicalFileName(fileName), extension))) {
1929+
if (!options.allowNonTsExtensions && !forEach(supportedExtensionsWithJsonIfResolveJsonModule || supportedExtensions, extension => fileExtensionIs(host.getCanonicalFileName(fileName), extension))) {
19291930
if (fail) fail(Diagnostics.File_0_has_unsupported_extension_The_only_supported_extensions_are_1, fileName, "'" + supportedExtensions.join("', '") + "'");
19301931
return undefined;
19311932
}

src/testRunner/unittests/tsbuild.ts

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,38 @@ namespace ts {
233233
assert.isBelow(fs.statSync("/src/core/index.js").mtimeMs, time(), "Upstream JS file should not have been rebuilt");
234234
});
235235
});
236+
237+
describe("tsbuild - with resolveJsonModule option", () => {
238+
const projFs = loadProjectFromDisk("tests/projects/resolveJsonModuleAndComposite");
239+
const allExpectedOutputs = ["/src/tests/dist/src/index.js", "/src/tests/dist/src/index.d.ts", "/src/tests/dist/src/hello.json"];
240+
241+
function verifyProjectWithResolveJsonModule(configFile: string, ...expectedDiagnosticMessages: DiagnosticMessage[]) {
242+
const fs = projFs.shadow();
243+
const host = new fakes.CompilerHost(fs);
244+
const builder = createSolutionBuilder(host, buildHost, [configFile], { dry: false, force: false, verbose: false });
245+
clearDiagnostics();
246+
builder.buildAllProjects();
247+
assertDiagnosticMessages(...expectedDiagnosticMessages);
248+
if (!expectedDiagnosticMessages.length) {
249+
// Check for outputs. Not an exhaustive list
250+
for (const output of allExpectedOutputs) {
251+
assert(fs.existsSync(output), `Expect file ${output} to exist`);
252+
}
253+
}
254+
}
255+
256+
it("with resolveJsonModule and include only", () => {
257+
verifyProjectWithResolveJsonModule("/src/tests/tsconfig_withInclude.json", Diagnostics.File_0_is_not_in_project_file_list_Projects_must_list_all_files_or_use_an_include_pattern);
258+
});
259+
260+
it("with resolveJsonModule and files containing json file", () => {
261+
verifyProjectWithResolveJsonModule("/src/tests/tsconfig_withFiles.json");
262+
});
263+
264+
it("with resolveJsonModule and include and files", () => {
265+
verifyProjectWithResolveJsonModule("/src/tests/tsconfig_withIncludeAndFiles.json");
266+
});
267+
});
236268
}
237269

238270
export namespace OutFile {
@@ -427,4 +459,4 @@ namespace ts {
427459
fs.makeReadonly();
428460
return fs;
429461
}
430-
}
462+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"hello": "world"
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import hello from "./hello.json"
2+
3+
export default hello.hello
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"compilerOptions": {
3+
"composite": true,
4+
"target": "esnext",
5+
"moduleResolution": "node",
6+
"module": "commonjs",
7+
"resolveJsonModule": true,
8+
"esModuleInterop": true,
9+
"allowSyntheticDefaultImports": true,
10+
"outDir": "dist"
11+
},
12+
"files": [
13+
"src/index.ts", "src/hello.json"
14+
]
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"compilerOptions": {
3+
"composite": true,
4+
"target": "esnext",
5+
"moduleResolution": "node",
6+
"module": "commonjs",
7+
"resolveJsonModule": true,
8+
"esModuleInterop": true,
9+
"allowSyntheticDefaultImports": true,
10+
"outDir": "dist"
11+
},
12+
"include": [
13+
"src/**/*"
14+
]
15+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"compilerOptions": {
3+
"composite": true,
4+
"target": "esnext",
5+
"moduleResolution": "node",
6+
"module": "commonjs",
7+
"resolveJsonModule": true,
8+
"esModuleInterop": true,
9+
"allowSyntheticDefaultImports": true,
10+
"outDir": "dist"
11+
},
12+
"files": [
13+
"src/hello.json"
14+
],
15+
"include": [
16+
"src/**/*"
17+
]
18+
}

0 commit comments

Comments
 (0)