154 points · by mambodog · 12y ago · 68 comments
I auditioned Component and RequireJS but both of those felt like a kludge. Browserify lets you do node style includes as well as compile your JS into separate bundles.
I wrote a very small structure and build system [1] for our front-end projects at work that uses Browserify / Gulp and can compile everything into separate modules. It uses nginx/apache for routing and since it's all HTML/CSS and compiled JS, it's lightning fast to deliver.
I think this guide is fantastic; we just need more things like this to lower the barrier to entry.
Also, though I found bower useful I disliked having yet another package manager, yet another manifest and yet another install step included in my development workflow. Using npm for both server and client side modules has been a dream.
That's why I prefer client-side loaders. All you have to do is copy the loader into a folder and create a .html file. I have my dev folder hosted by apache so I don't ever have to worry about starting some process, running npm install and watching it install the entire internet, etc.
Why the node community is so stubborn about this point is a mystery to me and it makes me wary of node in general, because who wants to be locked into an environment where such an obvious pain point is ignored due to stubbornness?
Most substantial applications are going to have at least some small amount of internal coupling around their business logic and configuration, and as long as you limit knowledge of the custom NODE_PATH to that code you should be alright.
eg. you could have
# assuming git, find repo root
APP_ROOT=$(git rev-parse --show-toplevel)
# /app is for private, checked in modules
# `npm root` will find your root node_modules dir
NODE_PATH="$APP_ROOT/app:`npm root`"
and then all of your app-coupled private modules can live in /app, and get checked into your git repo, while all the dependencies would be referenced in the package.json at the root of your app, and installed to node_modules (eg. npm root for the app package).I should emphasise that taking this approach requires quite a bit of developer responsibility to avoid coupling between modules when it's not required, and it's really overkill for small apps.
Basically, what would be your ideal solution?
Dictating that the only way to control dependency-loading is by manipulating the filesystem is really braindead.
My preference is to add structure with file names instead of directories: app.foo.bar.js instead of app/foo/bar.js
It doesn't work with browserify but it wouldn't be too hard to write a transform that does it.
// App Controller
var AppModel = require('./models/AppModel');
var NavView = require('./views/NavView');
var HomeView = require('./views/HomeView');
// Do stuff
You can immediately know whats going on inside of your class or module simply by looking at the imports, the LOC is greatly reduced, and refactoring, via imports, makes your code about a thousand times more maintainable.
One tip I can give is that I ended up organizing each of my React components and mixins as a module in their own folders and files and my gulpfile adds the parent source path to the NODE_PATH environment variable. By adding the coffeeify transform and .coffee extension to the browserify gulp task I can just do this in my code:
SomeComponent = require 'react-components/some-component'
SomeMixin = require 'react-mixins/some-mixin'
No need to worry about relative paths or if it is CoffeeScript code or plain Javascript.The other tip is to NOT require react within your browserfied code. Just load it as a script tag before your browserfied code and use window.React to get a reference.
I also didn't give the main reason for using the NODE_PATH method. Browserify doesn't resolve duplicate relative paths so if you have a structure like this:
/src
main.js
components/a.js
components/foo/b.js
components/foo/c.js
and a.js requires c.js via relative paths: require('./foo/c.js')
and b.js also requires c.js via require('./c.js')
then c.js is included twice in your bundled code since Browserify uses the pure string path as the key. If instead you put /src in your NODE_PATH you can do this in a.js: require('components/foo/c.js')
and this in b.js require('components/foo/c.js')
and c.js will only be bundled once since it has the same path.Also, if you're requiring large files that you know won't need processing by browserify (like react), you can use the 'noparse' option to prevent the performance hit during bundling. (Of course a cdn is potentially a better option.)
The script
- Creates multiple bundles, with Underscore template precompilation and source code minification (for some bundles).
- Converts every vendor library into shared modules (you don't want Underscore included in every bundle that needs it, but you want to be able to require() it in those bundles).
- Monitors your files for changes and recompiles bundles as needed when run with --watch.
I found working with Browserify a pleasant experience after RequireJS and r.js (its build tool).
https://github.com/bcoe/npm-typeahead
Both the libraries that the app depended on (typeahead.js, and jquery) had been published to npm. I found it really straight-forward to setup an asset-pipeline using Browserify.
I'm a big fan :)
What I ended up with was a seperate ts file that is just the module, and variables I export. See this:
http://jsbin.com/gidaneja/1/edit?js,output
So I do a "<reference path=" for the ambient paths of variables. Then I declare require so I can call browserify's require. Then I create a function with it's return type being used based on ambient files I included. Then I create an export variable and call the function to set it's value. Then I can use that variable without a problem everywhere.
I tried using the whole "import lodash = require('lodash.d.ts') thing and other variations and failed miserably. This is the only thing I got to work, and its working perfectly. Email me (email should be on my profile) if it's not clear, I know the explanation was kinda vague.
how can I develop a library with different modules, say lib/module/func, lib/module/other, and compile the library as static, then import it in another project and be able to require lib/module/other from the other project using only the compiled lib?
I can understand bundling a bunch of modules together for UMD builds, but if you need to bundle all of your NPM/browserifiable modules for other browserify apps, you are probably going about it the wrong way..
Advantages:
- requiring and pre-prosessing files other than js is easier
- ability to use AMD modules if you have to
- code splitting