Comparing version 0.1.0 to 0.2.0
var fs = require('fs'); | ||
var path = require('path'); | ||
var devLoader = fs.readFileSync(__dirname+'/loader.js', 'utf8'); | ||
var prodShim = fs.readFileSync(__dirname+'/shim.js', 'utf8'); | ||
var keysToInit = function(segments, global, skipLast) { | ||
var root = global; | ||
var notDefined = false; | ||
return segments.map(function(k, i) { | ||
if (skipLast && i === segments.length - 1) return false; | ||
defineExports = function(segments, hasExports) { | ||
if (segments || (segments === false && hasExports)) { | ||
return '\nexports = {}; module = {exports: exports};\n'; | ||
} else if (segments === undefined) { | ||
return '\nexports = undefined; module = undefined;\n'; | ||
} else { | ||
return ''; | ||
} | ||
} | ||
if (notDefined || !root[k]) { | ||
notDefined = true; | ||
return segments.slice(0, i+1).join('.'); | ||
} else { | ||
root = root[k]; | ||
return false; | ||
} | ||
}); | ||
}; | ||
var loaderFn = function(relativeToRoot, info, data, options) { | ||
var src, stat; | ||
var src, stat, initKeys; | ||
var variableName = path.basename(relativeToRoot, path.extname(relativeToRoot)); | ||
@@ -26,5 +31,8 @@ return function() { | ||
src = fs.readFileSync(fullPath, 'utf8'); | ||
stat = newStat; | ||
if (data.buildList[fullPath]) { | ||
src = src.replace(/^.*bldr.*$\n/gm,''); | ||
} | ||
if (info.transform) { | ||
@@ -34,3 +42,30 @@ if (!data.transforms[info.transform]) throw new Error('Transform: '+info.transform+' requested but not defined'); | ||
} | ||
stat = newStat; | ||
if (info.browser) return src; | ||
if (info.segments) { | ||
if (src.match(/module\.exports/)) { | ||
if (!initKeys) initKeys = keysToInit(info.segments, data.global, true); | ||
src = src.replace(/module\.exports/, info.segments.join('.')); | ||
} else { | ||
if (!initKeys) initKeys = keysToInit(info.segments, data.global); | ||
src = src.replace(/exports/g, info.segments.join('.')); | ||
} | ||
src = initKeys.map(function(k) { | ||
return k ? (k.match(/\./) ? '' : 'var ') + k + ' = {};\n' : ''; | ||
}).join('') + src; | ||
var root = data.global; | ||
info.segments.forEach(function(k) { | ||
if (!root[k]) root[k] = {}; | ||
root = root[k]; | ||
}); | ||
} else { | ||
if (src.match(/module\.exports/)) { | ||
src = src.replace(/module\.exports/, 'var ' + variableName); | ||
} else if (src.match(/exports/)) { | ||
src.replace(/exports/g, variableName); | ||
src = 'var ' + variableName + ' = {};\n' + src; | ||
} | ||
} | ||
return src; | ||
@@ -50,14 +85,7 @@ }; | ||
var output = (options.pre || ''); | ||
var files, filename, info, relativeToRoot, nextInfo, next, loader, src; | ||
var filename, info, relativeToRoot, loader, src; | ||
if (!options.dev) { | ||
output += "var exports, module;\n"; | ||
} | ||
if (data.usedDefine) output += prodShim; | ||
if (options.dev) output += devLoader; | ||
files = Object.keys(data.appList); | ||
for (var i=0; i<files.length; i++) { | ||
filename = files[i]; | ||
for (filename in data.appList) { | ||
info = data.appList[filename]; | ||
@@ -74,20 +102,7 @@ | ||
if (options.dev) { | ||
if (i===0) { | ||
output += defineExports(info.segments, src.match(/exports/)); | ||
} | ||
if (options.prefix) { | ||
data.sources['/'+relativeToRoot] = loader; | ||
relativeToRoot = path.join(options.prefix, relativeToRoot); | ||
} | ||
nextInfo = data.appList[files[i+1]]; | ||
next = JSON.stringify(nextInfo && !!nextInfo.segments); | ||
output += "loadScript('" + relativeToRoot + "', " + JSON.stringify(info) + ", " + next + ");\n"; | ||
data.sources['/'+relativeToRoot] = loader; | ||
if (options.prefix) relativeToRoot = path.join(options.prefix, relativeToRoot); | ||
output += "loadScript('" + relativeToRoot + "');\n"; | ||
} else { | ||
output += defineExports(info.segments, src.match(/exports/)); | ||
output += '\n// '+relativeToRoot+'\n'+ src; | ||
if (info.segments) { | ||
output += '\nbldr(' + JSON.stringify(info.segments) + ');\n'; | ||
} | ||
} | ||
@@ -94,0 +109,0 @@ data.exported[filename] = true; |
@@ -30,2 +30,3 @@ var fs = require('fs'); | ||
sources: {}, | ||
global: {}, | ||
transforms: {} | ||
@@ -124,2 +125,3 @@ }; | ||
opts = opts || {}; | ||
opts.browser = true; | ||
requirePath = requirePath[0] === '/' ? requirePath : path.join(baseDir, requirePath); | ||
@@ -126,0 +128,0 @@ addToApp(requirePath, opts); |
@@ -5,24 +5,3 @@ var loadScript = function(src, info, next) { | ||
script.async = false; | ||
script.onload = function() { | ||
if (info.segments) bldr(info.segments); | ||
if (next) { | ||
exports = {}; module = {exports: exports}; | ||
} else { | ||
exports = module = undefined; | ||
} | ||
}; | ||
document.head.appendChild(script); | ||
}; | ||
__filename = ''; | ||
require = function() { | ||
return function() { | ||
return { | ||
define: function() {}, | ||
require: function() {}, | ||
browser: function() {} | ||
}; | ||
}; | ||
}; | ||
{ | ||
"name": "bldr", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "bldr browser/define/require, a node/browser module builder", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -13,5 +13,5 @@ bldr - a module builder for node.js and the browser | ||
With bldr, the code you write runs directly from node. In the browser, the code you write runs directly | ||
in the browser, by referencing the loader script that bldr creates from your source tree. And in production, | ||
you bldr will package your code for into one or more files. | ||
With bldr, the code you write runs directly from node. In the browser during development, the code runs | ||
through a simple transform (available as an express route handler). In the browser for production, | ||
bldr will package your code into one or more files. | ||
@@ -77,3 +77,3 @@ It has the following api: | ||
// every view that needs it. The bldr comment is added so that | ||
// the line gets removed in production. | ||
// the line gets removed in the browser. | ||
var React = require('react'); // bldr | ||
@@ -123,7 +123,13 @@ | ||
```javascript | ||
// In development, we can install an express handler to serve | ||
// up our individual files for the browser. | ||
if (process.env.NODE_ENV !== 'production') { | ||
bldr.installExpress(server, {rootDir: __dirname, app: '/js/app.js'}); | ||
} | ||
``` | ||
```sh | ||
# We can build the dev loader shim for the browser | ||
$ bldr build app::www/app.js | ||
# Or package our app for procuction | ||
$ bldr package app::www/app.js | ||
# We can then package the app for procuction | ||
$ bldr package myapp app::www/app.js | ||
``` | ||
@@ -133,3 +139,3 @@ | ||
# We can also package the app into multiple files | ||
$ bldr package app::www/app.js app/admin::www/admin.js | ||
$ bldr package myapp app::www/app.js app/admin::www/admin.js | ||
``` | ||
@@ -154,3 +160,3 @@ | ||
```sh | ||
bldr build lib/deps::www/js/deps.js lib::www/js/app.js | ||
bldr package myapp lib/deps::www/js/deps.js lib::www/js/app.js | ||
``` | ||
@@ -160,3 +166,5 @@ | ||
I believe that this functionality is out of the scope of this package, and should be built into your build tools. You can see an example of `bldr` integrated with `make` in the examples directly. There is support for generating a depenency file that `make` will understand with the option `--deps`. More build tool integration hopefully coming. | ||
With the express module, the newest version the files will be served up during development. | ||
There is support for generating a depenency file that `make` will understand with the option `--deps`. More build tool integration hopefully coming. | ||
@@ -23,4 +23,4 @@ var fs = require('fs'); | ||
testbldr = undefined; | ||
options = {rootDir: __dirname}; | ||
bldr = b('testbldr', __filename); | ||
options = {rootDir: path.join(__dirname, 'fixtures')}; | ||
bldr = b('testbldr', __filename, {appDir: path.join(__dirname, 'fixtures')}); | ||
data = b.store['testbldr']; | ||
@@ -54,3 +54,3 @@ }); | ||
it('should use the bldr shim if define is used', function() { | ||
it('should replace module.exports if define is used', function() { | ||
var mode = bldr.define('./fixtures/exports'); | ||
@@ -87,2 +87,21 @@ checkResult(this.test, builder.make(data, options)); | ||
}); | ||
it('should only define shared keys once', function() { | ||
var mode = bldr.define('./fixtures/exports'); | ||
var more = bldr.define('./fixtures/exportmore'); | ||
checkResult(this.test, builder.make(data, options)); | ||
}); | ||
it('should only define shared keys once test two', function() { | ||
bldr = b('testbldr', __filename, {appDir: __dirname}); | ||
data = b.store['testbldr']; | ||
var more = bldr.define('./fixtures/exportmore'); | ||
var mode = bldr.define('./fixtures/exports'); | ||
checkResult(this.test, builder.make(data, options)); | ||
}); | ||
it('should not mess with browser defines', function() { | ||
bldr.browser('./fixtures/exportmore'); | ||
checkResult(this.test, builder.make(data, options)); | ||
}); | ||
}); |
@@ -1,11 +0,8 @@ | ||
var exports, module; | ||
exports = {}; module = {exports: exports}; | ||
// testglobal.js | ||
var testglobal = {}; | ||
// fixtures/testglobal.js | ||
var testglobal = module.exports = {}; | ||
// extends.js | ||
// fixtures/extends.js | ||
testglobal.msg = function() { | ||
}; |
;(function() { | ||
var bldr = function(ss) { | ||
var i, s, r = window; | ||
for (i=0; i<ss.length-1; i++) { | ||
if (!r[ss[i]]) r[ss[i]] = {}; | ||
r = r[ss[i]]; | ||
} | ||
r[ss[ss.length - 1]] = module.exports; | ||
}; | ||
var loadScript = function(src, info, next) { | ||
@@ -14,29 +6,6 @@ var script = document.createElement('script'); | ||
script.async = false; | ||
script.onload = function() { | ||
if (info.segments) bldr(info.segments); | ||
if (next) { | ||
exports = {}; module = {exports: exports}; | ||
} else { | ||
exports = module = undefined; | ||
} | ||
}; | ||
document.head.appendChild(script); | ||
}; | ||
loadScript('exports.js'); | ||
__filename = ''; | ||
require = function() { | ||
return function() { | ||
return { | ||
define: function() {}, | ||
require: function() {}, | ||
browser: function() {} | ||
}; | ||
}; | ||
}; | ||
exports = {}; module = {exports: exports}; | ||
loadScript('fixtures/exports.js', {"define":true,"segments":["testbldr","fixtures","exports"]}, undefined); | ||
})() |
;(function() { | ||
var exports, module; | ||
exports = {}; module = {exports: exports}; | ||
// testglobal.js | ||
var testglobal = {}; | ||
// fixtures/testglobal.js | ||
var testglobal = module.exports = {}; | ||
// extends.js | ||
// fixtures/extends.js | ||
testglobal.mymsg = function() { | ||
@@ -12,0 +9,0 @@ }; |
;(function() { | ||
var exports, module; | ||
exports = {}; module = {exports: exports}; | ||
// testglobal.js | ||
var testglobal = {}; | ||
// fixtures/testglobal.js | ||
var testglobal = module.exports = {}; | ||
// extends.js | ||
// fixtures/extends.js | ||
testglobal.msg = function() { | ||
@@ -12,0 +9,0 @@ }; |
(function(root,factory) { if (typeof define === "function" && define.amd) { define([], factory); } else { root.testglobal = factory(); }})(this, function() { | ||
var exports, module; | ||
exports = {}; module = {exports: exports}; | ||
// testglobal.js | ||
var testglobal = {}; | ||
// fixtures/testglobal.js | ||
var testglobal = module.exports = {}; | ||
// extends.js | ||
// fixtures/extends.js | ||
testglobal.msg = function() { | ||
@@ -12,0 +9,0 @@ }; |
@@ -1,3 +0,5 @@ | ||
exports.msg = function() { | ||
return 'more'; | ||
module.exports = { | ||
msg: function() { | ||
return 'more'; | ||
} | ||
}; |
@@ -1,1 +0,1 @@ | ||
var testglobal = module.exports = {}; | ||
module.exports = {}; |
280553
42
8292
165