-
Notifications
You must be signed in to change notification settings - Fork 45
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
Is it possible to ship an npm package ? #507
Comments
@yanna92yar, I had not considered bundling this up for NPM. Would that be useful for others to use it quickly? |
Hey @mgeisler But I will be happy to share my installation as it worked as web assembly pckg in a node environment. If this helps in anyway Edit: my bad we were talking about an npm package. Mine is a docker image (installs wasm/rust) that compiles using wasm as suggested in doc, packs the folder in node_modules, which works fine in node. Anyway if you are interested I can clean and send code later :) |
Yeah, I guess we'll do something similar if we publish such a package: build it with a GitHub Action and then upload the resolving Wasm file. It's been a few years since I published a npm package (and that was with normal JavaScript) 😄 So I would be curious to see the |
@mgeisler me neither ^^ But anyway, I'm sur the matrix of versions is good here in this Dockerfile : Having this repository along with the example folder, inside my npm project (to be able to import textwrap): FROM node:16-alpine
WORKDIR /usr/src/app
RUN apk add --update bash && rm -rf /var/cache/apk/* && \
apk add build-base curl
COPY package*.json ./
RUN npm cache clean --force && rm -rf node_modules
RUN curl https://sh.rustup.rs -sSf | \
sh -s -- -y
ENV PATH=/root/.cargo/bin:$PATH
RUN chmod +x $HOME/.cargo/env
RUN source "$HOME/.cargo/env"
RUN curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh
COPY . .
RUN wasm-pack build --target nodejs /usr/src/app/lingua-rs/
RUN wasm-pack build --target nodejs /usr/src/app/textwrap/examples/wasm/ && \
npm pack /usr/src/app/textwrap/examples/wasm/pkg/ && \
npm i textwrap-wasm-demo-0.1.0.tgz
RUN npm i
# EXPOSE 3000
CMD [ "npm", "run", "start" ] I struggled with the dependency version name, so no need to add it in my (same process worked fine for another dependency I had: Next issue, I had with import { createRequire } from 'module'
const require = createRequire(import.meta.url)
const { draw_wrapped_text, WasmOptions, WasmPenalties } = require('textwrap-wasm-demo')
const { createCanvas, GlobalFonts } = require('@napi-rs/canvas')
let fontpath = 'OpenSans_Condensed-Italic.ttf'
let fontFamily = 'sans-serif'
GlobalFonts.registerFromPath(fontpath, fontFamily)
export function loadTransformCanvas({
text,
canvasWidth,
canvasHeight,
fontSize,
penalities: {
nlinePenalty,
overflowPenalty,
shortLineFraction,
shortLastLinePenalty,
hyphenPenalty,
},
options: { lineWidth, breakWords, wordSeparator, wordSplitter, wrapAlgorithm },
}) {
const canvas = createCanvas(canvasWidth, canvasHeight)
let ctx = canvas.getContext('2d')
ctx.clearRect(0, 0, canvasWidth, canvasHeight)
ctx.font = `${fontSize}px ${fontFamily}`
const penalties = new WasmPenalties(
nlinePenalty,
overflowPenalty,
shortLineFraction,
shortLastLinePenalty,
hyphenPenalty
)
let options = new WasmOptions(
lineWidth,
breakWords,
wordSeparator,
wordSplitter,
wrapAlgorithm,
penalties
)
draw_wrapped_text(ctx, options, text, penalties)
return ctx.canvas.toBuffer('image/png')
} Having the file dangling in root folder: Note thé canvas in node environment So this was my attempt with packaging textwrap that I abandoned because lacking time.. |
Hi Martin, Kindky |
Hi @bacloud23, I am not using npm myself these days, so someone else should publish a package. Perhaps you can look into it? |
Hi,
Thank you for this bright solution @mgeisler
I have no expertise in webassembly or rust. and also I would like to be able to run it on Node server. I know there is a way but is there an easy way ?
Thanks again
The text was updated successfully, but these errors were encountered: