Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for TypeScript declaration map #821

Merged
merged 4 commits into from
Aug 17, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 15 additions & 13 deletions src/after-compile.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as path from 'path';

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

const outputFiles = getEmitOutput(instance, filePath);
const declarationFile = outputFiles
.filter(outputFile => outputFile.name.match(constants.dtsDtsxRegex))
.pop();
if (declarationFile !== undefined) {
const assetPath = path.relative(
compilation.compiler.context,
declarationFile.name
);
compilation.assets[assetPath] = {
source: () => declarationFile.text,
size: () => declarationFile.text.length
};
const declarationFiles: typescript.OutputFile[] = outputFiles
.filter((outputFile: typescript.OutputFile) => outputFile.name.match(constants.dtsDtsxRegex));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can (outputFile: typescript.OutputFile) just be outputFile? Again type inference should work

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, you are correct. I reinstalled VS Code, and it works now. I'm removing these.


if (Array.isArray(declarationFiles)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if you could tell me about this part some more? This is the crux of the PR really; previously the code assumed a single d.ts file. Now it's multiple; presumably that's allowing for the map files as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, exactly! Due to the regex change, getEmitOutput will return both d.ts and d.ts.map when declarationMap is true. It felt logical to handle declaration & map files together since they are linked together inside the d.ts file, and besides the map file can't live without the declaration file. When it comes to performance loss for using a forEach instead of a pop() when it's only one should not a problem since we're not using a million and millions of files (i hope).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is Array.isArray the right thing to do here? Presumably this will always be true as the result of the filter will always be an Array?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that is correct. But forEach will handle that. I'm only doing the array check to make sure forEach won't throw and error.

Edit: I see your point now. The original line was declarationFiles !== undefined. So I assumed (maybe wrongly) that I could also be undefined, even though I was surprised about that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Filter will always return an array. So I removed the check. It's safe to assume that we will always have an array, but it might be empty.

declarationFiles.forEach(file => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can file be declarationFile please?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure! Fixed!

const assetPath = path.relative(
compilation.compiler.context,
file.name
);
compilation.assets[assetPath] = {
source: () => file.text,
size: () => file.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 dtsDtsxRegex = /\.d\.ts(x?)(\.map)?$/i;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be dtsDtsxRegex be dtsDtsxOrDtsDtsxMapRegex please? It's verbose but hopefully clear.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Absolutely! I'll fix!

export const dtsTsTsxRegex = /(\.d)?\.ts(x?)$/i;
export const dtsTsTsxJsJsxRegex = /((\.d)?\.ts(x?)|js(x?))$/i;
export const tsTsxJsJsxRegex = /\.tsx?$|\.jsx?$/i;
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' }
]
}
}