Skip to content

Commit 2815aaf

Browse files
committedMar 1, 2017
browserify support
1 parent b67d12b commit 2815aaf

7 files changed

+93
-18
lines changed
 

‎.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@ jsdoc
2020

2121
nbproject
2222

23-
.jscodehints
23+
.jscodehints

‎README.md

+21-1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ Table of contents
4242
- [License](https://github.com/kethinov/roosevelt#license)
4343

4444

45+
4546
Why use Roosevelt?
4647
===
4748

@@ -56,6 +57,7 @@ Reasons for this include:
5657
- [LESS](http://lesscss.org) and [Closure Compiler](https://developers.google.com/closure/compiler) preconfigured out of the box to intelligently minify your external facing CSS and JS files.
5758

5859

60+
5961
Create and run a Roosevelt app
6062
===
6163

@@ -181,6 +183,7 @@ Default directory structure
181183
- `.gitignore`: a standard file which contains a list of files and folders to ignore if your project is in a git repo.
182184

183185

186+
184187
Default .gitignore
185188
---
186189

@@ -193,6 +196,7 @@ Some notable things ignored by default and why:
193196
- `node_modules`: This folder will be auto-generated when you run the `npm install` step to set up your app. Since some modules you might include later in your app can be platform-specific and are compiled for your OS during the install step, it's generally <a href='https://npmjs.org/doc/faq.html'>not recommended</a> to commit the `node_modules` folder to git.
194197

195198

199+
196200
Configure your app with parameters
197201
===
198202

@@ -244,6 +248,7 @@ App behavior parameters
244248
- Default: `{}`
245249

246250

251+
247252
HTTPS parameters
248253
---
249254

@@ -268,6 +273,7 @@ HTTPS parameters
268273
- Default: `false`
269274

270275

276+
271277
MVC parameters
272278
---
273279

@@ -284,13 +290,15 @@ MVC parameters
284290
- Default: `mvc/controllers`
285291

286292

293+
287294
Utility library parameters
288295
---
289296

290297
- `libPath`: Relative path on filesystem to where your optional utility library files are located. Defaults to `lib` if not set.
291298
- `libPathNodeModulesSymlink`: Name of the symlink to make in `node_modules` pointing to your `lib` directory. Set to `false` to disable making this symlink. Defaults to `lib` if not set.
292299

293300

301+
294302
Error page parameters
295303
---
296304

@@ -302,6 +310,7 @@ Error page parameters
302310
- Default: `503.js`
303311

304312

313+
305314
Statics parameters
306315
---
307316

@@ -320,8 +329,15 @@ Statics parameters
320329
- Default: `.build/css`
321330
- `jsPath`: Subdirectory within `staticsRoot` where your JS files are located. By default this folder will not be made public, but is instead meant to store unminified JS source files which will be minified and stored elsewhere when the app is started.
322331
- Default: `js`
332+
- `bundledJsPath`: Subdirectory within `staticsRoot` where you would like [browserify](http://browserify.org) to deposit bundled JS files it produces (if you use browserify).
333+
- Default: `js/.bundled`
334+
- `browserifyBundles`: Declare [browserify](http://browserify.org) bundles: one or more files in your `jsPath` for [browserify](http://browserify.org) to bundle via its [bundle method](https://github.com/substack/node-browserify#browserifyfiles--opts).
335+
- Default: `[]`
336+
- Example declaring one bundle: `[{outputFile: "bundle.js", files: ["landingPage.js", "main.js", "etc.js"], params: {someOpt: "someValue"}}]`
337+
- Example declaring multiple bundles: `[{outputFile: "bundle1.js", files: ["landingPage.js", "main.js", "etc.js"], params: {someOpt: "someValue"}}, {outputFile: "bundle2.js", files: ["somethingElse.js", "anotherThing.js", "etc.js"]}, etc...]`
338+
- Note: `params` is optional. If it is not set, these default params will be sent: `{paths: yourJsPath}`
323339
- `jsCompiler`: Which JS minifier, if any, to use. Must also be marked as a dependency in your app's package.json. Set to `none` to use no JS minifier.
324-
- Default: `{nodeModule: 'roosevelt-closure', params: {compilationLevel: 'ADVANCED'}}`.
340+
- Default: `{nodeModule: "roosevelt-closure", params: {compilationLevel: 'ADVANCED'}}`.
325341
- Also by default the module [roosevelt-closure](https://github.com/kethinov/roosevelt-closure) is marked as a dependency in package.json.
326342
- `jsCompilerWhitelist`: Whitelist of JS files to compile as an array. Leave undefined to compile all files. Supply a `:` character after each file name to delimit an alternate file path and/or file name for the minified file.
327343
- Default: `undefined`
@@ -330,6 +346,7 @@ Statics parameters
330346
- Default: `.build/js`
331347

332348

349+
333350
Public folder parameters
334351
---
335352

@@ -349,6 +366,7 @@ Public folder parameters
349366
- Default: `false`
350367

351368

369+
352370
Events
353371
---
354372

@@ -381,6 +399,7 @@ Event list
381399
- `res`: The [response object](http://expressjs.com/api.html#res.status) created by Express.
382400

383401

402+
384403
Making controller files
385404
===
386405

@@ -473,6 +492,7 @@ In addition to exposing a number of variables to Express and providing the MVC i
473492
- Includes the [method-override](https://github.com/expressjs/method-override) middleware.
474493

475494

495+
476496
Warning: Roosevelt is beta software!
477497
===
478498

‎lib/jsBundler.js

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// js bundler
2+
3+
require('colors');
4+
5+
var browserify = require('browserify'),
6+
fs = require('fs'),
7+
fse = require('fs-extra');
8+
9+
module.exports = function(app, callback) {
10+
var params = app.get('params'),
11+
promises = [];
12+
13+
// make js directory if not present
14+
if (!fs.existsSync(app.get('jsPath'))) {
15+
fse.mkdirsSync(app.get('jsPath'));
16+
console.log(('📁 ' + (app.get('package').name || 'Roosevelt') + ' making new directory ' + app.get('jsPath').replace(app.get('appDir'), '')).yellow);
17+
}
18+
19+
// make js bundled output directory if not present
20+
if (params.browserifyBundles.length && !fs.existsSync(params.bundledJsPath)) {
21+
fse.mkdirsSync(params.bundledJsPath);
22+
console.log(('📁 ' + (app.get('package').name || 'Roosevelt') + ' making new directory ' + app.get('appDir') + params.bundledJsPath.replace(app.get('appDir'), '')).yellow);
23+
}
24+
25+
params.browserifyBundles.forEach(function(bundle) {
26+
promises.push(function(bundle) {
27+
return new Promise(function(resolve, reject) {
28+
//console.log(app.get('staticsRoot') + app.get('jsPath'));
29+
browserify(bundle.files, bundle.params ? bundle.params : {
30+
paths: [app.get('jsPath')]
31+
}).bundle(function(err, jsCode) {
32+
if (err) {
33+
console.error(('❌ ' + (app.get('appName') || 'Roosevelt') + ' failed to write new JS file ' + app.get('appDir') + params.bundledJsPath + '/' + bundle.outputFile + ' due to syntax errors in the source JavaScript\nError: ' + err.message).red);
34+
throw err;
35+
}
36+
else {
37+
console.log(('📝 ' + (app.get('appName') || 'Roosevelt') + ' writing new JS file ' + app.get('appDir') + params.bundledJsPath + '/' + bundle.outputFile).green);
38+
fs.writeFileSync(params.bundledJsPath + '/' + bundle.outputFile, jsCode, 'utf8');
39+
}
40+
resolve();
41+
});
42+
});
43+
}(bundle));
44+
});
45+
46+
Promise.all(promises).then(function() {
47+
callback();
48+
});
49+
};

‎lib/jsCompiler.js

+3-6
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ module.exports = function(app, callback) {
1616
promises = [];
1717

1818
if (params.jsCompiler === 'none') {
19+
callback();
1920
return;
2021
}
2122

@@ -26,12 +27,8 @@ module.exports = function(app, callback) {
2627
catch (err) {
2728
console.error(('❌ ' + (app.get('appName') || 'Roosevelt') + ' failed to include your JS compiler! Please ensure that it is declared properly in your package.json and that it has been properly insalled to node_modules.').red);
2829
console.error(err);
29-
}
30-
31-
// make js directory if not present
32-
if (!fs.existsSync(app.get('jsPath'))) {
33-
fse.mkdirsSync(app.get('jsPath'));
34-
console.log(('📁 ' + (app.get('package').name || 'Roosevelt') + ' making new directory ' + app.get('jsPath').replace(app.get('appDir'), '')).yellow);
30+
callback();
31+
return;
3532
}
3633

3734
// make js compiled output directory if not present

‎lib/sourceParams.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ module.exports = function(app) {
3939
multipart: params.multipart || pkg.rooseveltConfig.multipart || {'multiples': true},
4040
maxLagPerRequest: params.maxLagPerRequest || pkg.maxLagPerRequest || 2000,
4141
shutdownTimeout: params.shutdownTimeout || pkg.shutdownTimeout || 30000,
42+
bodyParserUrlencodedParams: params.bodyParserUrlencodedParams || pkg.rooseveltConfig.bodyParserUrlencodedParams || { extended: true },
43+
bodyParserJsonParams: params.bodyParserJsonParams || pkg.rooseveltConfig.bodyParserJsonParams || {},
44+
4245
// https behavior - generally no defaults, user-defined
4346
https: params.https || pkg.rooseveltConfig.https || false,
4447
httpsOnly: params.httpsOnly || pkg.rooseveltConfig.httpsOnly || false,
@@ -49,16 +52,15 @@ module.exports = function(app) {
4952
ca: params.ca || pkg.rooseveltConfig.ca || null, // string or array of strings (file paths)
5053
requestCert: params.requestCert || pkg.rooseveltConfig.requestCert || false,
5154
rejectUnauthorized: params.rejectUnauthorized || pkg.rooseveltConfig.rejectUnauthorized || false,
52-
bodyParserUrlencodedParams: params.bodyParserUrlencodedParams || pkg.rooseveltConfig.bodyParserUrlencodedParams || { extended: true },
53-
bodyParserJsonParams: params.bodyParserJsonParams || pkg.rooseveltConfig.bodyParserJsonParams || {},
55+
5456
// mvc
5557
modelsPath: params.modelsPath || pkg.rooseveltConfig.modelsPath || 'mvc/models',
5658
modelsNodeModulesSymlink: params.modelsNodeModulesSymlink || pkg.rooseveltConfig.modelsNodeModulesSymlink || 'models',
5759
viewsPath: params.viewsPath || pkg.rooseveltConfig.viewsPath || 'mvc/views',
5860
viewEngine: params.viewEngine || pkg.rooseveltConfig.viewEngine || 'html: teddy',
5961
controllersPath: params.controllersPath || pkg.rooseveltConfig.controllersPath || 'mvc/controllers',
6062

61-
// Utility lib
63+
// utility lib
6264
libPath: params.libPath || pkg.rooseveltConfig.libPath || 'lib',
6365
libPathNodeModulesSymlink: params.libPathNodeModulesSymlink || pkg.rooseveltConfig.libPathNodeModulesSymlink || 'lib',
6466

@@ -75,6 +77,8 @@ module.exports = function(app) {
7577
cssCompiledOutput: params.staticsRoot + (params.cssCompiledOutput || pkg.rooseveltConfig.cssCompiledOutput || '.build/css'),
7678
cssCompilerWhitelist: params.cssCompilerWhitelist || pkg.rooseveltConfig.cssCompilerWhitelist || undefined,
7779
jsPath: params.staticsRoot + (params.jsPath || pkg.rooseveltConfig.jsPath || 'js'),
80+
bundledJsPath: params.staticsRoot + (params.bundledJsPath || pkg.rooseveltConfig.bundledJsPath || 'js/.bundled'),
81+
browserifyBundles: params.browserifyBundles || pkg.rooseveltConfig.browserifyBundles || [],
7882
jsCompiler: params.jsCompiler || pkg.rooseveltConfig.jsCompiler || {nodeModule: 'roosevelt-closure', params: {compilationLevel: 'ADVANCED'}},
7983
jsCompiledOutput: params.staticsRoot + (params.jsCompiledOutput || pkg.rooseveltConfig.jsCompiledOutput || '.build/js'),
8084
jsCompilerWhitelist: params.jsCompilerWhitelist || pkg.rooseveltConfig.jsCompilerWhitelist || undefined,

‎package.json

+7-6
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,30 @@
22
"name": "roosevelt",
33
"description": "Roosevelt MVC web framework",
44
"author": "Eric Newport <kethinov@gmail.com>",
5-
"version": "0.7.5",
5+
"version": "0.7.6",
66
"homepage": "https://github.com/kethinov/roosevelt",
77
"license": "CC-BY-4.0",
88
"main": "roosevelt.js",
99
"readmeFilename": "README.md",
1010
"engines": {
11-
"node": ">=4.0.0"
11+
"node": ">=6.0.0"
1212
},
1313
"engineStrict": true,
1414
"dependencies": {
1515
"body-parser": "^1.0.0",
16+
"browserify": "^14.0.0",
1617
"colors": "^1.0.0",
1718
"compression": "^1.0.0",
1819
"cookie-parser": "^1.3.0",
1920
"express": "^4.0.0",
21+
"express-minify-html": "^0.9.0",
2022
"formidable": "^1.0.0",
23+
"fs-extra": "^1.0.0",
24+
"html-validator": "^2.2.0",
2125
"method-override": "^2.0.0",
2226
"morgan": "^1.0.0",
2327
"parent-require": "^1.0.0",
24-
"fs-extra": "^1.0.0",
25-
"serve-favicon": "^2.3.0",
26-
"express-minify-html": "^0.9.0",
27-
"html-validator": "^2.2.0"
28+
"serve-favicon": "^2.3.0"
2829
},
2930
"devDependencies": {
3031
"eslint": "^3.5.0"

‎roosevelt.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,11 @@ module.exports = function(params) {
119119
preprocessCss();
120120

121121
function preprocessCss() {
122-
require('./lib/preprocessCss')(app, compileJs);
122+
require('./lib/preprocessCss')(app, bundleJs);
123+
}
124+
125+
function bundleJs() {
126+
require('./lib/jsBundler')(app, compileJs);
123127
}
124128

125129
function compileJs() {

0 commit comments

Comments
 (0)
Please sign in to comment.