-
Notifications
You must be signed in to change notification settings - Fork 149
Browserify
Browserify lets you convert node modules into browser-usable scripts.
search-index (as of 2015-03) is already prepared for browserify, but for me, it did not work out of the box.
So here is a short guide, on how I did get it to work.
Note, that I did this in Windows 7. So some parts of the instructions may not apply -or may not even be a problem- on other platforms (this mainly concerns modules that use node-gyp).
- node-gyp needs to be working, see https://github.com/TooTallNate/node-gyp#installation
- the search-index repository
And for reference, here specific setup, that I used:
- Windows 7
- Visual Studio 2010 (see also additional requirements for node-gyp)
- Node.js 0.12.0
-
go to the root directory of your cloned search-index repository
-
run
npm install
In an ideal world, you could now runbrowserify
and all would be well.
For me however, some additional steps were necessary: -
run
npm install WNdb
This was necessary, because WNdb seems to be a hidden dependency in the natural module - and runningbrowserify
complained that it was missing. -
run
npm install ffi
if it fails, runnpm install ffi -f
This was necessary due to a hidden dependency for lapack in natural - but lapack depends on ffi which refused to build on system. In the end, I persuaded ffi to build, by setting its node-gyp target version to 0.8.6 - surely not the nicest solution, but it finally got ffi to build. So first we need to install ffi with the force switch (i.e. -f), so that it is not deleted when it fails to build.
- getting ffi to build with the dirty hack of changing the targeted version:
editnode_modules\ffi\package.json
: in"scripts"
change
"install": "node-gyp rebuild"
to
"install": "node-gyp rebuild --target=\"0.8.6\""
- then run
npm install
for ffi and go back to the search-index's root directory
-
run
npm install lapack
Now that ffi is installed, we can add the missing dependency for lapack.
With lapack I again had a problem, because it depends on node-ffi. But since I already got ffi to work, and node-ffi is deprecated in favor of ffi, I just changed lapack's dependencies to ffi
Note: if node-ffi builds on your system, you do not need to change lapack's dependencies to ffi- edit
node_modules\lapack\lib\node-lapack\lapack.js
(~ at line 24): change
var FFI = require('node-ffi');
to
var FFI = require('ffi');
- edit
node_modules\lapack\lib\node-lapack\fortranArray.js
(~ at line 23): change
var FFI = require('node-ffi');
to
var FFI = require('ffi');
- edit
-
finally I was able to run browserify:
browserify browser.js -o si-browser.js -s searchindex
But the created script did not work yet:
search-index uses winston for logging. Now, winston depends on pkginfo which uses node's fs.readdirSync()
- a big no-no for browserify.
search-index already has some preparations for dealing with this problem, but I needed some additional changes to get it to work for me.
I needed to disable the use winston in search-index. The browserify entry-file \browser.js
already sets the flag winston = false
, but as far as I could see, the code in search-index does not yet evaluate this flag.
So I did some dirty hacking in order to disable winston based on the existing flag-value. The files that needed changing were:
lib\indexing\indexer.js
lib\logger\searchIndexLogger.js
lib\search-index.js
Basically, each location where require('winston')
is used, needs to be wrapped in if(winston){ ... }
and the searchIndexLogger
object be set to the console
object (which should be available in all browsers).
Now with the changes to search-index itself (i.e. disabling winston when browserified) I ran browserify browser.js -o si-browser.js -s searchindex
again:
This will create a standalone script file where search-index is either exported as global variable searchindex
or, if a require
function is available, as the module searchindex
.
Some slight changes were necessary to the example in example\
in order to get it to work with the created si-browser.js
file:
- copy
si-browser.js
toexamples/
- edit file
examples/basic.html
: add
<script src="si-browser.js"></script>
- edit file
examples/basic.js
: change
var si = require('../')({indexPath: 'si-example'});
to
var si = searchindex({indexPath: 'si-example'});
Now open basic.html
and enjoy the demo.
(note: the tags-filter in the demo did not work for, but that is something for another day)