Скопление и веб-сокет Apollo с подписками-transport-ws - PullRequest
1 голос
/ 16 мая 2019

Я пытаюсь использовать:

Используя Rollup (https://github.com/sveltejs/template/blob/master/rollup.config.js) для окончательного пакета при компиляции (npm run build), я получил эту ошибку:

Error: "Subcription is not exported by node_modules/subscriptions-transport-ws/dist/index.js"

Итак, я подумал о решении с (https://github.com/rollup/rollup-plugin-commonjs#custom-named-exports):

commonjs({
    namedExports: {
        './node_modules/subscriptions-transport-ws/dist/index.js': ['SubscriptionClient']
    }
}),

Теперь Роллуп говорит:

index.js → public/bundle.js...

(!) Mixing named and default exports
Consumers of your bundle will have to use bundle['default'] to access the default export, which may not be what you want. Use `output.exports: 'named'` to disable this warning

(!) Missing shims for Node.js built-ins
Creating a browser bundle that depends on 'events', 'https', 'http', 'url', 'zlib' and 'stream'. You might need to include https://www.npmjs.com/package/rollup-plugin-node-builtins

(!) Unresolved dependencies
https://rollupjs.org/guide/en#warning-treating-module-as-external-dependency
events (imported by node_modules\ws\lib\websocket.js, node_modules\ws\lib\websocket-server.js,  commonjs-external-events)
crypto (imported by node_modules\ws\lib\websocket.js, node_modules\ws\lib\websocket-server.js, node_modules\ws\lib\sender.js,  commonjs-external-crypto)
https (imported by node_modules\ws\lib\websocket.js,  commonjs-external-https)
net (imported by node_modules\ws\lib\websocket.js,  commonjs-external-net)
http (imported by node_modules\ws\lib\websocket.js, node_modules\ws\lib\websocket-server.js,  commonjs-external-http)
tls (imported by node_modules\ws\lib\websocket.js,  commonjs-external-tls)
url (imported by node_modules\ws\lib\websocket.js, node_modules\ws\lib\websocket-server.js,  commonjs-external-url)
stream (imported by node_modules\ws\lib\receiver.js,  commonjs-external-stream)
zlib (imported by node_modules\ws\lib\permessage-deflate.js,  commonjs-external-zlib)
bufferutil (imported by node_modules\ws\lib\buffer-util.js,  commonjs-external-bufferutil)
utf-8-validate (imported by node_modules\ws\lib\validation.js,  commonjs-external-utf-8-validate)
(!) Missing global variable names
Use output.globals to specify browser global variable names corresponding to external modules
events (guessing 'events')
crypto (guessing 'crypto')
https (guessing 'https')
http (guessing 'http')
net (guessing 'net')
tls (guessing 'tls')
url (guessing 'url')
zlib (guessing 'zlib')
bufferutil (guessing 'bufferutil')
stream (guessing 'stream')
utf-8-validate (guessing 'utf8Validate')
created public/bundle.js in 13.8s

Какой беспорядок. Я просто использую веб-ориентированные библиотеки, почему он жалуется на зависимости nodejs?!

Итак, я добавил эти строки в rollup.config.js:

import builtins from 'rollup-plugin-node-builtins'
import globals from 'rollup-plugin-node-globals'
...
plugins: [
  globals(),
  builtins(),

и все предыдущие ошибки исчезли.

Но теперь в браузере я получаю:

Uncaught ReferenceError: exports is not defined
    at client.js:45

Он жалуется на эту строку (я думаю):

Object.defineProperty(exports, "__esModule", { value: true });

Я полностью потерян.

Я создал простое репо: https://codesandbox.io/s/zn1mnon8jl.

Если вы откроете его в кодах и в окне, оно будет работать Чудо! Но если вы загрузите в формате .zip и выполните npm i && npm run dev, вы увидите проблему.

Что теперь делать?

...