Skip to content

Dynamic imports #1

Description

@newtack

I know this is not supported today, but would love to see this being supported in the future.

Use case: We have a dynamic server rendered web server where we use configuration and dynamic imports based on that configuration to fetch the relevant page handler.

Activity

  1. WardCunningham commented on May 11, 2021

    @WardCunningham

    Federated wiki uses dynamic imports for rarely used or newly created item formatting plugins. We haven't yet figured out exactly how Deploy helps us, perhaps as insolation from older or non-compliant browsers. We usually stop thinking these things when we remember we can't load plugins.

  2. Industrial commented on Nov 7, 2021

    @Industrial

    I'm trying to get https://github.com/exhibitionist-digital/ultra deployed (react 18, streaming SSR) but after forking it and removing the import maps I found that it uses dynamic imports, so it's currently not possible to deploy with.

  3. nnmrts commented on Nov 20, 2021

    @nnmrts

    @Industrial Same...I don't really see the reason for dynamic import being disabled, but I'm also not a backend expert.

    I tried a lot of things but I eventually gave up and now use a DigitalOcean Droplet...but a Deno Deploy solution would be really awesome, not just because of the money I now have to pay for DO, but because Deno Deploy is so easy and fast to work with.

  4. mashaal commented on Feb 23, 2022

    @mashaal

    I can see why dynamic imports of remote libs may be unwanted, but there is a lot of value in the ability to dynamically import local modules. Modern frontend frameworks could then have native code-splitting -- importing route and component files as needed, like @newtack explained above.

    Libraries like React and Solid are now supporting dynamic imports. It currently does work in Deno when server side rendering, so I imagine we will be seeing more of this in the near future: https://reactjs.org/docs/code-splitting.html#reactlazy

    Maybe we could declare all remote libs in an importmap -- then only allow local modules to be imported?

    @Industrial I did push and update to https://github.com/exhibitionist-digital/ultra which allows deployment to Deno Deploy, but yes, you need to ensure you don't actually use dynamic imports in your codebase.

  5. BrunoBernardino commented on Mar 31, 2022

    @BrunoBernardino

    Here's an example similar to the situation as OP that doesn't work right now in Deno Deploy: https://github.com/BrunoBernardino/deno-boilerplate-simple-website/blob/5d32fb081d6080564e91a8ca8fdd6921ab9420cc/routes.ts#L31-L35

    This means the alternative is to load all pages even if they're not going to be needed, so that it works in Deno Deploy: https://github.com/BrunoBernardino/deno-boilerplate-simple-website/blob/5d32fb081d6080564e91a8ca8fdd6921ab9420cc/routes.ts#L4-L12

    Not having this functionality means it'll be impossible for any similar performance logic to be used with Deno Deploy, which seems like a shame.

  6. johnhenry commented on Apr 21, 2022

    @johnhenry

    Not having dynamic imports prevents this from working: https://github.com/withastro/astro/tree/main/packages/integrations/deno

  7. ayoreis commented on Apr 29, 2022

    @ayoreis

    I'm building an web library (imagine React but 100× better) but would need Dynamic imports for async importing of data URLs. Of course works in Deno, but I would also like it to deployable to Deno Deploy. Would be great if you'd add this! Thanks.

  8. Wolven531 commented on May 20, 2022

    @Wolven531

    For those that land here from GQL (https://doc.deno.land/https://deno.land/x/gql@1.1.1/mod.ts or https://deno.land/x/gql@1.1.1), to avoid using the dynamic import in this module, I created a local version of the GraphQLHTTP function whose ONLY change is the dynamic import being changed to non-dynamic. The source is below

    import {
    	GQLOptions,
    	runHttpQuery,
    } from 'https://deno.land/x/gql@1.1.1/common.ts';
    import { renderPlaygroundPage } from 'https://deno.land/x/gql@1.1.1/graphiql/render.ts';
    import type { GQLRequest } from 'https://deno.land/x/gql@1.1.1/types.ts';
    
    // !!! all code below is from https://deno.land/x/gql@1.1.1/http.ts
    // except for removal of dynamic import
    
    /**
     * Create a new GraphQL HTTP middleware with schema, context etc
     * @param {GQLOptions} options
     *
     * @example
     * ```ts
     * const graphql = await GraphQLHTTP({ schema })
     *
     * for await (const req of s) graphql(req)
     * ```
     */
    export function GraphQLHTTP<
    	Req extends GQLRequest = GQLRequest,
    	Ctx extends { request: Req } = { request: Req },
    >({
    	playgroundOptions = {},
    	headers = {},
    	...options
    }: GQLOptions<Ctx, Req>) {
    	return async (request: Req) => {
    		if (options.graphiql && request.method === 'GET') {
    			if (request.headers.get('Accept')?.includes('text/html')) {
    				// !!! only modified portion is here
    				// const { renderPlaygroundPage } = await import(
    				// 	"./graphiql/render.ts"
    				// );
    				// !!! end of modified portion
    				const playground = renderPlaygroundPage({
    					...playgroundOptions,
    					endpoint: '/graphql',
    				});
    
    				return new Response(playground, {
    					headers: new Headers({
    						'Content-Type': 'text/html',
    						...headers,
    					}),
    				});
    			} else {
    				return new Response(
    					'"Accept" header value must include text/html',
    					{
    						status: 400,
    
    						headers: new Headers(headers),
    					},
    				);
    			}
    		} else {
    			if (!['PUT', 'POST', 'PATCH'].includes(request.method)) {
    				return new Response('Method Not Allowed', {
    					status: 405,
    					headers: new Headers(headers),
    				});
    			} else {
    				try {
    					const result = await runHttpQuery<Req, Ctx>(
    						await request.json(),
    						options,
    						{ request },
    					);
    
    					return new Response(JSON.stringify(result, null, 2), {
    						status: 200,
    						headers: new Headers({
    							'Content-Type': 'application/json',
    							...headers,
    						}),
    					});
    				} catch (e) {
    					console.error(e);
    					return new Response('Malformed request body', {
    						status: 400,
    						headers: new Headers(headers),
    					});
    				}
    			}
    		}
    	};
    }
  9. brundonsmith commented on Jun 22, 2022

    @brundonsmith

    Any movement on this? It's keeping me from using Deno Deploy for a project


    Edit: I came up with a workaround; I'll share it here for others that may be stuck.

    My situation is one where I didn't strictly need dynamic imports (the modules to be imported all exist in source control), I just wanted to import them dynamically for ergonomics; when I add a new file in a certain directory, I want it to be picked up without going and manually configuring it to be loaded.

    What I did as a workaround is I wrote a script to walk that directory and generate a TypeScript file that statically imports all the relevant modules, and re-exports their contents in a way where they can be looked up by module path. This manifest is then checked into source control, so by the time it gets to Deno Deploy everything is static. Hopefully I'll be able to drop this mechanism one day.

    You can see it here: https://github.com/brundonsmith/site/blob/master/routes-manifest-generate.ts

  10. orta commented on Jul 15, 2022

    @orta

    I'm hitting this because I need to set up a bunch of node-y / browser-y globals before my app's code is loaded due to a library not supporting deno - Azure/azure-sdk-for-js#13281 (comment) (gonna look into getting this uncoupled though dropped the dep by re-creating it in fetch myself, leaving the comment because it is still a legit case for why you might need the dynamic import)

  11. yidingww commented on Jul 16, 2022

    @yidingww

    Any progress on this? I am hitting this problem too when using presetWebFonts from UnoCSS

    screenshot

  12. 5310 commented on Jul 16, 2022

    @5310

    It's been over a year. This was the first issue created on this repository. If we were going to get a response, we would've gotten one by now.

  13. MarkBennett commented on Sep 8, 2022

    @MarkBennett

    Has anyone tried out this ponyfill? It claims to let you import from a string on Deno Deploy.

    https://github.com/ayoreis/import

  14. 10 remaining items

  15. TheYuriG commented on May 24, 2023

    @TheYuriG

    Goddamnit, there should really be some kind of warning about this when using it in development, even if it's just some CLI warning, Deno already does a great job constantly warning us to not perform anti-patterns. This should be definitely added to avoid us developers wasting so much time developing a feature that will not work on Deno Deploy (if you guys want it to be the defacto choice for Deno websites, that is), but will work just fine everywhere else (like the user above mentioned using Digital Ocean).

    I did a whole thing where I created blog posts and would dynamically load them from [blog]/[post].tsx and now I'll need to create a bunch of individual .tsx files instead of just being able to dynamically use them that way.

    I love the DX, but I'll be damned if the overall (lack of|outdated) documentation doesn't put a damper on my excitement about this runtime.

  16. BrunoBernardino commented on May 24, 2023

    @BrunoBernardino

    Goddamnit, there should really be some kind of warning about this when using it in development, even if it's just some CLI warning, Deno already does a great job constantly warning us to not perform anti-patterns. This should be definitely added to avoid us developers wasting so much time developing a feature that will not work on Deno Deploy (if you guys want it to be the defacto choice for Deno websites, that is), but will work just fine everywhere else (like the user above mentioned using Digital Ocean).

    I did a whole thing where I created blog posts and would dynamically load them from [blog]/[post].tsx and now I'll need to create a bunch of individual .tsx files instead of just being able to dynamically use them that way.

    I love the DX, but I'll be damned if the overall (lack of|outdated) documentation doesn't put a damper on my excitement about this runtime.

    Tell me about it. Compare https://github.com/BrunoBernardino/deno-boilerplate-simple-website/blob/main/routes.ts#L43-L44 (what's needed if Deno Deploy supported it):

    // NOTE: Use this instead once https://github.com/denoland/deploy_feedback/issues/1 is closed
    // const { pageContent } = await import(`./pages/${id}.ts`);

    vs https://github.com/BrunoBernardino/deno-boilerplate-simple-website/blob/main/routes.ts#L10-L24 (what's necessary for it to work on Deno Deploy):

    // NOTE: This won't be necessary once https://github.com/denoland/deploy_feedback/issues/1 is closed
    import * as indexPage from './pages/index.ts';
    import * as ssrPage from './pages/ssr.ts';
    import * as dynamicPage from './pages/dynamic.ts';
    import * as formPage from './pages/form.ts';
    import * as webComponentPage from './pages/web-component.ts';
    import * as reactPage from './pages/react.tsx';
    const pages = {
      index: indexPage,
      ssr: ssrPage,
      dynamic: dynamicPage,
      form: formPage,
      webComponent: webComponentPage,
      react: reactPage,
    };
  17. cowboyd commented on Jun 7, 2023

    @cowboyd

    This is a show stopper for using MDX on Deno Deploy without introducing a build step, which is a shame.

  18. yuhr commented on Jun 7, 2023

    @yuhr

    What I was surprised at is that Fresh states it has “No build step”, but actually it does a build internally. It's just hidden behind some UX design while development, but if one wants to track the project by Git (necessarily!), they has to be aware of this fact, as noticed in the generated fresh.gen.ts:

    // DO NOT EDIT. This file is generated by fresh.
    // This file SHOULD be checked into source version control.
    // This file is automatically updated during development when running `dev.ts`.
    

    It's okay for me, but feels like this could introduce chances of deployment failure, like forgetting to regenerate fresh.gen.ts when adding/removing new routes. Hopefully I would like to see Deno Deploy supports dynamic imports. Any kind of filesystem-based routing framework would be happy with it.

  19. added 2 commits that reference this issue on Jun 22, 2023
  20. ry commented on Jul 11, 2023

    @ry
    Member
  21. Industrial commented on Jul 11, 2023

    @Industrial

    This is a great feature! Now the sky is the limit =)

  22. ceifa commented on Jul 11, 2023

    @ceifa

    Actually only statically analyzable doesn't solve my use case :/

  23. TheYuriG commented on Jul 11, 2023

    @TheYuriG

    This doesn't help my use case either. If the issue is considered complete now, I'm glad that I've pivoted to another method of fetching my files, but it would have sucked if I had waited for this.

  24. cowboyd commented on Jul 12, 2023

    @cowboyd

    This is a show stopper for using MDX on Deno Deploy without introducing a build step, which is a shame.

    It turn's out I was wrong here. You can use MDX without a build step on deno deploy. For any other MDX users who come across this, it supports an evaluate() method which will compile the MDX dynamically and eval() it. (eval caveats apply!)

    You can't do static imports inside your MDX, but you can just do static imports inside your JS code and then pass those values down into the mdx scope.

  25. added a commit that references this issue on Jan 2, 2025
  26. added a commit that references this issue on Nov 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions