cytoscape / cytoscape.js

Graph theory (network) library for visualisation and analysis
https://js.cytoscape.org
MIT License
10.09k stars 1.64k forks source link

TypeScript: Cannot assign to read only property 'remove' when monkey-patching Array.prototype #2809

Closed HEmile closed 3 years ago

HEmile commented 3 years ago

Environment info

Current (buggy) behaviour

I'm trying to initialize Cytoscape.js for an obsidian plugin. I've imported Cytoscape as follows: import cytoscape from 'cytoscape';, and I'm calling it a bit later using const viz = cytoscape();.

I'm also using Rollup and Typescript.

The resulting main.js I'm using crashes with

app.js:1 Plugin failure: neo4j-graph-view TypeError: Cannot assign to read only property 'remove' of object '[object Object]'
    at eval (eval at <anonymous> (app.js:1), <anonymous>:52657:17)
    at e.<anonymous> (app.js:1)
    at app.js:1
    at Object.next (app.js:1)
    at a (app.js:1)

This error can be resolved by commenting out elesfn$u.remove in the resulting main.js. I think this is this line. Is this a bug/incompatibility?

The same crash happens when I just run in the codebase

const elesfn = Object.create(Array.prototype);
elesfn.remove = function(a:any) {};

My tsconfig.json:

{
  "compilerOptions": {
    "baseUrl": ".",
    "inlineSourceMap": true,
    "inlineSources": true,
    "module": "ESNext",
    "target": "es2015",
    "allowJs": true,
    "noImplicitAny": true,
    "moduleResolution": "node",
    "importHelpers": true,
    "lib": [
      "dom",
      "es5",
      "scripthost",
      "es2015"
    ],
    "esModuleInterop": true,
    "typeRoots": [
      "./node_modules/@types/"
    ]
  },
  "include": [
    "**/*.ts"
  ]
}

The rollup.config.js:

import typescript from '@rollup/plugin-typescript';
import {nodeResolve} from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import copy from 'rollup-plugin-copy';

export default {
  input: 'main.ts',
  output: {
    dir: '.',
    sourcemap: 'inline',
    format: 'cjs',
    exports: 'default',
  },
  external: ['obsidian'],
  plugins: [

    typescript(),
    nodeResolve({browser: true}),
    commonjs({
      include: 'node_modules/**',
    }),
    copy({
      targets: [
        {src: 'main.js', dest: '../../semantic-obsidian/Semantic Obsidian/.obsidian/plugins/neo4j-graph-view'},
      ],
      hook: 'writeBundle',
    }),
  ],
};
HEmile commented 3 years ago

I've been in contact with the developers of Obsidian. They've been manipulating Array.prototype as well, but in such a way that it becomes readonly. They'll fix this in their next release.