Skip to content

Conversation

@tbouffard
Copy link
Member

@tbouffard tbouffard commented May 9, 2022

Root and all package.json files

  • Fix license, version and authors
  • Remove the non-relevant .npmignore file at the project. It was designed for mxgraph, the whole directory structure
    changed, and we don't plan to publish the root package

core package

  • Generate type definition and javascript files for ESM output
    • dedicated folder for esm and types in the dist folder to clearly separate concerns
    • exported in the package.json
  • Add npm script for npm pack configuration
  • Drop the postinstall npm script that built the core bundle. It would have been run when the package is installed
    in dependent projects at dependencies install. And it would have failed as it tries to build the project from TypeScript
    sources that are not available in the npm package.

fix: ConstraintHandler now loads image as everywhere else in the code using Client.imageBasePath
Using import doesn't work as the image is not available in the npm package. It generated error like this in ViteJS
project:
Failed to resolve import "../../../images/point.gif" from "../core/dist/esm/view/handler/ConstraintHandler.js". Does the file exist?
This gif import had been introduced to make Anchors.stories.js retrieve the gif file. Configuring imageBasePath as
already done in other stories fixes the issue.

Don't export EditorCodec and StylesheetCodec as they generate errors.
This is a temporary solution to avoid error in dependent project (at least, with ViteJS projects).

closes #84

Notes

This contribution is done under the terms of the Apache 2.0 license as stated in https://www.apache.org/licenses/LICENSE-2.0.

How to use

Run

  • from the project root: npm install
  • then, from the core package: npm pack
  • you can finally use the core folder or the package tgz file in an external project (using npm link or npm install)

Testing project

This Pull Request initially add a TypeScript integration example built with ViteJS.
It allows testing the maxgraph core npm package in a TypeScript project. This ensures that the types and the esm code is correctly configured in the package and available for integration.
I don't know if we should keep this project in the repository or if it deserves its own repository. Feel free to comment about it.

About the gif import removal

The gif import was introduced to fix the Anchors example in b2bb10e#diff-1ef320fff9bb7fc4d6aa19a32348357901420e1724b8288d9a22f1f51fad34cc
Restoring the original Client.imageBasePath usage only required to configure the value in the story.

Commented exports

EditorCodec and StylesheetCodec are no more exported. I will test the package with a Rollup project and probably with other build tools/bundlers to see if they have issues when these 2 classes are exported.
In the meantime, if you find a way to restore the exports, feel free to comment.

tbouffard added 2 commits May 9, 2022 17:56
Root and all package.json files
  - Fix license, version and authors
  - Remove the non-relevant .npmignore file at the project. It was designed for mxgraph, the whole directory structure
  changed, and we don't plan to publish the root package

`core` package
  - Generate type definition and javascript files for ESM output
    - dedicated folder for `esm` and `types` in the `dist` folder to clearly separate concerns
    - exported in the package.json
  - Add npm script for `npm pack` configuration
  - Drop the `postinstall` npm script that built the `core` bundle. It would have been run when the package is installed
  in dependent projects at dependencies install. And it would have failed as it tries to build the project from TypeScript
  sources that are not available in the npm package.

fix: ConstraintHandler now loads image as everywhere else in the code using `Client.imageBasePath`
Using import doesn't work as the image is not available in the npm package. It generated error like this in ViteJS
project:
Failed to resolve import "../../../images/point.gif" from "../core/dist/esm/view/handler/ConstraintHandler.js". Does the file exist?
This gif import had been introduced to make `Anchors.stories.js` retrieve the gif file. Configuring `imageBasePath` as
already done in other stories fixes the issue.

Don't export EditorCodec and StylesheetCodec as they generate errors.
This is a temporary solution to avoid error in dependent project (at least, with ViteJS projects).
Let test the mxgraph core npm package in a TypeScript project. This ensures that the types and the esm code is correctly
configured in the package and available for integration.
Drop npm scripts in the root package.json file that only apply to the `core` package
@tbouffard tbouffard force-pushed the chore/prepare_npm_package branch from 48c5258 to e5b3756 Compare May 9, 2022 16:19
@tbouffard tbouffard changed the title chore: improve the configuration to be able to build the npm package chore: improve the configuration to build the npm package May 10, 2022
@junsikshim
Copy link
Collaborator

Great work!
I've never tried Vite yet, does it have some merits?

I agree with the gif import removal. I don't know what I was thinking...

@tbouffard
Copy link
Member Author

