From 2eebe77166833cf88e1e95fc11c55eae76f07b00 Mon Sep 17 00:00:00 2001 From: James Brantly Date: Tue, 29 Dec 2015 22:38:13 -0500 Subject: [PATCH] Add support for emitting declaration files --- .gitignore | 1 + index.ts | 3 +++ package.json | 5 ++-- test/declarationOutput/app.ts | 4 +++- .../expectedOutput-1.6/app.d.ts | 5 ++++ .../expectedOutput-1.6/bundle.js | 22 ++++++++++++++++++ .../expectedOutput-1.6/output.txt | 11 +++++---- .../expectedOutput-1.6/sub/dep.d.ts | 4 ++++ .../expectedOutput-1.7/app.d.ts | 5 ++++ .../expectedOutput-1.7/bundle.js | 22 ++++++++++++++++++ .../expectedOutput-1.7/output.txt | 11 +++++---- .../expectedOutput-1.7/sub/dep.d.ts | 4 ++++ .../expectedOutput-1.8/app.d.ts | 5 ++++ .../expectedOutput-1.8/bundle.js | 23 +++++++++++++++++++ .../expectedOutput-1.8/output.txt | 11 +++++---- .../expectedOutput-1.8/sub/dep.d.ts | 4 ++++ test/declarationOutput/sub/dep.ts | 8 +++++++ test/run.js | 22 ++++++++++++------ 18 files changed, 148 insertions(+), 22 deletions(-) create mode 100644 test/declarationOutput/expectedOutput-1.6/app.d.ts create mode 100644 test/declarationOutput/expectedOutput-1.6/sub/dep.d.ts create mode 100644 test/declarationOutput/expectedOutput-1.7/app.d.ts create mode 100644 test/declarationOutput/expectedOutput-1.7/sub/dep.d.ts create mode 100644 test/declarationOutput/expectedOutput-1.8/app.d.ts create mode 100644 test/declarationOutput/expectedOutput-1.8/sub/dep.d.ts create mode 100644 test/declarationOutput/sub/dep.ts diff --git a/.gitignore b/.gitignore index 7bc466bdf..2716444e5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ /*.js +/*.d.ts /*.log *.js.map bundle.js diff --git a/index.ts b/index.ts index 0c8854a08..74088c46d 100644 --- a/index.ts +++ b/index.ts @@ -569,6 +569,9 @@ function loader(contents) { var sourceMapFile = output.outputFiles.filter(file => !!file.name.match(/\.js(x?)\.map$/)).pop(); if (sourceMapFile) { sourceMapText = sourceMapFile.text } + + var declarationFile = output.outputFiles.filter(file => !!file.name.match(/\.d.ts$/)).pop(); + if (declarationFile) { this.emitFile(path.relative(this.options.context, declarationFile.name), declarationFile.text); } } if (outputText == null) throw new Error(`Typescript emitted no output for ${filePath}`); diff --git a/package.json b/package.json index 4d27751d6..8f095cc3b 100644 --- a/package.json +++ b/package.json @@ -39,10 +39,11 @@ "babel-loader": "^5.3.2", "escape-string-regexp": "^1.0.3", "fs-extra": "^0.22.1", + "glob": "^6.0.3", "mkdirp": "^0.5.1", "mocha": "^2.1.0", "rimraf": "^2.4.2", - "webpack": "^1.11.0", - "typescript": "^1.6.2" + "typescript": "^1.6.2", + "webpack": "^1.11.0" } } diff --git a/test/declarationOutput/app.ts b/test/declarationOutput/app.ts index 36e7b1987..77428dd19 100644 --- a/test/declarationOutput/app.ts +++ b/test/declarationOutput/app.ts @@ -1,4 +1,6 @@ -class Test { +import dep = require('./sub/dep'); + +class Test extends dep { doSomething() { } diff --git a/test/declarationOutput/expectedOutput-1.6/app.d.ts b/test/declarationOutput/expectedOutput-1.6/app.d.ts new file mode 100644 index 000000000..42c1b770a --- /dev/null +++ b/test/declarationOutput/expectedOutput-1.6/app.d.ts @@ -0,0 +1,5 @@ +import dep = require('./sub/dep'); +declare class Test extends dep { + doSomething(): void; +} +export = Test; diff --git a/test/declarationOutput/expectedOutput-1.6/bundle.js b/test/declarationOutput/expectedOutput-1.6/bundle.js index 8a522887a..39231401f 100644 --- a/test/declarationOutput/expectedOutput-1.6/bundle.js +++ b/test/declarationOutput/expectedOutput-1.6/bundle.js @@ -42,6 +42,28 @@ /************************************************************************/ /******/ ([ /* 0 */ +/***/ function(module, exports, __webpack_require__) { + + var __extends = (this && this.__extends) || function (d, b) { + for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); + }; + var dep = __webpack_require__(1); + var Test = (function (_super) { + __extends(Test, _super); + function Test() { + _super.apply(this, arguments); + } + Test.prototype.doSomething = function () { + }; + return Test; + })(dep); + module.exports = Test; + + +/***/ }, +/* 1 */ /***/ function(module, exports) { var Test = (function () { diff --git a/test/declarationOutput/expectedOutput-1.6/output.txt b/test/declarationOutput/expectedOutput-1.6/output.txt index a656a0bff..a721e05c5 100644 --- a/test/declarationOutput/expectedOutput-1.6/output.txt +++ b/test/declarationOutput/expectedOutput-1.6/output.txt @@ -1,4 +1,7 @@ - Asset Size Chunks Chunk Names -bundle.js 1.55 kB 0 [emitted] main -chunk {0} bundle.js (main) 154 bytes [rendered] - [0] ./.test/declarationOutput/app.ts 154 bytes {0} [built] \ No newline at end of file + Asset Size Chunks Chunk Names + app.d.ts 110 bytes [emitted] +sub/dep.d.ts 63 bytes [emitted] + bundle.js 2.16 kB 0 [emitted] main +chunk {0} bundle.js (main) 675 bytes [rendered] + [0] ./.test/declarationOutput/app.ts 521 bytes {0} [built] + [1] ./.test/declarationOutput/sub/dep.ts 154 bytes {0} [built] \ No newline at end of file diff --git a/test/declarationOutput/expectedOutput-1.6/sub/dep.d.ts b/test/declarationOutput/expectedOutput-1.6/sub/dep.d.ts new file mode 100644 index 000000000..0d9a53ad0 --- /dev/null +++ b/test/declarationOutput/expectedOutput-1.6/sub/dep.d.ts @@ -0,0 +1,4 @@ +declare class Test { + doSomething(): void; +} +export = Test; diff --git a/test/declarationOutput/expectedOutput-1.7/app.d.ts b/test/declarationOutput/expectedOutput-1.7/app.d.ts new file mode 100644 index 000000000..42c1b770a --- /dev/null +++ b/test/declarationOutput/expectedOutput-1.7/app.d.ts @@ -0,0 +1,5 @@ +import dep = require('./sub/dep'); +declare class Test extends dep { + doSomething(): void; +} +export = Test; diff --git a/test/declarationOutput/expectedOutput-1.7/bundle.js b/test/declarationOutput/expectedOutput-1.7/bundle.js index 8a522887a..39231401f 100644 --- a/test/declarationOutput/expectedOutput-1.7/bundle.js +++ b/test/declarationOutput/expectedOutput-1.7/bundle.js @@ -42,6 +42,28 @@ /************************************************************************/ /******/ ([ /* 0 */ +/***/ function(module, exports, __webpack_require__) { + + var __extends = (this && this.__extends) || function (d, b) { + for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); + }; + var dep = __webpack_require__(1); + var Test = (function (_super) { + __extends(Test, _super); + function Test() { + _super.apply(this, arguments); + } + Test.prototype.doSomething = function () { + }; + return Test; + })(dep); + module.exports = Test; + + +/***/ }, +/* 1 */ /***/ function(module, exports) { var Test = (function () { diff --git a/test/declarationOutput/expectedOutput-1.7/output.txt b/test/declarationOutput/expectedOutput-1.7/output.txt index a656a0bff..a721e05c5 100644 --- a/test/declarationOutput/expectedOutput-1.7/output.txt +++ b/test/declarationOutput/expectedOutput-1.7/output.txt @@ -1,4 +1,7 @@ - Asset Size Chunks Chunk Names -bundle.js 1.55 kB 0 [emitted] main -chunk {0} bundle.js (main) 154 bytes [rendered] - [0] ./.test/declarationOutput/app.ts 154 bytes {0} [built] \ No newline at end of file + Asset Size Chunks Chunk Names + app.d.ts 110 bytes [emitted] +sub/dep.d.ts 63 bytes [emitted] + bundle.js 2.16 kB 0 [emitted] main +chunk {0} bundle.js (main) 675 bytes [rendered] + [0] ./.test/declarationOutput/app.ts 521 bytes {0} [built] + [1] ./.test/declarationOutput/sub/dep.ts 154 bytes {0} [built] \ No newline at end of file diff --git a/test/declarationOutput/expectedOutput-1.7/sub/dep.d.ts b/test/declarationOutput/expectedOutput-1.7/sub/dep.d.ts new file mode 100644 index 000000000..0d9a53ad0 --- /dev/null +++ b/test/declarationOutput/expectedOutput-1.7/sub/dep.d.ts @@ -0,0 +1,4 @@ +declare class Test { + doSomething(): void; +} +export = Test; diff --git a/test/declarationOutput/expectedOutput-1.8/app.d.ts b/test/declarationOutput/expectedOutput-1.8/app.d.ts new file mode 100644 index 000000000..42c1b770a --- /dev/null +++ b/test/declarationOutput/expectedOutput-1.8/app.d.ts @@ -0,0 +1,5 @@ +import dep = require('./sub/dep'); +declare class Test extends dep { + doSomething(): void; +} +export = Test; diff --git a/test/declarationOutput/expectedOutput-1.8/bundle.js b/test/declarationOutput/expectedOutput-1.8/bundle.js index bc83d6a06..aeb9072bf 100644 --- a/test/declarationOutput/expectedOutput-1.8/bundle.js +++ b/test/declarationOutput/expectedOutput-1.8/bundle.js @@ -42,6 +42,29 @@ /************************************************************************/ /******/ ([ /* 0 */ +/***/ function(module, exports, __webpack_require__) { + + "use strict"; + var __extends = (this && this.__extends) || function (d, b) { + for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; + function __() { this.constructor = d; } + d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); + }; + var dep = __webpack_require__(1); + var Test = (function (_super) { + __extends(Test, _super); + function Test() { + _super.apply(this, arguments); + } + Test.prototype.doSomething = function () { + }; + return Test; + }(dep)); + module.exports = Test; + + +/***/ }, +/* 1 */ /***/ function(module, exports) { "use strict"; diff --git a/test/declarationOutput/expectedOutput-1.8/output.txt b/test/declarationOutput/expectedOutput-1.8/output.txt index 630b1ed13..e43f4d2fc 100644 --- a/test/declarationOutput/expectedOutput-1.8/output.txt +++ b/test/declarationOutput/expectedOutput-1.8/output.txt @@ -1,4 +1,7 @@ - Asset Size Chunks Chunk Names -bundle.js 1.56 kB 0 [emitted] main -chunk {0} bundle.js (main) 168 bytes [rendered] - [0] ./.test/declarationOutput/app.ts 168 bytes {0} [built] \ No newline at end of file + Asset Size Chunks Chunk Names + app.d.ts 110 bytes [emitted] +sub/dep.d.ts 63 bytes [emitted] + bundle.js 2.19 kB 0 [emitted] main +chunk {0} bundle.js (main) 703 bytes [rendered] + [0] ./.test/declarationOutput/app.ts 535 bytes {0} [built] + [1] ./.test/declarationOutput/sub/dep.ts 168 bytes {0} [built] \ No newline at end of file diff --git a/test/declarationOutput/expectedOutput-1.8/sub/dep.d.ts b/test/declarationOutput/expectedOutput-1.8/sub/dep.d.ts new file mode 100644 index 000000000..0d9a53ad0 --- /dev/null +++ b/test/declarationOutput/expectedOutput-1.8/sub/dep.d.ts @@ -0,0 +1,4 @@ +declare class Test { + doSomething(): void; +} +export = Test; diff --git a/test/declarationOutput/sub/dep.ts b/test/declarationOutput/sub/dep.ts new file mode 100644 index 000000000..7e350c3c6 --- /dev/null +++ b/test/declarationOutput/sub/dep.ts @@ -0,0 +1,8 @@ + +class Test { + doSomething() { + + } +} + +export = Test; \ No newline at end of file diff --git a/test/run.js b/test/run.js index 5a97aae13..dcfac86d4 100644 --- a/test/run.js +++ b/test/run.js @@ -7,7 +7,8 @@ var webpack = require('webpack'); var webpackVersion = require('webpack/package.json').version; var regexEscape = require('escape-string-regexp'); var typescript = require('typescript'); -var semver = require('semver') +var semver = require('semver'); +var glob = require('glob'); // force colors on for tests since expected output has colors require('colors').enabled = true; @@ -106,7 +107,7 @@ function createTest(test, testPath, options) { // replace the hash if found in the output since it can change depending // on environments and we're not super interested in it if (stats) { - fs.readdirSync(webpackOutput).forEach(function(file) { + glob.sync('**/*', {cwd: webpackOutput, nodir: true}).forEach(function(file) { var content = fs.readFileSync(path.join(webpackOutput, file), 'utf-8'); content = content.split(stats.hash).join('[hash]'); fs.writeFileSync(path.join(webpackOutput, file), content); @@ -116,7 +117,7 @@ function createTest(test, testPath, options) { // output results if (saveOutputMode) { // loop through webpackOutput and rename to .transpiled if needed - fs.readdirSync(webpackOutput).forEach(function(file) { + glob.sync('**/*', {cwd: webpackOutput, nodir: true}).forEach(function(file) { var patchedFileName = patch+'/'+file; currentSavedOutput[patchedFileName] = fs.readFileSync(path.join(webpackOutput, file), 'utf-8'); @@ -166,6 +167,13 @@ function createTest(test, testPath, options) { var statsFileName = 'output.txt'; + // do a little magic to normalize `\` to `/` for asset output + var newAssets = {}; + Object.keys(stats.compilation.assets).forEach(function(asset) { + newAssets[asset.replace(/\\/g, "/")] = stats.compilation.assets[asset]; + }); + stats.compilation.assets = newAssets; + var statsString = stats.toString({timings: false, version: false, hash: false}) .replace(new RegExp(regexEscape(testStagingPath+path.sep), 'g'), '') .replace(new RegExp(regexEscape(rootPath+path.sep), 'g'), '') @@ -191,13 +199,13 @@ function createTest(test, testPath, options) { if (!saveOutputMode) { // massage any .transpiled. files - fs.readdirSync(expectedOutput).forEach(function(file) { + glob.sync('**/*', {cwd: expectedOutput, nodir: true}).forEach(function(file) { if (/\.transpiled/.test(file)) { if (options.transpile) { // rename if we're in transpile mode var extension = path.extname(file); fs.renameSync( path.join(expectedOutput, file), - path.join(expectedOutput, path.basename(file, '.transpiled'+extension)+extension) + path.join(expectedOutput, path.dirname(file), path.basename(file, '.transpiled'+extension)+extension) ); } else { // otherwise delete @@ -208,8 +216,8 @@ function createTest(test, testPath, options) { }); // compare actual to expected - var actualFiles = fs.readdirSync(actualOutput), - expectedFiles = fs.readdirSync(expectedOutput) + var actualFiles = glob.sync('**/*', {cwd: actualOutput, nodir: true}), + expectedFiles = glob.sync('**/*', {cwd: expectedOutput, nodir: true}) .filter(function(file) { return !/^patch/.test(file); }), allFiles = {};