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

[rstream] in the web worker scope #425

Closed
bit-app-3000 opened this issue Oct 23, 2023 · 4 comments
Closed

[rstream] in the web worker scope #425

bit-app-3000 opened this issue Oct 23, 2023 · 4 comments

Comments

@bit-app-3000
Copy link

worker.js (esm) esbuild

import { fromIterable, trace } from '@thi.ng/rstream'

self.onmessage = function ({data}){
  fromIterable([1,3]).subscribe(trace('worker'))
}


|>  unhandled error: undefined
@bit-app-3000 bit-app-3000 changed the title Does the rstream library work in the web worker scope? Does the [rstream] library work in the web worker scope? Oct 23, 2023
@bit-app-3000 bit-app-3000 changed the title Does the [rstream] library work in the web worker scope? [rstream] in the web worker scope? Oct 23, 2023
@bit-app-3000 bit-app-3000 changed the title [rstream] in the web worker scope? [rstream] in the web worker scope Oct 23, 2023
@postspectacular
Copy link
Member

I don't know enough about your project setup, but I've not once run into any issues with using any of the packages in a worker. There's no reliance on any globals, so my guess this is a config issue with esbuild (sorry, no 1st hand experience with it, only via Vite). Can you please put up a small project somewhere for debugging/reproduction?

postspectacular added a commit that referenced this issue Oct 24, 2023
- also check for `import.meta.env.UMBRELLA_ASSERTS` for non-ViteJS tooling
- btw. this is **not** a fix for the esbuild issue in #425
(but part of its solution posted in comments)
postspectacular added a commit that referenced this issue Oct 24, 2023
- also check for `import.meta.env.UMBRELLA_GLOBALS` for non-ViteJS tooling
- btw. this is **not** a fix for the esbuild issue in #425
(but part of its solution posted in comments)
@postspectacular
Copy link
Member

@bit-app-3000 After a lot of digging, I found that esbuild does not support standard ESM import.meta look ups. This hasn't anything to do directly with thi.ng/rstream, but is required by the widely used assert() function of thi.ng/errors. To have esbuild deal with import.meta properly, we can write a little build plugin:

Plugin:

// plugin-import-meta-env.js

import { readFileSync } from "fs";

export default (prefixes = []) => ({
    name: "import.meta.env",
    setup({ onLoad }) {
        // only collect & expose selected (deemed safe!) env vars
        const env = { MODE: process.env.NODE_ENV };
        if (prefixes.length) {
            for (let k of Object.keys(process.env)) {
                if (prefixes.some((prefix) => k.startsWith(prefix))) {
                    env[k] = process.env[k];
                }
            }
        }
        const envFmt = `(${JSON.stringify(env)})`;
        // plugin hook (only apply to JS/TS files)
        onLoad({ filter: /\.[jt]sx?$/, namespace: "file" }, (args) => {
            const code = readFileSync(args.path, "utf8").replace(
                /\bimport\.meta\.env\b/g,
                envFmt
            );
            return { contents: code };
        });
    },
});

Then use a esbuild build script:

import { build } from "esbuild";
import pluginImportMetaEnv from "./plugin-import-meta-env.js";

await build({
    entryPoints: ["src/app.js", "src/store.js"],
    bundle: true,
    minify: true,
    outdir: "build",
    // can skip the looking for custom UMBRELLA prefix if you're just after using default behavior
    plugins: [pluginImportMetaEnv(["UMBRELLA_"])],
});

...and build via: node build.js

I've pushed some new releases (incl. the above linked commits), so also make sure to update your dependencies, just in case...

@bit-app-3000
Copy link
Author

bit-app-3000 commented Oct 24, 2023

thx for the help

found another workaround

--define:import.meta.env={}

can there be pitfalls?

@postspectacular
Copy link
Member

Cool. Good to know...

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