tbouffard commented May 18, 2022

Great work!

Thanks, I am still working on testing the package with other bundler/build-tools to have a more complete coverage.
We can also merge the PR as it is today and do the fix later if needed.

I've never tried Vite yet, does it have some merits?

About Vite, I choose it here because it allows to check the npm package in 2 ways

  • unbundled development: direct use of the esm js files, very (very) fast dev server startup and updates
  • production build with Rollup without any configuration: ensure that the Rollup bundler is able to bundle application with the maxGraph package

It also provides a lot of features not needed here (CSS modules, JSX/TSX, ...) with convenient defaults and a large plugins ecosystem. I like it also for simple project and prototyping because the default configuration lets you start very quickly (a single html page and an JS/TS file)
If you want an overview of such existing tools, you can have a look at https://css-tricks.com/comparing-the-new-generation-of-build-tools/

This was referenced Jun 4, 2022
@tbouffard
Copy link
Member Author

tbouffard commented Jun 9, 2022

Hi all, today I have been able to test the npm package produced by this Pull Request in an external repository: https://github.com/tbouffard/maxgraph-integration-examples.

I have been able to use the npm package in TypeScript projects built with Parcel, Rollup and ViteJS. I planned to add more integration projects in the future, but IMHO, the 3 existing projects already show that the integration is working.
Notice that I initiated the 3 projects from existing mxGraph examples created to demonstrate the TypeScript integration of mxGraph with typed-mxgraph. It was pretty straightforward. Mainly, remove the mx prefix and change the imports 😸
Even if the existing examples didn't use a lot of the former mxGraph API and custom code, this is a good sign for existing mxGraph users.

During the tests, I have seen some problems with existing function signatures and types, but I won't list them all here. I will create issues or Pull Requests to try to fix them later. There are some comments in the code of my repository.
This repository also describes how I managed to use the locally built maxgraph npm package if someone is interested. Feel free to provide feeback, I am pretty sure there are alternatives or better ways to manage the integration.

I have detected some issues on the generated types, that require the TS projects set "skipLibCheck": true in the TS compilerOptions.
I have attached at the end of this comment the error I get with the ts-example in this repository but I reproduce the problem with my external repository as well. I have been able to quickly fix some errors, but I didn't have time to do more today.
Last but not least, I have updated the GitHub workflow to ensure the ts-example builds without error.

I put this Pull Request in review; I consider this is a good first step to let people built the package locally and test it in their project.
Improvements and fixes could be added later.

Generated types errors
> @maxgraph/ts-example-built-with-vite@0.0.0 build
> tsc && vite build

../core/dist/types/view/cell/CellArray.d.ts:4:5 - error TS2416: Property 'concat' in type 'CellArray' is not assignable to the same property in base type 'Cell[]'.
Type '(items: any) => CellArray' is not assignable to type '{ (...items: ConcatArray[]): Cell[]; (...items: (Cell | ConcatArray)[]): Cell[]; }'.
Call signature return types 'CellArray' and 'Cell[]' are incompatible.
The types returned by 'map(...).pop()' are incompatible between these types.
Type 'Cell | undefined' is not assignable to type 'U | undefined'.
Type 'Cell' is not assignable to type 'U'.
'U' could be instantiated with an arbitrary type which could be unrelated to 'Cell'.

4 concat(items: any): CellArray;
~~~~~~

../core/dist/types/view/cell/CellArray.d.ts:5:5 - error TS2416: Property 'splice' in type 'CellArray' is not assignable to the same property in base type 'Cell[]'.
Type '(arg0: number, ...args: any) => CellArray' is not assignable to type '{ (start: number, deleteCount?: number | undefined): Cell[]; (start: number, deleteCount: number, ...items: Cell[]): Cell[]; }'.
Type 'CellArray' is not assignable to type 'Cell[]'.

5 splice(arg0: number, ...args: any): CellArray;
~~~~~~

../core/dist/types/view/cell/CellArray.d.ts:6:5 - error TS2416: Property 'slice' in type 'CellArray' is not assignable to the same property in base type 'Cell[]'.
Type '(...args: any) => CellArray' is not assignable to type '(start?: number | undefined, end?: number | undefined) => Cell[]'.
Type 'CellArray' is not assignable to type 'Cell[]'.

6 slice(...args: any): CellArray;
~~~~~

