2

There are a number of plugins I want jquery to automatically have. In a regular site I would simply include jquery and the script in order. How do I accomplish this with webpack? Assume there is no npm package for the plugins and I just want to load them from files.

2
  • Can you be a little more specific and give more detail based on the project you're working on? Off the top of my head I can recommend using the script-loader. Commented May 9, 2015 at 21:59
  • Honestly the specific plugins don't matter. Just to pick a few, let's say jeditable, datatables, and bootstrap switch. I can probably require each of them one by one in each file I want to use them (using the script-loader), but I would like it so that requiring jquery automatically has these plugins loaded. Thanks. Commented May 11, 2015 at 13:31

1 Answer 1

2

I will assume the following directory structure:

project
  |- app
  |   |- vendor
  |   |    |- plugins
  |   |    |    |- plugin-1.js
  |   |    |    |- plugin-2.js
  |   |    |    |- ...
  |   |    |
  |   |    |- jquery.js
  |   |
  |   |- jquery-with-plugins.js
  |   |- main.js
  |
  |- js
  |   |- bundle.js
  |
  |- webpack.config.js
  |- package.json
 ...

And here are the contents of the key files:

// app/jquery-with-plugins.js
require('vendor/jquery');
req = require.context('vendor/plugins', true, /\.js$/);
req.keys().forEach(function (plugin) {
  req(plugin);
});

module.exports = jQuery;

// app/main.js
var $ = require('jquery');

$(function () {
  $('.selector-1').use_plugin_1();
  $('.selector-2').use_plugin_2();
});

// webpack.config.js
var path = require('path');

module.exports = {
  context: path.join(__dirname, 'app'),
  entry: './main',
  output: {
    path: path.join(__dirname, 'js'),
    filename: 'bundle.js'
  },
  module: {
    loaders: [
      {
        test: /\.js$/,
        include: [
          path.join(__dirname, 'app/vendor')
        ],
        loader: 'script'
      }
    ]
  },
  resolve: {
    alias: {
      'jquery$': path.join(__dirname, 'app/jquery_with_plugins'),
      'vendor': path.join(__dirname, 'app/vendor')
    }
  },
  extensions: ['', '.js']
};

// package.json
{
  "name": "temp",
  "version": "0.0.0",
  "private": true,
  "scripts": {
    "build": "webpack --progress --colors"
  },
  "devDependencies": {
    "node-libs-browser": "^0.5.0",
    "script-loader": "^0.6.1",
    "webpack": "^1.9.4"
  }
}

Every JavaScript file in the app/vendor directory has been configured to load with the script-loader and hence will be executed in the global context. In app/jquery-with-plugins.js I first load jQuery and then every plugin so by the time I export jQuery (N.B. I don't have to export jQuery since it's already globally exposed but I think it's better to use CommonJS modules from here on out.) every plugin would already be attached to the jQuery object.

I've aliased jquery to refer to app/jquery-with-plugins.js (see the resolve.alias config) and hence I can require jQuery with all the plugins by simply doing require('jquery'). Even better, to be able to add a plugin and use it immediately you just need to add it to app/vendor/plugins. An example of that is shown in app/main.js

Finally, everything can be bundled to js/bundle.js by running npm run build.

Sign up to request clarification or add additional context in comments.

2 Comments

I haven't tried this yet but it looks awesome. Thank you!
Your welcome. Let me know if you run into any problems.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.