Skip to content

Instantly share code, notes, and snippets.

@kumatch
Last active March 1, 2018 17:44
Show Gist options
  • Save kumatch/7857227 to your computer and use it in GitHub Desktop.
Save kumatch/7857227 to your computer and use it in GitHub Desktop.
A browserify simple example.
var format = require('util').format;
module.exports = function (v) {
if (typeof v === "string") {
return format('%s OK.', v);
} else if (typeof v === "number") {
return v * 10;
} else {
return v;
}
};
var async = require('async');
var baz = require('./bar/baz');
exports.run = function (values, callback) {
async.map(values, function (v, next) {
next(null, baz(v));
}, callback);
};
module.exports = function(grunt) {
var pkg = grunt.file.readJSON('package.json');
grunt.initConfig({
browserify: {
dist: {
src: 'main.js',
dest: 'build.js'
}
}
});
Object.keys(pkg.devDependencies).forEach(function (devDependency) {
if (devDependency.match(/^grunt\-/)) {
grunt.loadNpmTasks(devDependency);
}
});
grunt.registerTask('default', [ 'browserify' ]);
};
<html>
<head>
</head>
<body>
<div id="content"></div>
<script src="build.js"></script>
</body>
</html>
var foo = require('./lib/foo.js');
var content = document.getElementById('content');
foo.run([ 3, 10, "foo", "bar", 42 ], function (err, results) {
if (err) throw err;
results.forEach(function (r) {
content.innerHTML += ' ' + r;
});
});
{
"private": true,
"dependencies": {
"async": "0.2.9"
},
"devDependencies": {
"grunt": "~0.4",
"grunt-browserify": "*"
},
"optionalDependencies": {},
"engines": {
"node": ">= 0.8.0"
}
}
@ /
├─ Gruntfile.js
├─ package.json
├─ index.html
├─ main.js
└┬ lib/
├─ foo.js
└┬ bar/
└─ baz.js
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment