core icon indicating copy to clipboard operation
core copied to clipboard

Map imports and advanced imports like Deno for MetaCall

Open viferga opened this issue 4 years ago • 1 comments

Recently @trgwii suggested the following addition for metacall.json:

{
  ...
  "imports": {
    "ramda": "https://raw.githubusercontent.com/ramda/ramda/v0.27.1/dist/ramda.min.js"
  }
}

And then do in Python (or any other language):

import ramda

An extension to the previous mechanism may be the following:

{
  "imports": {
    "ramda": "npm:ramda",
    "telethon": "pip3:telethon"
  }
}

Python is the language that would need import maps in order to implement this kind of imports. Other languages like Ruby or NodeJS may allow to do this easily like:

require 'https://raw.githubusercontent.com/ramda/ramda/v0.27.1/dist/ramda.min.js'
const R = require('https://raw.githubusercontent.com/ramda/ramda/v0.27.1/dist/ramda.min.js');

viferga avatar Dec 15 '20 23:12 viferga

I'd also like to add that import maps have their own issues, so they should only be used on an as-needed basis (if there is no other way to make a module work within a language that doesn't support string-based imports).

Ideas for resolutions:

  • "ramda": "npm:ramda" - loads / installs the module from npm (npm install or similar), and uses normal node module resolution (look for index.js / package.json "main") to load it (node loader probably takes care of this).
  • "telethon": "pip:telethon" - loads / installs the module from pip (hopefully into a virtualenv / pipenv environment) and loads the module using python loader
  • "http": "gem:http" - Installs a ruby gem and loads it using ruby.

When adding a module to the import map it should expose it to ALL languages simultaneously, so after adding any one of these, you should be able to load them from any language.

Languages should attempt to load a language-native module FIRST, before trying to consult the import map for external modules. Example: require('http') in node should always load the internal http module for node FIRST, even if the ruby gem is defined in the import map. The solution to this is to rename the module like: "ruby-http": "gem:http", then you should be able to require('ruby-http') from node.

For direct URL import mappings: "ramda": "https://raw.githubusercontent.com/ramda/ramda/v0.27.1/dist/ramda.min.js" We probably need some careful consideration how to handle this (which loader loads this module? Node, v8 / js, or other?) If anyone is going to work on this I would start with the package-manager based loaders, then we can look into HTTP / other file loaders later. (A potential solution might be to prefix the file with the loader: "ramda": "node:http://...")).

trgwii avatar Dec 16 '20 11:12 trgwii