Skip to content

Commit b4b0363

Browse files
mvilligerhedietjohnnyreilly
authored
Generate declaration files for js files if allowJs is set to true (#1483)
* Fixes #1260 by generating declaration files for js files if allowJs is set to true. * Fix declarationOutputAllowJs test Update expected test output for 4.7 and stabilize expectation to be OS independent. * Update package.json and CHANGELOG.md for 9.3.1 * Update CHANGELOG.md Co-authored-by: John Reilly <[email protected]> Co-authored-by: Henning Dieterichs <[email protected]> Co-authored-by: John Reilly <[email protected]>
1 parent 914cdae commit b4b0363

File tree

13 files changed

+194
-3
lines changed

13 files changed

+194
-3
lines changed

Diff for: CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## v9.3.1
4+
5+
* [Bug fix: Generate declaration files for js files if allowJs is set to true](https://github.com/TypeStrong/ts-loader/pull/1483) [#1260] - thanks @hediet and @mvilliger
6+
37
## v9.3.0
48

59
* [simplify configuration for fork-ts-checker-webpack-plugin](https://github.com/TypeStrong/ts-loader/pull/1451) - thanks @piotr-oles

Diff for: package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ts-loader",
3-
"version": "9.3.0",
3+
"version": "9.3.1",
44
"description": "TypeScript loader for webpack",
55
"main": "index.js",
66
"types": "dist",

Diff for: src/after-compile.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -355,15 +355,21 @@ function provideSolutionErrorsToWebpack(
355355
}
356356

357357
/**
358-
* gather all declaration files from TypeScript and output them to webpack
358+
* gather all declaration files from TypeScript and output them to webpack.
359+
* JavaScript declaration files are included if `allowJs` is set.
359360
*/
360361
function provideDeclarationFilesToWebpack(
361362
filesToCheckForErrors: TSFiles,
362363
instance: TSInstance,
363364
compilation: webpack.Compilation
364365
) {
366+
const filePathRegex =
367+
instance.compilerOptions.allowJs === true
368+
? constants.dtsTsTsxJsJsxRegex
369+
: constants.dtsTsTsxRegex;
370+
365371
for (const { fileName } of filesToCheckForErrors.values()) {
366-
if (fileName.match(constants.tsTsxRegex) === null) {
372+
if (fileName.match(filePathRegex) === null) {
367373
continue;
368374
}
369375

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import dep = require('./sub/dep');
2+
3+
class Test {
4+
private _field?: dep;
5+
}
6+
7+
export = Test;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
declare class Test {
2+
private _field?;
3+
}
4+
export = Test;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export class Test {
2+
/**
3+
* @param {number} x
4+
*/
5+
doSomething(x: number): number;
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* ATTENTION: The "eval" devtool has been used (maybe by default in mode: "development").
3+
* This devtool is neither made for production nor for readable output files.
4+
* It uses "eval()" calls to create a separate source file in the browser devtools.
5+
* If you are trying to read the output file, select a different devtool (https://webpack.js.org/configuration/devtool/)
6+
* or disable the default devtool with "devtool: false".
7+
* If you are looking for production-ready output files, see mode: "production" (https://webpack.js.org/configuration/mode/).
8+
*/
9+
/******/ (() => { // webpackBootstrap
10+
/******/ "use strict";
11+
/******/ var __webpack_modules__ = ({
12+
13+
/***/ "./app.ts":
14+
/*!****************!*\
15+
!*** ./app.ts ***!
16+
\****************/
17+
/***/ ((module) => {
18+
19+
eval("\nvar Test = /** @class */ (function () {\n function Test() {\n }\n return Test;\n}());\nmodule.exports = Test;\n\n\n//# sourceURL=webpack:///./app.ts?");
20+
21+
/***/ })
22+
23+
/******/ });
24+
/************************************************************************/
25+
/******/ // The module cache
26+
/******/ var __webpack_module_cache__ = {};
27+
/******/
28+
/******/ // The require function
29+
/******/ function __webpack_require__(moduleId) {
30+
/******/ // Check if module is in cache
31+
/******/ var cachedModule = __webpack_module_cache__[moduleId];
32+
/******/ if (cachedModule !== undefined) {
33+
/******/ return cachedModule.exports;
34+
/******/ }
35+
/******/ // Create a new module (and put it into the cache)
36+
/******/ var module = __webpack_module_cache__[moduleId] = {
37+
/******/ // no module.id needed
38+
/******/ // no module.loaded needed
39+
/******/ exports: {}
40+
/******/ };
41+
/******/
42+
/******/ // Execute the module function
43+
/******/ __webpack_modules__[moduleId](module, module.exports, __webpack_require__);
44+
/******/
45+
/******/ // Return the exports of the module
46+
/******/ return module.exports;
47+
/******/ }
48+
/******/
49+
/************************************************************************/
50+
/******/
51+
/******/ // startup
52+
/******/ // Load entry module and return exports
53+
/******/ // This entry module is referenced by other modules so it can't be inlined
54+
/******/ var __webpack_exports__ = __webpack_require__("./app.ts");
55+
/******/
56+
/******/ })()
57+
;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
asset bundle.js 2.19 KiB [emitted] (name: main)
2+
asset .output/sub/dep.d.ts 99 bytes [emitted]
3+
asset .output/app.d.ts 59 bytes [emitted]
4+
./app.ts 128 bytes [built] [code generated] [1 error]
5+
6+
ERROR in app.ts
7+
./app.ts 4:18-21
8+
[tsl] ERROR in app.ts(4,19)
9+
 TS2709: Cannot use namespace 'dep' as a type.
10+
ts-loader-default_609318b4f68865d3
11+
12+
webpack compiled with 1 error
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* ATTENTION: The "eval" devtool has been used (maybe by default in mode: "development").
3+
* This devtool is neither made for production nor for readable output files.
4+
* It uses "eval()" calls to create a separate source file in the browser devtools.
5+
* If you are trying to read the output file, select a different devtool (https://webpack.js.org/configuration/devtool/)
6+
* or disable the default devtool with "devtool: false".
7+
* If you are looking for production-ready output files, see mode: "production" (https://webpack.js.org/configuration/mode/).
8+
*/
9+
/******/ (() => { // webpackBootstrap
10+
/******/ "use strict";
11+
/******/ var __webpack_modules__ = ({
12+
13+
/***/ "./app.ts":
14+
/*!****************!*\
15+
!*** ./app.ts ***!
16+
\****************/
17+
/***/ ((module) => {
18+
19+
eval("\nvar Test = /** @class */ (function () {\n function Test() {\n }\n return Test;\n}());\nmodule.exports = Test;\n\n\n//# sourceURL=webpack:///./app.ts?");
20+
21+
/***/ })
22+
23+
/******/ });
24+
/************************************************************************/
25+
/******/ // The module cache
26+
/******/ var __webpack_module_cache__ = {};
27+
/******/
28+
/******/ // The require function
29+
/******/ function __webpack_require__(moduleId) {
30+
/******/ // Check if module is in cache
31+
/******/ var cachedModule = __webpack_module_cache__[moduleId];
32+
/******/ if (cachedModule !== undefined) {
33+
/******/ return cachedModule.exports;
34+
/******/ }
35+
/******/ // Create a new module (and put it into the cache)
36+
/******/ var module = __webpack_module_cache__[moduleId] = {
37+
/******/ // no module.id needed
38+
/******/ // no module.loaded needed
39+
/******/ exports: {}
40+
/******/ };
41+
/******/
42+
/******/ // Execute the module function
43+
/******/ __webpack_modules__[moduleId](module, module.exports, __webpack_require__);
44+
/******/
45+
/******/ // Return the exports of the module
46+
/******/ return module.exports;
47+
/******/ }
48+
/******/
49+
/************************************************************************/
50+
/******/
51+
/******/ // startup
52+
/******/ // Load entry module and return exports
53+
/******/ // This entry module is referenced by other modules so it can't be inlined
54+
/******/ var __webpack_exports__ = __webpack_require__("./app.ts");
55+
/******/
56+
/******/ })()
57+
;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
asset bundle.js 2.19 KiB [emitted] (name: main)
2+
./app.ts 128 bytes [built] [code generated]
3+
webpack compiled successfully
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
export class Test {
3+
/**
4+
* @param {number} x
5+
*/
6+
doSomething(x) {
7+
return x;
8+
}
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"compilerOptions": {
3+
"declaration": true,
4+
"declarationDir": ".output",
5+
"allowJs": true
6+
},
7+
"files": [
8+
"./app.ts",
9+
"./sub/dep.js"
10+
]
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module.exports = {
2+
mode: 'development',
3+
entry: './app.ts',
4+
output: {
5+
filename: 'bundle.js'
6+
},
7+
resolve: {
8+
extensions: ['.ts', '.js']
9+
},
10+
module: {
11+
rules: [
12+
{ test: /\.ts$/, loader: 'ts-loader' }
13+
]
14+
}
15+
}

0 commit comments

Comments
 (0)