Skip to content

Commit c8e3fec

Browse files
Jon Wallstenjohnnyreilly
authored andcommitted
Added support for TypeScript declaration map (#821)
* Added support for TypeScript declaration map * Generated test output for new tests * Renamed regex to more verbose
1 parent 2c661e0 commit c8e3fec

File tree

16 files changed

+187
-11
lines changed

16 files changed

+187
-11
lines changed

src/after-compile.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import * as path from 'path';
2-
32
import { collectAllDependants, formatErrors } from './utils';
43
import * as constants from './constants';
54
import {
@@ -246,10 +245,11 @@ function provideDeclarationFilesToWebpack(
246245
}
247246

248247
const outputFiles = getEmitOutput(instance, filePath);
249-
const declarationFile = outputFiles
250-
.filter(outputFile => outputFile.name.match(constants.dtsDtsxRegex))
251-
.pop();
252-
if (declarationFile !== undefined) {
248+
const declarationFiles = outputFiles.filter(outputFile =>
249+
outputFile.name.match(constants.dtsDtsxOrDtsDtsxMapRegex)
250+
);
251+
252+
declarationFiles.forEach(declarationFile => {
253253
const assetPath = path.relative(
254254
compilation.compiler.context,
255255
declarationFile.name
@@ -258,7 +258,7 @@ function provideDeclarationFilesToWebpack(
258258
source: () => declarationFile.text,
259259
size: () => declarationFile.text.length
260260
};
261-
}
261+
});
262262
}
263263
}
264264

src/constants.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export const ScriptTargetES2015 = 2;
1212
export const ModuleKindCommonJs = 1;
1313

1414
export const tsTsxRegex = /\.ts(x?)$/i;
15-
export const dtsDtsxRegex = /\.d\.ts(x?)$/i;
15+
export const dtsDtsxOrDtsDtsxMapRegex = /\.d\.ts(x?)(\.map)?$/i;
1616
export const dtsTsTsxRegex = /(\.d)?\.ts(x?)$/i;
1717
export const dtsTsTsxJsJsxRegex = /((\.d)?\.ts(x?)|js(x?))$/i;
1818
export const tsTsxJsJsxRegex = /\.tsx?$|\.jsx?$/i;

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ function getEmit(
291291
loader.addDependency(rawFilePath);
292292

293293
const allDefinitionFiles = [...instance.files.keys()].filter(defFilePath =>
294-
defFilePath.match(constants.dtsDtsxRegex)
294+
defFilePath.match(constants.dtsDtsxOrDtsDtsxMapRegex)
295295
);
296296

297297
// Make this file dependent on *all* definition files in the program

src/instances.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import chalk, { Chalk } from 'chalk';
55

66
import { makeAfterCompile } from './after-compile';
77
import { getConfigFile, getConfigParseResult } from './config';
8-
import { EOL, dtsDtsxRegex } from './constants';
8+
import { EOL, dtsDtsxOrDtsDtsxMapRegex } from './constants';
99
import { getCompilerOptions, getCompiler } from './compilerSetup';
1010
import { makeError, formatErrors } from './utils';
1111
import * as logger from './logger';
@@ -206,7 +206,7 @@ function successfulTypeScriptInstance(
206206
try {
207207
const filesToLoad = loaderOptions.onlyCompileBundledFiles
208208
? configParseResult.fileNames.filter(fileName =>
209-
dtsDtsxRegex.test(fileName)
209+
dtsDtsxOrDtsDtsxMapRegex.test(fileName)
210210
)
211211
: configParseResult.fileNames;
212212
filesToLoad.forEach(filePath => {

src/watch-run.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export function makeWatchRun(instance: TSInstance) {
3636
// (skip @types/* and modules with typings)
3737
for (const filePath of instance.files.keys()) {
3838
if (
39-
filePath.match(constants.dtsDtsxRegex) &&
39+
filePath.match(constants.dtsDtsxOrDtsDtsxMapRegex) &&
4040
!filePath.match(constants.nodeModules)
4141
) {
4242
updateFile(instance, filePath);

test/comparison-tests/create-and-execute-test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ if (fs.statSync(testPath).isDirectory() &&
4848
it('should have the correct output', createTest(testToRun, testPath, {}));
4949

5050
if (testToRun === 'declarationOutput' ||
51+
testToRun === 'declarationOutputWithMaps' ||
5152
testToRun === 'importsWatch' ||
5253
testToRun === 'declarationWatch' ||
5354
testToRun === 'issue71' ||
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import dep = require('./sub/dep');
2+
3+
class Test extends dep {
4+
doSomething() {
5+
6+
}
7+
}
8+
9+
export = Test;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import dep = require('./sub/dep');
2+
declare class Test extends dep {
3+
doSomething(): void;
4+
}
5+
export = Test;
6+
//# sourceMappingURL=app.d.ts.map

test/comparison-tests/declarationOutputWithMaps/expectedOutput-3.0/app.d.ts.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/******/ (function(modules) { // webpackBootstrap
2+
/******/ // The module cache
3+
/******/ var installedModules = {};
4+
/******/
5+
/******/ // The require function
6+
/******/ function __webpack_require__(moduleId) {
7+
/******/
8+
/******/ // Check if module is in cache
9+
/******/ if(installedModules[moduleId]) {
10+
/******/ return installedModules[moduleId].exports;
11+
/******/ }
12+
/******/ // Create a new module (and put it into the cache)
13+
/******/ var module = installedModules[moduleId] = {
14+
/******/ i: moduleId,
15+
/******/ l: false,
16+
/******/ exports: {}
17+
/******/ };
18+
/******/
19+
/******/ // Execute the module function
20+
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
21+
/******/
22+
/******/ // Flag the module as loaded
23+
/******/ module.l = true;
24+
/******/
25+
/******/ // Return the exports of the module
26+
/******/ return module.exports;
27+
/******/ }
28+
/******/
29+
/******/
30+
/******/ // expose the modules object (__webpack_modules__)
31+
/******/ __webpack_require__.m = modules;
32+
/******/
33+
/******/ // expose the module cache
34+
/******/ __webpack_require__.c = installedModules;
35+
/******/
36+
/******/ // define getter function for harmony exports
37+
/******/ __webpack_require__.d = function(exports, name, getter) {
38+
/******/ if(!__webpack_require__.o(exports, name)) {
39+
/******/ Object.defineProperty(exports, name, { enumerable: true, get: getter });
40+
/******/ }
41+
/******/ };
42+
/******/
43+
/******/ // define __esModule on exports
44+
/******/ __webpack_require__.r = function(exports) {
45+
/******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
46+
/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
47+
/******/ }
48+
/******/ Object.defineProperty(exports, '__esModule', { value: true });
49+
/******/ };
50+
/******/
51+
/******/ // create a fake namespace object
52+
/******/ // mode & 1: value is a module id, require it
53+
/******/ // mode & 2: merge all properties of value into the ns
54+
/******/ // mode & 4: return value when already ns object
55+
/******/ // mode & 8|1: behave like require
56+
/******/ __webpack_require__.t = function(value, mode) {
57+
/******/ if(mode & 1) value = __webpack_require__(value);
58+
/******/ if(mode & 8) return value;
59+
/******/ if((mode & 4) && typeof value === 'object' && value && value.__esModule) return value;
60+
/******/ var ns = Object.create(null);
61+
/******/ __webpack_require__.r(ns);
62+
/******/ Object.defineProperty(ns, 'default', { enumerable: true, value: value });
63+
/******/ if(mode & 2 && typeof value != 'string') for(var key in value) __webpack_require__.d(ns, key, function(key) { return value[key]; }.bind(null, key));
64+
/******/ return ns;
65+
/******/ };
66+
/******/
67+
/******/ // getDefaultExport function for compatibility with non-harmony modules
68+
/******/ __webpack_require__.n = function(module) {
69+
/******/ var getter = module && module.__esModule ?
70+
/******/ function getDefault() { return module['default']; } :
71+
/******/ function getModuleExports() { return module; };
72+
/******/ __webpack_require__.d(getter, 'a', getter);
73+
/******/ return getter;
74+
/******/ };
75+
/******/
76+
/******/ // Object.prototype.hasOwnProperty.call
77+
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
78+
/******/
79+
/******/ // __webpack_public_path__
80+
/******/ __webpack_require__.p = "";
81+
/******/
82+
/******/
83+
/******/ // Load entry module and return exports
84+
/******/ return __webpack_require__(__webpack_require__.s = "./app.ts");
85+
/******/ })
86+
/************************************************************************/
87+
/******/ ({
88+
89+
/***/ "./app.ts":
90+
/*!****************!*\
91+
!*** ./app.ts ***!
92+
\****************/
93+
/*! no static exports found */
94+
/***/ (function(module, exports, __webpack_require__) {
95+
96+
"use strict";
97+
eval("\nvar __extends = (this && this.__extends) || (function () {\n var extendStatics = function (d, b) {\n extendStatics = Object.setPrototypeOf ||\n ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||\n function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };\n return extendStatics(d, b);\n }\n return function (d, b) {\n extendStatics(d, b);\n function __() { this.constructor = d; }\n d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());\n };\n})();\nvar dep = __webpack_require__(/*! ./sub/dep */ \"./sub/dep.ts\");\nvar Test = /** @class */ (function (_super) {\n __extends(Test, _super);\n function Test() {\n return _super !== null && _super.apply(this, arguments) || this;\n }\n Test.prototype.doSomething = function () {\n };\n return Test;\n}(dep));\nmodule.exports = Test;\n\n\n//# sourceURL=webpack:///./app.ts?");
98+
99+
/***/ }),
100+
101+
/***/ "./sub/dep.ts":
102+
/*!********************!*\
103+
!*** ./sub/dep.ts ***!
104+
\********************/
105+
/*! no static exports found */
106+
/***/ (function(module, exports, __webpack_require__) {
107+
108+
"use strict";
109+
eval("\nvar Test = /** @class */ (function () {\n function Test() {\n }\n Test.prototype.doSomething = function () {\n };\n return Test;\n}());\nmodule.exports = Test;\n\n\n//# sourceURL=webpack:///./sub/dep.ts?");
110+
111+
/***/ })
112+
113+
/******/ });
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Asset Size Chunks Chunk Names
2+
bundle.js 5.13 KiB main [emitted] main
3+
app.d.ts.map 194 bytes [emitted]
4+
app.d.ts 143 bytes [emitted]
5+
sub/dep.d.ts.map 142 bytes [emitted]
6+
sub/dep.d.ts 96 bytes [emitted]
7+
Entrypoint main = bundle.js
8+
[./app.ts] 910 bytes {main} [built]
9+
[./sub/dep.ts] 182 bytes {main} [built]
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
declare class Test {
2+
doSomething(): void;
3+
}
4+
export = Test;
5+
//# sourceMappingURL=dep.d.ts.map

test/comparison-tests/declarationOutputWithMaps/expectedOutput-3.0/sub/dep.d.ts.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
class Test {
3+
doSomething() {
4+
5+
}
6+
}
7+
8+
export = Test;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"compilerOptions": {
3+
"declaration": true,
4+
"declarationMap": true
5+
}
6+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
}
16+
17+

0 commit comments

Comments
 (0)