Frage

I have

A package with a plugin system. Each plugin is a package One can load a plugin by alling the method use and pass the plugins export as argument:

package = require('thePackage').use( require('thePlugin'), require('anotherPlugin'));

i want

If the argument of use is a string use should require the module.

package = require('thePackage').use('thePlugin','anotherPlugin')

the question

Can browserify resolve this usage of require?

I am not sure if browserify just looks for reqire calls in the top levl or if actualy eveluat the code.

War es hilfreich?

Lösung

Browserify processes only require() calls with literals, i.e. require('theplugin'); It will not include modules that can be required with something like:

function use(moduleName) {
    require(moduleName);
}
use("someModule");

You can see it in the code, by looking at module-deps (which is the module for scanning the modules) which calls node-detective, which by default returns only literals (strings) for require() calls.

You can still achieve what you want, and use the require() with parameters inside your functions for plugins, but then you will have to be more explicit with what you include in your browserified package. Browserify will not traverse through those plugin modules if it won't see a literal require, so you need to --require them manually. for example browserify --require ./src/plugins/plugin.js

Module loading resolution (i.e. argument for require()) might not work the same under node.js and in a browserified environment, so be careful if you're doing some clever module name resolution in your use() function.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top