Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure that JSON files are included in build module resolution (#905) #1101

Merged
merged 3 commits into from May 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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' }]
}
};