OpenFn/kit

Compiler: add default export even if there's an export already in the code (strict check for default export, not just /export/)

Open

#831 opened on Nov 23, 2024

View on GitHub
 (6 comments) (0 reactions) (1 assignee)TypeScript (19 forks)auto 404
DevXbuggood first issue

Repository metrics

Stars
 (20 stars)
PR merge metrics
 (PR metrics pending)

Description

Description.

Runtime seems to always require a compiled job to have a default export of an array. Hence, those without a default export break the runtime.

Because

  1. we access the default export. https://github.com/OpenFn/kit/blob/4376f7b3e615c0b0144deecf6d65d4a03dbb3abf/packages/runtime/src/execute/expression.ts#L146
  2. we expect an array and want to iterate it https://github.com/OpenFn/kit/blob/4376f7b3e615c0b0144deecf6d65d4a03dbb3abf/packages/runtime/src/execute/expression.ts#L53

Currently,

We create a default export and then put all top-level-expressions in it. If there's no top-level-expression and empty array is exported to make the runtime happy.

export default []; // to make runtime happy!

But,

when there's an export (either named or not) that isn't a default export, we ignore the creation of the default export which makes the runtime sad.

There might be programs to be run in this job but because of a single non-default export. They don't get executed.

eg.

import {fn} from "@openfn/common"

fn(state => {
  state.data.name = "doc-han"
  return state;
})

export const han = "Han"; // prevents our runtime from running

fn(() => {
  state.data.isGithub = true;
  return state;
})


Is there a reason we don't accommodate this? @josephjclark I feel we can have other exports and still generate our default export. Also, if the user provides a default export(which isn't encouraged) we can also check if it exports an Array.

Here we do a regex match. https://github.com/OpenFn/kit/blob/4376f7b3e615c0b0144deecf6d65d4a03dbb3abf/packages/compiler/src/transforms/ensure-exports.ts#L15

For the concern of having several types of export nodes. A default export seems to always have only one type of Node representation. The name of the node only changes across ast-generator.

Proposed solution.

  1. Instead of regex matching the type 'Export ', Look for default export node instead.
  2. check if the default export node has declaration that's an Array

Contributor guide