Skip to content
This repository has been archived by the owner on Dec 21, 2020. It is now read-only.

Create a bundling setup for hopr-chat to ensure only needed dependencies are to be compiled into the binary #12

Closed
2 tasks
0xjjpa opened this issue Jun 15, 2020 · 2 comments
Assignees
Milestone

Comments

@0xjjpa
Copy link
Contributor

0xjjpa commented Jun 15, 2020

Introduction

Right now, hopr-chat works as a simple CommonJS (.cjs) application, that is first compiled into JavaScript from TypeScript, and then executed using Node.js v12.x. As we are looking to deliver hopr-chat as a standalone binary that can be used in multiple operating systems like Windows, we need to bundle all our node dependencies with binary bundling tooling like electron-builder, nexe or pkg. Unfortunately, as hopr-core (and hence hopr-chat) relies in very specific libraries that require low-level APIs (e.g. wrtc), it would be easier to first ensure we only bundle what is needed to ensure only external dependencies are needed and then passing those to binary bundlers.

Task Description

  • Create an aegir, rollup or similar setup to bundle hopr-chat after being compiled into JavaScript. The process can be two fold (e.g. first compile, then bundle) or into a single step using adequate plugins (e.g. compiling and bundling at the same time).
  • Create a new branch that provides the aforementioned setup and test it by running node bundled.js while removing all other compiled dependencies generated, as to ensure all code needed by hopr-chat has been included in bundled.js.

Definition of Complete

The task is considered completed when a task inside package.json called build:bundle is able to compile TypeScript and bundle the result into a single-entry point file.

Testing Criteria

  • The compiled bundled can be executed using node and rely in no other external files.
  • The filed underwent a tree-shaking process to ensure only needed dependencies were included.

Notes

Previous work has been done to allow this. A sample rollup configuration used is below, but bear in mind it might not be complete or is lacking. babel (and thus .babelrc) was used to do the compilation of TypeScript, but some external dependencies are still causing issues. In particular, ganache-core, used for testing is being embedded and breaking the build. Tickets on hoprnet/hopr-core-ethereum#63 and hoprnet/hopr-testing#1 are meant to solve this problem.

rollup.config.js

import resolve from "@rollup/plugin-node-resolve";
import babel from "@rollup/plugin-babel";
import commonjs from "@rollup/plugin-commonjs";
import json from "@rollup/plugin-json";
import alias from "@rollup/plugin-alias";
import path from "path";


export default {
  input: "index.ts",
  external: ["wrtc", "ganache-core", "web3-core"],
  output: {
    name: "HOPRChat",
    file: "hopr-chat.js",
    format: "cjs",
  },
  plugins: [
    alias({
      "package.json": path.join(__dirname, "package.json")
    }),
    resolve({ 
      extensions: ['.js', '.jsx', '.ts', '.tsx', '.json']
    }), 
    commonjs(), 
    babel({ 
      exclude: './node_modules/**', 
      extensions: ['.js', '.jsx', '.ts', '.tsx']
    }),
    json()
  ],
};

.babelrc

{
  "presets": ["@babel/preset-typescript"],
  "plugins": ["@babel/plugin-syntax-class-properties"]
}
@0xjjpa 0xjjpa self-assigned this Jun 16, 2020
@0xjjpa
Copy link
Contributor Author

0xjjpa commented Jul 6, 2020

Pushing this one as currently our node-bin setup is working. We'll eventually figure an alternative whenever we go for a different transport that isn't wrtc. Pushing from sprint.

@0xjjpa 0xjjpa transferred this issue from hoprnet/hopr-core Aug 5, 2020
@0xjjpa 0xjjpa added this to the Sprint 11 milestone Aug 5, 2020
@nionis
Copy link
Contributor

nionis commented Dec 21, 2020

No longer relevant.

@nionis nionis closed this as completed Dec 21, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants