From 7df81488ab42a1198d5f4a8ccbbdf40cd1095ed6 Mon Sep 17 00:00:00 2001 From: Brent Erickson Date: Wed, 6 May 2020 15:35:51 -0700 Subject: [PATCH 1/3] Fix #905 Ensure that json files are resolved if resolveJsonModule flag is set in tsconfig --- src/instances.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/instances.ts b/src/instances.ts index c8b99c1e5..195f733d0 100644 --- a/src/instances.ts +++ b/src/instances.ts @@ -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 From 4eb03b2a40b3b5e22f33712cc1dfe6e816aebed2 Mon Sep 17 00:00:00 2001 From: Brent Erickson Date: Wed, 6 May 2020 16:47:26 -0700 Subject: [PATCH 2/3] Add test --- test/comparison-tests/resolveJsonModule/app.ts | 3 +++ .../expectedOutput-3.8/output.txt | 17 +++++++++++++++++ .../expectedOutput-transpile-3.8/bundle.js | 1 + .../expectedOutput-transpile-3.8/output.txt | 17 +++++++++++++++++ .../resolveJsonModule/file.json | 3 +++ .../resolveJsonModule/tsconfig.json | 7 +++++++ .../resolveJsonModule/webpack.config.js | 12 ++++++++++++ 7 files changed, 60 insertions(+) create mode 100644 test/comparison-tests/resolveJsonModule/app.ts create mode 100644 test/comparison-tests/resolveJsonModule/expectedOutput-3.8/output.txt create mode 100644 test/comparison-tests/resolveJsonModule/expectedOutput-transpile-3.8/bundle.js create mode 100644 test/comparison-tests/resolveJsonModule/expectedOutput-transpile-3.8/output.txt create mode 100644 test/comparison-tests/resolveJsonModule/file.json create mode 100644 test/comparison-tests/resolveJsonModule/tsconfig.json create mode 100644 test/comparison-tests/resolveJsonModule/webpack.config.js diff --git a/test/comparison-tests/resolveJsonModule/app.ts b/test/comparison-tests/resolveJsonModule/app.ts new file mode 100644 index 000000000..9a972817a --- /dev/null +++ b/test/comparison-tests/resolveJsonModule/app.ts @@ -0,0 +1,3 @@ +import * as file from "./file.json"; + +console.log(file.foo); diff --git a/test/comparison-tests/resolveJsonModule/expectedOutput-3.8/output.txt b/test/comparison-tests/resolveJsonModule/expectedOutput-3.8/output.txt new file mode 100644 index 000000000..6dff3ed53 --- /dev/null +++ b/test/comparison-tests/resolveJsonModule/expectedOutput-3.8/output.txt @@ -0,0 +1,17 @@ + Asset Size Chunks Chunk Names +bundle.js 1.12 KiB 0 main +Entrypoint main = bundle.js +[0] ./app.ts 232 bytes {0} [built] [failed] [2 errors] + +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/ + +ERROR in tsconfig.json +[tsl] ERROR + TS18003: No inputs were found in config file 'tsconfig.json'. Specified 'include' paths were '["ap.ts","file.json"]' and 'exclude' paths were '[]'. + +ERROR in ./app.ts +Module build failed (from index.js): +Error: error while parsing tsconfig.json + at Object.loader (dist/index.js:19:18) \ No newline at end of file diff --git a/test/comparison-tests/resolveJsonModule/expectedOutput-transpile-3.8/bundle.js b/test/comparison-tests/resolveJsonModule/expectedOutput-transpile-3.8/bundle.js new file mode 100644 index 000000000..40d86baa7 --- /dev/null +++ b/test/comparison-tests/resolveJsonModule/expectedOutput-transpile-3.8/bundle.js @@ -0,0 +1 @@ +!function(e){var t={};function r(n){if(t[n])return t[n].exports;var o=t[n]={i:n,l:!1,exports:{}};return e[n].call(o.exports,o,o.exports,r),o.l=!0,o.exports}r.m=e,r.c=t,r.d=function(e,t,n){r.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:n})},r.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},r.t=function(e,t){if(1&t&&(e=r(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var n=Object.create(null);if(r.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var o in e)r.d(n,o,function(t){return e[t]}.bind(null,o));return n},r.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return r.d(t,"a",t),t},r.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},r.p="",r(r.s=0)}([function(e,t,r){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var n=r(1);console.log(n.foo)},function(e){e.exports={foo:"bar"}}]); \ No newline at end of file diff --git a/test/comparison-tests/resolveJsonModule/expectedOutput-transpile-3.8/output.txt b/test/comparison-tests/resolveJsonModule/expectedOutput-transpile-3.8/output.txt new file mode 100644 index 000000000..6dff3ed53 --- /dev/null +++ b/test/comparison-tests/resolveJsonModule/expectedOutput-transpile-3.8/output.txt @@ -0,0 +1,17 @@ + Asset Size Chunks Chunk Names +bundle.js 1.12 KiB 0 main +Entrypoint main = bundle.js +[0] ./app.ts 232 bytes {0} [built] [failed] [2 errors] + +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/ + +ERROR in tsconfig.json +[tsl] ERROR + TS18003: No inputs were found in config file 'tsconfig.json'. Specified 'include' paths were '["ap.ts","file.json"]' and 'exclude' paths were '[]'. + +ERROR in ./app.ts +Module build failed (from index.js): +Error: error while parsing tsconfig.json + at Object.loader (dist/index.js:19:18) \ No newline at end of file diff --git a/test/comparison-tests/resolveJsonModule/file.json b/test/comparison-tests/resolveJsonModule/file.json new file mode 100644 index 000000000..b42f309e7 --- /dev/null +++ b/test/comparison-tests/resolveJsonModule/file.json @@ -0,0 +1,3 @@ +{ + "foo": "bar" +} \ No newline at end of file diff --git a/test/comparison-tests/resolveJsonModule/tsconfig.json b/test/comparison-tests/resolveJsonModule/tsconfig.json new file mode 100644 index 000000000..e99a711f7 --- /dev/null +++ b/test/comparison-tests/resolveJsonModule/tsconfig.json @@ -0,0 +1,7 @@ +{ + "compilerOptions": { + "resolveJsonModule": true, + "composite": true + }, + "include": ["ap.ts", "file.json"] +} diff --git a/test/comparison-tests/resolveJsonModule/webpack.config.js b/test/comparison-tests/resolveJsonModule/webpack.config.js new file mode 100644 index 000000000..8711af1b9 --- /dev/null +++ b/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' }] + } +}; From bed8dcd9fe962bb1c084410d4fea34701ddeaa1c Mon Sep 17 00:00:00 2001 From: Brent Erickson Date: Wed, 6 May 2020 18:35:38 -0700 Subject: [PATCH 3/3] PR Comments --- CHANGELOG.md | 3 +++ package.json | 2 +- .../expectedOutput-3.8/bundle.js | 1 + .../expectedOutput-3.8/output.txt | 19 ++++++------------- .../expectedOutput-transpile-3.8/output.txt | 18 +++++------------- .../resolveJsonModule/tsconfig.json | 2 +- 6 files changed, 17 insertions(+), 28 deletions(-) create mode 100644 test/comparison-tests/resolveJsonModule/expectedOutput-3.8/bundle.js diff --git a/CHANGELOG.md b/CHANGELOG.md index 6180bebaf..8b13316dd 100644 --- a/CHANGELOG.md +++ b/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 diff --git a/package.json b/package.json index dbb0d1bcf..822a3fd5b 100644 --- a/package.json +++ b/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", diff --git a/test/comparison-tests/resolveJsonModule/expectedOutput-3.8/bundle.js b/test/comparison-tests/resolveJsonModule/expectedOutput-3.8/bundle.js new file mode 100644 index 000000000..4332c1464 --- /dev/null +++ b/test/comparison-tests/resolveJsonModule/expectedOutput-3.8/bundle.js @@ -0,0 +1 @@ +!function(e){var t={};function r(n){if(t[n])return t[n].exports;var o=t[n]={i:n,l:!1,exports:{}};return e[n].call(o.exports,o,o.exports,r),o.l=!0,o.exports}r.m=e,r.c=t,r.d=function(e,t,n){r.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:n})},r.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},r.t=function(e,t){if(1&t&&(e=r(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var n=Object.create(null);if(r.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var o in e)r.d(n,o,function(t){return e[t]}.bind(null,o));return n},r.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return r.d(t,"a",t),t},r.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},r.p="",r(r.s=0)}([function(e,t,r){"use strict";t.__esModule=!0;var n=r(1);console.log(n.foo)},function(e){e.exports={foo:"bar"}}]); \ No newline at end of file diff --git a/test/comparison-tests/resolveJsonModule/expectedOutput-3.8/output.txt b/test/comparison-tests/resolveJsonModule/expectedOutput-3.8/output.txt index 6dff3ed53..10e73c4e6 100644 --- a/test/comparison-tests/resolveJsonModule/expectedOutput-3.8/output.txt +++ b/test/comparison-tests/resolveJsonModule/expectedOutput-3.8/output.txt @@ -1,17 +1,10 @@ - Asset Size Chunks Chunk Names -bundle.js 1.12 KiB 0 main + 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 232 bytes {0} [built] [failed] [2 errors] +[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/ - -ERROR in tsconfig.json -[tsl] ERROR - TS18003: No inputs were found in config file 'tsconfig.json'. Specified 'include' paths were '["ap.ts","file.json"]' and 'exclude' paths were '[]'. - -ERROR in ./app.ts -Module build failed (from index.js): -Error: error while parsing tsconfig.json - at Object.loader (dist/index.js:19:18) \ No newline at end of file +You can also set it to 'none' to disable any default behavior. Learn more: https://webpack.js.org/concepts/mode/ \ No newline at end of file diff --git a/test/comparison-tests/resolveJsonModule/expectedOutput-transpile-3.8/output.txt b/test/comparison-tests/resolveJsonModule/expectedOutput-transpile-3.8/output.txt index 6dff3ed53..46b8ad0ff 100644 --- a/test/comparison-tests/resolveJsonModule/expectedOutput-transpile-3.8/output.txt +++ b/test/comparison-tests/resolveJsonModule/expectedOutput-transpile-3.8/output.txt @@ -1,17 +1,9 @@ - Asset Size Chunks Chunk Names -bundle.js 1.12 KiB 0 main + Asset Size Chunks Chunk Names +bundle.js 1.03 KiB 0 [emitted] main Entrypoint main = bundle.js -[0] ./app.ts 232 bytes {0} [built] [failed] [2 errors] +[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/ - -ERROR in tsconfig.json -[tsl] ERROR - TS18003: No inputs were found in config file 'tsconfig.json'. Specified 'include' paths were '["ap.ts","file.json"]' and 'exclude' paths were '[]'. - -ERROR in ./app.ts -Module build failed (from index.js): -Error: error while parsing tsconfig.json - at Object.loader (dist/index.js:19:18) \ No newline at end of file +You can also set it to 'none' to disable any default behavior. Learn more: https://webpack.js.org/concepts/mode/ \ No newline at end of file diff --git a/test/comparison-tests/resolveJsonModule/tsconfig.json b/test/comparison-tests/resolveJsonModule/tsconfig.json index e99a711f7..bbc3d30d2 100644 --- a/test/comparison-tests/resolveJsonModule/tsconfig.json +++ b/test/comparison-tests/resolveJsonModule/tsconfig.json @@ -3,5 +3,5 @@ "resolveJsonModule": true, "composite": true }, - "include": ["ap.ts", "file.json"] + "include": ["app.ts", "file.json"] }