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

Include improved TS library in this package #52

Open
dabreegster opened this issue Mar 4, 2024 · 4 comments
Open

Include improved TS library in this package #52

dabreegster opened this issue Mar 4, 2024 · 4 comments

Comments

@dabreegster
Copy link
Owner

In the main projects actually using the route-snapper, I'm not using https://github.com/dabreegster/route_snapper/blob/main/route-snapper/lib.js. Instead there's a newer TS version of this library, split into a few pieces:

At least that first piece should be included in the NPM package here. The other two pieces could be listed as example references, but since they're Svelte specific, not much code, and make UI choices about wording / styling that's not likely to be general, I don't think they belong in the package.

I need to figure out how to include a TS file in an NPM package correctly. The NPM package is partly generated by wasm-pack and already includes some auto-generated type declarations, so it has to play well with that. Shouldn't be too hard though.

CC @tordans -- if you'd want to use the TS library and not just copy in the short-term, I'll prioritize this

@tordans
Copy link
Contributor

tordans commented Mar 9, 2024

FYI I started looking into out how to add the library to ReactMapGL https://visgl.github.io/react-map-gl/ in an Astro Project. Any hints on that are welcome, did anyone add it to a react project, yet?

@tordans
Copy link
Contributor

tordans commented Mar 9, 2024

I was wondering if the package is following the recommendations on how to export methods, because it looks like VS Code / Typescript magic that allows to "import package" when using a function seems to not work. Using the import used in your svelte example I get…

image

Updates:

Those tools might be helpful to test the package

@dabreegster
Copy link
Owner Author

Hmm, the package config is auto-generated by wasm-pack and might be kind of strange. The one difference that jumps out is that I think you have to do import init, { RouteSnapper, fetchWithProgress } from "route-snapper". The default export is init to initialize the WASM piece, and the rest should be named exports. Does that work? (Edit: looking at lib.js, init is re-exported as a named parameter, so what you have should be working...)

If you want to start with the newer TS package, then as an immediate work-around, you could try copying https://github.com/dabreegster/ltn/blob/main/web/src/common/snapper/route_tool.ts into your code. The only svelte-specific bit here is import { routeToolGj, snapMode, undoLength } from "./stores";. These are Svelte stores. When the GeoJSON to render changes, these stores will have new values set. I'm not sure how ReactMapGL works, but if it gives you some kind of state variable for the GeoJSON source that you can set, plumbing that in ought to work.

My next step on this issue is to figure out how to include the .ts library in the NPM package. The wasm-pack generated package.json looks like this:

{
  "name": "route-snapper",
  "description": "Draw routes in MapLibre snapped to a street network using client-side routing",
  "version": "0.4.0",
  "license": "Apache-2.0",
  "repository": {
    "type": "git",
    "url": "https://github.com/dabreegster/route_snapper"
  },
  "files": [
    "route_snapper_bg.wasm",
    "route_snapper.js",
    "route_snapper.d.ts",
    "lib.js"
  ],
  "module": "route_snapper.js",
  "types": "route_snapper.d.ts",
  "sideEffects": [
    "./snippets/*"
  ]
}

So there will be a little work to figure out what a normal TS NPM package looks like, and get the auto-generated route_snapper.d.ts types to also include the manually-written TS lib.

@dabreegster
Copy link
Owner Author

I've got some ideas to simplify things here -- I'm going to try building + publishing just the WASM library as one NPM package, then making a second package with the nice TS wrapper. I'm having trouble finding a minimal working template to do that, though -- I've been trying the Vite vanilla TS starter, but it doesn't appear suitable for publishing libraries, as it's insisting on an index.html and main.ts. Happen to know any simple templates or guides to follow? Personal recommendations win over outdated Medium articles ;)

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

2 participants