node.js - Browserify - How to call function bundled in a file generated through browserify in browser -


i new nodejs , browserify. started link .

i have file main.js contains code

var unique = require('uniq');  var data = [1, 2, 2, 3, 4, 5, 5, 5, 6];  this.logdata =function(){ console.log(unique(data)); }; 

now install uniq module npm:

 npm install uniq 

then bundle required modules starting @ main.js single file called bundle.js browserify command:

browserify main.js -o bundle.js 

the generated file looks this:

(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);throw new error("cannot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){ var unique = require('uniq');  var data = [1, 2, 2, 3, 4, 5, 5, 5, 6];  this.logdata =function(){ console.log(unique(data)); };  },{"uniq":2}],2:[function(require,module,exports){ "use strict"  function unique_pred(list, compare) {   var ptr = 1     , len = list.length     , a=list[0], b=list[0]   for(var i=1; i<len; ++i) {     b =     = list[i]     if(compare(a, b)) {       if(i === ptr) {         ptr++         continue       }       list[ptr++] =     }   }   list.length = ptr   return list }  function unique_eq(list) {   var ptr = 1     , len = list.length     , a=list[0], b = list[0]   for(var i=1; i<len; ++i, b=a) {     b =     = list[i]     if(a !== b) {       if(i === ptr) {         ptr++         continue       }       list[ptr++] =     }   }   list.length = ptr   return list }  function unique(list, compare, sorted) {   if(list.length === 0) {     return []   }   if(compare) {     if(!sorted) {       list.sort(compare)     }     return unique_pred(list, compare)   }   if(!sorted) {     list.sort()   }   return unique_eq(list) }  module.exports = unique },{}]},{},[1]) 

after including bundle.js file index.htm page, how call logdata function ??

by default, browserify doesn't let access modules outside of browserified code – if want call code in browserified module, you're supposed browserify code module. see http://browserify.org/ examples of that.

of course, explicitly make method accessible outside this:

window.logdata =function(){   console.log(unique(data)); }; 

then call logdata() anywhere else on page.


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -