Skip to content

Commit

Permalink
docs(ts): use relative instead of absolute imports
Browse files Browse the repository at this point in the history
Use relative instead of absolute imports to make npm link
and regular packages work correclty.

fix Kitware#2311
  • Loading branch information
daker committed Mar 12, 2022
1 parent 890d758 commit fcf257c
Show file tree
Hide file tree
Showing 13 changed files with 79 additions and 44 deletions.
4 changes: 3 additions & 1 deletion Sources/Common/Core/Endian/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { Nullable } from "../../../types";

/**
* Get the endianness
*/
export function getEndianness(): string | null;
export function getEndianness(): Nullable<string>;

export const ENDIANNESS : string;

Expand Down
12 changes: 7 additions & 5 deletions Sources/Interaction/Widgets/OrientationMarkerWidget/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import vtkAnnotatedCubeActor from "../../../Rendering/Core/AnnotatedCubeActor";
import vtkAxesActor from "../../../Rendering/Core/AxesActor";
import vtkRenderer from "../../../Rendering/Core/Renderer";
import vtkRenderWindowInteractor from "../../../Rendering/Core/RenderWindowInteractor";
import { Nullable } from "../../../types";

export enum Corners {
TOP_LEFT,
Expand Down Expand Up @@ -48,7 +49,7 @@ export interface vtkOrientationMarkerWidget extends vtkObject {
/**
* Gets the parent renderer, if any.
*/
getParentRenderer(): vtkRenderer | null;
getParentRenderer(): Nullable<vtkRenderer>;

/**
* Get wheter the orientation marker is enabled.
Expand Down Expand Up @@ -89,13 +90,13 @@ export interface vtkOrientationMarkerWidget extends vtkObject {

/**
* Get the actor associated with the widget.
* @param actor
* @param {vtkAnnotatedCubeActor | vtkAxesActor} actor The actor instance.
*/
setActor(actor: vtkAnnotatedCubeActor | vtkAxesActor): void;

/**
* Sets the parent renderer
* @param ren The parent renderer
* @param {vtkRenderer} ren The parent renderer
*/
setParentRenderer(ren: vtkRenderer): boolean;

Expand Down Expand Up @@ -135,9 +136,10 @@ export interface vtkOrientationMarkerWidget extends vtkObject {
setViewportCorner(viewportCorner: Corners): boolean;

/**
* Set the viewport size. The sizeFactor should be between 0.0 and 1.0.
* Set the viewport size.
* The sizeFactor should be between 0.0 and 1.0.
* It says how much of the main render window to color.
* @param sizeFactor
* @param {Number} sizeFactor
* @default 0.2
*/
setViewportSize(sizeFactor: number): void;
Expand Down
8 changes: 4 additions & 4 deletions Sources/Rendering/Core/Mapper/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Bounds, Range } from "../../../types";
import { Bounds, Nullable, Range } from "../../../types";
import vtkAbstractMapper3D, { IAbstractMapper3DInitialValues } from "../AbstractMapper3D";

export enum ColorMode {
Expand Down Expand Up @@ -151,17 +151,17 @@ export interface vtkMapper extends vtkAbstractMapper3D {
/**
* Get the array name to color by.
*/
getColorByArrayName(): string | null;
getColorByArrayName(): Nullable<string>;

/**
* Provide read access to the color texture coordinate array
*/
getColorCoordinates(): Float32Array | null;
getColorCoordinates(): Nullable<Float32Array>;

/**
* Provide read access to the color array.
*/
getColorMapColors(): Uint8Array | null;
getColorMapColors(): Nullable<Uint8Array>;

/**
* Return the method of coloring scalar data.
Expand Down
5 changes: 3 additions & 2 deletions Sources/Rendering/Core/Mapper2D/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Nullable } from "../../../types";
import vtkAbstractMapper, { IAbstractMapperInitialValues } from "../AbstractMapper";

export enum ColorMode {
Expand Down Expand Up @@ -73,12 +74,12 @@ export interface vtkMapper2D extends vtkAbstractMapper {
/**
* Get the array name to color by.
*/
getColorByArrayName(): string | null;
getColorByArrayName(): Nullable<string>;

/**
* Provide read access to the color array.
*/
getColorMapColors(): Uint8Array | null;
getColorMapColors(): Nullable<Uint8Array>;

/**
* Return the method of coloring scalar data.
Expand Down
7 changes: 4 additions & 3 deletions Sources/Rendering/Core/Renderer/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Bounds } from '../../../types';
import { Bounds, Nullable } from '../../../types';

import vtkCamera from '../Camera';
import vtkLight from '../Light';
Expand Down Expand Up @@ -128,6 +128,7 @@ export interface vtkRenderer extends vtkViewport {
*
*/
getClippingRangeExpansion(): number;

/**
*
* @default null
Expand Down Expand Up @@ -226,7 +227,7 @@ export interface vtkRenderer extends vtkViewport {
*
* @default null
*/
getRenderWindow(): vtkRenderWindow | null;
getRenderWindow(): Nullable<vtkRenderWindow>;

/**
*
Expand Down Expand Up @@ -337,7 +338,7 @@ export interface vtkRenderer extends vtkViewport {
* Specify the camera to use for this renderer.
* @param {vtkCamera} camera The camera object to use.
*/
setActiveCamera(camera: vtkCamera | null): boolean;
setActiveCamera(camera: vtkCamera): boolean;

/**
*
Expand Down
5 changes: 3 additions & 2 deletions Sources/Rendering/Misc/SynchronizableRenderWindow/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Nullable } from "../../../types";
import vtkRenderWindow, { IRenderWindowInitialValues } from "../../Core/RenderWindow";

// Keeps state for client / server scene synchronization.
Expand Down Expand Up @@ -27,8 +28,8 @@ export interface ISynchronizerContext {

export interface ISynchronizableRenderWindowInitialValues extends IRenderWindowInitialValues {
synchronizerContextName?: string; // default: 'default':
synchronizerContext?: ISynchronizerContext | null;
synchronizedViewId?: string | null;
synchronizerContext?: Nullable<ISynchronizerContext>;
synchronizedViewId?: Nullable<string>;
}

// Server-side view state.
Expand Down
4 changes: 2 additions & 2 deletions Sources/Rendering/Misc/TextureLODsDownloader/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { vtkObject } from "../../../interfaces";
import { CrossOrigin } from "../../../types";
import { CrossOrigin, Nullable } from "../../../types";
import vtkTexture from "../../Core/Texture";

/**
Expand Down Expand Up @@ -27,7 +27,7 @@ export interface vtkTextureLODsDownloader extends vtkObject {
/**
* Get the crossorigin attribute
*/
getCrossOrigin(): CrossOrigin | null;
getCrossOrigin(): Nullable<CrossOrigin>;

/**
* Get the list of files to download
Expand Down
26 changes: 13 additions & 13 deletions Sources/Rendering/OpenGL/RenderWindow/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { vtkAlgorithm, vtkObject } from '../../../interfaces';
import { Size, Vector2, Vector3 } from '../../../types';
import { Nullable, Size, Vector2, Vector3 } from '../../../types';
import { vtkRenderer } from '../../../Rendering/Core/Renderer';
import { VtkDataTypes } from '../../../Common/Core/DataArray';
import vtkTexture from '../../Core/Texture';
Expand Down Expand Up @@ -199,7 +199,7 @@ export interface vtkOpenGLRenderWindow extends vtkOpenGLRenderWindowBase {
*
* @param options
*/
get3DContext(options: object): WebGLRenderingContext | null;
get3DContext(options: object): Nullable<WebGLRenderingContext>;

/**
*
Expand Down Expand Up @@ -241,20 +241,20 @@ export interface vtkOpenGLRenderWindow extends vtkOpenGLRenderWindowBase {

/**
*
* @param vtktype
* @param numComps
* @param useFloat
* @param {VtkDataTypes} vtktype
* @param {Number} numComps
* @param {Boolean} useFloat
*/
getDefaultTextureInternalFormat(vtktype: VtkDataTypes, numComps: number, useFloat: boolean): void;

/**
*
* @param {HTMLImageElement} img
*
* @param {HTMLImageElement} img The background image.
*/
setBackgroundImage(img: HTMLImageElement): void;

/**
*
*
* @param {Boolean} value
*/
setUseBackgroundImage(value: boolean): void;
Expand All @@ -273,7 +273,7 @@ export interface vtkOpenGLRenderWindow extends vtkOpenGLRenderWindowBase {
* @param {String} format
* @param {IOptions} options
*/
captureNextImage(format: string, options: IOptions): Promise<string> | null;
captureNextImage(format: string, options: IOptions): Nullable<Promise<string>>;

/**
*
Expand All @@ -297,20 +297,20 @@ export interface vtkOpenGLRenderWindow extends vtkOpenGLRenderWindowBase {

/**
*
* @param stream
* @param {vtkViewStream} stream The vtkViewStream instance.
*/
setViewStream(stream: vtkViewStream): boolean;

/**
*
* @param {Vector2} size
* @param {Vector2} size
*/
setSize(size: Vector2): void;

/**
*
* @param {Number} x
* @param {Number} y
* @param {Number} x
* @param {Number} y
*/
setSize(x: number, y: number): void;

Expand Down
21 changes: 21 additions & 0 deletions Sources/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
import { vtkOutputPort } from "./interfaces";

/** Alias type for value that can be null */
declare type Nullable<T> = T | null;

/**
* Alias type for number that are floats
* @ignorenaming
*/
declare type float = number;

/**
* Alias type for number that are doubles.
* @ignorenaming
*/
declare type double = number;

/**
* Alias type for number that are integer
* @ignorenaming
*/
declare type int = number;

declare type TypedArray =
| number[]
| Uint32Array
Expand Down
9 changes: 7 additions & 2 deletions Sources/vtk.d.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
interface IvtkObject {
vtkClass: string;
[attrName: string]: unknown;
}

/**
*
* @param obj
* @return
*/
declare function vtk(obj: object): any;
declare function vtk(obj: IvtkObject): unknown;

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

export default vtk;
File renamed without changes.
20 changes: 11 additions & 9 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ 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');
const relatifyImports = require('./Utilities/build/rewrite-imports.js');

const IGNORE_LIST = [
/[/\\]example_?[/\\]/,
Expand Down Expand Up @@ -182,14 +182,16 @@ export default {
transform(content, base) {
// transforms typescript defs to use absolute package imports
if (base.endsWith('.d.ts')) {
return absolutifyImports(content.toString(), (relImport) => {
const importPath = path.join(path.dirname(base), relImport);
const relativeStart = path.join(__dirname, 'Sources');
// rollup builds are for the @kitware/vtk.js package
return path.join(
'@kitware/vtk.js',
path.relative(relativeStart, importPath)
);
return relatifyImports(content.toString(), (relImport) => {
let importPath = relImport;
if (relImport.startsWith('../')) {
importPath = relImport.slice(3);
}

if (!importPath.startsWith('../')) {
importPath = `./${importPath}`;
}
return importPath;
});
}
return content;
Expand Down
2 changes: 1 addition & 1 deletion webpack.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const ESLintWebpackPlugin = require('eslint-webpack-plugin');
const pkg = require('./package.json');
const settings = require('./webpack.settings.js');

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

// basic regex for matching imports
const importRegex = /(?:import|from) ['"]([^'"]*)['"]/g;
Expand Down

0 comments on commit fcf257c

Please sign in to comment.