From dd0a0598b096431b65426e4292da18b7f22a5fda Mon Sep 17 00:00:00 2001 From: Thomas Hermann Date: Fri, 30 Aug 2019 21:49:19 -0400 Subject: [PATCH 1/4] Fix absolute imports of JS files using TypeScript --- src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 9c250b74..8a4eb9e0 100644 --- a/src/index.js +++ b/src/index.js @@ -71,7 +71,7 @@ module.exports = ( // we need to catch here because the plugin will // error if there's no tsconfig in the working directory try { - resolvePlugins.push(new TsconfigPathsPlugin({ silent: true })); + resolvePlugins.push(new TsconfigPathsPlugin({ silent: true, extensions: [".ts", ".tsx", ".js"] })); const tsconfig = tsconfigPaths.loadConfig(); if (tsconfig.resultType === "success") { From c904ed9cc36ff76fd5992bb418b76662f54fefa3 Mon Sep 17 00:00:00 2001 From: Thomas Hermann Date: Fri, 30 Aug 2019 22:45:42 -0400 Subject: [PATCH 2/4] Add unit test for tsconfig paths with allowjs --- test/unit/tsconfig-paths-allowjs/input.ts | 3 + test/unit/tsconfig-paths-allowjs/module.js | 1 + .../tsconfig-paths-allowjs/output-coverage.js | 86 +++++++++++++++++++ test/unit/tsconfig-paths-allowjs/output.js | 66 ++++++++++++++ .../unit/tsconfig-paths-allowjs/tsconfig.json | 11 +++ 5 files changed, 167 insertions(+) create mode 100644 test/unit/tsconfig-paths-allowjs/input.ts create mode 100644 test/unit/tsconfig-paths-allowjs/module.js create mode 100644 test/unit/tsconfig-paths-allowjs/output-coverage.js create mode 100644 test/unit/tsconfig-paths-allowjs/output.js create mode 100644 test/unit/tsconfig-paths-allowjs/tsconfig.json diff --git a/test/unit/tsconfig-paths-allowjs/input.ts b/test/unit/tsconfig-paths-allowjs/input.ts new file mode 100644 index 00000000..02af28c5 --- /dev/null +++ b/test/unit/tsconfig-paths-allowjs/input.ts @@ -0,0 +1,3 @@ +import module from '@module'; + +console.log(module); diff --git a/test/unit/tsconfig-paths-allowjs/module.js b/test/unit/tsconfig-paths-allowjs/module.js new file mode 100644 index 00000000..ff8b4c56 --- /dev/null +++ b/test/unit/tsconfig-paths-allowjs/module.js @@ -0,0 +1 @@ +export default {}; diff --git a/test/unit/tsconfig-paths-allowjs/output-coverage.js b/test/unit/tsconfig-paths-allowjs/output-coverage.js new file mode 100644 index 00000000..431e4c2a --- /dev/null +++ b/test/unit/tsconfig-paths-allowjs/output-coverage.js @@ -0,0 +1,86 @@ +module.exports = +/******/ (function(modules, runtime) { // webpackBootstrap +/******/ "use strict"; +/******/ // The module cache +/******/ var installedModules = {}; +/******/ +/******/ // The require function +/******/ function __webpack_require__(moduleId) { +/******/ +/******/ // Check if module is in cache +/******/ if(installedModules[moduleId]) { +/******/ return installedModules[moduleId].exports; +/******/ } +/******/ // Create a new module (and put it into the cache) +/******/ var module = installedModules[moduleId] = { +/******/ i: moduleId, +/******/ l: false, +/******/ exports: {} +/******/ }; +/******/ +/******/ // Execute the module function +/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__); +/******/ +/******/ // Flag the module as loaded +/******/ module.l = true; +/******/ +/******/ // Return the exports of the module +/******/ return module.exports; +/******/ } +/******/ +/******/ +/******/ __webpack_require__.ab = __dirname + "/"; +/******/ +/******/ // the startup function +/******/ function startup() { +/******/ // Load entry module and return exports +/******/ return __webpack_require__(439); +/******/ }; +/******/ // initialize runtime +/******/ runtime(__webpack_require__); +/******/ +/******/ // run startup +/******/ return startup(); +/******/ }) +/************************************************************************/ +/******/ ({ + +/***/ 306: +/***/ (function(__unusedmodule, __webpack_exports__, __webpack_require__) { + +"use strict"; +__webpack_require__.r(__webpack_exports__); +/* harmony default export */ __webpack_exports__["default"] = ({}); + + +/***/ }), + +/***/ 439: +/***/ (function(__unusedmodule, exports, __webpack_require__) { + +"use strict"; + +exports.__esModule = true; +var _module_1 = __webpack_require__(306); +console.log(_module_1["default"]); + + +/***/ }) + +/******/ }, +/******/ function(__webpack_require__) { // webpackRuntimeModules +/******/ "use strict"; +/******/ +/******/ /* webpack/runtime/make namespace object */ +/******/ !function() { +/******/ // define __esModule on exports +/******/ __webpack_require__.r = function(exports) { +/******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) { +/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }); +/******/ } +/******/ Object.defineProperty(exports, '__esModule', { value: true }); +/******/ }; +/******/ }(); +/******/ +/******/ } +); diff --git a/test/unit/tsconfig-paths-allowjs/output.js b/test/unit/tsconfig-paths-allowjs/output.js new file mode 100644 index 00000000..d1058d0d --- /dev/null +++ b/test/unit/tsconfig-paths-allowjs/output.js @@ -0,0 +1,66 @@ +module.exports = +/******/ (function(modules, runtime) { // webpackBootstrap +/******/ "use strict"; +/******/ // The module cache +/******/ var installedModules = {}; +/******/ +/******/ // The require function +/******/ function __webpack_require__(moduleId) { +/******/ +/******/ // Check if module is in cache +/******/ if(installedModules[moduleId]) { +/******/ return installedModules[moduleId].exports; +/******/ } +/******/ // Create a new module (and put it into the cache) +/******/ var module = installedModules[moduleId] = { +/******/ i: moduleId, +/******/ l: false, +/******/ exports: {} +/******/ }; +/******/ +/******/ // Execute the module function +/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__); +/******/ +/******/ // Flag the module as loaded +/******/ module.l = true; +/******/ +/******/ // Return the exports of the module +/******/ return module.exports; +/******/ } +/******/ +/******/ +/******/ __webpack_require__.ab = __dirname + "/"; +/******/ +/******/ // the startup function +/******/ function startup() { +/******/ // Load entry module and return exports +/******/ return __webpack_require__(468); +/******/ }; +/******/ +/******/ // run startup +/******/ return startup(); +/******/ }) +/************************************************************************/ +/******/ ({ + +/***/ 17: +/***/ (function() { + +eval("require")("@module"); + + +/***/ }), + +/***/ 468: +/***/ (function(__unusedmodule, exports, __webpack_require__) { + +"use strict"; + +exports.__esModule = true; +var _module_1 = __webpack_require__(17); +console.log(_module_1["default"]); + + +/***/ }) + +/******/ }); diff --git a/test/unit/tsconfig-paths-allowjs/tsconfig.json b/test/unit/tsconfig-paths-allowjs/tsconfig.json new file mode 100644 index 00000000..5a906978 --- /dev/null +++ b/test/unit/tsconfig-paths-allowjs/tsconfig.json @@ -0,0 +1,11 @@ +{ + "compilerOptions": { + "module": "commonjs", + "moduleResolution": "node", + "baseUrl": ".", + "paths": { + "@*": ["./*"] + }, + "allowJs": true + } +} From a48bf1f4d16a68f66785a03a8fa8e7a428a2c66c Mon Sep 17 00:00:00 2001 From: Thomas Date: Thu, 5 Sep 2019 12:51:52 -0400 Subject: [PATCH 3/4] Align extensions for tsconfig-paths with webpack config --- src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 8a4eb9e0..5e98e2af 100644 --- a/src/index.js +++ b/src/index.js @@ -71,7 +71,7 @@ module.exports = ( // we need to catch here because the plugin will // error if there's no tsconfig in the working directory try { - resolvePlugins.push(new TsconfigPathsPlugin({ silent: true, extensions: [".ts", ".tsx", ".js"] })); + resolvePlugins.push(new TsconfigPathsPlugin({ silent: true, extensions: SUPPORTED_EXTENSIONS })); const tsconfig = tsconfigPaths.loadConfig(); if (tsconfig.resultType === "success") { From 9cd830623381771d82e4c1f641558b26e73c2c03 Mon Sep 17 00:00:00 2001 From: Thomas Date: Thu, 5 Sep 2019 13:03:47 -0400 Subject: [PATCH 4/4] Align extensions for tsconfig-paths with webpack config only if allowJs --- src/index.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/index.js b/src/index.js index 5e98e2af..90627572 100644 --- a/src/index.js +++ b/src/index.js @@ -71,9 +71,15 @@ module.exports = ( // we need to catch here because the plugin will // error if there's no tsconfig in the working directory try { - resolvePlugins.push(new TsconfigPathsPlugin({ silent: true, extensions: SUPPORTED_EXTENSIONS })); - const tsconfig = tsconfigPaths.loadConfig(); + const fullTsconfig = require(tsconfig.configFileAbsolutePath) + + const tsconfigPathsOptions = { silent: true } + if (fullTsconfig.compilerOptions.allowJs) { + tsconfigPathsOptions.extensions = SUPPORTED_EXTENSIONS + } + resolvePlugins.push(new TsconfigPathsPlugin(tsconfigPathsOptions)); + if (tsconfig.resultType === "success") { tsconfigMatchPath = tsconfigPaths.createMatchPath(tsconfig.absoluteBaseUrl, tsconfig.paths); }