Skip to content

Commit 227d99a

Browse files
authored
Ensure that JSON files are included in build module resolution (#905) (#1101)
* Fix #905 Ensure that json files are resolved if resolveJsonModule flag is set in tsconfig * Add test * PR Comments
1 parent bbc6d81 commit 227d99a

File tree

11 files changed

+57
-1
lines changed

11 files changed

+57
-1
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Changelog
22

3+
## v7.0.3
4+
* [Ensure that JSON files are included in build module resolution](https://github.com/TypeStrong/ts-loader/pull/1101) - thanks @berickson1
5+
36
## v7.0.2
47
* [Make content hash consistent across machines](https://github.com/TypeStrong/ts-loader/pull/1085) - thanks @elyalvarado
58

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ts-loader",
3-
"version": "7.0.2",
3+
"version": "7.0.3",
44
"description": "TypeScript loader for webpack",
55
"main": "index.js",
66
"types": "dist",

src/instances.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,13 @@ export function initializeInstance(
351351
}
352352

353353
function getScriptRegexp(instance: TSInstance) {
354+
// If resolveJsonModules is set, we should accept json files
355+
if (instance.configParseResult.options.resolveJsonModule) {
356+
// if allowJs is set then we should accept js(x) files
357+
return instance.configParseResult.options.allowJs === true
358+
? /\.tsx?$|\.json$|\.jsx?$/i
359+
: /\.tsx?$|\.json$/i;
360+
}
354361
// if allowJs is set then we should accept js(x) files
355362
return instance.configParseResult.options.allowJs === true
356363
? /\.tsx?$|\.jsx?$/i
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import * as file from "./file.json";
2+
3+
console.log(file.foo);

test/comparison-tests/resolveJsonModule/expectedOutput-3.8/bundle.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Asset Size Chunks Chunk Names
2+
bundle.js 1 KiB 0 [emitted] main
3+
../app.d.ts 11 bytes [emitted]
4+
Entrypoint main = bundle.js
5+
[0] ./app.ts 99 bytes {0} [built]
6+
[1] ./file.json 18 bytes {0} [built]
7+
8+
WARNING in configuration
9+
The 'mode' option has not been set, webpack will fallback to 'production' for this value. Set 'mode' option to 'development' or 'production' to enable defaults for each environment.
10+
You can also set it to 'none' to disable any default behavior. Learn more: https://webpack.js.org/concepts/mode/

test/comparison-tests/resolveJsonModule/expectedOutput-transpile-3.8/bundle.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Asset Size Chunks Chunk Names
2+
bundle.js 1.03 KiB 0 [emitted] main
3+
Entrypoint main = bundle.js
4+
[0] ./app.ts 135 bytes {0} [built]
5+
[1] ./file.json 18 bytes {0} [built]
6+
7+
WARNING in configuration
8+
The 'mode' option has not been set, webpack will fallback to 'production' for this value. Set 'mode' option to 'development' or 'production' to enable defaults for each environment.
9+
You can also set it to 'none' to disable any default behavior. Learn more: https://webpack.js.org/concepts/mode/
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"foo": "bar"
3+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"compilerOptions": {
3+
"resolveJsonModule": true,
4+
"composite": true
5+
},
6+
"include": ["app.ts", "file.json"]
7+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module.exports = {
2+
entry: './app.ts',
3+
output: {
4+
filename: 'bundle.js'
5+
},
6+
resolve: {
7+
extensions: ['.ts', '.json']
8+
},
9+
module: {
10+
rules: [{ test: /\.tsx?$/, loader: 'ts-loader' }]
11+
}
12+
};

0 commit comments

Comments
 (0)