Skip to content

Commit 893ad44

Browse files
authored
chore: improve the configuration to build the npm package (maxGraph#87)
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).
1 parent 6180434 commit 893ad44

27 files changed

+304
-102
lines changed

.github/workflows/build.yml

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,22 @@ jobs:
3131
matrix:
3232
os: ['ubuntu-20.04', 'macos-11', 'windows-2019']
3333
steps:
34-
- uses: actions/checkout@v2
34+
- uses: actions/checkout@v3
3535
- name: Setup node
36-
uses: actions/setup-node@v2
36+
uses: actions/setup-node@v3
3737
with:
38-
node-version: '16'
39-
- name: Install dependencies and build @maxgraph/core
38+
node-version-file: '.nvmrc'
39+
- name: Install dependencies
4040
run: npm install
41-
- name: Generate TypeScript declaration files
41+
- name: Build @maxgraph/core - umd
42+
working-directory: packages/core
43+
run: npm run build
44+
- name: Build @maxgraph/core - esm
45+
working-directory: packages/core
46+
run: npm run generate-esm
47+
- name: Generate @maxgraph/core types
48+
working-directory: packages/core
4249
run: npm run generate-types
50+
- name: Build ts-example
51+
working-directory: packages/ts-example
52+
run: npm run build

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ javascript/examples/**/dist
77
*.iml
88
dist
99
package-lock.json
10+
*.tgz
1011

1112
# dependencies
1213
/node_modules

.npmignore

Lines changed: 0 additions & 22 deletions
This file was deleted.

.nvmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
16

