letorbi / tarp.require Goto Github PK
View Code? Open in Web Editor NEWA lightweight & asynchronous JavaScript loader for CommonJS and NodeJS modules.
License: GNU Lesser General Public License v3.0
A lightweight & asynchronous JavaScript loader for CommonJS and NodeJS modules.
License: GNU Lesser General Public License v3.0
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?
It's good to have this library in bower
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.
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.
Env:
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.
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?
https://xhr.spec.whatwg.org/#the-open()-method
However you're implementing this, is apparently in danger of being dropped in future browsers.
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.
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.
I tried to use require.js in a simple test-case, and got some cliend-side errors. A full description is here:
http://stackoverflow.com/questions/21377892/using-node-js-modules-in-html
Given the following code in a module:
let sub = require ("foo");
Submodule foo
is not preloaded. This is apparently due to the space between require
and (
.
URL: http://localhost:8080/test/mongo.html
Script:
const require = Tarp.require;
require('mongorito');
Error: require.js:111 GET http://localhost:8080/test/node_modules/mongorito.js 404 (Not Found)
Notice that the real node_modules
folder is one level above the test
folder and not inside it.
How to fix that?
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 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
).
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
It would be grate, if it will be possible that the require preloader will ignore out-commented require calls.
This fails on Android 2.3.x platform
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.
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.
So I have an on-click button on index.html, which refers to a function in the main.js module, which can't be seen.
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!
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.
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.
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?
add package.json
to be able to npm i -S tarp.require
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
when it
const EventEmitter = require('events');
that trans to
http://localhost:63342/janus-videoroom-api/node_modules/events.js
but in reallity it in
http://localhost:63342/janus-videoroom-api/node_modules/event/events.js
,even other module it not event.js , it can be any *.js.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.