javascriptnode.jsclosuresbrowserifymodule-pattern

Are closures/module pattern obsolete when using browserify?


I have been using the module pattern while programming front end code that is browserified before being served to the client. From what I've been reading it seems like Browserify puts different files and their respective code in their own closures to emulate node style module separation. Does this mean that when I'm using Browserify that it defeats the purpose of closures/the module pattern? if this is the case it seems like using the module pattern and wrapping my code into a closure, that will then be stored in another closure, could be an unnecessary and performance reducing pattern. Should I treat code differently when writing modules that will be compiled with Browserify?


Solution

  • Should I treat code differently when writing modules that will be compiled with Browserify?

    No. But when you are writing modules, i.e. JavaScript files that are interpreted as CommonJS or ES6 modules, you should not use the module pattern (IIFE) indeed. Modules already have their own scope, all you need to do is using module.exports or export declarations.

    See also Namespacing with IIFE in ES6?.