Giter Site home page Giter Site logo

ayoreis / import Goto Github PK

View Code? Open in Web Editor NEW
25.0 25.0 7.0 44 KB

A dynamic imports polyfill for Deno Deploy and compiled executables

Home Page: https://deno.land/x/import

License: MIT License

TypeScript 100.00%
compile deno deploy dynamic-import modules polyfill

import's Introduction

๐Ÿ‘‹ Hello, ๐ŸŒ world!
I'm Ayo,
I make/do stuff/things.

import's People

Contributors

ayoreis avatar danielr1996 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

import's Issues

Module source is being botched by mod.ts:95-101

I'm trying to work around the dynamic import issue with Deno Deploy (denoland/deploy_feedback#1) and I'm getting the following error when using importModule vs. native import:

SyntaxError: Unexpected token 'default'
    at AsyncFunction (<anonymous>)
    at buildAndEvaluate (https://deno.land/x/[email protected]/mod.ts:58:12)
    at async importModule (https://deno.land/x/[email protected]/mod.ts:70:16)
    at async getNotebook (file:///src/observable.ts:30:31)

I'm trying to dynamically import Observable notebooks. It works fine when running the Deno script locally and using native import.

Here's an example: https://api.observablehq.com/d/6388e91a5ea79803.js?v=3

The source is importing with native import or when you fetch this is:

function _1(md){return(
md`# Deno Function Test`
)}

function _addTwo(){return(
x => x + 2
)}

function _result(addTwo){return(
addTwo(2)
)}

export default function define(runtime, observer) {
  const main = runtime.module();
  main.variable(observer()).define(["md"], _1);
  main.variable(observer("addTwo")).define("addTwo", _addTwo);
  main.variable(observer("result")).define("result", ["addTwo"], _result);
  return main;
}

However, what I'm seeing for body at AsyncFunction (mod.ts:103) is:

// deno:https://api.observablehq.com/d/6388e91a5ea79803.js?v=3&api_key=a9cb22008fc47c23b8bc2d0dc269949b1d1aaeee&cache=1670648818850
function _1(md) {
  return md`# Deno Function Test`;
}
function _addTwo() {
  return (x) => x + 2;
}
function _result(addTwo) {
  return addTwo(2);
}
function define(runtime, observer) {
  const main = runtime.module();
  main.variable(observer()).define(["md"], _1);
  main.variable(observer("addTwo")).define("addTwo", _addTwo);
  main.variable(observer("result")).define("result", ["addTwo"], _result);
  return main;
}
return {default
};
: 
  define

It appears that this is the culprit: https://github.com/ayoreis/import/blob/main/mod.ts#L95-L101

Could this package possible work in combination with `deno compile`

I know the README explicetely states that it is inteded for use with deno deploy. However I was wondering if this could also be used to support dynamic imports for a deno compiled application, since deno doesn't allow dynamic imports since two years after opening the issue .

I couldn't get importModule to work at all, however importString seemed to work at first, but when I compile my application I get

ERROR: [plugin: deno] Failed to call 'deno info' on 'https://denopkg.dev/gh/danielr1996/bumpup@main/packages/log/mod.ts'
    {
      detail: Error: Failed to call 'deno info' on 'https://denopkg.dev/gh/danielr1996/bumpup@main/packages/log/mod.ts'
        at Module.info (https://deno.land/x/[email protected]/src/deno.ts:26:19)
        at async loadFromCLI (https://deno.land/x/[email protected]/src/native_loader.ts:24:42)
        at async load (https://deno.land/x/[email protected]/src/native_loader.ts:9:20)
        at async callback (https://deno.land/x/[email protected]/wasm.js:973:28)
        at async handleRequest (https://deno.land/x/[email protected]/wasm.js:701:30),
      id: "",
      location: {
        column: 18,
        file: "file:///C:/Users/danie/Documents/Entwicklung/bumpup",
        length: 68,
        line: 1,
        lineText: 'import {log} from "https://denopkg.dev/gh/danielr1996/bumpup@main/packages/log/mod.ts";',
        namespace: "",
        suggestion: ""
      },
      notes: [],
      pluginName: "deno",
      text: "Failed to call 'deno info' on 'https://denopkg.dev/gh/danielr1996/bumpup@main/packages/log/mod.ts'"
    }

As it seems indeed esbuild is used to compile the string instead of the dynamic data uri import. deno info works locally for me

$ deno info https://denopkg.dev/gh/danielr1996/bumpup@main/packages/log/mod.ts
local: C:\Users\danie\AppData\Local\deno\deps\https\denopkg.dev\649ed91de23002ed1a193a9522b3f0538bd7d6a726bfdb033defe48d62c502e8
emit: C:\Users\danie\AppData\Local\deno\gen\https\denopkg.dev\649ed91de23002ed1a193a9522b3f0538bd7d6a726bfdb033defe48d62c502e8.js
type: TypeScript
dependencies: 3 unique (total 1.03KB)

https://denopkg.dev/gh/danielr1996/bumpup@main/packages/log/mod.ts (53B)
โ””โ”€โ”ฌ https://denopkg.dev/gh/danielr1996/bumpup@main/packages/log/src/log.ts (363B)
  โ””โ”€โ”ฌ https://denopkg.dev/gh/danielr1996/bumpup@main/packages/log/deps.ts (60B)
    โ””โ”€โ”€ https://denopkg.dev/gh/danielr1996/bumpup@main/packages/cli/src/lib/types.ts (

so im unsure why this error gets thrown.

I will look further into esbuild, maybe this provides a solution to my problem, but it would be really nice if this library could be used to use dynamic imports with deno compile

Using `importModule` inside `importString`

I have a use case where I need to import other modules from a JS string:

import { importString, importModule } from "https://deno.land/x/import/mod.ts";

let code = `
const renderer = async ()=>{
    const { render } = await importModule('https://deno.land/x/mustache_ts/mustache.ts');
    console.log(render)
    const template = '{{title}} spends {{calc}}'
    const view = {
        title: 'Joe',
        calc: () => 2 + 4
    }
    const output = render(template, view)
    return output
}
export default renderer
`

const {default:renderer} = await importString(code);
renderer().then(console.log) // expected: "Joe spends 6

Possible solution: passing an object of "dependencies" to the "importString" function as a second parameter, in which we could use the importModule or even other custom modules as well.

loads incorrect file

Calling await importModule("./MY_FILE.ts") loads https://deno.land/x/[email protected]/MY_FILE.ts instead of local file MY_FILE.ts. Native dynamic import works fine. How can i fix this? (Deno 1.29.4 on Arch linux)

Strange behavior while importing react

I have a different use case, where I build my SSR react entry-server.tsx using Vite (either with noExternals or with externals), then try to dynamically import it into Deno, but what happens is that it while processing the output from esbuild it doesn't find some React part, for instance useState that is being used by some component.

Not sure if esbuild is treeShaking my files the output in this case, but in any case I can't correctly import my module. Any advice?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.