typescript - 如何使用 Typescript 进行 cytoscape 初始化?

标签 typescript npm ecmascript-6 webpack-2 cytoscape.js

我用 Typescript 构建了一个 React 组件,它使用 cytoscape (及其 types )作为 headless 模型。我的目标是创建一个 NPM 包,以便我可以将其直接导入其他项目。

我的图书馆:

  1. 当我将 cytoscape.js 作为静态 Assets 导入到我的 index.html 中时工作
  2. 当我在新的 es6 应用程序中 npm install 我的组件时不会: Uncaught ReferenceError: cytoscape is not defined at new WbsLayout。尽管在安装我的组件时 cytoscape 被自动安装在 node_modules 中,但还是如此。

typescript 来源

WbsLayout.tsx:

export class WbsLayout  {
    //...
    public cy: Cy.Core;
    constructor(graph: any, Options?: Options) {           
        this.cy = cytoscape({ // <-- Works or fails, see cases #1 and #2 explained above
            headless: true,
            elements: graph
        });
        //...
    }
    //...
}

请注意,我没有在我的组件源中的任何地方import cytoscape from 'cytoscape'(或任何类似的方式),因为我没有任何东西,因为 Typescript 和方式似乎cytoscape 的类型已定义,我不需要它来使我的组件与以 html 导入的 cytoscape 一起工作。虽然这可能是 NPN 包的问题......

Wbs.tsx:

接收 Prop WbsLayout 的简单 React 组件。

cytoscape 的分型(仅相关部分)

declare namespace Cy {
    //...
    interface Core { }

    //...
    interface Static {
        (options?: Cy.CytoscapeOptions): Core;
        (extensionName: string, foo: string, bar: any): Core;
        version: string;
    }
}

declare module "cytoscape" {
    export = cytoscape;
}
declare var cytoscape: Cy.Static;

Webpack 2 配置

Wbs.tsxWbsLayout.tsx 和其他使用 Webpack 2 捆绑到 @nodeject/wbs-react 中:

module.exports = {
  entry: {
    'test/index': './src/test.tsx',
    'dist/index': './src/index.tsx'
  },

  output: {
    filename: "./[name].js",
    libraryTarget: 'umd',
    library: 'wbs-react',
    umdNamedDefine: true
  },

  devtool: "source-map",

  resolve: {
    extensions: [".webpack.js", ".web.js", ".ts", ".tsx", ".js"]
  },

  node: {
    fs: "empty",
    child_process: "empty"
  },

  module: {
    rules: [    
      {
        enforce: 'pre',
        test: /\.js$/,
        loader: "source-map-loader"
      },
           
      {
        test: /\.tsx?$/,
        loader: "ts-loader"
      },
      {
        test: /\.css$/,
        loader: "style-loader!css-loader"
      },
    ]
  },
  
  externals: {
    "react": {
      root: 'React',
      commonjs2: 'react',
      commonjs: 'react',
      amd: 'react'
    },

    "react-dom": {
      root: 'ReactDOM',
      commonjs2: 'react-dom',
      commonjs: 'react-dom',
      amd: 'react-dom'
    },

    "cytoscape": {
      root: 'Cy',
      commonjs2: 'cytoscape',
      commonjs: 'cytoscape',
      amd: 'cytoscape'
    },

    "chai": {
      root: 'chai',
      commonjs2: 'chai',
      commonjs: 'chai',
      amd: 'chai'
    }
  }
};

tsconfig.json

{
  "compilerOptions": {
    "outDir": "./temp/",
    "sourceMap": false,
    "noImplicitAny": true,
    "module": "commonjs",
    "target": "es2015",
    "allowSyntheticDefaultImports": true,
    "moduleResolution": "node",
    "jsx": "react"
  },
  "exclude": [
    "node_modules",
    "dist",
    "temp",
    "test"
  ]
}

新的 es6 应用程序源

import React from 'react';
import ReactDOM from 'react-dom';
import {Wbs, WbsLayout} from '@nodeject/wbs-react';

let graph = {
  nodes: [
    { data: { id: '1' } },
    { data: { id: '1.1', parent: '1' } }
  ],
  edges: [
    { data: { id: 'e1', source: '1', target: '1.1' } }
  ]
};

layout: new WbsLayout(graph); // <-- Fails here (case #2)

ReactDOM.render(<Wbs layout={layout}/>, document.querySelector('#wbs_example'));

最佳答案

我注意到如果我从 webpack 的外部完全删除 cytoscape,它就可以工作,我找到了答案。这是我需要做的:

"cytoscape": {
  root: 'Cy',
  commonjs2: 'cytoscape',
  commonjs: 'cytoscape',
  amd: 'cytoscape'
 }

必须是:

"cytoscape": {
  root: 'cytoscape', <--------
  commonjs2: 'cytoscape',
  commonjs: 'cytoscape',
  amd: 'cytoscape'
 }

关于typescript - 如何使用 Typescript 进行 cytoscape 初始化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44547808/

相关文章:

typescript - 使用类验证器验证字符串文字类型

javascript - {...object, property : value} work with spread syntax? 如何

webpack - 找不到相对于目录的预设 "es2015"

npm 安装 : Tried to download(404): https://node-inspector. s3.amazonaws.com/debug/v0.7.7/node-v51-linux-x64.tar.gz

npm - '模块构建失败 : Error: Couldn't find preset "es2015" relative to directory' from newly created gatsby project

javascript - 如何在ES6箭头函数调用的函数中使用 `this`?

javascript - mat-spinner 是如何在 mat-table 中渲染的?

angular - 是否可以将动态值传递给 Angular 6 中的自定义表单验证器?

typescript - 要求参数是 typescript 中给定类的(子)类的实例

javascript - 如何在react-native中重置IOS文件夹