Skip to content

Commit

Permalink
docs(ts): allow vtk classes discovery
Browse files Browse the repository at this point in the history
  • Loading branch information
daker committed Mar 3, 2022
1 parent a146140 commit 8b88691
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 2 deletions.
1 change: 0 additions & 1 deletion Sources/index.d.ts

This file was deleted.

15 changes: 15 additions & 0 deletions Sources/vtk.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
*
* @param obj
* @return
*/
declare function vtk(obj: object): any;

/**
* Nest register method under the vtk function
* @param vtkClassName
* @param constructor
*/
declare function register(vtkClassName: string, constructor: any): void;

export default vtk;
36 changes: 36 additions & 0 deletions Utilities/rollup/plugin-generate-references.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import * as fs from 'fs';
import path from 'path';

export function generateDtsReferences(pluginOptions) {

const rootDts = 'index.d.ts';
const dtsReferences = [
'/// <reference path="./types.d.ts" />',
'/// <reference path="./interfaces.d.ts" />',
];

return {
name: 'generate-references',
generateBundle(outputOptions, bundle, isWrite) {
const files = Object.keys(bundle);

for (let i = 0; i < files.length; i++) {
const file = files[i];
let filename = file.replace('.js', '.d.ts');

const dts = path.join(pluginOptions['dest'], filename);

if (fs.existsSync(dts) && filename !== rootDts) {
filename = filename.replace(/\\/g, '/');
dtsReferences.push(`/// <reference path="./${filename}" />`);
}
}

this.emitFile({
type: 'asset',
fileName: rootDts,
source: dtsReferences.join("\r\n")
});
}
};
}
11 changes: 10 additions & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import copy from 'rollup-plugin-copy';
import pkg from './package.json';

import { rewriteFilenames } from './Utilities/rollup/plugin-rewrite-filenames';
import { generateDtsReferences } from './Utilities/rollup/plugin-generate-references';

const absolutifyImports = require('./Utilities/build/absolutify-imports.js');

Expand Down Expand Up @@ -169,7 +170,10 @@ export default {
dest: outputDir,
rename(name, ext, fullPath) {
const filename = `${name}.${ext}`;
if (filename === 'index.d.ts') {
if (
filename === 'index.d.ts' &&
path.dirname(fullPath) !== 'Sources'
) {
const moduleName = path.basename(path.dirname(fullPath));
return `../${moduleName}.d.ts`;
}
Expand All @@ -193,6 +197,9 @@ export default {
},
],
}),
generateDtsReferences({
dest: outputDir,
}),
copy({
flatten: true,
targets: [
Expand All @@ -204,6 +211,7 @@ export default {
{ src: 'Utilities/build/macro-shim.js', dest: outputDir, rename: 'macro.js' },
{ src: '*.txt', dest: outputDir },
{ src: '*.md', dest: outputDir },
{ src: 'tsconfig.json', dest: outputDir },
{ src: '.npmignore', dest: outputDir },
{ src: 'LICENSE', dest: outputDir },
{
Expand All @@ -214,6 +222,7 @@ export default {
pkg.name = '@kitware/vtk.js';
pkg.main = './index.js';
pkg.module = './index.js';
pkg.types = './index.d.ts';
return JSON.stringify(pkg, null, 2);
},
},
Expand Down

0 comments on commit 8b88691

Please sign in to comment.