Browserify的自定义依赖项名称无效

joe*_*net 9 node.js browserify

我试图让browserify的自定义依赖项名称与内存流一起使用.我使用内存流的原因是因为此代码注定要在AWS Lambda中运行,该Lamb Lambda将接收多个"文件"作为输入,并且不会通过文件系统对lambda可用.

根据https://github.com/substack/node-browserify上的文档,似乎这应该是可能的:

b.require(文件,选择)

file也可以是一个流,但你也应该使用opts.basedir,以便相对要求可以解析.

使用opts的expose属性指定自定义依赖项名称.require('./ vendor/angular/angular.js',{expose:'angular'})启用require('angular')

码:

const browserify = require('browserify')
const path = require('path')
const File = require('vinyl')

const jsIndex = new File({
    file: path.resolve('index.js'),
    contents: new Buffer('var two = require("./two")')})
const jsTwo  = new File({
    file: path.resolve('two.js'),
    contents: new Buffer('console.log("Hello from two.js")')})

browserify({
        entries: [ jsIndex ],
        require: [ jsTwo ],
        basedir: path.resolve('.')
    })
    .bundle((err, res) => {
        if (err) console.log(err.message)
    })
    .pipe(process.stdout)
Run Code Online (Sandbox Code Playgroud)

错误:

Cannot find module './two' from '...'

编辑:

另一个人在node-browserify github页面上发布了类似的问题:https://github.com/substack/node-browserify/issues/1622

car*_*ant 1

Browserify 大量使用文件系统,因为它实现了自己的模块解析,该解析使用与 Node 的require. 为了让 Browserify 使用内存中的源文件进行捆绑,一种解决方案是对文件系统进行猴子修补 - 以类似于mock-fs用于测试的模块的方式。

但是,您实际上只想对源文件执行此操作。Browserify 对捆绑包中包含的其他文件使用其模块解析,并且必须将这些文件添加到内存文件系统中会很乏味。修补的文件系统函数应该检查涉及内存中源文件的调用,相应地处理它们并将其他调用转发到原始实现。(mock-fs执行类似的操作,检测发生在内部的文件系统调用require并将这些调用转发到原始实现。)

'use strict';

const fs = require('fs');
const path = require('path');
const toStream = require('string-to-stream');

// Create an object hash that contains the source file contents as strings,
// keyed using the resolved file names. The patched fs methods will look in
// this map for source files before falling back to the original
// implementations.

const files = {};
files[path.resolve('one.js')] =
    'console.log("Hello from one.js");' +
    'var two = require("./two");' +
    'exports.one = 1;';
files[path.resolve('two.js')] =
    'console.log("Hello from two.js");' +
    'exports.two = 2;';

// The three fs methods that need to be patched take a file name as the
// first parameter - so the patch mechanism is the same for all three.

function patch(method, replacement) {

    var original = fs[method];
    fs[method] = function (...args) {

        var name = path.resolve(args[0]);
        if (files[name]) {
            args[0] = name;
            return replacement.apply(null, args);
        } else {
            return original.apply(fs, args);
        }
    };
}

patch('createReadStream', function (name) {

    return toStream(files[name]);
});

patch('lstat', function (...args) {

    args[args.length - 1](null, {
        isDirectory: function () { return false; },
        isFile: function () { return true; },
        isSymbolicLink: function () { return false; }
    });
});

patch('stat', function (...args) {

    args[args.length - 1](null, {
        isDirectory: function () { return false; },
        isFile: function () { return true; }
    });
});

// With the fs module patched, browserify can be required and the bundle
// can be built.

const browserify = require('browserify');

browserify()
    .require(path.resolve('one.js'), { entry: true, expose: 'one' })
    .require(path.resolve('two.js'), { expose: 'two' })
    .bundle()
    .pipe(process.stdout);
Run Code Online (Sandbox Code Playgroud)

requireexpose

在你的问题中,你提到了expose。和模块是使用 捆绑的one.js,因此可以使用选项中指定的名称在浏览器中需要它们。如果这不是您想要的,您可以直接使用它们,它们将是捆绑包内部的模块。two.jsrequireexposeadd

<!doctype html>
<html>
<head>
    <title>so-39397429</title>
</head>
<body>
    <script src="./bundle.js"></script>
    <script>
        // At this point, the bundle will have loaded and the entry
        // point(s) will have been executed. Exposed modules can be
        // required in scripts, like this:
        console.log(require('one'));
        console.log(require('two'));
    </script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

在调查这个 tsify 问题时,我花了一些时间研究 Browserifyrequireexpose选项。这些选项有助于从捆绑包外部请求模块,但我完全不确定它们对捆绑包内模块之间的需求有任何影响(这就是您所需要的)。此外,它们的实现有点令人困惑 - 特别是这部分,其中公开的名称有时会在前面加上斜杠。

乙烯基流

在你的问题中,你使用了vinyl. 但是,我无法用于vinyl读取流。虽然它确实实现了pipe,但它不是事件发射器,并且似乎没有实现on基于 pre-Streams 2 API - 这是 Browserify 期望的结果createReadStream