Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Importing divviup-ts from Node #89

Closed
divergentdave opened this issue Aug 12, 2022 · 2 comments
Closed

Importing divviup-ts from Node #89

divergentdave opened this issue Aug 12, 2022 · 2 comments

Comments

@divergentdave
Copy link
Contributor

I ran into some issues when trying to import the divviup-ts library from plain Node, and I'm wondering if there's some combination of TypeScript settings we could use to improve this. It appears we are using TypeScript's classic module resolution strategy, and internal module dependencies use "non-relative module imports". I got things to work by changing all internal dependencies to use relative imports, (a very invasive change, which we may not want to do) and importing the result as follows.

{
  "dependencies": {
    "divviup-ts": "file:../../Code/ppm/divviup-ts/dist/src"
  }
}
const DAPClient = require("divviup-ts/dap").default;
@divergentdave
Copy link
Contributor Author

divergentdave commented Aug 31, 2022

These links provide a good overview of the situation: https://stackoverflow.com/a/57539446, microsoft/TypeScript#15479 (comment), and microsoft/TypeScript#15479 (comment). Compiler options will only affect the compiler's internal resolution of imports, but won't affect code generation.

We are presented with a choice between exclusively using relative module import paths or adding another build step after tsc to transforms imports or patch resolution. The TypeScript core developer in the above issue thread appears to advocate for using relative import paths when Node.js is a target. On the other hand, non-relative module imports are plainly much more readable and easier to work with. Our modules nest three or four layers deep, and we have a lot of import statements pointing up multiple directories.

I think this would be best resolved via Parcel, following #34. It appears that the "alias" field can address this.

@divergentdave
Copy link
Contributor Author

#34 has solved most of this, the only remaining hiccup is that the DAPClient constructor is exported under the name default. Here's my updated code sample.

{
  "dependencies": {
    "@divviup/dap": "file:../../Code/ppm/divviup-ts/packages/dap"
  }
}
const DAPClient = require("@divviup/dap").default;

async function main() {
  const client = new DAPClient({
    type: "sum",
    bits: 2,
    taskId: ...,
    leader: ...,
    helper: ...,
    minBatchDurationSeconds: 300,
  });

  console.log("Start time is", Math.floor(new Date().getTime() / 1000 / 300) * 300);
  for (var i = 0; i < 10; i++) {
    await client.sendMeasurement(1);
  }
  console.log("Done uploading");
  console.log("End time is", Math.ceil(new Date().getTime() / 1000 / 300) * 300);
}

main().catch(console.error);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant