Pulling my hair out here looking for a simple solution to share code, required via NPM, across multiple Browserify or Webpack bundles. Thinking, is there such a thing as a file "bridge"?
This isn't due to compile time (I'm aware of watchify) but rather the desire to extract out all of my vendor specific libs into vendor.js
so to keep my app.js
filesize down and to not crash the browser with massive sourcemaps. Plus, I find it way cleaner should the need to view the compiled js arise. And so:
// vendor.js
require('react');
require('lodash');
require('other-npm-module');
require('another-npm-module');
Its very important that the code be loaded from NPM as opposed to Bower, or saved into some 'vendor' directory in order to be imported via a relative path and identified via a shim. I'd like to keep every library reference pulled via NPM except for my actual application source.
In app.js
I keep all of my sourcecode, and via the externals
array, exclude vendor libraries listed above from compilation:
// app.js
var React = require('react');
var _ = require('lodash');
var Component = React.createClass()
// ...
And then in index.html
, I require both files
// index.html
<script src='vendor.js'></script>
<script src='app.js'></script>
Using Browserify or Webpack, how can I make it so that app.js
can "see" into those module loaded via npm? I'm aware of creating a bundle with externals and then referencing the direct file (in, say, node_modules
) via an alias, but I'm hoping to find a solution that is more automatic and less "Require.js" like.
Basically, I'm wondering if it is possible to bridge the two so that app.js
can look inside vendor.js
in order to resolve dependencies. This seems like a simple, straightforward operation but I can't seem to find an answer anywhere on this wide, wide web.
Thanks!
With webpack you'd use multiple entry points and the CommonChunkPlugin.
Taken from the webpack docs:
To split your app into 2 files, say
app.js
andvendor.js
, you can require the vendor files invendor.js
. Then pass this name to the CommonChunkPlugin as shown below.This will remove all modules in the vendor chunk from the app chunk. The
bundle.js
will now contain just your app code, without any of it’s dependencies. These are invendor.bundle.js
.In your HTML page load
vendor.bundle.js
beforebundle.js
.Listing all the vendor files/modules and using CommonChunkPlugin is indeed the recommended way. This gets pretty tedious though, and error prone.
Consider these NPM modules:
fastclick
andmprogress
. Since they have not adopted the CommonJS module format, you need to give webpack a hand, like this:Now assuming you would want both
fastclick
andmprogress
in your vendor chunk, you would probably try this:Alas, it doesn't work. You need to match the calls to
require()
:It gets old, even with some
resolve.alias
trickery. Here is my workaround. CommonChunkPlugin lets you specify a callback that will return whether or not you want a module to be included in the vendor chunk. If your own source code is in a specificsrc
directory, and the rest is in thenode_modules
directory, just reject the modules based on their path:Where
module.resource
is the path to the module being considered. You could also do the opposite, and include only the module if it is insidenode_modules_dir
, i.e.:but in my situation, I'd rather say: "put everything that is not in my source source tree in a vendor chunk".
Hope that helps.
Source: https://github.com/webpack/webpack/issues/2372#issuecomment-213149173