ForbesLindesay / browserify-middleware

express middleware for browserify, done right
http://browserify.org
MIT License
381 stars 66 forks source link

cannot insertGlobalVars #124

Closed neilk closed 5 years ago

neilk commented 5 years ago

I can't figure out how to pass insertGlobalVars through browserify-middleware. Can you help me figure this out? I'll submit a documentation patch one way or another.

I have some values that my node server knows about, and I'd like to bundle them into JavaScript.

In a simple script with plain old browserify, this works, bundling the module in the 'clientApp' directory.

const browserify = require('browserify');
const MyGlobals = { foo: 1, bar: 2 };
browserify('/path/to/clientAppDir', { 
    insertGlobalVars: {
        MyGlobals: () => JSON.stringify(MyGlobals),
    }
})
.bundle()
.pipe(process.stdout);

I expected this, with browserify-middleware to give a similar result. It's part of a longer handler, but this is roughly what it's doing:

const browserify = require('browserify-middleware');
const MyGlobals = { foo: 1, bar: 2 };
app.use('/clientApp', browserify('/path/to/clientAppDir', {
        insertGlobalVars: {
            MyGlobals: () => JSON.stringify(MyGlobals),
        },
    }
));

However, the globals are not included in the resulting file.

I've resorted to tracing it all the way through to insert-module-globals, but it's confusing, since I'm not really sure what the correct behavior would be.

I've tried setting insertGlobalVars in the browserify.settings, but that doesn't seem to change anything.

neilk commented 5 years ago

Never mind, solved by upgrading to current version.

Previously, build-bundle.js picked which options to pass along to browserify. The newer version doesn't mess with options that it doesn't have to.

Thanks!