From 3639cbd0aefb9f2f7c63b6cb60bd49c71a1eb257 Mon Sep 17 00:00:00 2001 From: Evgeniy Timokhov Date: Wed, 19 May 2021 06:34:04 +0100 Subject: [PATCH] Backport #1317 to v8 (#1319) * Backport "Fixed impossibility to have several instances of ts-loader with different compiler options" #1317 to v8 * regen comparison tests --- CHANGELOG.md | 5 + package.json | 2 +- src/index.ts | 6 +- .../loaderOptionsCaching/app.ts | 2 + .../expectedOutput-4.1/bundle.js | 123 ++++++++++++++++++ .../expectedOutput-4.1/output.txt | 6 + .../expectedOutput-transpile-4.1/bundle.js | 123 ++++++++++++++++++ .../expectedOutput-transpile-4.1/output.txt | 6 + .../submodule-es5/index.ts | 2 + .../submodule-es5/tsconfig.json | 5 + .../submodule-es6/index.ts | 7 + .../submodule-es6/tsconfig.json | 5 + .../loaderOptionsCaching/tsconfig.json | 5 + .../loaderOptionsCaching/webpack.config.js | 32 +++++ 14 files changed, 326 insertions(+), 3 deletions(-) create mode 100644 test/comparison-tests/loaderOptionsCaching/app.ts create mode 100644 test/comparison-tests/loaderOptionsCaching/expectedOutput-4.1/bundle.js create mode 100644 test/comparison-tests/loaderOptionsCaching/expectedOutput-4.1/output.txt create mode 100644 test/comparison-tests/loaderOptionsCaching/expectedOutput-transpile-4.1/bundle.js create mode 100644 test/comparison-tests/loaderOptionsCaching/expectedOutput-transpile-4.1/output.txt create mode 100644 test/comparison-tests/loaderOptionsCaching/submodule-es5/index.ts create mode 100644 test/comparison-tests/loaderOptionsCaching/submodule-es5/tsconfig.json create mode 100644 test/comparison-tests/loaderOptionsCaching/submodule-es6/index.ts create mode 100644 test/comparison-tests/loaderOptionsCaching/submodule-es6/tsconfig.json create mode 100644 test/comparison-tests/loaderOptionsCaching/tsconfig.json create mode 100644 test/comparison-tests/loaderOptionsCaching/webpack.config.js diff --git a/CHANGELOG.md b/CHANGELOG.md index 4eebfdc47..810283ee5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Changelog +## v8.3.0 + +* [Fixed impossibility to have several instances of ts-loader with different compiler options](https://github.com/TypeStrong/ts-loader/issues/1316) - thanks @timocov +* This is a backport from v9.2.0 for webpack 4 compatibility + ## v8.2.0 * [Use caches for module resolution and type reference directives when using compiler default functions](https://github.com/TypeStrong/ts-loader/pull/1287) - thanks @sheetalkamat - uses: https://github.com/microsoft/TypeScript/pull/43700 diff --git a/package.json b/package.json index f4bdf0c9c..4b00904bf 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ts-loader", - "version": "8.2.0", + "version": "8.3.0", "description": "TypeScript loader for webpack", "main": "index.js", "types": "dist", diff --git a/src/index.ts b/src/index.ts index ac3502a2c..aaea09336 100644 --- a/src/index.ts +++ b/src/index.ts @@ -161,8 +161,10 @@ function getOptionsHash(loaderOptions: LoaderOptions) { const hash = crypto.createHash('sha256'); Object.keys(loaderOptions).forEach(key => { const value = loaderOptions[key]; - if (value) { - hash.update(key + value.toString()); + if (value !== undefined) { + const valueString = + typeof value === 'function' ? value.toString() : JSON.stringify(value); + hash.update(key + valueString); } }); return hash.digest('hex').substring(0, 16); diff --git a/test/comparison-tests/loaderOptionsCaching/app.ts b/test/comparison-tests/loaderOptionsCaching/app.ts new file mode 100644 index 000000000..fbb9debdf --- /dev/null +++ b/test/comparison-tests/loaderOptionsCaching/app.ts @@ -0,0 +1,2 @@ +import './submodule-es5'; +import './submodule-es6'; diff --git a/test/comparison-tests/loaderOptionsCaching/expectedOutput-4.1/bundle.js b/test/comparison-tests/loaderOptionsCaching/expectedOutput-4.1/bundle.js new file mode 100644 index 000000000..9dd55846a --- /dev/null +++ b/test/comparison-tests/loaderOptionsCaching/expectedOutput-4.1/bundle.js @@ -0,0 +1,123 @@ +/******/ (function(modules) { // webpackBootstrap +/******/ // 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; +/******/ } +/******/ +/******/ +/******/ // expose the modules object (__webpack_modules__) +/******/ __webpack_require__.m = modules; +/******/ +/******/ // expose the module cache +/******/ __webpack_require__.c = installedModules; +/******/ +/******/ // define getter function for harmony exports +/******/ __webpack_require__.d = function(exports, name, getter) { +/******/ if(!__webpack_require__.o(exports, name)) { +/******/ Object.defineProperty(exports, name, { enumerable: true, get: getter }); +/******/ } +/******/ }; +/******/ +/******/ // 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 }); +/******/ }; +/******/ +/******/ // create a fake namespace object +/******/ // mode & 1: value is a module id, require it +/******/ // mode & 2: merge all properties of value into the ns +/******/ // mode & 4: return value when already ns object +/******/ // mode & 8|1: behave like require +/******/ __webpack_require__.t = function(value, mode) { +/******/ if(mode & 1) value = __webpack_require__(value); +/******/ if(mode & 8) return value; +/******/ if((mode & 4) && typeof value === 'object' && value && value.__esModule) return value; +/******/ var ns = Object.create(null); +/******/ __webpack_require__.r(ns); +/******/ Object.defineProperty(ns, 'default', { enumerable: true, value: value }); +/******/ if(mode & 2 && typeof value != 'string') for(var key in value) __webpack_require__.d(ns, key, function(key) { return value[key]; }.bind(null, key)); +/******/ return ns; +/******/ }; +/******/ +/******/ // getDefaultExport function for compatibility with non-harmony modules +/******/ __webpack_require__.n = function(module) { +/******/ var getter = module && module.__esModule ? +/******/ function getDefault() { return module['default']; } : +/******/ function getModuleExports() { return module; }; +/******/ __webpack_require__.d(getter, 'a', getter); +/******/ return getter; +/******/ }; +/******/ +/******/ // Object.prototype.hasOwnProperty.call +/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); }; +/******/ +/******/ // __webpack_public_path__ +/******/ __webpack_require__.p = ""; +/******/ +/******/ +/******/ // Load entry module and return exports +/******/ return __webpack_require__(__webpack_require__.s = "./app.ts"); +/******/ }) +/************************************************************************/ +/******/ ({ + +/***/ "./app.ts": +/*!****************!*\ + !*** ./app.ts ***! + \****************/ +/*! no exports provided */ +/***/ (function(module, __webpack_exports__, __webpack_require__) { + +"use strict"; +eval("__webpack_require__.r(__webpack_exports__);\n/* harmony import */ var _submodule_es5__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./submodule-es5 */ \"./submodule-es5/index.ts\");\n/* harmony import */ var _submodule_es5__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(_submodule_es5__WEBPACK_IMPORTED_MODULE_0__);\n/* harmony import */ var _submodule_es6__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./submodule-es6 */ \"./submodule-es6/index.ts\");\n/* harmony import */ var _submodule_es6__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__webpack_require__.n(_submodule_es6__WEBPACK_IMPORTED_MODULE_1__);\n\n\n\n\n//# sourceURL=webpack:///./app.ts?"); + +/***/ }), + +/***/ "./submodule-es5/index.ts": +/*!********************************!*\ + !*** ./submodule-es5/index.ts ***! + \********************************/ +/*! no static exports found */ +/***/ (function(module, exports) { + +eval("var string = 'Hello from es5 file';\r\nconsole.log(string);\r\n\n\n//# sourceURL=webpack:///./submodule-es5/index.ts?"); + +/***/ }), + +/***/ "./submodule-es6/index.ts": +/*!********************************!*\ + !*** ./submodule-es6/index.ts ***! + \********************************/ +/*! no static exports found */ +/***/ (function(module, exports) { + +eval("const set = new Set([42]);\r\nfor (const value of set) {\r\n console.log(value);\r\n}\r\nconst string = 'Hello from es6 file';\r\nconsole.log(string);\r\n\n\n//# sourceURL=webpack:///./submodule-es6/index.ts?"); + +/***/ }) + +/******/ }); \ No newline at end of file diff --git a/test/comparison-tests/loaderOptionsCaching/expectedOutput-4.1/output.txt b/test/comparison-tests/loaderOptionsCaching/expectedOutput-4.1/output.txt new file mode 100644 index 000000000..48c547f8d --- /dev/null +++ b/test/comparison-tests/loaderOptionsCaching/expectedOutput-4.1/output.txt @@ -0,0 +1,6 @@ + Asset Size Chunks Chunk Names +bundle.js 5.21 KiB main [emitted] main +Entrypoint main = bundle.js +[./app.ts] 52 bytes {main} [built] +[./submodule-es5/index.ts] 59 bytes {main} [built] +[./submodule-es6/index.ts] 145 bytes {main} [built] \ No newline at end of file diff --git a/test/comparison-tests/loaderOptionsCaching/expectedOutput-transpile-4.1/bundle.js b/test/comparison-tests/loaderOptionsCaching/expectedOutput-transpile-4.1/bundle.js new file mode 100644 index 000000000..9dd55846a --- /dev/null +++ b/test/comparison-tests/loaderOptionsCaching/expectedOutput-transpile-4.1/bundle.js @@ -0,0 +1,123 @@ +/******/ (function(modules) { // webpackBootstrap +/******/ // 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; +/******/ } +/******/ +/******/ +/******/ // expose the modules object (__webpack_modules__) +/******/ __webpack_require__.m = modules; +/******/ +/******/ // expose the module cache +/******/ __webpack_require__.c = installedModules; +/******/ +/******/ // define getter function for harmony exports +/******/ __webpack_require__.d = function(exports, name, getter) { +/******/ if(!__webpack_require__.o(exports, name)) { +/******/ Object.defineProperty(exports, name, { enumerable: true, get: getter }); +/******/ } +/******/ }; +/******/ +/******/ // 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 }); +/******/ }; +/******/ +/******/ // create a fake namespace object +/******/ // mode & 1: value is a module id, require it +/******/ // mode & 2: merge all properties of value into the ns +/******/ // mode & 4: return value when already ns object +/******/ // mode & 8|1: behave like require +/******/ __webpack_require__.t = function(value, mode) { +/******/ if(mode & 1) value = __webpack_require__(value); +/******/ if(mode & 8) return value; +/******/ if((mode & 4) && typeof value === 'object' && value && value.__esModule) return value; +/******/ var ns = Object.create(null); +/******/ __webpack_require__.r(ns); +/******/ Object.defineProperty(ns, 'default', { enumerable: true, value: value }); +/******/ if(mode & 2 && typeof value != 'string') for(var key in value) __webpack_require__.d(ns, key, function(key) { return value[key]; }.bind(null, key)); +/******/ return ns; +/******/ }; +/******/ +/******/ // getDefaultExport function for compatibility with non-harmony modules +/******/ __webpack_require__.n = function(module) { +/******/ var getter = module && module.__esModule ? +/******/ function getDefault() { return module['default']; } : +/******/ function getModuleExports() { return module; }; +/******/ __webpack_require__.d(getter, 'a', getter); +/******/ return getter; +/******/ }; +/******/ +/******/ // Object.prototype.hasOwnProperty.call +/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); }; +/******/ +/******/ // __webpack_public_path__ +/******/ __webpack_require__.p = ""; +/******/ +/******/ +/******/ // Load entry module and return exports +/******/ return __webpack_require__(__webpack_require__.s = "./app.ts"); +/******/ }) +/************************************************************************/ +/******/ ({ + +/***/ "./app.ts": +/*!****************!*\ + !*** ./app.ts ***! + \****************/ +/*! no exports provided */ +/***/ (function(module, __webpack_exports__, __webpack_require__) { + +"use strict"; +eval("__webpack_require__.r(__webpack_exports__);\n/* harmony import */ var _submodule_es5__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./submodule-es5 */ \"./submodule-es5/index.ts\");\n/* harmony import */ var _submodule_es5__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(_submodule_es5__WEBPACK_IMPORTED_MODULE_0__);\n/* harmony import */ var _submodule_es6__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./submodule-es6 */ \"./submodule-es6/index.ts\");\n/* harmony import */ var _submodule_es6__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__webpack_require__.n(_submodule_es6__WEBPACK_IMPORTED_MODULE_1__);\n\n\n\n\n//# sourceURL=webpack:///./app.ts?"); + +/***/ }), + +/***/ "./submodule-es5/index.ts": +/*!********************************!*\ + !*** ./submodule-es5/index.ts ***! + \********************************/ +/*! no static exports found */ +/***/ (function(module, exports) { + +eval("var string = 'Hello from es5 file';\r\nconsole.log(string);\r\n\n\n//# sourceURL=webpack:///./submodule-es5/index.ts?"); + +/***/ }), + +/***/ "./submodule-es6/index.ts": +/*!********************************!*\ + !*** ./submodule-es6/index.ts ***! + \********************************/ +/*! no static exports found */ +/***/ (function(module, exports) { + +eval("const set = new Set([42]);\r\nfor (const value of set) {\r\n console.log(value);\r\n}\r\nconst string = 'Hello from es6 file';\r\nconsole.log(string);\r\n\n\n//# sourceURL=webpack:///./submodule-es6/index.ts?"); + +/***/ }) + +/******/ }); \ No newline at end of file diff --git a/test/comparison-tests/loaderOptionsCaching/expectedOutput-transpile-4.1/output.txt b/test/comparison-tests/loaderOptionsCaching/expectedOutput-transpile-4.1/output.txt new file mode 100644 index 000000000..48c547f8d --- /dev/null +++ b/test/comparison-tests/loaderOptionsCaching/expectedOutput-transpile-4.1/output.txt @@ -0,0 +1,6 @@ + Asset Size Chunks Chunk Names +bundle.js 5.21 KiB main [emitted] main +Entrypoint main = bundle.js +[./app.ts] 52 bytes {main} [built] +[./submodule-es5/index.ts] 59 bytes {main} [built] +[./submodule-es6/index.ts] 145 bytes {main} [built] \ No newline at end of file diff --git a/test/comparison-tests/loaderOptionsCaching/submodule-es5/index.ts b/test/comparison-tests/loaderOptionsCaching/submodule-es5/index.ts new file mode 100644 index 000000000..23c844e8a --- /dev/null +++ b/test/comparison-tests/loaderOptionsCaching/submodule-es5/index.ts @@ -0,0 +1,2 @@ +const string = 'Hello from es5 file'; +console.log(string); diff --git a/test/comparison-tests/loaderOptionsCaching/submodule-es5/tsconfig.json b/test/comparison-tests/loaderOptionsCaching/submodule-es5/tsconfig.json new file mode 100644 index 000000000..fa3e10a3d --- /dev/null +++ b/test/comparison-tests/loaderOptionsCaching/submodule-es5/tsconfig.json @@ -0,0 +1,5 @@ +{ + "compilerOptions": { + "target": "es5" + } +} diff --git a/test/comparison-tests/loaderOptionsCaching/submodule-es6/index.ts b/test/comparison-tests/loaderOptionsCaching/submodule-es6/index.ts new file mode 100644 index 000000000..f59c741a4 --- /dev/null +++ b/test/comparison-tests/loaderOptionsCaching/submodule-es6/index.ts @@ -0,0 +1,7 @@ +const set = new Set([42]); +for (const value of set) { + console.log(value); +} + +const string = 'Hello from es6 file'; +console.log(string); diff --git a/test/comparison-tests/loaderOptionsCaching/submodule-es6/tsconfig.json b/test/comparison-tests/loaderOptionsCaching/submodule-es6/tsconfig.json new file mode 100644 index 000000000..ea71f9415 --- /dev/null +++ b/test/comparison-tests/loaderOptionsCaching/submodule-es6/tsconfig.json @@ -0,0 +1,5 @@ +{ + "compilerOptions": { + "target": "es6" + } +} \ No newline at end of file diff --git a/test/comparison-tests/loaderOptionsCaching/tsconfig.json b/test/comparison-tests/loaderOptionsCaching/tsconfig.json new file mode 100644 index 000000000..fa3e10a3d --- /dev/null +++ b/test/comparison-tests/loaderOptionsCaching/tsconfig.json @@ -0,0 +1,5 @@ +{ + "compilerOptions": { + "target": "es5" + } +} diff --git a/test/comparison-tests/loaderOptionsCaching/webpack.config.js b/test/comparison-tests/loaderOptionsCaching/webpack.config.js new file mode 100644 index 000000000..82a2f161b --- /dev/null +++ b/test/comparison-tests/loaderOptionsCaching/webpack.config.js @@ -0,0 +1,32 @@ +module.exports = { + mode: 'development', + entry: './app.ts', + output: { + filename: 'bundle.js' + }, + resolve: { + extensions: ['.ts'] + }, + module: { + rules: [ + { + test: /submodule-es6.*\.ts$/, + loader: 'ts-loader', + options: { + compilerOptions: { + target: 'es6', + }, + }, + }, + { + test: /submodule-es5.*\.ts$/, + loader: 'ts-loader', + options: { + compilerOptions: { + target: 'es5', + }, + }, + } + ] + } +}