exhibitionist-digital / ultra

Zero-Legacy Deno/React Suspense SSR Framework

Home Page:https://ultrajs.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

dynamic import with variable fails with module not found when running on compiled code

dagda1 opened this issue · comments

commented

I have the following code:

import { createFactory, Factory } from "../factory/factory.ts";
const computedPath = Deno.env.COMPUTED;

export async function makeContext() {
  // we need to load the schema here also
  const { computed } = await import(computedPath);

  const factory = createFactory(computed, 'inspector');

  return {
    factory
  }
}

This results in a module not found error when running the code that has been built in the .ultra directory. Is it possible to use dynamic imports with variables?

If I change this line:

  const { computed } = await import(computedPath);

to

  const { computed } = await import('../computed.ts');

and run

deno task build
./.ultra
deno task start

Then it loads fine.

Variable dynamic imports are basically impossible to build a module graph for, which we do to vendor the correct dependencies. What is the use case for this? Is it possible to adapt the code to a switch or some other kind of conditional?

eg.

import { createFactory, Factory } from "../factory/factory.ts";
const computedPath = Deno.env.COMPUTED;

export async function makeContext() {
  // we need to load the schema here also
  let computed: SomeComputedType
  
  switch (computedPath) {
    case 'path-one':
        const module = await import('./path-one.ts')
        computed = module.computed
        break
    default:
        const module = await import('./default.ts')
        computed = module.computed
        break
  }

  const factory = createFactory(computed, 'inspector');

  return {
    factory
  }
}
commented

Thanks for the reply.

The use case is for a CLI tool that is an object inspector and something we want to inspect does not serialize, i.e. it has functions so our idea was to pass a file as command line switch or something that we could then dynamically import.

I'm not sure I entirely understand, you're building a Deno based CLI tool, are you using this server side?

commented

The idea is to make an executable from the app.

Oh right! I haven't attempted this at all, using deno compile I assume?

commented

@deckchairlabs, yes, that is correct and then hopefully using it in electron. I realise this is not the main use case, so thank you for your patience.

Quite alright, pretty interested to know how far it gets currently? Are you just compiling the .ultra output directory?

I feel like this is something we should support. I'll have a play around this week and see what kind of work needs to be done to support it.

commented

I've not been able to get a build yet.

You very nicely fixed the skypack bug and then I have #156 which I can work around by manually changing the entry.

Is the issue those dynamic files just don't end up in the output directory?

commented

the problem is (and why a convention won't work) is that the location won't be known until runtime, i.e. it will be in some cwd of the users choosing

Interesting, which is why you need a CLI flag. Feel free to start a repo with minimal example and I'll have a tinker

commented

@deckchairlabs oh , thank you so much. I will certainly do that. Deno compile is one of the reasons we went down this path and then I saw how easy it was to create a react ssr app with ultra compared to the madness of webpack. Props to the project

@dagda1 Ah thanks for the nice words! Yeah, webpack hell can go suck an egg in my opinion.

Cool, I'll have some time this week to have a play around. I'll probably also try compiling one of our minimal examples

commented

@deckchairlabs here is a reproduction repo.

The README has the steps to recreate. Let me know if there is anything else I can do.

@deckchairlabs here is a reproduction repo.

The README has the steps to recreate. Let me know if there is anything else I can do.

I get a 404 visiting that link?

commented

@deckchairlabs oops, it had private visibility. I've changed it to public, please try again

https://github.com/thefrontside/ultra-compile-repo-

Ho @dagda1, sorry for the delay in looking into this. It would require too much work on the Ultra end to get this supported. I've also noticed that dynamic imports aren't supported in executables https://deno.land/manual@v1.25.2/tools/compiler#unavailable-in-executables