https://discord.cloudflare.com logo
Join Discord
Powered by
# workers-discussions
  • j

    johtso

    03/10/2023, 11:34 PM
    dynamic imports are no fun because you lose typing
  • h

    HardAtWork

    03/10/2023, 11:35 PM
    Yeah, a lot of libraries expect that. Sometimes, you just gotta rewrite it yourself
  • j

    James

    03/10/2023, 11:36 PM
    Depending on how the library works, you can do things like
    --define SOME_VAR=foo
    to replace things in code at build time
  • j

    James

    03/10/2023, 11:37 PM
    So if it's looking for
    process.env.API_KEY
    , something like
    --define process.env.API_KEY="foo"
    should work (or could be accomplished with a custom build)
  • j

    johtso

    03/10/2023, 11:38 PM
    ooooh, what's this feature?
  • j

    johtso

    03/10/2023, 11:39 PM
    is this wrangler?
  • j

    James

    03/10/2023, 11:39 PM
    Yeah this is wrangler. Though any build tooling (esbuild, webpack, etc.) all have some way of doing "defines" like that
  • j

    James

    03/10/2023, 11:40 PM
    https://developers.cloudflare.com/workers/wrangler/commands/#publish - scroll down a bit to
    --define
  • j

    James

    03/10/2023, 11:40 PM
    It's effectively wrapping this: https://esbuild.github.io/api/#define
  • j

    johtso

    03/10/2023, 11:41 PM
    oh nice!
  • j

    johtso

    03/10/2023, 11:41 PM
    so I can have the secret in my github secrets and bundle it into my code at build time
  • j

    johtso

    03/10/2023, 11:46 PM
    @James is that across the entire bundled code? so I could use it to replace strings in dependencies too?
  • j

    James

    03/10/2023, 11:46 PM
    I think so
  • j

    James

    03/10/2023, 11:46 PM
    I've not tested outside of user code tbh
  • j

    johtso

    03/10/2023, 11:46 PM
    do you know if it works with dev mode?
  • j

    James

    03/10/2023, 11:47 PM
    it does yeah. It should be documented on that same page under
    dev
    too
  • j

    johtso

    03/10/2023, 11:47 PM
    nice!
  • j

    johtso

    03/10/2023, 11:47 PM
    will have a play, should get me a long way toward forcing some packages to work
  • j

    James

    03/10/2023, 11:48 PM
    awesome, good luck 😄
  • j

    johtso

    03/10/2023, 11:48 PM
    obviously relies on the thing you're replacing to be globally unique..
  • j

    johtso

    03/10/2023, 11:48 PM
    which shouldn't be a problem if it's some kind of
    process.env['FOO']
    thing
  • j

    James

    03/10/2023, 11:49 PM
    yeah, true
  • j

    johtso

    03/10/2023, 11:49 PM
    and I could also use it to nuke
    import { URL } from "node:url";
  • j

    johtso

    03/10/2023, 11:50 PM
    although not clear on how the syntax deals with spaces
  • j

    James

    03/10/2023, 11:51 PM
    I'd probably reach for a custom build if you do anything complex. We have stuff like this with an esbuild config for example:
    Copy code
    define: {
        'process.env.NODE_ENV': JSON.stringify('production')
    },
  • j

    James

    03/10/2023, 11:52 PM
    I've never tried using it to nuke imports like that - I'm not sure that'd work - would depend on if this happens before or after bundle 🤔
  • h

    HardAtWork

    03/10/2023, 11:53 PM
    What’s with the stringify?
  • j

    James

    03/10/2023, 11:54 PM
    copy/pasta and trimmed. It's more complex than that in real code, but you have to wrap it in quotes, so that when it's inlined you get
    "production"
    and not just
    production
    otherwise your code will treat
    production
    as a literal and complain it's undefined
  • j

    James

    03/10/2023, 11:54 PM
    you could also just do
    'process.env.NODE_ENV': '"production"'
    here
  • j

    James

    03/10/2023, 11:54 PM
    but I pulled that from a large code snippet that does more stuffs
1...233523362337...2509Latest