Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

patch(): node build follow up #8652

Merged
merged 27 commits into from
Feb 5, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## [next]

- patch(): Node entry point follow up [#8652](https://github.com/fabricjs/fabric.js/pull/8652)
- patch(): Added WebGLProbe to env, removed isLikelyNode, added specific env dispose ( instead of cleanup JSDOM ) [#8652](https://github.com/fabricjs/fabric.js/pull/8652)
- ci(): Removed the browser publish script [#8656](https://github.com/fabricjs/fabric.js/pull/8656)
- feat(): Node entry point [#8632](https://github.com/fabricjs/fabric.js/pull/8632)
- chore(): Change import and export strategy [#8622](https://github.com/fabricjs/fabric.js/pull/8622)
Expand Down
4 changes: 2 additions & 2 deletions fabric.ts
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ import { Textbox } from './src/shapes/Textbox';
import { Group } from './src/shapes/Group';
import { ActiveSelection } from './src/shapes/ActiveSelection';
import { Image } from './src/shapes/Image';
import { getEnv, getDocument, getWindow, setEnv } from './src/env';
import { getEnv, getDocument, getWindow, setEnvForTests } from './src/env';
import { createCollectionMixin } from './src/Collection';
import { parseAttributes } from './src/parser/parseAttributes';
import { parseElements } from './src/parser/parseElements';
Expand Down Expand Up @@ -416,7 +416,7 @@ export {
getEnv,
getDocument,
getWindow,
setEnv,
setEnvForTests,
parseStyleAttribute,
parsePointsAttribute,
parseFontDeclaration,
Expand Down
2 changes: 1 addition & 1 deletion src/env/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const getEnv = (): TFabricEnv => {
document,
window,
isTouchSupported,
GLProbe: new WebGLProbe(),
WebGLProbe: new WebGLProbe(),
dispose() {
// noop
},
Expand Down
8 changes: 7 additions & 1 deletion src/env/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import { TFabricEnv } from './types';
import { getEnv as getBrowserEnv } from './browser';
import type { DOMWindow } from 'jsdom';

let env: TFabricEnv;

Expand All @@ -20,4 +21,9 @@ export const getEnv = () => env || getBrowserEnv();

export const getDocument = (): Document => getEnv().document;

export const getWindow = (): Window => getEnv().window;
export const getWindow = (): Window | DOMWindow => getEnv().window;

export const setEnvForTests = (window: Window | DOMWindow) => {
env.document = window.document;
env.window = window;
};
6 changes: 2 additions & 4 deletions src/env/node.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable no-restricted-globals */
import type { Canvas as NodeCanvas } from 'canvas';
import { JSDOM } from 'jsdom';
// @ts-ignore
import utils from 'jsdom/lib/jsdom/living/generated/utils.js';
import { config } from '../config';
import { NodeGLProbe } from '../filters/GLProbes/NodeGLProbe';
Expand All @@ -16,9 +17,6 @@ const { window: JSDOMWindow } = new JSDOM(
'%3C!DOCTYPE%20html%3E%3Chtml%3E%3Chead%3E%3C%2Fhead%3E%3Cbody%3E%3C%2Fbody%3E%3C%2Fhtml%3E'
),
{
features: {
FetchExternalResources: ['img'],
},
resources: 'usable',
// needed for `requestAnimationFrame`
pretendToBeVisual: true,
Expand All @@ -39,7 +37,7 @@ export const getEnv = (): TFabricEnv => {
document: JSDOMWindow.document,
window: JSDOMWindow,
isTouchSupported: false,
GLProbe: new NodeGLProbe(),
WebGLProbe: new NodeGLProbe(),
dispose(element) {
const impl = jsdomImplForWrapper(element);
if (impl) {
Expand Down
5 changes: 3 additions & 2 deletions src/env/types.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { GLProbe } from '../filters/GLProbes/GLProbe';
import type { DOMWindow } from 'jsdom';

export type TCopyPasteData = {
copiedText?: string;
copiedStyle?: Record<string, string>;
};
export type TFabricEnv = {
document: Document;
window: Window;
window: Window | DOMWindow;
isTouchSupported: boolean;
GLProbe: GLProbe;
WebGLProbe: GLProbe;
dispose(element: Element): void;
copyPasteData: TCopyPasteData;
};
6 changes: 3 additions & 3 deletions src/filters/BaseFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ export abstract class AbstractBaseFilter<T> {
fragmentSource: string = this.getFragmentSource(),
vertexSource: string = this.vertexSource
) {
const { GLProbe } = getEnv();
if (GLProbe.GLPrecision && GLProbe.GLPrecision !== GLPrecision.high) {
const { WebGLProbe } = getEnv();
if (WebGLProbe.GLPrecision && WebGLProbe.GLPrecision !== GLPrecision.high) {
fragmentSource = fragmentSource.replace(
new RegExp(highPsourceCode, 'g'),
highPsourceCode.replace(GLPrecision.high, GLProbe.GLPrecision)
highPsourceCode.replace(GLPrecision.high, WebGLProbe.GLPrecision)
);
}
const vertexShader = gl.createShader(gl.VERTEX_SHADER);
Expand Down
6 changes: 3 additions & 3 deletions src/filters/FilterBackend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ let filterBackend: FilterBackend;
* Verifies if it is possible to initialize webgl or fallback on a canvas2d filtering backend
*/
export function initFilterBackend(): FilterBackend {
const { GLProbe } = getEnv();
GLProbe.queryGL();
if (config.enableGLFiltering && GLProbe.isSupported(config.textureSize)) {
const { WebGLProbe } = getEnv();
WebGLProbe.queryWebGL();
if (config.enableGLFiltering && WebGLProbe.isSupported(config.textureSize)) {
return new WebGLFilterBackend({ tileSize: config.textureSize });
} else {
return new Canvas2dFilterBackend();
Expand Down
2 changes: 1 addition & 1 deletion src/filters/GLProbes/GLProbe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ export enum GLPrecision {

export abstract class GLProbe {
declare GLPrecision: GLPrecision | undefined;
abstract queryGL(): void;
abstract queryWebGL(): void;
abstract isSupported(textureSize: number): boolean;
}
2 changes: 1 addition & 1 deletion src/filters/GLProbes/NodeGLProbe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { GLProbe } from './GLProbe';
* - https://github.com/akira-cn/node-canvas-webgl
*/
export class NodeGLProbe extends GLProbe {
queryGL() {
queryWebGL() {
// noop
}
isSupported() {
Expand Down
2 changes: 1 addition & 1 deletion src/filters/GLProbes/WebGLProbe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export class WebGLProbe extends GLProbe {
/**
* query browser for WebGL
*/
queryGL() {
queryWebGL() {
const canvas = createCanvasElement();
const gl = canvas.getContext('webgl');
if (gl) {
Expand Down
6 changes: 4 additions & 2 deletions src/util/dom_misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { getDocument } from '../env';
* @param {Object} [attributes] Attributes to set on a wrapper
* @return {HTMLElement} wrapper
*/
export function wrapElement(element: HTMLElement, wrapper: HTMLElement) {
export function wrapElement(element: HTMLElement, wrapper: HTMLDivElement) {
if (element.parentNode) {
element.parentNode.replaceChild(wrapper, element);
}
Expand All @@ -33,10 +33,12 @@ export function getScrollLeftTop(element: HTMLElement) {
// to account for ShadowDOM. We still want to traverse up out of ShadowDOM,
// but the .parentNode of a root ShadowDOM node will always be null, instead
// it should be accessed through .host. See http://stackoverflow.com/a/24765528/4383938
// @ts-ignore
while (element && (element.parentNode || element.host)) {
// Set element to element parent, or 'host' in case of ShadowDOM
// @ts-ignore
element = element.parentNode || element.host;

// @ts-expect-error because element is typed as HTMLElement but it can go up to document
if (element === getDocument()) {
left = body.scrollLeft || docElement.scrollLeft || 0;
top = body.scrollTop || docElement.scrollTop || 0;
Expand Down
2 changes: 1 addition & 1 deletion test/node_test_setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ var virtualWindow = new jsdom.JSDOM(
resources: new CustomResourceLoader(),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do we really need this?
Is just a console log...
And then we don't need to reconfigure the env so we won't need to expose setEnvForTests as well

Copy link
Member

Choose a reason for hiding this comment

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

we won't need to expose it in fabricJS, nor remove it, we will probably be able to import it directly from the build folder.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I mean we don't need this configuration

Copy link
Contributor Author

@ShaMan123 ShaMan123 Feb 5, 2023

Choose a reason for hiding this comment

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

The CustomResourceLoader justs log to console a line
It is meaningless

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So we can use the node env from fabric w/o this headache

pretendToBeVisual: true
}).window;
fabric.setEnv({ ...fabric.getEnv(), window: virtualWindow, document: virtualWindow.document });
fabric.setEnvForTests(virtualWindow);


// QUnit Logging
Expand Down