Build fails with resolve errors - socket.io

I am trying to setup socket.io-client inside a svelte component.
The docs say that I can do import { io } from 'socket.io-client'.
But when I try to run the app using the serve target, the command fails with the following logs:
Bundle complete. Watching for file changes...
Bundling...
'bufferutil' is imported by bufferutil?commonjs-external, but could not be resolved – treating it as an external dependency
'utf-8-validate' is imported by utf-8-validate?commonjs-external, but could not be resolved – treating it as an external dependency
Circular dependency: node_modules\util\util.js -> node_modules\util\node_modules\inherits\inherits.js -> node_modules\util\util.js
Circular dependency: node_modules\util\util.js -> node_modules\util\node_modules\inherits\inherits.js -> C:\Users\munda\Documents\upwork\upwork-gameshow\node_modules\util\util.js?commonjs-proxy -> node_modules\util\util.js
#rollup/plugin-typescript: Rollup 'sourcemap' option must be set to generate source maps.
No name was provided for external module 'tty' in output.globals – guessing 'tty'
No name was provided for external module 'os' in output.globals – guessing 'os'
No name was provided for external module 'fs' in output.globals – guessing 'fs'
No name was provided for external module 'child_process' in output.globals – guessing 'require$$0'
No name was provided for external module 'http' in output.globals – guessing 'require$$1'
No name was provided for external module 'https' in output.globals – guessing 'require$$2'
No name was provided for external module 'net' in output.globals – guessing 'net'
No name was provided for external module 'tls' in output.globals – guessing 'tls'
No name was provided for external module 'crypto' in output.globals – guessing 'require$$0$3'
No name was provided for external module 'zlib' in output.globals – guessing 'zlib'
No name was provided for external module 'bufferutil' in output.globals – guessing 'require$$1$1'
No name was provided for external module 'stream' in output.globals – guessing 'require$$0$2'
No name was provided for external module 'utf-8-validate' in output.globals – guessing 'require$$0$1'
Creating a browser bundle that depends on Node.js built-in modules ("tty", "os", "http", "https", "zlib" and "stream"). You might need to include https://github.com/snowpackjs/rollup-plugin-polyfill-node
Steps I took to generate the app:
Initiate an nx-workspace using yarn create nx-workspace
Install the svelte generators using yarn add #nxext/svelte, link: https://nx.dev/community#:~:text=the%20Nx%20workflow-,%40nxext/svelte,-Nx%20plugin%20to
Generate a svelte application using the installed generator.
Add the socket.io client side code in the component (basic socket initialisation as mentioned in docs).
Since my server and client are not going to be hosted on the same domain, add the import { io } from 'socket.io-client' and initialize the socket using const socket = io(SERVER_URL)(in my case this was http://localhost:3000)
Install socket.io client using yarn socket.io-client.
Run the serve target using nx run-many --target=serve --all --parallel, fingers crossed.
Result: The above mentioned log.
What am I missing?

I needed to install the following missing dependencies:
bufferutil
utf-8-validate
A simple yarn add bufferutil utf-8-validate fixed it for me. This is mentioned in docs for socket.io-client package or socket.io official documentation website.
This did fix builds on my PC (windows) but I could not get the same thing running on mac. I tried deleting node_modules and yarn.lock, re-running yarn.
Finally I had to go through the CDN route.
This is how I did it:
Move the socket initialisation logic in a function.
<script>
...
const initialiseSocket = () => {
socket = io('http://localhost:3000');
socket.on('messages', (data) => {
//...
});
}
</script>
Add a svelte:head tag to load the socket-io.client from CDN and pass the function to this script's on:load.
<svelte:head>
<script
src="https://cdn.socket.io/4.2.0/socket.io.min.js"
integrity="sha384-PiBR5S00EtOj2Lto9Uu81cmoyZqR57XcOna1oAuVuIEjzj0wpqDVfD0JA9eXlRsj"
crossorigin="anonymous"
on:load={initialiseSocket}></script>
</svelte:head>

Related

How to use HTTP in hyperstack

I have made a basic install of a hyperstack rails app using hyperstack.org's installation instructions, trying to add a HTTP.get request in an after_mount callback.
Not really sure what else I could try, thought HTTP would be a standard option
class App < HyperComponent
include Hyperstack::Router
after_mount do
HTTP.get('/example.json')
end
render do
DIV() do
'App'
# NodeDisplay
# define routes using the Route psuedo component. Examples:
# Route('/foo', mounts: Foo) : match the path beginning with /foo and mount component Foo here
# Route('/foo') { Foo(...) } : display the contents of the block
# Route('/', exact: true, mounts: Home) : match the exact path / and mount the Home component
# Route('/user/:id/name', mounts: UserName) : path segments beginning with a colon will be captured in the match param
# see the hyper-router gem documentation for more details
end
end
end
the error received is:
Uncaught error: HTTP: uninitialized constant App::HTTP
in App (created by Hyperstack::Internal::Component::TopLevelRailsComponent)
in Hyperstack::Internal::Component::TopLevelRailsComponent
Simple answer: The HTTP library is not by default included in Opal or Hyperstack.
You can include it as part of the Opal jQuery wrapper, or with a minimal Opal Browser::HTTP library.
To add the jQuery wrapper to your Hyperstack application do the following:
Import the Hypestack jquery wrapper by adding
import 'hyperstack/component/jquery', client_only: true
to your config/initializers/hyperstack.rb file.
Then include the actual jquery javascript code in your assets:
If using webpacker run yarn add jquery in your terminal, and then add this line to the javascripts/packs/client_only.js file:
jQuery = require('jquery');
If not using webpacker instead add import 'jquery', client_only: true to the hyperstack initializer file.
If you just want to use the more minimal Browser::HTTP module, add
import 'browser/http
to your config/initializers/hyperstack.rb file.
After changing your hyperstack.rb you will have to clear the rails tmp cache by running rm -rf tmp/cache
Note: When using the browser version you will need to use Browser::HTTP instead of simply HTTP.

Rollup and Apollo websocket with subscriptions-transport-ws

I'm trying to use:
Svelte (https://github.com/sveltejs/template) and
Svelte-Apollo (https://github.com/timhall/svelte-apollo).
Using Rollup (https://github.com/sveltejs/template/blob/master/rollup.config.js) for the final bundle when compiling (npm run build) I got this error:
Error: "Subcription is not exported by node_modules/subscriptions-transport-ws/dist/index.js"
So I thought of solving with (https://github.com/rollup/rollup-plugin-commonjs#custom-named-exports):
commonjs({
namedExports: {
'./node_modules/subscriptions-transport-ws/dist/index.js': ['SubscriptionClient']
}
}),
Now Rollup says:
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
What a mess. I'm just using web oriented libraries, why it complains about nodejs dependencies?!
So I added these lines in rollup.config.js:
import builtins from 'rollup-plugin-node-builtins'
import globals from 'rollup-plugin-node-globals'
...
plugins: [
globals(),
builtins(),
and all the previous errors are gone.
But now in browser I get:
Uncaught ReferenceError: exports is not defined
at client.js:45
It complains about this line (I think):
Object.defineProperty(exports, "__esModule", { value: true });
I'm totally lost.
I created a simple repo: https://codesandbox.io/s/zn1mnon8jl.
If you open it in codesandbox it works! Miracle! But if you download as a .zip and execute npm i && npm run dev you can see the problem.
What to do now?
Try adding this inline rollup plugin before the nodeResolve plugin
{
// needed to specifically use the browser bundle for subscriptions-transport-ws
name: 'use-browser-for-subscriptions-transport-ws',
resolveId(id) {
if (id === 'subscriptions-transport-ws')
return path.resolve('node_modules/subscriptions-transport-ws/dist/client.js');
},
},

Yocto parallel configuration packages files conflict

I have a base package that gives my functionality (wireguard-tools, taken from internet).
This package includes no configuration files for the network interfaces (as it should).
Then I created a few packages with this configuration files that are only to be deployed one for each respective image (e.g. image-1 includes wireguard-1-conf while image-2 includes wireguard-2-conf).
I would like to setup SystemD, but I can only do this when I have an interface configured, and it will only happen when the *-conf package is installed.
Unfortunately, the SystemD service file ("wg-quick#.service") is deployed by wireguard-tools package and my dependent package, the *-conf one, cannot see it:
ERROR: Function failed: SYSTEMD_SERVICE_wireguard-1-conf value wg-quick#wg0.service does not exist
I managed to do a dirty workaround but I feel dirtiest doing this in my *-conf recipe:
do_install_append () {
touch ${D}${systemd_system_unitdir}/wg-quick#wg0.service
pkg_postinst_${PN} () {
rm -f $D/${systemd_system_unitdir}/wg-quick#wg0.service
How should I proceed to make it work "the right way"?
Is there an elegant way of making "wg-quick#.service" from wireguard-tools accessible to *-conf?
Thanks in advance.
Additional Info
My *-conf recipes inherit systemd and include wireguard-tools dependency:
inherit systemd
...
DEPENDS_${PN} = "wireguard-tools"
RDEPENDS_${PN} = "wireguard-tools"
I so nothing else worth to mention in my recipes.

Receiving a "module not found" and "field 'browser' doesn't contain a valid alias configuration" deploying my site to Netlify

My GatsbyJS site runs fine locally but does not produce a successful build when deploying to Netlify. I've researched the error I'm receiving and haven't had any luck. Changing the case of the file name or changing the file path doesn't work.
Link to repository
On your local machine, run the command gatsby build will result in the error you show in the images.
You will notice the error lines:
Error: ./src/components/header.js
... Can't resolve 'components/variables.css' in ...
opt/build/repo/node_modules/components/variables.css doesn't exist
tells you it is trying to resolve the components/variables.css as a module in your project.
Solution
Change the import line for variables.css in header.js:
src/components/header.js
import styled from 'styled-components'
import 'components/variables.css'
...
to the following:
src/components/header.js
import styled from 'styled-components'
import './variables.css'
...
Make sure netlify knows what version of node you need to build your app.
https://www.netlify.com/docs/build-settings/#build-environment-variables
Also make sure your project is able to build from scratch. Clone the repository into a new directory and try to build it.

i18n dart library for translation won't run

Hello i run the command
pub run intl:extract_to_arb --output-dir=target/directory
But i take this error message:
Package "intl" is not an immediate dependency.
Cannot run executables in transitive dependencies.
i follow this site https://www.dartdocs.org/documentation/intl/0.11.9/index.html and i use api 1.15.1
There is another way to do that?
Finally i have a solution:
pub run intl:extract_to_arb --output-dir="/Volumes/Case Sensitive Part/Projects/MyProjectFolder/" web/dart/**/*.dart
This generate a file intl_messages.arb (a Json for the translator).
From the translator you can receive some file like this
intl_messages_fr.arb
intl_messages_de.arb
intl_messages_{locale}.arb to generalize.
After this:
pub run intl:generate_from_arb --generated-file-prefix=notneeded web/dart/**/*.dart ./intl_messages_fr.arb ./intl_messages_de.arb ./intl_messages_{locale}.arb
This generate a class dart named: "messages_all.dart" and dart class named "messages_messages_de.dart", "messages_messages_fr.dart", "messages_messages_{locale}.dart".
Only one problem left. I receive some info from last command generate_from_arb:
No ##locale or _locale field found in intl_messages_it, assuming 'messages_it' based on the file name.
No ##locale or _locale field found in intl_messages_de, assuming 'messages_de' based on the file name.
I thing that i should put this tag ##locale (##it) inside the translation arb files.

Resources