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

Building problem with typescript #31

Open
bergmorten opened this issue Aug 12, 2022 · 9 comments
Open

Building problem with typescript #31

bergmorten opened this issue Aug 12, 2022 · 9 comments

Comments

@bergmorten
Copy link

I'm having problem with building a typescript project that referees this package. The problem seems to be that the Node version of the code is using a require of an ESM module which neither TSC or ESBUILD like.

I can get the project to build if I force to use the ESM module of the project, but then I does not get access to the file system. Is the WASM code for the ESM build enabled with file access?

@bmaranville
Copy link
Member

Raw filesystem access is the only reason there is a different build for node vs browser at this point - the noderawfs flag is passed to the emscripten compiler at build time for node, but not for the browser (including it seems to break the browser build).

I think it depends on how h5wasm is loaded into the referring package you're using. If you do require("h5wasm") or await import("h5wasm") or similar I suspect it will work, since that is how I test it.

Can you tell me what package you're trying to build?

@bergmorten
Copy link
Author

Ok, I figurate different the difference between web and node, and why file systems did not work in the ESM build.

I think the problem is that the ht-util.js script from emscripten has ems syntax and not cjs, and this get messy with the import.meta.url and require statements. When I use TSC or ESBUILD it does like the ems version better since then both files are esm modules. Have you a working example of using h5wasm in a node project with typescript?

The h5wasm works when I use it with mjs, but if you try to use h5wasm win typescript then both tsc and esbuilds fails. I've mange to get this to work with using esbuild and then a search and replace into the compiled code.

Possible solutions?

  1. Use rollup/esbuild to pack both hdf5_hl.ts and hdf5_util.js into cjs for Node.
    or
  2. try emscription without exporting esm module

@bmaranville
Copy link
Member

I didn't really want to support the commonjs build anymore, as it adds complexity to the project. I had to do the same thing as you when building the commonjs module: search and replace on the output.

I am able to build a test typescript project using h5wasm v0.4.6 if I set "type": "module" in the package.json of the test project and use a tsconfig.json like this:

{
  "compilerOptions": {
    "module": "es6",
    "esModuleInterop": true,
    "target": "es6",
    "moduleResolution": "node",
    "sourceMap": true,
    "outDir": "dist",
    "types": ["node"]
  },
  "lib": ["es2020"],
}

Then an index.ts that looks like this compiles and runs just fine in node (slightly modified from the node tests in the h5wasm test folder):

import { strict as assert } from 'assert';
import h5wasm from 'h5wasm';

async function bool_test() {
  await h5wasm.ready;
  var f = new h5wasm.File('./array.h5', 'r');

  const bool_data = f.get('bool');
  assert(bool_data instanceof h5wasm.Dataset);
 
  console.log('value:', bool_data.value);
  // should be:  [ false, true, true, false ]
  console.log('metadata: ', bool_data.metadata);
}

export const tests = [
  {
    description: 'Read boolean datasets',
    test: bool_test,
  },
];
export default tests;

bool_test();

(note that you have to npm install node-ts if you want to use the assert as above)

@mousseq
Copy link

mousseq commented Apr 2, 2024

I realize you've written this package for maximum portability (browser, node), however, if I try to build a simple test file (like yours above) with both module and moduleResolution set to "nodenext", I receive a typescript error indicating that relative import paths need explicit file extensions.

node_modules/h5wasm/src/hdf5_hl.d.ts:1:113 - error TS2834: Relative import paths need explicit file extensions in ECMAScript imports when '--moduleResolution' is 'node16' or 'nodenext'. Consider adding an extension to the import path.

1 import type { Status, Metadata, H5Module, CompoundMember, CompoundTypeMetadata, EnumTypeMetadata, Filter } from "./hdf5_util_helpers";

Unfortunately, I appear to be stuck as other modules that I need require nodenext. Would you consider modifying this package to be compatible with nodenext?

@bmaranville
Copy link
Member

I would be happy to make that modification, but I lack expertise in the subtleties of packaging. I would welcome a PR from someone with more experience!

@bmaranville
Copy link
Member

@mousseq - can you try out the fix suggested by @axelboc in #71 ?
If it works for you, I'll merge it in and make a new patch release.

@mousseq
Copy link

mousseq commented Apr 3, 2024 via email

@bmaranville
Copy link
Member

@mousseq : Patched version published just now... v0.7.4

@mousseq
Copy link

mousseq commented Apr 3, 2024 via email

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

3 participants