Skip to content

Commit

Permalink
Added support for TypeScript declaration map (#821)
Browse files Browse the repository at this point in the history
* Added support for TypeScript declaration map
* Generated test output for new tests
* Renamed regex to more verbose
  • Loading branch information
Jon Wallsten authored and johnnyreilly committed Aug 17, 2018
1 parent 2c661e0 commit c8e3fec
Show file tree
Hide file tree
Showing 16 changed files with 187 additions and 11 deletions.
12 changes: 6 additions & 6 deletions src/after-compile.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as path from 'path';

import { collectAllDependants, formatErrors } from './utils';
import * as constants from './constants';
import {
Expand Down Expand Up @@ -246,10 +245,11 @@ function provideDeclarationFilesToWebpack(
}

const outputFiles = getEmitOutput(instance, filePath);
const declarationFile = outputFiles
.filter(outputFile => outputFile.name.match(constants.dtsDtsxRegex))
.pop();
if (declarationFile !== undefined) {
const declarationFiles = outputFiles.filter(outputFile =>
outputFile.name.match(constants.dtsDtsxOrDtsDtsxMapRegex)
);

declarationFiles.forEach(declarationFile => {
const assetPath = path.relative(
compilation.compiler.context,
declarationFile.name
Expand All @@ -258,7 +258,7 @@ function provideDeclarationFilesToWebpack(
source: () => declarationFile.text,
size: () => declarationFile.text.length
};
}
});
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const ScriptTargetES2015 = 2;
export const ModuleKindCommonJs = 1;

export const tsTsxRegex = /\.ts(x?)$/i;
export const dtsDtsxRegex = /\.d\.ts(x?)$/i;
export const dtsDtsxOrDtsDtsxMapRegex = /\.d\.ts(x?)(\.map)?$/i;
export const dtsTsTsxRegex = /(\.d)?\.ts(x?)$/i;
export const dtsTsTsxJsJsxRegex = /((\.d)?\.ts(x?)|js(x?))$/i;
export const tsTsxJsJsxRegex = /\.tsx?$|\.jsx?$/i;
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ function getEmit(
loader.addDependency(rawFilePath);

const allDefinitionFiles = [...instance.files.keys()].filter(defFilePath =>
defFilePath.match(constants.dtsDtsxRegex)
defFilePath.match(constants.dtsDtsxOrDtsDtsxMapRegex)
);

// Make this file dependent on *all* definition files in the program
Expand Down
4 changes: 2 additions & 2 deletions src/instances.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import chalk, { Chalk } from 'chalk';

import { makeAfterCompile } from './after-compile';
import { getConfigFile, getConfigParseResult } from './config';
import { EOL, dtsDtsxRegex } from './constants';
import { EOL, dtsDtsxOrDtsDtsxMapRegex } from './constants';
import { getCompilerOptions, getCompiler } from './compilerSetup';
import { makeError, formatErrors } from './utils';
import * as logger from './logger';
Expand Down Expand Up @@ -206,7 +206,7 @@ function successfulTypeScriptInstance(
try {
const filesToLoad = loaderOptions.onlyCompileBundledFiles
? configParseResult.fileNames.filter(fileName =>
dtsDtsxRegex.test(fileName)
dtsDtsxOrDtsDtsxMapRegex.test(fileName)
)
: configParseResult.fileNames;
filesToLoad.forEach(filePath => {
Expand Down
2 changes: 1 addition & 1 deletion src/watch-run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export function makeWatchRun(instance: TSInstance) {
// (skip @types/* and modules with typings)
for (const filePath of instance.files.keys()) {
if (
filePath.match(constants.dtsDtsxRegex) &&
filePath.match(constants.dtsDtsxOrDtsDtsxMapRegex) &&
!filePath.match(constants.nodeModules)
) {
updateFile(instance, filePath);
Expand Down
1 change: 1 addition & 0 deletions test/comparison-tests/create-and-execute-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ if (fs.statSync(testPath).isDirectory() &&
it('should have the correct output', createTest(testToRun, testPath, {}));

if (testToRun === 'declarationOutput' ||
testToRun === 'declarationOutputWithMaps' ||
testToRun === 'importsWatch' ||
testToRun === 'declarationWatch' ||
testToRun === 'issue71' ||
Expand Down
9 changes: 9 additions & 0 deletions test/comparison-tests/declarationOutputWithMaps/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import dep = require('./sub/dep');

class Test extends dep {
doSomething() {

}
}

export = Test;
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import dep = require('./sub/dep');
declare class Test extends dep {
doSomething(): void;
}
export = Test;
//# sourceMappingURL=app.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/******/ (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 static exports found */
/***/ (function(module, exports, __webpack_require__) {

"use strict";
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?");

/***/ }),

/***/ "./sub/dep.ts":
/*!********************!*\
!*** ./sub/dep.ts ***!
\********************/
/*! no static exports found */
/***/ (function(module, exports, __webpack_require__) {

"use strict";
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?");

/***/ })

/******/ });
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Asset Size Chunks Chunk Names
bundle.js 5.13 KiB main [emitted] main
app.d.ts.map 194 bytes [emitted]
app.d.ts 143 bytes [emitted]
sub/dep.d.ts.map 142 bytes [emitted]
sub/dep.d.ts 96 bytes [emitted]
Entrypoint main = bundle.js
[./app.ts] 910 bytes {main} [built]
[./sub/dep.ts] 182 bytes {main} [built]
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
declare class Test {
doSomething(): void;
}
export = Test;
//# sourceMappingURL=dep.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions test/comparison-tests/declarationOutputWithMaps/sub/dep.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

class Test {
doSomething() {

}
}

export = Test;
6 changes: 6 additions & 0 deletions test/comparison-tests/declarationOutputWithMaps/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"compilerOptions": {
"declaration": true,
"declarationMap": true
}
}
17 changes: 17 additions & 0 deletions test/comparison-tests/declarationOutputWithMaps/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module.exports = {
mode: 'development',
entry: './app.ts',
output: {
filename: 'bundle.js'
},
resolve: {
extensions: ['.ts', '.js']
},
module: {
rules: [
{ test: /\.ts$/, loader: 'ts-loader' }
]
}
}


0 comments on commit c8e3fec

Please sign in to comment.