Getting started with Yabble - loading the CommonJS module on the browser side

Anyone familiar with Yabble or other CommonJS loaders on the browser side?

I am experimenting with Node.js and would love to create Javascript modules that can be interchangeable on the server side and client side. It may end up being more “because it's awesome” is something more than “because it's practical and useful.”

As such, I'm basically trying to get the CommonJS method require()to work on the browser side, which is what Yabble should do. I don’t know where to start. I can't seem to find any documentation other than what is found in the Yabble Github readme , and that helps a little.

Essentially, everything I did fits in the HTML page ...

<script src="yabble.js"></script>

<!-- Uses require -->
<script>
    require.setModuleRoot('http://localhost:8030/')
    my_module = require('my_module')
</script>

But anytime when I call a function require(), I get an exception Synchronous require() is not supported..

Can someone help me get started? Where should I download yabble.js, where should I request a call? Is there a special way to run my Javascript modules?

+5
source share
2 answers

When loading the Javascript code that should use the function require()in the browser, the function should be the entry point to this code require.run().

e.g. Good:

<script src="yabble.js"></script>

<script>
    require.setModuleRoot('http://localhost:8030/')
    require.run('my_module') // <-- Uses require() function somewhere
</script>

e.g. Bad (will get an error Synchronous require() is not supported):

<script src="yabble.js"></script>
<script src="http://localhost:8030/my_module.js"></script> <!-- <== Use require function somewhere -->

FYI, , Yabble . Javascript, , require(), .js script , .js script .

, .js, , require(). , ...

if (False) { require('some_module'); }

... Yabble .

+6

. :

var x = require('something_remote.js')

, (.. ), , . - JavaScript (, , ) . , async . :

http://www.sitepen.com/blog/2010/07/16/asynchronous-commonjs-modules-for-the-browser-and-introducing-transporter/

, , , require() , XHR, , , , / . , .

, , , :

require("something.js", function () { // called later, after something.js has loaded! })

, RequireJS :

http://requirejs.org/docs/start.html

, ?

JavaScript-, NodeJS .., "" , , .

JS: -)

+2

All Articles