当前位置: 首页 > 软件库 > Web应用开发 > >

browserify-middleware

express middleware for browserify, done right
授权协议 MIT License
开发语言 JavaScript
所属分类 Web应用开发
软件类型 开源软件
地区 不详
投 递 者 张永嘉
操作系统 跨平台
开源组织
适用人群 未知
 软件概览

browserify-middleware

middleware for browserify v2 with sensible defaults for the ultimate in ease of use

In addition to the basics, browserify-middleware has the following features out of the box:

  • source-maps are automatically enabled for debugging
  • automatically rebuilds whenever files change in development
  • minification automatically enabled for production
  • gzip automatically enabled for production
  • etags for caching automatically enabled for production

With the exception of serving up directories (which requires req.path from express) everything is entirely framework independent. Simply pass in req res, and a callback that will only be called in the event of an error.

If you think I've missed something, be sure to open an issue or submit a pull request.

Build Status

Usage

See example directory for a complete server

var browserify = require('browserify-middleware');
var express = require('express');
var app = express();

//provide browserified versions of all the files in a directory
app.use('/js', browserify(__dirname + '/client/dir'));

//provide a browserified file at a path
app.get('/js/file.js', browserify(__dirname + '/client/file.js'));

//provide a bundle exposing `require` for a few npm packages.
app.get('/js/bundle.js', browserify(['hyperquest', 'concat-stream']));

//provide a bundle for a few npm packages plus run main.js
app.get('/js/bundle.js', browserify(['hyperquest', 'concat-stream', {__dirname + '/client/main.js': {run: true}}]));

app.listen(3000);

Multiple Bundles Example

Multiple bundles can sometimes lead to better caching performance. If you had multiple different JavaScript modules in ./client that all depended on hyperquest and concat-stream and were used on different pages, you may want to split those two modules into separate files so that they are only loaded once for someone browsing arround the site:

var shared = ['hyperquest', 'concat-stream'];
app.get('/js/bundle.js', browserify(shared));
app.use('/js', browserify('./client', {external: shared}))

Then on your HTML pages you can just have:

page1.html

<script src="/js/bundle.js"></script>
<script src="/js/beep.js"></script>

page2.html

<script src="/js/bundle.js"></script>
<script src="/js/boop.js"></script>

This way, booth beep.js and boop.js can require the shared modules (hyperquest and concat-stream) but they aren't actually contained within that file.

API

browserify('./path/to/file.js'[, options])

Return the middleware to serve a browserified version of the file. The file path is relative to the calling module, not to process.cwd().

browserify('./path/to/directory/'[, options])

Return the middleware to serve a browserified version of all the files in a directory. The directory path is relative to the calling module, not to process.cwd().

browserify(['module-a', 'module-b'][, options])

Return middleware that will expose require for each of the modules in the array. This will work even if those modules are also in the external array.

browserify([{'module-d': {expose: 'dee'}}][, options])

Require module-d with custom options (to be passed on to browserify). In this case module-d will be exposed as dee. This can be mixed and matched with plain strings. Note that these modules must not appear in the external array.

options / settings

The options passed to each middleware function override the defaults specified in settings.

Setings has two properties settings.production and settings.development which specify the default settings for each environment. The current environment is specified by settings.mode and defaults to process.env.NODE_ENV || 'development'

Production defaults:

production.cache = true; // equivalent to "public, max-age=60"
production.precompile = true;
production.minify = true;
production.gzip = true;
production.debug = false;

To update:

browserify.settings.production('cache', '7 days');

Development defaults:

development.cache = 'dynamic';
development.precompile = false;
development.minify = false;
development.gzip = false;
development.debug = true;

To update:

browserify.settings.development('gzip', true);

The following defaults are the same for production and development:

external = [];
ignore = [];
ignoreMissing = false;
transform = [];
insertGlobals = false;
detectGlobals = true;
standalone = false;
grep = /\.js$/

To update:

