Skip to content

Instantly share code, notes, and snippets.

@leizongmin
Created September 23, 2016 08:00
Show Gist options
  • Save leizongmin/2533af311c85af6cb53ae4c8970b304c to your computer and use it in GitHub Desktop.
Save leizongmin/2533af311c85af6cb53ae4c8970b304c to your computer and use it in GitHub Desktop.
使用browserify自动打包
'use strict';
const fs = require('fs');
const path = require('path');
const browserify = require('browserify');
const express = require('express');
const open = require('open');
const app = express();
app.get('/entry/:name.js', function (req, res, next) {
const file = path.resolve(__dirname, 'src', req.params.name + '.js');
fs.exists(file, ok => {
if (!ok) return next();
const b = browserify();
b.add(file).bundle((err, buf) => {
if (err) return next(err);
console.log('bundle %s', file);
res.end(buf);
});
});
});
app.use('/static', express.static(path.resolve(__dirname, 'static')));
app.listen(3000, err => {
if (err) throw err;
open('http://127.0.0.1:3000/static/index.html');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment