Skip to content

Commit 8f22d97

Browse files
committed
fix types in tests, add skia backend
1 parent d2ecf20 commit 8f22d97

31 files changed

+271
-257
lines changed

eslint.config.mjs

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

konva-node/demo.js

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

konva-node/index.js

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

konva-node/package.json

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

package.json

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,23 @@
33
"version": "9.3.22",
44
"description": "HTML5 2d canvas library.",
55
"author": "Anton Lavrenov",
6+
"type": "module",
67
"files": [
78
"README.md",
89
"konva.js",
910
"konva.min.js",
10-
"lib",
11-
"cmj"
11+
"lib"
1212
],
13-
"main": "./lib/index-node.js",
14-
"browser": "./lib/index.js",
15-
"typings": "./lib/index-types.d.ts",
13+
"exports": {
14+
".": {
15+
"default": "./lib/index.js"
16+
},
17+
"./setup-node-canvas": {
18+
"default": "./lib/setup-node-canvas.js"
19+
}
20+
},
21+
"main": "./lib/index.js",
22+
"types": "./lib/index-types.d.ts",
1623
"scripts": {
1724
"start": "npm run test:watch",
1825
"compile": "npm run clean && npm run tsc && cp ./src/index-types.d.ts ./lib/index-types.d.ts && npm run rollup",
@@ -22,10 +29,12 @@
2229
"test:build": "PARCEL_WORKER_BACKEND=process parcel build ./test/unit-tests.html --dist-dir ./test-build --target none --public-url ./ --no-source-maps",
2330
"test:browser": "npm run test:build && mocha-headless-chrome -f ./test-build/unit-tests.html -a disable-web-security -a no-sandbox -a disable-setuid-sandbox",
2431
"test:watch": "rm -rf ./.parcel-cache && PARCEL_WORKERS=0 parcel serve ./test/unit-tests.html ./test/manual-tests.html ./test/sandbox.html ./test/text-paths.html ./test/bunnies.html",
25-
"test:node:compiled": "rm -rf ./.test-temp && mkdir ./.test-temp && (tsc -p ./test/tsconfig.json --outDir ./.test-temp || true) && mocha './.test-temp/test/unit/**/*.js' -r ./test/node-global-setup.mjs --exit && rm -rf ./.test-temp && npm run test:import",
26-
"test:node": "npm run test:node:compiled",
32+
33+
"test:node:canvas": "rm -rf ./.test-temp && mkdir ./.test-temp && (tsc -p ./test/tsconfig.json --outDir ./.test-temp || true) && node ./rename-imports-test.mjs && mocha './.test-temp/test/unit/**/*.js' -r ./test/node-canvas-global-setup.mjs --exit && rm -rf ./.test-temp && npm run test:import",
34+
"test:node:skia": "rm -rf ./.test-temp && mkdir ./.test-temp && (tsc -p ./test/tsconfig.json --outDir ./.test-temp || true) && node ./rename-imports-test.mjs && mocha './.test-temp/test/unit/**/*.js' -r ./test/node-skia-global-setup.mjs --exit && rm -rf ./.test-temp && npm run test:import",
35+
"test:node": "npm run test:node:canvas && npm run test:node:skia",
2736
"tsc": "tsc --removeComments",
28-
"rollup": "rollup -c --bundleConfigAsCjs",
37+
"rollup": "rollup -c",
2938
"clean": "rm -rf ./lib && rm -rf ./types && rm -rf ./cmj && rm -rf ./test-build",
3039
"watch": "rollup -c -w",
3140
"size": "size-limit"
@@ -61,18 +70,18 @@
6170
}
6271
],
6372
"devDependencies": {
64-
"@parcel/transformer-image": "2.13.2",
65-
"@size-limit/preset-big-lib": "^11.1.6",
73+
"@parcel/transformer-image": "2.15.4",
74+
"@size-limit/preset-big-lib": "^11.2.0",
6675
"@types/mocha": "^10.0.10",
67-
"canvas": "^3.1.0",
68-
"chai": "5.1.2",
76+
"canvas": "^3.1.2",
77+
"chai": "5.2.1",
6978
"filehound": "^1.17.6",
70-
"gulp": "^5.0.0",
79+
"gulp": "^5.0.1",
7180
"gulp-concat": "^2.6.1",
7281
"gulp-connect": "^5.7.0",
7382
"gulp-exec": "^5.0.0",
7483
"gulp-jsdoc3": "^3.0.0",
75-
"gulp-rename": "^2.0.0",
84+
"gulp-rename": "^2.1.0",
7685
"gulp-replace": "^1.1.4",
7786
"gulp-typescript": "^5.0.1",
7887
"gulp-uglify": "^3.0.2",
@@ -82,12 +91,13 @@
8291
"mocha-headless-chrome": "^4.0.0",
8392
"parcel": "2.13.3",
8493
"process": "^0.11.10",
85-
"rollup": "^4.31.0",
94+
"rollup": "^4.46.2",
8695
"rollup-plugin-typescript2": "^0.36.0",
87-
"size-limit": "^11.1.6",
88-
"ts-mocha": "^10.0.0",
96+
"size-limit": "^11.2.0",
97+
"skia-canvas": "^2.0.2",
98+
"ts-mocha": "^11.1.0",
8999
"ts-node": "^10.9.2",
90-
"typescript": "^5.7.3"
100+
"typescript": "^5.9.2"
91101
},
92102
"keywords": [
93103
"canvas",

rename-imports-test.mjs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import FileHound from 'filehound';
2+
import fs from 'fs';
3+
4+
const files = FileHound.create().paths('./.test-temp').ext(['js', 'ts']).find();
5+
6+
files.then((filePaths) => {
7+
filePaths.forEach((filepath) => {
8+
fs.readFile(filepath, 'utf8', (err, text) => {
9+
if (!text.match(/import .* from/g)) {
10+
return;
11+
}
12+
text = text.replace(/(import .* from\s+['"])(.*)(?=['"])/g, '$1$2.js');
13+
if (text.match(/export .* from/g)) {
14+
text = text.replace(/(export .* from\s+['"])(.*)(?=['"])/g, '$1$2.js');
15+
}
16+
17+
if (err) throw err;
18+
19+
// stupid replacement back
20+
text = text.replace("from 'canvas.js';", "from 'canvas';");
21+
text = text.replace("from 'chai.js';", "from 'chai';");
22+
text = text.replace("from 'skia-canvas.js';", "from 'skia-canvas';");
23+
24+
// Handle import("./x/y/z") syntax.
25+
text = text.replace(/(import\s*\(\s*['"])(.*)(?=['"])/g, '$1$2.js');
26+
27+
fs.writeFile(filepath, text, function (err) {
28+
if (err) {
29+
throw err;
30+
}
31+
});
32+
});
33+
});
34+
});
35+
36+
// Removed CommonJS export rewriting to keep ESM output intact

rename-imports.mjs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,4 @@ files.then((filePaths) => {
3434
});
3535
});
3636

37-
const indexFiles = ['lib/index.js', 'lib/index-node.js', 'lib/Core.js'];
38-
indexFiles.forEach((filepath) => {
39-
fs.readFile(filepath, 'utf8', (err, text) => {
40-
text = text.replace('exports.default =', 'module.exports =');
41-
fs.writeFile(filepath, text, function (err) {
42-
if (err) {
43-
throw err;
44-
}
45-
});
46-
});
47-
});
37+
// Removed CommonJS export rewriting to keep ESM output intact

rollup.config.mjs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,12 @@ export default {
1111
sourcemap: false,
1212
freeze: false,
1313
},
14-
// { file: pkg.module, format: 'es', sourcemap: true }
1514
],
16-
// Indicate here external modules you don't wanna include in your bundle (i.e.: 'lodash')
1715
external: [],
1816
watch: {
1917
include: 'src/**',
2018
},
2119
plugins: [
22-
// Allow json resolution
23-
// json(),
2420
// Compile TypeScript files
2521
typescript({
2622
useTsconfigDeclarationDir: true,
@@ -32,14 +28,5 @@ export default {
3228
},
3329
},
3430
}),
35-
// // Allow bundling cjs modules (unlike webpack, rollup doesn't understand cjs)
36-
// commonjs(),
37-
// // Allow node_modules resolution, so you can use 'external' to control
38-
// // which external modules to include in the bundle
39-
// // https://github.com/rollup/rollup-plugin-node-resolve#usage
40-
// resolve(),
41-
42-
// Resolve source maps to the original source
43-
// sourceMaps()
4431
],
4532
};

src/Global.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,11 @@ export const Konva = {
184184
// insert Konva into global namespace (window)
185185
// it is required for npm packages
186186
_injectGlobal(Konva) {
187+
if (typeof glob.Konva !== 'undefined') {
188+
console.error(
189+
'Severa Konva instances detected. It is not recommended to use multiple Konva instances in the same environment.'
190+
);
191+
}
187192
glob.Konva = Konva;
188193
},
189194
};

src/Util.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@ import { Konva } from './Global';
22
import { Context } from './Context';
33
import { IRect, RGB, Vector2d } from './types';
44

5+
const ensureBrowser = () => {
6+
if (typeof document === 'undefined') {
7+
throw new Error(
8+
'document is undefined, this is not a browser environment. For node.js env add `import "konva/register-node"`'
9+
);
10+
}
11+
};
12+
513
/*
614
* Last updated November 2011
715
* By Simon Sarris
@@ -424,7 +432,7 @@ const OBJECT_ARRAY = '[object Array]',
424432
yellowgreen: [154, 205, 5],
425433
},
426434
RGB_REGEX = /rgb\((\d{1,3}),(\d{1,3}),(\d{1,3})\)/;
427-
let animQueue: Array<Function> = [];
435+
let animQueue: Array<Function> = [];
428436

429437
const req =
430438
(typeof requestAnimationFrame !== 'undefined' && requestAnimationFrame) ||
@@ -505,6 +513,7 @@ export const Util = {
505513
}
506514
},
507515
createCanvasElement() {
516+
ensureBrowser();
508517
const canvas = document.createElement('canvas');
509518
// on some environments canvas.style is readonly
510519
try {
@@ -513,6 +522,7 @@ export const Util = {
513522
return canvas;
514523
},
515524
createImageElement() {
525+
ensureBrowser();
516526
return document.createElement('img');
517527
},
518528
_isInDocument(el: any) {

0 commit comments

Comments
 (0)