browserify.settings('external', ['hyperquest']);
//or
browserify.settings({
  ignoreMissing: true,
  insertGlobals: true,
  transform: ['rfileify']
});

Custom Environments:

You can also create a new custom environment:

var test = browserify.settings.env('test');
test('minify', true);
//or
test({
  debug: true
});

cache

The cache setting determines how long content can be cached in the client's web browsers (and any caching proxies) and whether or not to cache bundles server side. Any value other than false will result in them being cached server side. The 'dynamic' cache option is special. It works like watchify and only re-compiles the files that have changed. This is the fastest option for development. It does not enable any client side caching.

If cache is true the client will recieve Cache Control of "public, max-age=60", which caches for 60 seconds.

If cache is a string in the form accepted by ms it becomes: "public, max-age=" + (ms(cache)/1000)

If cache is a number, it is treated as being in milliseconds so becomes: "public, max-age=" + (cache/1000)

If cache is an object of the form {private: true || false, maxAge: '10 minutes'} it becomes the apropriate string.

If cache is any other string it will be sent directly to the client.

N.B. that if caching is enabled, the server never times out its cache, no matter what the timeout set for the client.

precompile

The precompile setting enables bundles to be precompiled/built and readily cached immediately on server startup. This option is not available when using browserify with a directory. If precompile is set to true, the bundle will be compiled & cached at server start.

// Precompile a browserified file at a path
app.get('/js/file.js', browserify('./client/file.js', {
  cache: true,
  precompile: true
}));

// Precompile a bundle exposing `require` for a few npm packages.
app.get('/js/bundle.js', browserify(['hyperquest', 'concat-stream'], {
  cache: true,
  precompile: true
}));

N.B. It only makes sense to use precompiling when caching is enabled. If caching is disabled, no precompiling will happen.

minify

If minify is true, UglifyJS will be used to minify the resulting code. This is true by default in production. If you set it to an object, the object will be passed to uglify-js as options:

  • warnings (default false) - pass true to display compressor warnings
  • mangle (default true) - pass false to skip mangling names
  • output (default null) - pass an object to specify additional output options. The defaults are optimized for best compression.
  • compress (default {}) - pass false to skip compressing entirely. Pass an object to specify custom compressor options.

gzip

If gzip is true, GZip will be enabled when clients support it. This increases the memory required for caching by aproximately 50% but the speed boost can be considerable. It is true by default in production.

debug

If debug is true, a source map will be added to the code. This is very useful when debugging. debug is false in production.

basedir

If debug is true you can provide a string pathname for basedir and the paths of your files in the source-map will be displayed relative to that file. This is great for hiding the details of your local file system or tidying up the debugging of a large app.

plugins

An array of objects of the form {plugin: 'name', options: {object}}.

grep

The regular expression, something like /\.(?:js|coffee|ls)$/, that a filename must pass to be served using browserify from a directory.

hooks

There are a number of hooks that you can implement to modify the source at a few stages of processing.

e.g.

app.get('/index.js', browserify('/index.js', {
  preminify: function (source) {
    return angularJsMinifier(source);
  }
}));

The available hooks are currently:

  • postcompile - fires after compilation, but before any minfication/gzipping
  • preminify - fires before minfication (but only if minify is enabled)
  • postminify - fires after minfication (but only if minify is enabled)

The main use case you might have for this would be adding extra minfication steps that are able to make additional assumptions about your code. These hooks can return either a string or a Promise for a string.

Others

