Search code examples
javascriptbrowserifycommonjs

Using browserify with a minified JavaScript library


  1. Can a minified JavaScript library be "required" and bundled using Browserify? In other words, does Browserify require the JavaScript file to be in source format?
  2. If a JavaScript file is not a CommonJS module (does not export anything), can it be bundled using Browserify? In other words, What does require('xyz.js') do if xyz.js is not a CommonJS module.

Solution

    1. In case it's properly exporting its properties (e.g. using exports or module.exports) and loading modules using require() then yes.
    2. Of course it can be bundled, but you can't access its properties/data from the result of the require() call. However if it's using for example the global object for its exports, you can access it after you require the file.

    xyz.js:

    window.myExport = "hello";
    

    main.js:

    var xyz = require("xyz");
    xyz.myExport; // undefined
    window.myExport; // "hello"