browserify的standalone的含义

白话:就像Jquery的$, 把你打包后的函数挂在window下你指定的名字下

废话:白话看不懂,就看下面的废话,你不得不花更多时间理解
--standalone AAA的含义
$ browserify foo.js --standalone AAA > bundle.js
This command will export the contents of foo.js under the external module name AAA. If a module system is detected in the host environment, it will be used. Otherwise a window global named AAA will be exported.
当在主机上检测到module system时,直接使用module system。没有检测到module system时,使用window global named AAA

在浏览器中时,window.AAA下挂的是你的module.exports
而在node环境下,AAA不起作用
看下面的代码

    if (typeof exports === "object" && typeof module !== "undefined") {
        module.exports = f()
    } else if (typeof define === "function" && define.amd) {
        define([], f)
    } else {
        var g;
        if (typeof window !== "undefined") {
            g = window
        } else if (typeof global !== "undefined") {
            g = global
        } else if (typeof self !== "undefined") {
            g = self
        } else {
            g = this
        }
        g.AAA = f()
    }

所以说standalone其实是一种模式。这么重要的功能设计成一个不起眼的选项,

名字还这么长,这么怪。可以说设计的缺点。

 

顺便聊下bare的含义
--bare
假设在程序中用了Buffer(Buffer在nodejs中是一个内建的类)的是时候。browserify遇到Buffer,会在bundle中添加Buffer类的前端实现。然而,--bare会阻止它添加Buffer类。这样,Buffer在node中是什么就是什么,程序是原汁原味的,原来是什么,现在还是什么,这也是为什么叫bare的原因。

posted on 2017-01-22 09:16  嘉禾李小龙  阅读(594)  评论(0编辑  收藏  举报

导航