Open oberstet opened 6 years ago
Thanks for the informative post, I was able to excise cbor
and msgpack5
from my bundle with webpack.
82kb isn't big in a vacuum, but what app only requires 1 dependency? Autobahn is the biggest dependency in my bundle, so I'm looking for any way to reduce its size.
For me the issue isn't size but rather integration into the build process for my front-end app (SPA/PWA/whatever), which is using webpack. Usually I "don't have to do anything special" to be able to do import { foo } from 'bar'
in my source code and have and have it get integrated into the bundle. However, I haven't yet figured out how to do this with autobahn. (Admittedly, I just started trying it today. Are there any examples of this in the wild that people could share with me?)
I tried to exclude when.js
in my project but there are two usages of when.function.call
in session.js
.
So even when using ES6 Promises, when.js cannot easily be excluded.
@nidi3 here's my webpack config that completely removes when.js
from the bundle. I had to remove a ton of unrelated config, hopefully I didn't delete anything relevant.
const config = {
module: {
rules: [
{
test (p) {
const has = str => p.includes(path.normalize(str)) // Windows...
return /when\.js/.test(p) || has('when/monitor/console')
},
use: 'null-loader'
},
],
},
plugins: [
// Stubs when/function so "when" can be totally replaced.
new webpack.NormalModuleReplacementPlugin(
/when\/function/,
corePath('common/webpack/when-func-stub.js')
),
],
}
corePath('common/webpack/when-func-stub.js')
returns a filepath.
// when-func-stub.js
function call (endpoint, ...args) {
const result = endpoint(...args)
return Promise.resolve(result)
}
module.exports = { call }
@guanzo that looks good, I'll try this tomorrow, thanks for sharing!
Yes, work nicely.
I tried to exclude when.js in my project but there are two usages of when.function.call in session.js.
oh, ok. that is a (separate) bug then. exactly to allow to switch the underlying promise implementation to be used in the whole library. @om26er could you have a look pls?
Another little issue when using native promises is the typescript typing. I had to add this to make when.js promises compatible with native promises:
type BuiltInPromise<T> = Promise<T>
declare namespace When {
interface Promise<T> extends BuiltInPromise<T> {
finally(action: () => void | null | undefined): Promise<T>;
}
}
But this is probably an issue for the when.js project.
thanks for your notes and code snippet!
rgd the pluggable deferred/promise issue: filed as https://github.com/crossbario/autobahn-js/issues/512
For "reasons", one team is having a problem with the mere size of AutobahnJS.
We don't want to publish even more variants of ABJS, possibly tuned down and optimized rgd deps for various specific scenarios, so instead we should:
Current size:
See here https://github.com/crossbario/autobahn-js-browser
Now, I don't have the full info here (why is 82kB too big, when you need a complete JS run-time or even a full browser thing anyways), but lets just put that as a premise (ABJS is too big).
The actual reason that the built library has this size isn't that our code is that large:
The reason is that we have a set of bigger required deps, from https://github.com/crossbario/autobahn-js/blob/master/package.json#L12:
However, not all of these are actually hard deps - depending on context!
Let me untangle the reasons for these deps:
msgpack5 and cbor are obviously only needed if you want those serialization formats. So if you are fine with JSON, those are not needed, as both browsers and Nodejs run-time envs have built-in JSON.
when is only needed if you run in an older JS env that doesn't come with its own support for JS promises. if you have ECMAScript 6 or higher, ABJS itself doesn't need "when" (though that library provides more sugar that isn't in ECMAScript 6)
ws is only needed if the JS env doesnt come with WebSocket support. browsers have that built in.
crypto-js: this is required by ABJS itself for WAMP-CRA and WAMP-Cryptosign:
However, we only (currently) actually use 3 functions from that (see above). So we could of course copy-pasta those and include it in ABJS, doing away with the full dep. Well, this kinda sucks of course.
lastly, tweenacl: https://tweetnacl.js.org/
we need this for WAMP-cryptosign and XBR. we cannot do away with this, but this is the smallest dep of all. nothing to win here ..