The remaining settings are all passed through to browserify, you should look at the browserify readme if you want to know more:

  • options.external - an array of module names that will be required from external bundles (see browserify/multiple bundles) (default: [])
  • options.ignore - an aray of module names that are prevented from showing up in the output bundle (default: [])
  • options.ignoreMissing - set to true to ignore errors when a module can't be found (default: false).
  • options.transform - an array of transforms to transform top level modules (default: []). Each item can be:
    • "transform-name" - the npm name of the transform
    • transformFunction - the transform function
    • ["transform-name" | tranformFunction, {option1: true, ...}] - the transform and some options
  • options.insertGlobals - set to true to always insert process, global etc. without analysing the AST for faster builds but larger bundles (Note that options.minify may cause the globals to be removed again anyway) (default: false)
  • options.detectGlobals - set to false to skip adding process, global etc. Setting this to false may break more npm modules (default: true).
  • options.noParse - an array of module names that should not be parsed for require statements of node.js style globals, can speed up loading things like jQuery that are huge but never use require.
  • options.standalone - generate a standalone build (in a umd wrapper) with this name, you probably don't want this.
  • options.extensions - an array of optional extra extensions for the module lookup machinery to use when the extension has not been specified. By default browserify considers only .js and .json files in such cases.
  • options.resolve - lets you override the default resolution algorithm (e.g. use browserify to compile component modules)
  • options.basedir - this shouldn't be needed as browserify-middleware already resolves to absolute paths.

You can optionally pass a single item instead of an array to any of the options that take an array.

License

MIT

If you find it useful, a donation via gittip would be appreciated.

  • Browserify是一个浏览器端代码模块化工具 简介 服务器端NodeJS自带模块功能,可以使用require和module.exports构建项目 随着项目的增大,浏览器端任务越来越重,依赖关系越来越复杂,需要使用工具实现模块化。 Browserify通过require和module.exports来导入和导出。 Browserify的原理:部署时处理代码依赖,将模块打包为一个文件。 存在的问

  • 1、安装和生成: npm install -g browserify browserify main.js > bundle.js 2、解析: browserify 可以让我们在前端引用npm 模块,或自定义的模块 使用require('XX')来引用npm 模块 用require('./XX.js')引用当前目录下自定义模块 一般会在main 或app js 里引用模块来实现入口js 编写完成后

  • 在浏览器环境无法识别下面四个变量module,exports,require,global,但是在node环境中有定义。 含有这几个变量的js文件需要用类似browserify或者webpack的打包工具进行打包成浏览器可以运行的js代码。 这里先简单介绍下browserify的用法: 1.全局安装browserify包: cnpm install browserify -g2.需要打包的代码ma

 相关资料
  • Browserify 可以让你使用类似于 node 的 require() 的方式来组织浏览器端的 Javascript 代码,通过预编译让前端 Javascript 可以直接使用 Node NPM 安装的一些库。 安装: npm install -g browserify 示例 这是 main.js 的内容,像普通的 nodejs 程序那样使用 require() 加载库和文件: var foo

  • 在进入第二种方法前,首先先介绍一下会用到 Browserify、Gulp、Babelify 三种前端开发常会用到的工具: Browserify 如同官网上说明的:Browserify lets you require('modules') in the browser by bundling up all of your dependencies.,Browserify 是一个可以让你在浏览器端也

  • 问题内容: 我试图在使用fs对象的文件上使用browserify。当我将其浏览器化时,对的调用不会转换并返回。 有什么解决方法吗?我在stackoverlow和其他地方看到了一些建议,但似乎没有一个完全实现的建议。 我实际上希望为我教的课使用browserify创建一个google web打包的应用程序。 提前致谢。 问题答案: 浏览器应该使用哪个文件系统?HTML5文件系统实际上无法与传统文件系

  • 我安装了模块(browserify、react、reactify),并尝试使用browserify处理jsx文件。 C:\dev\React。js 错在哪里?

  • 在上,使用require ex调用所有库。 并生成一个包含jquery的bundle

  • 基本用法 管理前端模块 生成前端模块 脚本文件的实时生成 browserify-middleware模块 参考链接 随着JavaScript程序逐渐模块化,在ECMAScript 6推出官方的模块处理方案之前,有两种方案在实践中广泛采用:一种是AMD模块规范,针对模块的异步加载,主要用于浏览器端;另一种是CommonJS规范,针对模块的同步加载,主要用于服务器端,即node.js环境。 Brows