简体   繁体   中英

load socket.io with Angular 2 AOT

I've created a project in angular2 with socket.io to emit / listen socket communication. Everything works fine in JIT ( or npm start) but when I try to compile the code via rollup to lunch it as AOT for production usage It don't work.

Node version : 6.9.4 npm version : 3.10.6

typings.json

    {
  "globalDependencies": {
    "core-js": "registry:dt/core-js#0.0.0+20160725163759",
    "jasmine": "registry:dt/jasmine#2.2.0+20160621224255",
    "node": "registry:dt/node#7.0.0+20170110233017",
    "socket.io-client": "registry:dt/socket.io-client#1.4.4+20161116080703"
  },
  "ambientDependencies": {
    "node": "github:DefinitelyTyped/DefinitelyTyped/node/node.d.ts#138ad74b9e8e6c08af7633964962835add4c91e2",
    "socket-io-client": "github:DefinitelyTyped/DefinitelyTyped/socket.io-client/socket.io-client.d.ts#7de6c3dd94feaeb21f20054b9f30d5dabc5efabd",
    "es6-shim": "registry:dt/es6-shim#0.31.2+20160317120654"
  }
}

systemjs.config.js

map : {"socket.io-client": 'npm:socket.io-client'}
packages : { "socket.io-client": {
            main: './socket.io.js',
            "defaultExtension": "js"
        } }

package.json : "socket.io-client": "^1.7.2"

mycomponent.ts

import * as io from 'socket.io-client';
var url = 'http://localhost:4500';
export var socket = io(url);

"node_modules/.bin/ngc" -p tsconfig-aot.json executes successfully but "node_modules/.bin/rollup" -c rollup-config.js gives error : cannot call a namespace ('io')

If i change my component import line and set import io from socket.io-client then i get error module 'socket.io-client' has no default export

I would appreciate if someone can guide me through as this is my second day of troubleshooting and trying as per other forums.

Thanks in Advance, Kapil

Keep import:

import * as io from 'socket.io-client';

And add plugin to rollup.js:

{
    name: 'replace io imports',
    transform: code => ({
        code: code.replace(/import\s*\*\s*as\s*io/g, 'import io'),
        map: { mappings: '' }
    })
},

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM