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

Hook for modifying module body code #80

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 22 additions & 10 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export interface Options {
emitOnNoIncludedFileNotFound?: boolean;
headerPath: string;
headerText: string;
transformModuleBody: (moduleBody: string, moduleName?: string) => string
}

export interface ModLine {
Expand Down Expand Up @@ -183,7 +184,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 +425,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 @@ -544,16 +545,27 @@ export function bundle(options: Options): BundleResult {
return (lines.length === 0 ? '' : i + lines.join(newline + i)) + newline;
}

function transformModuleBody(moduleBody: string, moduleName?: string) {
if (typeof options.transformModuleBody === 'function') {
moduleBody = options.transformModuleBody(moduleBody, moduleName);
}
return moduleBody
}

function formatModule(file: string, lines: string[]) {
let out = '';
let moduleBody = mergeModulesLines(lines);

if (outputAsModuleFolder) {
return mergeModulesLines(lines);
}
moduleBody = transformModuleBody(moduleBody);
return moduleBody;

} else {
let moduleName = getExpName(file);
moduleBody = transformModuleBody(moduleBody, moduleName);

out += 'declare module \'' + getExpName(file) + '\' {' + newline;
out += mergeModulesLines(lines);
out += '}' + newline;
return out;
return 'declare module \'' + moduleName + '\' {' + newline +
moduleBody + '}' + newline;
}
}

// main info extractor
Expand Down Expand Up @@ -587,7 +599,7 @@ export function bundle(options: Options): BundleResult {
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*$/, '');
const code = fs.readFileSync(file, { encoding: 'utf8' }).replace(bomOptExp, '').replace(/\s*$/, '');
res.indent = detectIndent(code) || indent;

// buffer multi-line comments, handle JSDoc
Expand Down
38 changes: 38 additions & 0 deletions test/expected/transformModuleBody/foo-mx.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Dependencies for this module:
// ../../src/typings/external.d.ts

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

declare module 'foo-mx/lib/exported-sub' {
import Foo = require('foo-mx/Foo');
import mod2 = require('external2');
export class ExternalContainer {
something: mod2.AnotherType;
}
export function bar(foo: Foo): string;
export function bazz(value: string, option?: boolean): string;
/* INJECTED VIA transformModuleBody */
}

declare module 'foo-mx/Foo' {
class Foo {
foo: string;
constructor(secret?: string);
/**
* Bars the foo.
*/
barFoo(): void;
/** Foos the baz. */
fooBaz(): void;
}
export = Foo;
/* INJECTED VIA transformModuleBody */
}
25 changes: 25 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,31 @@ describe('dts bundle', function () {
assert.strictEqual(getFile(actualFile), getFile(expectedFile));
});

testit('transformModuleBody', function (actDir, expDir) {
var result = dts.bundle({
name: 'foo-mx',
main: path.join(actDir, 'index.d.ts'),
newline: '\n',
verbose: true,
headerPath: "none",
transformModuleBody(moduleBody) {
return moduleBody + '/* INJECTED VIA transformModuleBody */\n';
}
});
var name = 'foo-mx.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',
'Foo.d.ts',
'lib/exported-sub.d.ts',
'lib/only-internal.d.ts'
]);
assert.strictEqual(getFile(actualFile), getFile(expectedFile));
});

//testit('includeExclude_cli', function (actDir, expDir) { }); // No exclude options available from CLI.

(function testit(name, assertion, run) {
Expand Down