Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading AMD module inside JavaScriptCore

I would like to load a javascript module inside JavascriptCore (on iOS).

I'm fetching the text of the file via a normal HTTP request on the iOS side. So I can parse that entire string into the JScontext.

But now I'd like to load that module, and ideally, resolve any other dependencies, although this is not essential.

I've tried using requireJS for node, but it seems to have lots of dependency errors, and perhaps it is a bit out of date.

I've also tried steal, but I'm not sure if that is the right path either. I've also tried this. https://github.com/millermedeiros/nodefy

Essentially I want to do what require js does in the browser, but in a pure javascript environment (no browser, no node).

I also want to bundle everything down using browserify, and have looked at things like this - RequireJS load string

The problem is, when I go to browserify the code, (requiring, requirejs) the process fails with unfound dependencies?

Can anyone point me in the right direction?

like image 705
Chris Barry Avatar asked May 12 '17 17:05

Chris Barry


People also ask

What is AMD module loader?

Asynchronous module definition (AMD) is a specification for the programming language JavaScript. It defines an application programming interface (API) that defines code modules and their dependencies, and loads them asynchronously if desired.

What is CommonJS and AMD?

AMD and CommonJS are both Javascript module loader. They accomplish the same task but works different. AMD is better for browser, hence, the name 'Asynchronous', as it loads each distinct module in async manner instead of loading in one large file. No extra steps are required to use AMD, it works out-of-the-box.


1 Answers

The technologies you're mentioning aren't completely compatible with one another. Consider a line like require('meow'). With browserify/webpack/etc the file that will get loaded is node_modules/meow/main.js (depending on what package.json says is the main file). In requirejs this same file will load the path /meow through the browser or from the baseUrl root of your project. The way to override this is to add config options in requirejs. This is really painful because it effectively causes you to create an entry for every package in node_modules. In short, you can't really combine requirejs and browserify without a lot of work.

To set-up code splitting using browserify or webpack you need to define a new entry point for the bundle. Then when you load it from your application you need to load that js bundle by doing something like fetch('https://myserver.com/bundle2.js').then().... There are a handful of tools that can help you do this using either webpack or browserify.

  1. Some strategies with Browserify - http://esa-matti.suuronen.org/blog/2013/04/15/asynchronous-module-loading-with-browserify/
  2. Webpack - https://webpack.github.io/docs/code-splitting.html

The web's story isn't very good around this right now (as of 2017). Most systems do this in some custom way.

like image 142
Parris Avatar answered Sep 21 '22 22:09

Parris