Skip to content
russaa edited this page Mar 19, 2015 · 2 revisions

Build search-index for Browsers

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).

Prerequisites

And for reference, here specific setup, that I used:

Build Browserify

  1. go to the root directory of your cloned search-index repository

  2. run npm install
    In an ideal world, you could now run browserify and all would be well.
    For me however, some additional steps were necessary:

  3. run npm install WNdb
    This was necessary, because WNdb seems to be a hidden dependency in the natural module - and running browserify complained that it was missing.

  4. run npm install ffi
    if it fails, run npm 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:
    edit node_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
  1. 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');
  2. 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.

HACKs in search-index

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).

Get example\basic.html to work

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:

  1. copy si-browser.js to examples/
  2. edit file examples/basic.html: add
    <script src="si-browser.js"></script>
  3. 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)