Skip to content

Commit

Permalink
Add support for emitting declaration files
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrantly committed Jan 10, 2016
1 parent 1ec91c8 commit 2eebe77
Show file tree
Hide file tree
Showing 18 changed files with 148 additions and 22 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/*.js
/*.d.ts
/*.log
*.js.map
bundle.js
Expand Down
3 changes: 3 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
4 changes: 3 additions & 1 deletion test/declarationOutput/app.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class Test {
import dep = require('./sub/dep');

class Test extends dep {
doSomething() {

}
Expand Down
5 changes: 5 additions & 0 deletions test/declarationOutput/expectedOutput-1.6/app.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import dep = require('./sub/dep');
declare class Test extends dep {
doSomething(): void;
}
export = Test;
22 changes: 22 additions & 0 deletions test/declarationOutput/expectedOutput-1.6/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down
11 changes: 7 additions & 4 deletions test/declarationOutput/expectedOutput-1.6/output.txt
Original file line number Diff line number Diff line change
@@ -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]
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]
4 changes: 4 additions & 0 deletions test/declarationOutput/expectedOutput-1.6/sub/dep.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
declare class Test {
doSomething(): void;
}
export = Test;
5 changes: 5 additions & 0 deletions test/declarationOutput/expectedOutput-1.7/app.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import dep = require('./sub/dep');
declare class Test extends dep {
doSomething(): void;
}
export = Test;
22 changes: 22 additions & 0 deletions test/declarationOutput/expectedOutput-1.7/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down
11 changes: 7 additions & 4 deletions test/declarationOutput/expectedOutput-1.7/output.txt
Original file line number Diff line number Diff line change
@@ -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]
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]
4 changes: 4 additions & 0 deletions test/declarationOutput/expectedOutput-1.7/sub/dep.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
declare class Test {
doSomething(): void;
}
export = Test;
5 changes: 5 additions & 0 deletions test/declarationOutput/expectedOutput-1.8/app.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import dep = require('./sub/dep');
declare class Test extends dep {
doSomething(): void;
}
export = Test;
23 changes: 23 additions & 0 deletions test/declarationOutput/expectedOutput-1.8/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
11 changes: 7 additions & 4 deletions test/declarationOutput/expectedOutput-1.8/output.txt
Original file line number Diff line number Diff line change
@@ -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]
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]
4 changes: 4 additions & 0 deletions test/declarationOutput/expectedOutput-1.8/sub/dep.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
declare class Test {
doSomething(): void;
}
export = Test;
8 changes: 8 additions & 0 deletions test/declarationOutput/sub/dep.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

class Test {
doSomething() {

}
}

export = Test;
22 changes: 15 additions & 7 deletions test/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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');

Expand Down Expand Up @@ -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'), '')
Expand All @@ -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
Expand All @@ -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 = {};

Expand Down

0 comments on commit 2eebe77

Please sign in to comment.