Skip to content

Commit

Permalink
Initial browser builder
Browse files Browse the repository at this point in the history
  • Loading branch information
jhs committed Sep 1, 2011
1 parent b8ff40c commit f930c4c
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 0 deletions.
50 changes: 50 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
require 'json'

LIBRARY = [ "api.js", "feed.js", "lib.js", "cli.js" ]
BROWSER_TOOLS = Dir.glob("browser/*").select{|x| x != "browser/export.js" }

task :default => :export

desc 'Export Follow for use in web browsers'
task :export => ([:clean, "cli.js"] + LIBRARY + BROWSER_TOOLS) do |task|
build = "./build"
target = "#{build}/follow"

sh "mkdir", "-p", target
LIBRARY.each do |js|
sh "node", "browser/export.js", js, "#{target}/#{js}"
end

BROWSER_TOOLS.each do |file|
sh "cp", file, build
end

File.open("#{build}/boot.js", 'w') do |boot|
requirejs_paths = { 'request' => '../request.jquery',
# 'foo' => 'bar', etc.
}

opts = { # 'baseUrl' => "follow",
'paths' => requirejs_paths,
}

main_js = "follow/cli.js"

boot.write([ 'require(',
'// Options',
opts.to_json() + ',',
'',
'// Modules',
[main_js].to_json() + ',',
'',
'// Code to run when ready',
'function(main) { return main(); }',
');'
].join("\n"));
end
end

desc 'Clean up build files'
task :clean do
sh "rm", "-rf", "./build"
end
78 changes: 78 additions & 0 deletions browser/export.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#!/usr/bin/env node
// Export Follow in browser-friendly format.
//
// Copyright 2011 Iris Couch
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

var fs = require('fs')
, path = require('path')
;

if(process.argv[1] === module.filename) {
var source = process.argv[2]
, dest = process.argv[3]
;

if(!source || !dest)
throw new Error("usage: browser-export.js <source> <target>");

install(source, dest, function(er) {
if(er) throw er;
});
}

function install(filename, target, callback) {
//console.log('Exporting: ' + filename);
fs.readFile(filename, null, function(er, content) {
if(er && er.errno) er = new Error(er.stack); // Make a better stack trace.
if(er) return callback(er);

// Strip the shebang.
content = content.toString('utf8');
var content_lines = content.split(/\n/);
content_lines[0] = content_lines[0].replace(/^(#!.*)$/, '// $1');

// TODO
// content_lines.join('\n'), maybe new Buffer of that

//Convert the Node module (CommonJS) to RequireJS.
var require_re = /\brequire\(['"]([\w\d\-_\/\.]+?)['"]\)/g;

// No idea why I'm doing this but it's cool.
var match, dependencies = {};
while(match = require_re.exec(content))
dependencies[ match[1] ] = true;
dependencies = Object.keys(dependencies);
dependencies.forEach(function(dep) {
//console.log(' depends: ' + dep);
})

// In order to keep the error message line numbers correct, this makes an ugly final file.
content = [ 'require.def(function(require, exports, module) {'
//, 'var module = {};'
//, 'var exports = {};'
//, 'module.exports = exports;'
, ''
, content_lines.join('\n')
, '; return(module.exports);'
, '}); // define'
].join('');
//content = new Buffer(content);

fs.writeFile(target, content, 'utf8', function(er) {
if(er) return callback(er);
return callback();
})
})
}
14 changes: 14 additions & 0 deletions browser/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Follow</title>
</head>
<body>
<div id="boot">Booting...</div>
<div id="results"></div>
<div><pre><code id="con"></code></pre></div>
<script src="jquery-1.6.1.min.js"></script>
<script data-main="boot.js" src="require.js"></script>
</body>
</html>

0 comments on commit f930c4c

Please sign in to comment.