【问题标题】:How do I use the `--node` option in browserify with the API如何通过 API 在 browserify 中使用 `--node` 选项
【发布时间】:2016-08-04 17:15:36
【问题描述】:

CLI 具有高级选项 --node,该选项捆绑以在节点内使用。

文档说明:

--node

Alias for --bare and --no-browser-field.

关闭浏览器字段的 API 选项很简单,但 --bare 选项让我感到困惑。

--bare

Alias for both --no-builtins, --no-commondir, and sets --insert-global-vars
to just "__filename,__dirname". This is handy if you want to run bundles in
node.

尤其是--insert-global-vars 真的让我很困惑。

那么问题来了:

如何使用browserify([files] [, opts]) api 获得与--bare 相同的结果?

【问题讨论】:

    标签: browserify


    【解决方案1】:

    这是根据Browserify source code 参数基于--bare 参数设置选项的代码:

    if (argv.bare) {
        argv.builtins = false;
        argv.commondir = false;
        if (argv.igv === undefined) {
            argv.igv = '__filename,__dirname';
        }
    }
    
    if (argv.igv) {
        var insertGlobalVars = {};
        var wantedGlobalVars = argv.igv.split(',');
        Object.keys(insertGlobals.vars).forEach(function (x) {
            if (wantedGlobalVars.indexOf(x) === -1) {
                insertGlobalVars[x] = undefined;
            }
        });
    }
    

    所以你应该把它传递给browserify()opts参数:

    const files = [...];
    const opts = {
        builtins: false,
        commondir: false,
        insertGlobalVars: ['__filename', '__dirname']
    };
    
    browserify(files, opts);
    

    【讨论】:

    • 哦,我也在源代码中找到了它。谢谢您的帮助。这行得通!
    • 对于其他偶然发现此问题的人,opts.browserField: false 也应设置为模仿 --node CLI 选项。
    猜你喜欢
    • 1970-01-01
    • 2016-07-29
    • 2014-02-21
    • 2017-06-15
    • 2022-12-17
    • 2015-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多