browserify / browserify-handbook

how to build modular applications with browserify
Other
4.58k stars 292 forks source link

don't understand how to convert to browser #47

Closed rickdog closed 9 years ago

rickdog commented 9 years ago

Section "bundling for the browser" explains how to convert for browser inclusion. I'm trying to run virtual-dom directly in browser, so I browserify it:

browserify h.js > bundle.js

h.js contains:

var h = require("./virtual-hyperscript/index.js")
module.exports = h

the generated bundle.js begins with:

(function e(t,n,r){function s(o,u){ ...
var h = require("./virtual-hyperscript/index.js")
module.exports = h

my index.html:

<html>
  <body>
    <script src="bundle.js"></script>
   <script>
   h('div', { ...  // generates ReferenceError: h is not defined
  </body>
</html>

how do I grab the h variable??

ghost commented 9 years ago

Instead of 2 <script> tags, it's more common to have a single script tag. Otherwise you would need to set globals which cause problems.

So just put:

var h = require('virtual-dom/h')
var elem = h('div', 'whatever')

in a file such as main.js then you just do:

$ browserify main.js > bundle.js

Then you can put just a single <script> tag into the page.

rickdog commented 9 years ago

ty, mr lambda (cmder.exe?)(LISP?)

I'm making the change to node so I've a bit to learn.