README.md

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Chrome, Edge, Firefox, Safari, Chromium based browsers (Brave, Opera, ....) for
3232
maxGraph is under active development. An alpha version will be released soon with the npm package once [licensing](#license)
3333
and [versioning](https://github.com/maxGraph/maxGraph/issues/92) issues have been resolved.
3434

35-
In the meantime, you can test the project by running the [Storybook examples](#development).
35+
In the meantime, you can test the project by running the [Storybook examples](#development) or [build the npm package locally](#build-local-npm-package).
3636

3737

3838
## <a id="license"></a> Transition to Apache 2.0 license
@@ -52,10 +52,11 @@ const container = <HTMLElement>document.getElementById('graph-container');
5252
// Disables the built-in context menu
5353
InternalEvent.disableContextMenu(container);
5454

55+
const graph = new Graph(container);
56+
graph.setPanning(true); // Use mouse right button for panning
5557
// Gets the default parent for inserting new cells. This
5658
// is normally the first child of the root (ie. layer 0).
5759
const parent = graph.getDefaultParent();
58-
graph.setPanning(true); // Use mouse right button for panning
5960

6061
// Adds cells to the model in a single step
6162
graph.batchUpdate(() => {
@@ -65,7 +66,7 @@ graph.batchUpdate(() => {
6566
});
6667
```
6768

68-
You will see something like in the following:
69+
You will see something like in the following _maxGraph panning_ demo:
6970

7071
![maxGraph panning demo](docs/maxgraph_demo.gif "maxGraph panning demo")
7172

@@ -162,6 +163,12 @@ and select `@mxgraph/html`.
162163
Since both commands are in watch mode, so it's recommended to open two terminals and run them separately. When a file is saved from the core package, the html storybook will be automatically updated.
163164

164165

165-
### Building the npm package for local usage
166+
### <a id="build-local-npm-package"></a> Building the npm package for usage in external project
167+
168+
Run
169+
- from the project root: `npm install`
170+
- then, from the `packages/core` folder: `npm pack`
171+
172+
The `packages/core` folder or the generated `packages/core/maxgraph-core-***.tgz` file are now ready for use in an external project, using [npm link](https://docs.npmjs.com/cli/v8/commands/npm-link) or `npm install`.
166173

167-
Will be documented soon.
174+
Examples of use can be found in this [repository].(https://github.com/tbouffard/maxgraph-integration-examples).

package.json

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,21 @@
11
{
22
"name": "maxgraph",
3-
"description": "max-graph is a fully client side JavaScript diagramming library that uses SVG and HTML for rendering.",
4-
"version": "1.0.0",
3+
"license": "Apache-2.0",
4+
"version": "0.1.0",
5+
"description": "maxGraph is a fully client side JavaScript diagramming library that uses SVG and HTML for rendering.",
56
"homepage": "https://github.com/maxGraph/maxGraph",
6-
"author": {
7-
"name": "",
8-
"email": ""
9-
},
10-
"license": "MIT",
117
"repository": {
128
"type": "git",
139
"url": "git+https://github.com/maxGraph/maxGraph"
1410
},
1511
"bugs": {
1612
"url": "https://github.com/maxGraph/maxGraph/issues"
1713
},
18-
"main": "./javascript/dist/build.js",
1914
"scripts": {
2015
"dev": "node ./scripts/dev",
2116
"bootstrap": "lerna bootstrap --no-ci",
2217
"postinstall": "lerna bootstrap --no-ci",
23-
"docs": "jsdoc -c jsdoc.json",
24-
"generate-types": "lerna run generate-types"
18+
"docs": "jsdoc -c jsdoc.json"
2519
},
2620
"devDependencies": {
2721
"@babel/core": "^7.16.0",
@@ -33,6 +27,7 @@
3327
"babel-loader": "^8.2.3",
3428
"better-docs": "^2.3.2",
3529
"css-loader": "^6.5.1",
30+
"cross-env": "~7.0.3",
3631
"file-loader": "^6.2.0",
3732
"jsdoc": "^3.6.7",
3833
"lerna": "^4.0.0",
@@ -44,6 +39,5 @@
4439
"webpack": "^5.64.4",
4540
"webpack-cli": "^4.9.1",
4641
"webpack-merge": "^5.8.0"
47-
},
48-
"sideEffects": true
42+
}
4943
}

packages/core/index.d.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

packages/core/package.json

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,32 @@
11
{
22
"name": "@maxgraph/core",
3-
"version": "1.0.0",
4-
"description": "",
5-
"module": "./src/index.ts",
6-
"main": "./dist/maxgraph.js",
7-
"types": "./dist/maxgraph.d.ts",
3+
"license": "Apache-2.0",
4+
"version": "0.1.0",
5+
"description": "maxGraph is a fully client side JavaScript diagramming library that uses SVG and HTML for rendering.",
6+
"main": "dist/maxgraph.js",
7+
"module": "dist/esm/index.js",
8+
"types": "dist/types/index.d.ts",
89
"files": [
9-
"./dist"
10+
"dist"
1011
],
12+
"homepage": "https://github.com/maxGraph/maxGraph",
13+
"repository": {
14+
"type": "git",
15+
"url": "git+https://github.com/maxGraph/maxGraph"
16+
},
17+
"bugs": {
18+
"url": "https://github.com/maxGraph/maxGraph/issues"
19+
},
1120
"scripts": {
1221
"dev": "webpack --watch --mode=development",
13-
"build": "NODE_ENV=production webpack --mode=production",
14-
"postinstall": "webpack --mode=development",
15-
"generate-types": "tsc --emitDeclarationOnly"
22+
"build": "cross-env NODE_ENV=production webpack --mode=production",
23+
"generate-types": "tsc --emitDeclarationOnly",
24+
"generate-esm": "tsc --emitDeclarationOnly false --declaration false --declarationDir null",
25+
"prepack": "run-s generate-types generate-esm build"
1626
},
17-
"author": "",
18-
"license": "ISC",
1927
"devDependencies": {
20-
"circular-dependency-plugin": "^5.2.2"
28+
"circular-dependency-plugin": "^5.2.2",
29+
"npm-run-all": "~4.1.5"
2130
},
2231
"sideEffects": true
2332
}

packages/core/src/Client.ts

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,9 @@
55

66
class Client {
77
/**
8-
* Class: Client
9-
*
10-
* Bootstrapping mechanism for the mxGraph thin client. The production version
11-
* of this file contains all code required to run the mxGraph thin client, as
12-
* well as global constants to identify the browser and operating system in
13-
* use. You may have to load chrome://global/content/contentAreaUtils.js in
14-
* your page to disable certain security restrictions in Mozilla.
15-
*
16-
* Contains the current version of the mxGraph library. The strings that
17-
* communicate versions of mxGraph use the following format.
18-
*
19-
* versionMajor.versionMinor.buildNumber.revisionNumber
20-
*
21-
* Current version is 4.2.2.
8+
* Contains the current version of the maxGraph library.
229
*/
23-
static VERSION = '4.2.2';
10+
static VERSION = '0.1.0';
2411

2512
/**
2613
* Optional global config variable to specify the extension of resource files.
@@ -37,7 +24,7 @@ class Client {
3724

3825
static setResourceExtension = (value: string) => {
3926
Client.mxResourceExtension = value;
40-
27+
4128
// Removes dependency with mxResources.
4229
// Client.mxResourceExtension can be used instead.
4330
// mxResources.extension = value;
@@ -143,7 +130,7 @@ class Client {
143130
* contains the assignment. Trailing slashes are automatically removed.
144131
*/
145132
static imageBasePath = '.';
146-
133+
147134
static setImageBasePath = (value: string) => {
148135
if (typeof value !== 'undefined' && value.length > 0) {
149136
// Adds a trailing slash if required
@@ -416,9 +403,9 @@ class Client {
416403
* @param id unique id for the link element to check if it already exists
417404
*/
418405
static link = (
419-
rel: string,
420-
href: string,
421-
doc: Document | null=null,
406+
rel: string,
407+
href: string,
408+
doc: Document | null=null,
422409
id: string | null=null
423410
) => {
424411
doc = doc || document;

packages/core/src/editor/EditorToolbar.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import { NODETYPE } from '../util/Constants';
2424
import Translations from '../util/Translations';
2525
import MaxLog from '../gui/MaxLog';
2626
import Codec from '../serialization/Codec';
27-
import { DropHandler } from 'src/view/other/DragSource';
27+
import type { DropHandler } from '../view/other/DragSource';
2828

2929
/**
3030
* Toolbar for the editor. This modifies the state of the graph
@@ -204,10 +204,10 @@ export class EditorToolbar {
204204
* @param funct - Optional JavaScript function that takes the {@link Editor} as the first and only argument that is executed after the mode has been selected.
205205
*/
206206
addMode(
207-
title: string,
208-
icon: string,
209-
mode: string,
210-
pressed: string | null=null,
207+
title: string,
208+
icon: string,
209+
mode: string,
210+
pressed: string | null=null,
211211
funct: Function | null=null
212212
): any {
213213
const clickHandler = () => {
@@ -238,11 +238,11 @@ export class EditorToolbar {
238238
* Default is true.
239239
*/
240240
addPrototype(
241-
title: string,
242-
icon: string,
243-
ptype: Function | Cell,
244-
pressed: string,
245-
insert: (editor: Editor, cell: Cell, me: MouseEvent, cellUnderMousePointer?: Cell | null) => void,
241+
title: string,
242+
icon: string,
243+
ptype: Function | Cell,
244+
pressed: string,
245+
insert: (editor: Editor, cell: Cell, me: MouseEvent, cellUnderMousePointer?: Cell | null) => void,
246246
toggle: boolean=true
247247
): HTMLImageElement | HTMLButtonElement {
248248
// Creates a wrapper function that is in charge of constructing

packages/core/src/index.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,11 @@ export { default as VisibleChange } from './view/undoable_changes/VisibleChange'
4848

4949
export { EditorKeyHandler, EditorKeyHandlerCodec } from './editor/EditorKeyHandler';
5050
export { EditorPopupMenu, EditorPopupMenuCodec } from './editor/EditorPopupMenu';
51-
export { EditorToolbar, EditorToolbarCodec } from './editor/EditorToolbar';
51+
// TODO restore EditorToolbarCodec if needed
52+
// currently, the following error occurs when running vitejs build
53+
// [ERROR] No matching export in "../core/dist/esm/editor/EditorToolbar.js" for import "EditorToolbarCodec"
54+
// export { EditorToolbar, EditorToolbarCodec } from './editor/EditorToolbar';
55+
export { EditorToolbar } from './editor/EditorToolbar';
5256
export { Editor, EditorCodec } from './editor/Editor';
5357

5458
export { default as CellHighlight } from './view/cell/CellHighlight';
@@ -156,7 +160,11 @@ export { default as Rectangle } from './view/geometry/Rectangle';
156160
export { default as EdgeStyle } from './view/style/EdgeStyle';
157161
export { default as Perimeter } from './view/style/Perimeter';
158162
export { default as StyleRegistry } from './view/style/StyleRegistry';
159-
export { Stylesheet, StylesheetCodec } from './view/style/Stylesheet';
163+
// TODO restore StylesheetCodec if needed
164+
// currently, the following error occurs when running vitejs build
165+
// [ERROR] No matching export in "../core/dist/esm/view/style/Stylesheet.js" for import "StylesheetCodec"
166+
// export { Stylesheet, StylesheetCodec } from './view/style/Stylesheet';
167+
export { Stylesheet } from './view/style/Stylesheet';
160168

161169
export * as DomHelpers from './util/domHelpers';
162170

@@ -200,3 +208,6 @@ export { default as CellArray } from './view/cell/CellArray';
200208
export { default as TemporaryCellStates } from './view/cell/TemporaryCellStates';
201209
export { default as ConnectionConstraint } from './view/other/ConnectionConstraint';
202210
export { default as Multiplicity } from './view/other/Multiplicity';
211+
212+
// Ensure types are exported in the type definitions
213+
export * from './types';

packages/core/src/util/styleUtils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import Rectangle from '../view/geometry/Rectangle';
1919
import Cell from '../view/cell/Cell';
2020
import GraphDataModel from '../view/GraphDataModel';
2121
import CellArray from '../view/cell/CellArray';
22-
import { CellStateStyle, CellStyle, NumericCellStateStyleKeys } from 'src/types';
22+
import type { CellStateStyle, CellStyle, NumericCellStateStyleKeys } from '../types';
2323

2424
/**
2525
* Removes the cursors from the style of the given DOM node and its

packages/core/src/view/handler/ConstraintHandler.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ import ConnectionConstraint from '../other/ConnectionConstraint';
2626
import Point from '../geometry/Point';
2727
import Cell from '../cell/Cell';
2828

29-
import pointImg from '../../../images/point.gif';
30-
3129
/**
3230
* Handles constraints on connection targets. This class is in charge of
3331
* showing fixed points when the mouse is over a vertex and handles constraints
@@ -39,8 +37,7 @@ class ConstraintHandler {
3937
/**
4038
* {@link Image} to be used as the image for fixed connection points.
4139
*/
42-
// pointImage: mxImage;
43-
pointImage = new Image(pointImg, 5, 5);
40+
pointImage = new Image(`${Client.imageBasePath}/point.gif`, 5, 5);
4441

4542
/**
4643
* Reference to the enclosing {@link mxGraph}.

packages/core/src/view/mixins/EdgeMixin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import InternalEvent from '../event/InternalEvent';
99
import Dictionary from '../../util/Dictionary';
1010
import { Graph } from '../Graph';
1111
import Point from '../geometry/Point';
12-
import { CellStyle } from 'src/types';
12+
import type { CellStyle } from '../../types';
1313

1414
declare module '../Graph' {
1515
interface Graph {

packages/core/src/view/mixins/VertexMixin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import Geometry from '../geometry/Geometry';
33
import { Graph } from '../Graph';
44
import CellArray from '../cell/CellArray';
55
import { mixInto } from '../../util/Utils';
6-
import { CellStyle } from 'src/types';
6+
import type { CellStyle } from '../../types';
77

88
declare module '../Graph' {
99
interface Graph {

packages/core/tsconfig.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
"baseUrl": ".",
44
"allowSyntheticDefaultImports": true,
55
"declaration": true,
6-
"declarationDir": "./dist",
6+
"declarationDir": "./dist/types",
77
"declarationMap": false,
88
"emitDeclarationOnly": true,
99
"esModuleInterop": true,
1010
"isolatedModules": true,
1111
"module": "ES2020",
1212
"lib": ["dom"],
1313
"moduleResolution": "node",
14-
"outDir": "./dist",
14+
"outDir": "./dist/esm",
1515
"strict": true,
1616
"target": "ES2020",
1717
"skipLibCheck": false,

0 commit comments

Comments
 (0)