An example of using browserify and browserify-shim to modularize javascript and make it more easily testable.
npm install bower browserify browserify-shim gulp gulp-rename gulp-uglify vinyl-buffer vinyl-source-stream
- add
node_modules
to.gitignore
- add some required json to
package.json
to get browserify-shim to work with jquery bower init
and press enter a bunch of times to create abower.json
bower install --save-dev jquery
- add
bower_components
to.gitignore
- create a browserify module in
assets/js/mymodule.js
- example - create a script that uses it such as
assets/js/app.js
- example - add a gulpfile.js or just use this one
gulp
to create the first build- include the compiled js file in your html with a script tag
- If your compiled js is in
assets/cache
, add it to.gitignore
npm install
to get gulp, browserify, bower, etc.bower install
to get jquerygulp build
to compile app.min.js to assets/cache- load index.html in your browser
- there are a few important configs under the browser, browserify and browserify-shim keys in package.json
- browserify cannot stream to another task unless you use vinyl-source-stream to define a source and vinyl-buffer to buffer before continuing.
- sourcemaps are a bit difficult since browserify has it's own sourcemaps it generates via --debug.