exhibitionist-digital / ultra

Zero-Legacy Deno/React Suspense SSR Framework
https://ultrajs.dev
MIT License
3k stars 68 forks source link

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

Closed dagda1 closed 2 years ago

dagda1 commented 2 years ago

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.

deckchairlabs commented 2 years ago

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
  }
}
dagda1 commented 2 years ago

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.

deckchairlabs commented 2 years ago

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

dagda1 commented 2 years ago

The idea is to make an executable from the app.

deckchairlabs commented 2 years ago

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

dagda1 commented 2 years ago

@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.

deckchairlabs commented 2 years ago

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

deckchairlabs commented 2 years ago

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.

dagda1 commented 2 years ago

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.

deckchairlabs commented 2 years ago

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

dagda1 commented 2 years ago

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

deckchairlabs commented 2 years ago

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

dagda1 commented 2 years ago

@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

deckchairlabs commented 2 years ago

@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

dagda1 commented 2 years ago

@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 commented 2 years ago

@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?

dagda1 commented 2 years ago

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

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

deckchairlabs commented 2 years ago

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