Skip to content

Commit

Permalink
Ensure that JSON files are included in build module resolution (TypeS…
Browse files Browse the repository at this point in the history
…trong#905) (TypeStrong#1101)

* Fix TypeStrong#905
Ensure that json files are resolved if resolveJsonModule flag is set in tsconfig

* Add test

* PR Comments
  • Loading branch information
berickson1 committed May 22, 2020
1 parent ea7132f commit 0546ce0
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,8 @@
# Changelog

## v7.0.3
* [Ensure that JSON files are included in build module resolution](https://github.com/TypeStrong/ts-loader/pull/1101) - thanks @berickson1

## v7.0.2
* [Make content hash consistent across machines](https://github.com/TypeStrong/ts-loader/pull/1085) - thanks @elyalvarado

Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "ts-loader",
"version": "7.0.2",
"version": "7.0.3",
"description": "TypeScript loader for webpack",
"main": "index.js",
"types": "dist",
Expand Down
7 changes: 7 additions & 0 deletions src/instances.ts
Expand Up @@ -351,6 +351,13 @@ export function initializeInstance(
}

function getScriptRegexp(instance: TSInstance) {
// If resolveJsonModules is set, we should accept json files
if (instance.configParseResult.options.resolveJsonModule) {
// if allowJs is set then we should accept js(x) files
return instance.configParseResult.options.allowJs === true
? /\.tsx?$|\.json$|\.jsx?$/i
: /\.tsx?$|\.json$/i;
}
// if allowJs is set then we should accept js(x) files
return instance.configParseResult.options.allowJs === true
? /\.tsx?$|\.jsx?$/i
Expand Down
3 changes: 3 additions & 0 deletions test/comparison-tests/resolveJsonModule/app.ts
@@ -0,0 +1,3 @@
import * as file from "./file.json";

console.log(file.foo);

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

@@ -0,0 +1,10 @@
Asset Size Chunks Chunk Names
bundle.js 1 KiB 0 [emitted] main
../app.d.ts 11 bytes [emitted]
Entrypoint main = bundle.js
[0] ./app.ts 99 bytes {0} [built]
[1] ./file.json 18 bytes {0} [built]

WARNING in configuration
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.
You can also set it to 'none' to disable any default behavior. Learn more: https://webpack.js.org/concepts/mode/

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

@@ -0,0 +1,9 @@
Asset Size Chunks Chunk Names
bundle.js 1.03 KiB 0 [emitted] main
Entrypoint main = bundle.js
[0] ./app.ts 135 bytes {0} [built]
[1] ./file.json 18 bytes {0} [built]

WARNING in configuration
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.
You can also set it to 'none' to disable any default behavior. Learn more: https://webpack.js.org/concepts/mode/
3 changes: 3 additions & 0 deletions test/comparison-tests/resolveJsonModule/file.json
@@ -0,0 +1,3 @@
{
"foo": "bar"
}
7 changes: 7 additions & 0 deletions test/comparison-tests/resolveJsonModule/tsconfig.json
@@ -0,0 +1,7 @@
{
"compilerOptions": {
"resolveJsonModule": true,
"composite": true
},
"include": ["app.ts", "file.json"]
}
12 changes: 12 additions & 0 deletions test/comparison-tests/resolveJsonModule/webpack.config.js
@@ -0,0 +1,12 @@
module.exports = {
entry: './app.ts',
output: {
filename: 'bundle.js'
},
resolve: {
extensions: ['.ts', '.json']
},
module: {
rules: [{ test: /\.tsx?$/, loader: 'ts-loader' }]
}
};

0 comments on commit 0546ce0

Please sign in to comment.