Coder Social home page Coder Social logo

letorbi / tarp.require Goto Github PK

View Code? Open in Web Editor NEW
115.0 12.0 27.0 329 KB

A lightweight & asynchronous JavaScript loader for CommonJS and NodeJS modules.

License: GNU Lesser General Public License v3.0

JavaScript 61.85% HTML 38.15%
javascript commonjs module-loader

tarp.require's People

Contributors

danvk avatar letorbi avatar lwr avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

tarp.require's Issues

How can I change the module_path?

I want to add a module_path to require.js with a sub dictionary at the user's dictionary (like /home/Alice/js_modules) and keep the original path as well. How can I do that?

Does not work on local files

require does not work if the scrips are loaded locally (ie. if loaded from a file:///whatever.html page). This appears to be due to the fact that local files don't specify a mime type and that XMLHttpRequest then assumes that the files are XML and attempts to parse them as such.

Adding request.overrideMimeType('text/javascript'); just before request.open("GET", href, asyn); appears to fix the problem.

Async preload do not use the correct path

If I require a script async, then all require in this script, that do not use a relative path will do not use the configured path for preload. e.g.:
/index.html contains:
Tarp.require('./index', true);
index.js contains:
require('my_mod');
This will load the /index.js and than try to load the /my_mod.js. But correct would be to load the /node_modules/my_mod.js.
Later if the my_mod will be used, the correct my_mod will be loaded.

Relative require doesn't, in fact, work

Env:

  • Latest version of chrome
  • latest master standalone

Dir structure:
root/

index.html
js/

node_modules

vuejs-0.8.4

src

main.js
config.js

main.js:

require('./config.js')

index.html:

require('js/node_modules/vuejs-0.7.4/src/main')

Error: 404 loading file:///path/to/file/path/to/file/js/node_modules/vue-0.8.4/src/config.js Uncaught NetworkError: A network error occurred. (see the problem?)

I need this working tonight and will start investigating shortly.

conflict in project where antlr4 uses require and ace used requirejs

H,

I'm trying to use https://github.com/antlr/antlr4-javascript which uses the require from this project and also use ace (git://github.com/ajaxorg/ace.git#master) that is loaded by https://github.com/jrburke/requirejs.

yet the two have the same function name; require. And antlr code has the require throughout it to run on nodejs.

when I put them in separate pages no problem; but in the same page:

SmoothieError: 'require' already defined in global scope

because the requirejs is fighting it

Any way to get these to play together?

Require relative paths doesn't unwind

I've been considering a simple case to demonstrate, but I'll just post what I noticed for now.

I have a project something like

Voxelarium/Voxelarium.js - requires ./src/voxels.js
Voxelarium/src/voxels.js - required dynamically (./voxes/voxel_###.js) until it fails to find the next in line. (the 6 that are known to work do load correctly)
then Voxelarium.js requires ./src/sector.js

at this point the request that goes to the server is 'http://localhost/src/src/sector.js' (it keeps 'src' in there from when voxels.js loaded a requirement from a relative path...

---Voxelarim.js---

require( "./src/voxels.js" )
require( "./src/sector.js" )

-- Voxels.js ---
require( "./voxels/voxel_1.js" );


In voxels.js I previously was doing

        xhrObj.open('GET', `./src/voxels/voxel_${n}.js`, false);
        xhrObj.send(null);
        eval(xhrObj.responseText);

        //require( `./voxels/voxel_${n}.js` )

which I realized I could just replace with require now that I'm using your module... but then loading started failing.

Path config do not work as aspected

A path config like
<script>var TarpConfig = { require: { paths: "./lib/"} };</script>
do not work in the latest version. Now it's necessary to write
<script>var TarpConfig = { require: { paths: [(new URL("./lib/external/", location.href)).href] } };</script>
Otherwise I get the message:
TypeError: . is not a valid URL.

require a module from parent folder

The following HTML file:

<script src='require.js'></script>
<script>var module1 = require('module1'); </script>

works OK (on http://localhost). But, when I move the HTML file to a subfolder and change it to:

<script src='../require.js'></script>
<script>var module1 = require('../module1'); </script>

I get the following error:

 SmoothieError: unable to load module1 (404 Not Found)

Maybe the 'require' parser does not support going to parent folder?

HTTP Redirects do not work in IE11

HTTP redirects are not resolved properly in IE11, since it doesn't support the XMLHttpRequest.responseURL.

This might cause the same module to be loaded twice, if it is requested via different module-IDs that point to the same module-URL (e.g. ./foo/../bar and ./bar).

Supply code under MIT/BSD licence or equivalent

Hi Torben - we think your browser-based "require" system is really marvellous - it's the smallest version with the tighest and most logical code we found after a long search - many are over 1000 lines :)
We would love to use it in our project. Unfortunately our project rules prohibit the use of GPL3-based code since the viral nature of the licence would end up impacting all our dependencies, and this is the most recent version of the licence we could find for your algorithm.
We'd be REALLY grateful if you'd consider issuing a licence for the code that was MIT/BSD or a similar one - there'd be all of the benefits of ensuring it was preserved in the public domain as well as allowing it to be used in a wider variety of projects which integrate code from different sources.
Many thanks for considering this!
Antranig

http://opensource.org/licenses/BSD-3-Clause
http://opensource.org/licenses/MIT

Fix: make require work with web workers

To make tarp.require work from within a web worker, I had to add following changes:

var locationRef = location.protocol === 'blob:' ? location.origin : location.href;

var pwd = (new URL(id[0] == "." ? (parent ? parent.uri : locationRef) : config.paths[0], locationRef)).href;

When a blob: is detected as a protocol, it means we are in a worker thread and need to make sure we get the correct location.
(was too short to create a pull request)

It works also within workers in a standalone electron application.

"SCRIPT1086: Module import or export statement unexpected here require.min.js (2,170)"

The title shows my current error in the console for an app I'm trying to build.

My HTML is as it is in this Gist, except I have my actual Key in the file on disc: https://gist.github.com/DragonOsman/506bd2bae1dfe4c03a4bbe2a6d830324

And my main script file is called scripts.js, which is as it is here: https://gist.github.com/DragonOsman/c6e8fb15343544e662f474c5a526d1c2

The file country-currency-map.js is a Node module. Please help me fix this issue.

Edit: I also have an error saying:

HTTP404: NOT FOUND - The server has not found anything matching the requested URI (Uniform Resource Identifier).
(XHR)GET - http://localhost:8080/node_modules/node_modules/country-currency-map/country-currency-map.js

 SCRIPT5022: http://localhost:8080/node_modules/node_modules/country-currency-map/country-currency-map.js 404

I have the correct path mentioned in the call to require in my script file. And the script file itself that I'm using as my main script is a client-side script.

Can't access node_modules/@tarp when using express.static

This probably isn't a bug, I'm just having trouble getting this to work for my particular website structure. Sorry if the answer is obvious, I've just gotten stuck and could use some help.
Structure:
node_modules
static
--js
----main.js
--index.html
index.js

I put the 2 lines in index.html to load in tarp, but I get this error:
http://localhost:3000/node_modules/@tarp/require/require.min.js net::ERR_ABORTED 404 (Not Found)
I beleive this is because of the following line in my index.js file, which serves up the website:
app.use('/', express.static(path.join(__dirname, 'static')))
This seems to append "static" to the beginning of all server paths, which is great to access my html file, but not for loading tarp. Any suggestions for how to fix this? Thanks!

Circular import

test.html:

<!doctype html>
<html>
<head>
<script src="js/require.js"></script>
<script>
require('a');
</script>
</head>
</html>

a.js:

require('b')

b.js:

require('a')

This test case causes chrome to go into swap.

According to nodejs docs, this should never happen because each file is imported only once, the second time it is read from cache.

`import` cannot be called from a module

Firefox 59 throws an error when an ES6 module is loaded via:

import * as foo from 'fooModule';

This seems to be due to the fact that import statements are only allowed on the top-level of a script, but the module-code loaded by Tarp.require is wrapped into a function.

require a module from node_modules

My HTML file requires a module that requires another module that requires a module from the node_modules folder (which in turn requires some more modules from node_modules).

If I understand the docs correctly, currently the only way to include these modules is to add their folder in the initialization of smoothie, and then refer to these folders with an index, i.e., "1:[module_name]". But, then it will not work with Node.js...

Is there a way to require node_modues that works both in HTML and in Node.js?

can't create parser in IE Worker

Hi Torben,
not sure where Smoothie is going, but I thought you might have a solution for the below:
var parser = URL ? new URL(location.href) : document.createElement('A');
in IE in a web worker.

IE 10 has no support for URL and workers cannot create DOM objects.
As a result, using require from a web worker fails. I can't seem to think of a solution...
Eric

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.