📦 Using external libraries

Hello @dan_oz ,

I do execute the next command everytime npm run build && npm run start. Addionally the index.js file generated is executed succesfully and I am able to add some data in Google databases through Firestore from Google Firebase. Here is the rollup output:

$ npm run build && npm run start

> defender-rollup-demo@1.0.0 build
> rollup -c


src/index.ts → dist/index.js...
created dist/index.js in 5.9s

Taking into account the next package.json config file:

{
  "name": "defender-rollup-demo",
  "version": "1.0.0",
  "main": "dist/index.js",
  "license": "MIT",
  "scripts": {
    "build": "rollup -c",
    "start": "node dist/index.js"
  },
  "devDependencies": {
    "@rollup/plugin-commonjs": "^16.0.0",
    "@rollup/plugin-json": "^4.1.0",
    "@rollup/plugin-node-resolve": "^10.0.0",
    "@rollup/plugin-typescript": "^6.1.0",
    "@types/is-odd": "^3.0.0",
    "builtin-modules": "^3.1.0",
    "rollup": "^2.33.3",
    "tslib": "^2.0.3",
    "typescript": "^4.1.2"
  },
  "dependencies": {
    "@openzeppelin/contracts": "4.7.3",
    "defender-autotask-client": "1.37.0",
    "defender-relay-client": "1.37.0",
    "defender-sentinel-client": "1.37.0",
    "dotenv": "^8.2.0",
    "ethers": "5.0.3",
    "firebase": "9.14.0",
    "firebase-admin": "11.3.0",
    "is-odd": "^3.0.1",
    "web3": "1.2.9"
  }
}

I have included in the rollup.config.js file the dependencies:

import resolve from "@rollup/plugin-node-resolve";
import commonjs from "@rollup/plugin-commonjs";
import typescript from "@rollup/plugin-typescript";
import json from "@rollup/plugin-json";
import builtins from "builtin-modules";

export default {
  input: "src/index.ts",
  output: {
    file: "dist/index.js",
    format: "cjs",
  },
  plugins: [
    resolve({ preferBuiltins: true }),
    commonjs(),
    json({ compact: true }),
    typescript(),
  ],
  external: [
    ...builtins,
    "ethers",
    "web3",
    "axios",
    /^defender-relay-client(\/.*)?$/,
    "firebase",
    /^firebase-admin(\/.*)?$/,
  ],
};

It seems that these modules are not treated properly by Rollup.js as it states here treating-module-as-externa-dependency and you need to use node-resolve.

Do you guys have an example how to do this or could help me out in this matter? It is a bit limited the number of libraries included by default in Defender so having an example of this would resolve dependencies that might arise between Defender and other API services.

Thanks in advance for any help you can provide @dan_oz .

Best regards,
:fire: Pedro Reyes :fire: