Conflict between standalone & require
piranna opened this issue · comments
I'm building a framework that have a simple plugins architecture with peerDependencies, where the plugins need to access to some resources of the host project. This scheme is working on Node.js, and I'm trying to mimic it in browsers by following the instructions on the readme to build multiple bundles.
It works as is, but since the framework has some build-in plugins so for basic use cases it can work without any other files, I'm using the standalone
option so it can be used from the window object. Problem is, that I've found that by using the standalone
option the require()
function is not available anymore, so if I want both to access the framework main code from a namespace on the window object and at the same time by doing a require() call (mainly done by the plugins) I need to use an externally provided require()
function.
Is it on purpose? Why? What alternatives do I have?
If it's a bug, where should a look to fix it easily?
Is this the same problem I'm facing?
running browserify index.js -s MySomething -o bundle.js
works as desired but
running browserify index.js -s MySomething -r ./deps/react-proxy.js:react -o bundle.js
where ./deps-react-proxy.js
is
module.exports = window.React;
produces a bundle where MySomething
is not the module.exports
from index.js
but it's the same as window.React
.
I'm not sure if it's the same problem, since I've only tested about using standalone & require both over the module itself instead of use standalone over the module and require over one of its dependencies, but I believe both issues are related and it's the same problem.
I've investigated about it and seems the problem is at browser-pack instead, since there's there an if-elseif statement that only allow have enabled one of them...
We've done a simple workaround:
-
use the require() generation method giving the package name as the
expose
argument (this is done automatically on grunt-browseify by settting an alias from the package name to the package main file) -
expose the package namespace on the global
window
object manually:if(typeof kurentoClient == 'undefined') window.kurentoClient = require('kurento-client');
With this, our package can be used on the browser both with require()
and as a global object. I think this is simple enough so it can be added to browser-pack...
@nnarhinen I think your issue is what I explain in #1120.
I submitted browserify/browser-pack/pull/51 re: this.