../core/dist/types/view/cell/CellArray.d.ts:7:5 - error TS2416: Property 'map' in type 'CellArray' is not assignable to the same property in base type 'Cell[]'.
Type '(arg0: any, ...args: any) => CellArray' is not assignable to type '(callbackfn: (value: Cell, index: number, array: Cell[]) => U, thisArg?: any) => U[]'.
Call signature return types 'CellArray' and 'U[]' are incompatible.
The types returned by 'pop()' are incompatible between these types.
Type 'Cell | undefined' is not assignable to type 'U | undefined'.
Type 'Cell' is not assignable to type 'U'.
'U' could be instantiated with an arbitrary type which could be unrelated to 'Cell'.

7 map(arg0: any, ...args: any): CellArray;
~~~

../core/dist/types/view/cell/CellArray.d.ts:8:5 - error TS2416: Property 'filter' in type 'CellArray' is not assignable to the same property in base type 'Cell[]'.
Type '(arg0: any, ...args: any) => CellArray' is not assignable to type '{ (predicate: (value: Cell, index: number, array: Cell[]) => value is S, thisArg?: any): S[]; (predicate: (value: Cell, index: number, array: Cell[]) => unknown, thisArg?: any): Cell[]; }'.
Call signature return types 'CellArray' and 'any[]' are incompatible.
The types returned by 'map(...).pop()' are incompatible between these types.
Type 'Cell | undefined' is not assignable to type 'U | undefined'.
Type 'Cell' is not assignable to type 'U'.
'U' could be instantiated with an arbitrary type which could be unrelated to 'Cell'.

8 filter(arg0: any, ...args: any): CellArray;
~~~~~~

../core/dist/types/view/layout/HierarchicalLayout.d.ts:203:5 - error TS2416: Property 'traverse' in type 'HierarchicalLayout' is not assignable to the same property in base type 'GraphLayout'.
Type '(vertex: Cell, directed: boolean | undefined, edge: Cell | null | undefined, allVertices: { [key: string]: Cell; } | null | undefined, currentComp: { [key: string]: Cell | null; }, hierarchyVertices: GraphHierarchyNode[], filledVertexSet?: { ...; } | ... 1 more ... | undefined) => { ...; }' is not assignable to type '(vertex: Cell, directed?: boolean | undefined, func?: Function | undefined, edge?: Cell | undefined, visited?: Dictionary<Cell, boolean> | undefined) => void'.

203 traverse(vertex: Cell, directed: boolean | undefined, edge: Cell | null | undefined, allVertices: {
~~~~~~~~

../core/dist/types/view/layout/SwimlaneLayout.d.ts:220:5 - error TS2416: Property 'traverse' in type 'SwimlaneLayout' is not assignable to the same property in base type 'GraphLayout'.
Type '(vertex: Cell | null | undefined, directed: boolean, edge: Cell | null, allVertices: { [key: string]: Cell; } | null | undefined, currentComp: { [key: string]: Cell; }, hierarchyVertices: GraphHierarchyNode[], filledVertexSet: { ...; } | ... 1 more ... | undefined, swimlaneIndex: number) => { ...; }' is not assignable to type '(vertex: Cell, directed?: boolean | undefined, func?: Function | undefined, edge?: Cell | undefined, visited?: Dictionary<Cell, boolean> | undefined) => void'.

220 traverse(vertex: Cell | null | undefined, directed: boolean, edge: Cell | null, allVertices: {

@tbouffard tbouffard marked this pull request as ready for review June 9, 2022 18:10
@tbouffard tbouffard requested review from junsikshim and mcyph June 9, 2022 18:10
@tbouffard
Copy link
Member Author

cc @lalicw, @Seebiscuit, @wrw1997, @cd-yang for testing as you noticed interest for a relase or the npm package

* Contains the current version of the maxGraph library.
*/
static VERSION = '4.2.2';
static VERSION = '0.1.0';
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ℹ️ in the future, we will automate the value update during release

@tbouffard
Copy link
Member Author

tbouffard commented Jul 5, 2022

I am going to merge this Pull Request to let people build the npm package locally and test it in their projects.

@tbouffard tbouffard merged commit 893ad44 into development Jul 5, 2022
@tbouffard tbouffard deleted the chore/prepare_npm_package branch July 5, 2022 06:08
@tbouffard tbouffard added the chore Build, CI/CD or repository tasks (issues/PR maintenance, environments, ...) label Nov 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

chore Build, CI/CD or repository tasks (issues/PR maintenance, environments, ...)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

How to integrate maxGraph into my own project

2 participants