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

时间:2017-06-14 14:37:06

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

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

我的图书馆:

  1. 当我在index.html中导入cytoscape.js作为静态资产时
  2. 当我在新的es6应用中npm install我的组件时不会: Uncaught ReferenceError: cytoscape is not defined at new WbsLayout。尽管在安装我的组件时,cytoscape会自动安装在node_modules中。
  3. 打字稿来源

      

    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&#39;我的定义是定义的,我不需要它来使我的组件与html中导入的cytoscape一起工作。也许这对NPN包来说是一个问题......

      

    Wbs.tsx:

    一个简单的React组件,它接收prop WbsLayout。

    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 config

    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'));
    

2 个答案:

答案 0 :(得分:1)

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

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

必须是:

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

答案 1 :(得分:0)

您是否尝试将其导入为..

const cytoscape = require('cytoscape');

如果是,那么您也可以发布tsconfig.json