Asset API for Node.js
Code for the File Ninja
npm install pathfinder
- make it so any node.js code works in the browser
- make it easy to piece together code from modules without
require(copy the code from one place to another)
- compile out asset digests
- make it easy to wrap any code in
requirefor the browser
- be a foundation for any rich watching functionality
- cache requirements globally, so there's just a list. makes things much faster
global ||= windowin browser
Here's what it outputs
requiredefine"/myModule/file.js"// your module code;
Then when you call
require('./anotherFile.js');, it knows that the path of this file is
/myModule/file.js, so it knows that must be
For things like jQuery, if you only need it in the browser, you can just assume it's a global variable.
If you want to have it be required, you can do this:
requiredefine"/jquery.js"return moduleexports = exports$ = $;;
That makes it so you can still take advantage of things like Google's CDN.
For browser-only libraries, you're free to manually write the require statements:
requiredefine '/jquery.js'module.exports = exports.$ = $requiredefine '/jquery.form.js'module.exports = exports.$ = $
If you want to be able to use it in node (forgetting about the fact that jQuery requires some sort of dom), you actually have to wrap the code in this method. You can do that by downloading the library into your project and generating the output like this:
# ./jquery.coffee# @include './vendor/jquery.1.7.0.js'module.exports = $
Then when you compile it, it will import the jquery source code, and wrap it in the define block!
requiredefine"/jquery.js"// all the jquery 1.7.0 code...moduleexports = $;;
require('backbone')for example, in any of your client side code that will also be used server side. Unless you're not going to be using your code as a library, in which case it doesn't really matter. In that case, just create an
index.jsfile or something that sets all the global variables for node like you use them in the browser, i.e.
global.Backbone = require('backbone'); global._ = require('underscore');. This also makes it so you don't have to wrap them in require blocks for your app code.
- If you have to create a wrapper for the library, either the author didn't build their framework well enough, or you're using the code for something it wasn't meant for. As a library author, you can write your own build task that replaces the
requirestatements in your node code with the actual code, which outputs as your client version of the file. This is basically what jade does to give you jade in the browser.
Files referenced with the
@import directive will be directly copied into the location of the directive.
Files referenced with the
// @import './models'// @import './views'// @import './controllers'alert "application"
might become this CoffeeScript
alert "models"alert "views"alert "controllers"alert "application"
Paths work just like they do in Node.js:
./relative/path: relative paths are relative to the current file
/absolute/path: absolute paths are relative to the current project
library: libraries are keys
Pathfinder = require 'pathfinder'pathfinder = root: processcwdpathfindercompile file string ->console.log stringpathfinderrequirements
file = pathfinderfind "application"
Pathfinder.js doesn't include a watcher, but it's setup to be easy to use with one. It's used in Design.io for example.
watch /\js|coffee/update: file ->filedirname
Outputs a JSON map of key to compressed, digest version of a file!
(The MIT License)
Copyright © 2011 Lance Pollard <firstname.lastname@example.org>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.