Skip to content

Does not work with ESM #354

@paceaux

Description

@paceaux

I've got a spandx.config.js file with the following code:

const config = {
  host: {
    prod: "prod.foo.redhat.com",
    stage: "stage.foo.redhat.com",
    qa: "qa.foo.redhat.com",
    dev: "dev.foo.redhat.com",
  },
  port: 1337,
  open: true,
  startPath: "/template",
  verbose: true,
  portalChrome: {
    resolveSPAComments: false,
  },
  primer: {
    preview: true,
  },
  proxy: {
    host: "http://squid.corp.redhat.com:3128",
    pattern: "^https://(.*?).redhat.com",
  },
  bs: {
    https: sslReady
      ? { cert: "ssl/spandx.pem", key: "ssl/spandx-key.pem" } // 🔒
      : true, // 🔓
    codeSync: true,
  },
  routes: {
    "/template": {
      host: "http://localhost:3030",
      path: "/template/", // vite dev server requires a trailing slash
    },
    "/": {
      host: {
        dev: "https://access.dev.redhat.com",
        qa: "https://access.qa.redhat.com",
        stage: "https://access.stage.redhat.com",
        prod: "https://access.redhat.com",
      },
    },
  },
  silent: true, // this prints spandx logs in the console
};

export default config;

When I run the command spandx --config=spandx.config.js,

I get an error:

[spandx] Error: 'host' is the wrong type, must be either string or object
[spandx]     at isSingleHost (/node_modules/spandx/app/config.js:104:15)
[spandx]     at processConf (/node_modules/spandx/app/config.js:149:23)
[spandx]     at create (/node_modules/spandx/app/config.js:66:9)

My host is a valid object. So it makes no sense that I'd get an error saying that it isn't.

But after digging into this a little bit, the problem is that when it's exported as an ESM module, the config is under the property .default:

Image

The only way that I can bypass this error is by making each property a constant, and then exporting all of the constants:

import * as fs from 'fs';

const sslReady =
  fs.existsSync("ssl/spandx.pem") && fs.existsSync("ssl/spandx-key.pem");

if (!sslReady) {
  console.log("Launching with invalid SSL cert 🔓");
} else {
  console.log("Launching with valid SSL cert 🔒");
}

const host = {
  prod: "prod.foo.redhat.com",
  stage: "stage.foo.redhat.com",
  qa: "qa.foo.redhat.com",
  dev: "dev.foo.redhat.com",
};
const port = 1337;
const open = true;
const startPath = "/template";
const verbose = true;
const portalChrome = {
  resolveSPAComments: false,
};
const primer = {
  preview: true,
};
const proxy = {
  host: "http://squid.corp.redhat.com:3128",
  pattern: "^https://(.*?).redhat.com",
};
const bs = {
  https: sslReady
    ? { cert: "ssl/spandx.pem", key: "ssl/spandx-key.pem" } // 🔒
    : true, // 🔓
  codeSync: true,
};
const routes = {
  "/template": {
    host: "http://localhost:3030",
    path: "/template/", // vite dev server requires a trailing slash
  },
  "/": {
    host: {
      dev: "https://access.dev.redhat.com",
      qa: "https://access.qa.redhat.com",
      stage: "https://access.stage.redhat.com",
      prod: "https://access.redhat.com",
    },
  },
};
const silent = true; // this prints spandx logs in the console

export { host, port, open, startPath, verbose, portalChrome, primer, proxy, bs, routes, silent }

And when I do that, I then get a new error:

Error: spandx is configured for multi-host mode ('host' is an object map), but one or more routes have environment names that don't match the names from 'host'
[spandx]     at validateHosts (/node_modules/spandx/app/config.js:122:15)
[spandx]     at create (/node_modules/spandx/app/config.js:69:5)

I don't get this error when I used the exact same values if my config is a CJS export.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions