Skip to content

Commit 2eebe77

Browse files
committed
Add support for emitting declaration files
1 parent 1ec91c8 commit 2eebe77

File tree

18 files changed

+148
-22
lines changed

18 files changed

+148
-22
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/*.js
2+
/*.d.ts
23
/*.log
34
*.js.map
45
bundle.js

index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,9 @@ function loader(contents) {
569569

570570
var sourceMapFile = output.outputFiles.filter(file => !!file.name.match(/\.js(x?)\.map$/)).pop();
571571
if (sourceMapFile) { sourceMapText = sourceMapFile.text }
572+
573+
var declarationFile = output.outputFiles.filter(file => !!file.name.match(/\.d.ts$/)).pop();
574+
if (declarationFile) { this.emitFile(path.relative(this.options.context, declarationFile.name), declarationFile.text); }
572575
}
573576

574577
if (outputText == null) throw new Error(`Typescript emitted no output for ${filePath}`);

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,11 @@
3939
"babel-loader": "^5.3.2",
4040
"escape-string-regexp": "^1.0.3",
4141
"fs-extra": "^0.22.1",
42+
"glob": "^6.0.3",
4243
"mkdirp": "^0.5.1",
4344
"mocha": "^2.1.0",
4445
"rimraf": "^2.4.2",
45-
"webpack": "^1.11.0",
46-
"typescript": "^1.6.2"
46+
"typescript": "^1.6.2",
47+
"webpack": "^1.11.0"
4748
}
4849
}

test/declarationOutput/app.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
class Test {
1+
import dep = require('./sub/dep');
2+
3+
class Test extends dep {
24
doSomething() {
35

46
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import dep = require('./sub/dep');
2+
declare class Test extends dep {
3+
doSomething(): void;
4+
}
5+
export = Test;

test/declarationOutput/expectedOutput-1.6/bundle.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,28 @@
4242
/************************************************************************/
4343
/******/ ([
4444
/* 0 */
45+
/***/ function(module, exports, __webpack_require__) {
46+
47+
var __extends = (this && this.__extends) || function (d, b) {
48+
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
49+
function __() { this.constructor = d; }
50+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
51+
};
52+
var dep = __webpack_require__(1);
53+
var Test = (function (_super) {
54+
__extends(Test, _super);
55+
function Test() {
56+
_super.apply(this, arguments);
57+
}
58+
Test.prototype.doSomething = function () {
59+
};
60+
return Test;
61+
})(dep);
62+
module.exports = Test;
63+
64+
65+
/***/ },
66+
/* 1 */
4567
/***/ function(module, exports) {
4668

4769
var Test = (function () {
Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
Asset Size Chunks Chunk Names
2-
bundle.js 1.55 kB 0 [emitted] main
3-
chunk {0} bundle.js (main) 154 bytes [rendered]
4-
[0] ./.test/declarationOutput/app.ts 154 bytes {0} [built]
1+
Asset Size Chunks Chunk Names
2+
app.d.ts 110 bytes [emitted]
3+
sub/dep.d.ts 63 bytes [emitted]
4+
bundle.js 2.16 kB 0 [emitted] main
5+
chunk {0} bundle.js (main) 675 bytes [rendered]
6+
[0] ./.test/declarationOutput/app.ts 521 bytes {0} [built]
7+
[1] ./.test/declarationOutput/sub/dep.ts 154 bytes {0} [built]
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
declare class Test {
2+
doSomething(): void;
3+
}
4+
export = Test;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import dep = require('./sub/dep');
2+
declare class Test extends dep {
3+
doSomething(): void;
4+
}
5+
export = Test;

test/declarationOutput/expectedOutput-1.7/bundle.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,28 @@
4242
/************************************************************************/
4343
/******/ ([
4444
/* 0 */
45+
/***/ function(module, exports, __webpack_require__) {
46+
47+
var __extends = (this && this.__extends) || function (d, b) {
48+
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
49+
function __() { this.constructor = d; }
50+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
51+
};
52+
var dep = __webpack_require__(1);
53+
var Test = (function (_super) {
54+
__extends(Test, _super);
55+
function Test() {
56+
_super.apply(this, arguments);
57+
}
58+
Test.prototype.doSomething = function () {
59+
};
60+
return Test;
61+
})(dep);
62+
module.exports = Test;
63+
64+
65+
/***/ },
66+
/* 1 */
4567
/***/ function(module, exports) {
4668

4769
var Test = (function () {
Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
Asset Size Chunks Chunk Names
2-
bundle.js 1.55 kB 0 [emitted] main
3-
chunk {0} bundle.js (main) 154 bytes [rendered]
4-
[0] ./.test/declarationOutput/app.ts 154 bytes {0} [built]
1+
Asset Size Chunks Chunk Names
2+
app.d.ts 110 bytes [emitted]
3+
sub/dep.d.ts 63 bytes [emitted]
4+
bundle.js 2.16 kB 0 [emitted] main
5+
chunk {0} bundle.js (main) 675 bytes [rendered]
6+
[0] ./.test/declarationOutput/app.ts 521 bytes {0} [built]
7+
[1] ./.test/declarationOutput/sub/dep.ts 154 bytes {0} [built]
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
declare class Test {
2+
doSomething(): void;
3+
}
4+
export = Test;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import dep = require('./sub/dep');
2+
declare class Test extends dep {
3+
doSomething(): void;
4+
}
5+
export = Test;

test/declarationOutput/expectedOutput-1.8/bundle.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,29 @@
4242
/************************************************************************/
4343
/******/ ([
4444
/* 0 */
45+
/***/ function(module, exports, __webpack_require__) {
46+
47+
"use strict";
48+
var __extends = (this && this.__extends) || function (d, b) {
49+
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
50+
function __() { this.constructor = d; }
51+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
52+
};
53+
var dep = __webpack_require__(1);
54+
var Test = (function (_super) {
55+
__extends(Test, _super);
56+
function Test() {
57+
_super.apply(this, arguments);
58+
}
59+
Test.prototype.doSomething = function () {
60+
};
61+
return Test;
62+
}(dep));
63+
module.exports = Test;
64+
65+
66+
/***/ },
67+
/* 1 */
4568
/***/ function(module, exports) {
4669

4770
"use strict";
Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
Asset Size Chunks Chunk Names
2-
bundle.js 1.56 kB 0 [emitted] main
3-
chunk {0} bundle.js (main) 168 bytes [rendered]
4-
[0] ./.test/declarationOutput/app.ts 168 bytes {0} [built]
1+
Asset Size Chunks Chunk Names
2+
app.d.ts 110 bytes [emitted]
3+
sub/dep.d.ts 63 bytes [emitted]
4+
bundle.js 2.19 kB 0 [emitted] main
5+
chunk {0} bundle.js (main) 703 bytes [rendered]
6+
[0] ./.test/declarationOutput/app.ts 535 bytes {0} [built]
7+
[1] ./.test/declarationOutput/sub/dep.ts 168 bytes {0} [built]
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
declare class Test {
2+
doSomething(): void;
3+
}
4+
export = Test;

test/declarationOutput/sub/dep.ts

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;

test/run.js

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ var webpack = require('webpack');
77
var webpackVersion = require('webpack/package.json').version;
88
var regexEscape = require('escape-string-regexp');
99
var typescript = require('typescript');
10-
var semver = require('semver')
10+
var semver = require('semver');
11+
var glob = require('glob');
1112

1213
// force colors on for tests since expected output has colors
1314
require('colors').enabled = true;
@@ -106,7 +107,7 @@ function createTest(test, testPath, options) {
106107
// replace the hash if found in the output since it can change depending
107108
// on environments and we're not super interested in it
108109
if (stats) {
109-
fs.readdirSync(webpackOutput).forEach(function(file) {
110+
glob.sync('**/*', {cwd: webpackOutput, nodir: true}).forEach(function(file) {
110111
var content = fs.readFileSync(path.join(webpackOutput, file), 'utf-8');
111112
content = content.split(stats.hash).join('[hash]');
112113
fs.writeFileSync(path.join(webpackOutput, file), content);
@@ -116,7 +117,7 @@ function createTest(test, testPath, options) {
116117
// output results
117118
if (saveOutputMode) {
118119
// loop through webpackOutput and rename to .transpiled if needed
119-
fs.readdirSync(webpackOutput).forEach(function(file) {
120+
glob.sync('**/*', {cwd: webpackOutput, nodir: true}).forEach(function(file) {
120121
var patchedFileName = patch+'/'+file;
121122
currentSavedOutput[patchedFileName] = fs.readFileSync(path.join(webpackOutput, file), 'utf-8');
122123

@@ -166,6 +167,13 @@ function createTest(test, testPath, options) {
166167

167168
var statsFileName = 'output.txt';
168169

170+
// do a little magic to normalize `\` to `/` for asset output
171+
var newAssets = {};
172+
Object.keys(stats.compilation.assets).forEach(function(asset) {
173+
newAssets[asset.replace(/\\/g, "/")] = stats.compilation.assets[asset];
174+
});
175+
stats.compilation.assets = newAssets;
176+
169177
var statsString = stats.toString({timings: false, version: false, hash: false})
170178
.replace(new RegExp(regexEscape(testStagingPath+path.sep), 'g'), '')
171179
.replace(new RegExp(regexEscape(rootPath+path.sep), 'g'), '')
@@ -191,13 +199,13 @@ function createTest(test, testPath, options) {
191199

192200
if (!saveOutputMode) {
193201
// massage any .transpiled. files
194-
fs.readdirSync(expectedOutput).forEach(function(file) {
202+
glob.sync('**/*', {cwd: expectedOutput, nodir: true}).forEach(function(file) {
195203
if (/\.transpiled/.test(file)) {
196204
if (options.transpile) { // rename if we're in transpile mode
197205
var extension = path.extname(file);
198206
fs.renameSync(
199207
path.join(expectedOutput, file),
200-
path.join(expectedOutput, path.basename(file, '.transpiled'+extension)+extension)
208+
path.join(expectedOutput, path.dirname(file), path.basename(file, '.transpiled'+extension)+extension)
201209
);
202210
}
203211
else { // otherwise delete
@@ -208,8 +216,8 @@ function createTest(test, testPath, options) {
208216
});
209217

210218
// compare actual to expected
211-
var actualFiles = fs.readdirSync(actualOutput),
212-
expectedFiles = fs.readdirSync(expectedOutput)
219+
var actualFiles = glob.sync('**/*', {cwd: actualOutput, nodir: true}),
220+
expectedFiles = glob.sync('**/*', {cwd: expectedOutput, nodir: true})
213221
.filter(function(file) { return !/^patch/.test(file); }),
214222
allFiles = {};
215223

0 commit comments

Comments
 (0)