Skip to content
Open
7 changes: 6 additions & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,11 @@ module.exports = function (grunt) {
testConflicts: {
src: ['test/src/conflicts/dirname/index.ts'],
outDir: 'test/build/conflicts/dirname'
}
},
testDotSlash: {
src: ['test/src/dot-slash/index.ts'],
outDir: 'test/build/dot-slash'
}
},
mochaTest: {
options: {
Expand All @@ -92,6 +96,7 @@ module.exports = function (grunt) {
'ts:testEs6',
'ts:testCommonJs',
'ts:testConflicts',
'ts:testDotSlash',
'run'
]);

Expand Down
12 changes: 6 additions & 6 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ export function bundle(options: Options): BundleResult {
mainFileContent += generatedLine + "\n";
});
mainFile = path.resolve(baseDir, "dts-bundle.tmp." + exportName + ".d.ts");
fs.writeFileSync(mainFile, mainFileContent, 'utf8');
fs.writeFileSync(mainFile, mainFileContent, { encoding: 'utf8' });
}

trace('\n### find typings ###');
Expand Down Expand Up @@ -424,7 +424,7 @@ export function bundle(options: Options): BundleResult {
}
}

fs.writeFileSync(outFile, content, 'utf8');
fs.writeFileSync(outFile, content, { encoding: 'utf8' });
bundleResult.emitted = true;
} else {
warning(" XXX Not emit due to exist files not found.")
Expand Down Expand Up @@ -517,7 +517,7 @@ export function bundle(options: Options): BundleResult {
}

function getModName(file: string) {
return path.relative(baseDir, path.dirname(file) + path.sep + path.basename(file).replace(/\.d\.ts$/, ''));
return path.relative(baseDir, path.dirname(file) + path.sep + path.basename(file).replace(/index\.d\.ts$|\.d\.ts$/, ''));
}

function getExpName(file: string) {
Expand Down Expand Up @@ -584,9 +584,6 @@ export function bundle(options: Options): BundleResult {
res.fileExists = false;
return res;
}
if (fs.lstatSync(file).isDirectory()) { // if file is a directory then lets assume commonjs convention of an index file in the given folder
file = path.join(file, 'index.d.ts');
}
const code = fs.readFileSync(file, 'utf8').replace(bomOptExp, '').replace(/\s*$/, '');
res.indent = detectIndent(code) || indent;

Expand Down Expand Up @@ -717,6 +714,9 @@ export function bundle(options: Options): BundleResult {
if(!fs.existsSync(full) || fs.existsSync(full + '.d.ts')) {
full += '.d.ts';
}
if (fs.lstatSync(full).isDirectory()) {
full = path.join(full, 'index.d.ts'); // normalize to actual file path
}
trace(' - import relative %s (%s)', moduleName, full);

pushUnique(res.relativeImports, full);
Expand Down
16 changes: 16 additions & 0 deletions test/expected/dot-slash/bundle.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

declare module 'bundle' {
import { SomeOtherClass } from "bundle/SomeOtherClass";
export interface IShouldBeThereOnlyOnce {
name: string;
}
export function getOther(): SomeOtherClass;
}

declare module 'bundle/SomeOtherClass' {
import { IShouldBeThereOnlyOnce } from "bundle/";
export class SomeOtherClass {
static saveTheWorld(once: IShouldBeThereOnlyOnce): Promise<any>;
}
}

8 changes: 4 additions & 4 deletions test/expected/externals/foo-mx.d.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@

declare module 'foo-mx' {
import exp = require('foo-mx/lib/exported-sub');
import mod1 = require('foo-mx/index//external1');
import mod1 = require('foo-mx///external1');
export import Foo = require('foo-mx/Foo');
export function run(foo?: Foo): Foo;
export function flep(): exp.ExternalContainer;
export function bar(): mod1.SomeType;
}

declare module 'foo-mx/index//external1' {
declare module 'foo-mx///external1' {
export class SomeType {
foo(): void;
}
}

declare module 'foo-mx/index//external2' {
declare module 'foo-mx///external2' {
export class AnotherType {
foo(): void;
}
}

declare module 'foo-mx/lib/exported-sub' {
import Foo = require('foo-mx/Foo');
import mod2 = require('foo-mx/index//external2');
import mod2 = require('foo-mx///external2');
export class ExternalContainer {
something: mod2.AnotherType;
}
Expand Down
6 changes: 3 additions & 3 deletions test/expected/includeExclude/foo-mx.d.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@

declare module 'foo-mx' {
import exp = require('foo-mx/lib/exported-sub');
import mod1 = require('foo-mx/index//external1');
import mod1 = require('foo-mx///external1');
export import Foo = require('foo-mx/Foo');
export function run(foo?: Foo): Foo;
export function flep(): exp.ExternalContainer;
export function bar(): mod1.SomeType;
}

declare module 'foo-mx/index//external1' {
declare module 'foo-mx///external1' {
export class SomeType {
foo(): void;
}
}

declare module 'foo-mx/index//external2' {
declare module 'foo-mx///external2' {
export class AnotherType {
foo(): void;
}
Expand Down
8 changes: 8 additions & 0 deletions test/src/dot-slash/SomeOtherClass.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import {IShouldBeThereOnlyOnce} from "./"; // The tricky part

export class SomeOtherClass {

public static saveTheWorld(once: IShouldBeThereOnlyOnce): Promise<any> {
return null;
}
}
13 changes: 13 additions & 0 deletions test/src/dot-slash/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict';

import {SomeOtherClass} from "./SomeOtherClass";

const other: SomeOtherClass = new SomeOtherClass();

export interface IShouldBeThereOnlyOnce {
name: string,
}

export function getOther(): SomeOtherClass {
return null;
}
50 changes: 50 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -532,4 +532,54 @@ describe('dts bundle', function () {
]);
assert.strictEqual(getFile(actualFile), getFile(expectedFile));
});

(function testit(name, assertion, run) {
var buildDir = path.resolve(__dirname, 'build', name);
var call = function (done) {
var testDir = path.join(tmpDir, name);
var expDir = path.join(expectDir, name);

mkdirp.sync(testDir);

ncp.ncp(buildDir, testDir, function (err) {
if (err) {
done(err);
return;
}
assertion(testDir, expDir);
done();
});
};

var label = 'bundle ' + name;

if (run === 'skip') {
it.skip(label, call);
}
else if (run === 'only') {
it.only(label, call);
}
else {
it(label, call);
}
})('dot-slash', function (actDir, expDir) {
var result = dts.bundle({
name: 'bundle',
main: path.join(actDir, '../dot-slash', 'index.d.ts'),
newline: '\n',
verbose: true,
headerPath: "none"
});
var name = 'bundle.d.ts';
var actualFile = path.join(actDir, name);
assert.isTrue(result.emitted, "not emit " + actualFile);
var expectedFile = path.join(expDir, name);
assertFiles(actDir, [
name,
'index.d.ts',
'SomeOtherClass.d.ts'
]);
assert.strictEqual(getFile(actualFile), getFile(expectedFile));
})

});