2

I'm using webpack to concatenate JS libraries and JS files of my own. Currently the setup is like this

var wpStream = require('webpack-stream')';
var files = ['jquery.js', 'angular.js', 'app.js', 'controller.js', 'etc.js'];

gulp.task('pack', function(){
  return gulp.src(files)
    .pipe(wpStream({
      output:{filename: 'bundle.js'}
    }).pipe(gulp.dest('dist'));
});

This works well. My file is created and it has all of the content specified in my files array. However on page load, I get the error jQuery is not defined. Additionally, in my console when I type jQuery there is not jQuery, but rather jQuery111206520785381790835. And when I append a dot to see the list of methods, there's just the normal object methods (hasOwnProperty, toString, etc).

How do I access jQuery? What has webpack done with it?

Community
  • 1
  • 1
1252748
  • 13,412
  • 30
  • 97
  • 213

1 Answers1

0

You have to make jQuery globally accessible with webpack ProvidePlugin

var webpack = require('webpack');
var wpStream = require('webpack-stream');
var path = require('path');
var files = ['app.js', 'controller.js', 'etc.js'];

gulp.task('pack', function () {
    return gulp.src(files)
        .pipe(wpStream(
            {
                output: {
                    filename: 'bundle.js'
                },
                plugins: [
                    new webpack.ProvidePlugin({
                        angular: "angular",
                        $: "jquery",
                        jQuery: "jquery"
                    })
                ],
                resolve: {
                    root: path.resolve('./vendor'), // directory that contains jquery.js and angular.js
                    extensions: ['', '.js']
                }
            }
        ).pipe(gulp.dest('dist'));
});
Steffen
  • 3,079
  • 1
  • 28
  • 30
  • `{ cache: false, output: { filename: 'bundle.js' }, module: { }, plugins:[ new webpack.optimize.UglifyJsPlugin({ compress: { warnings: false } }), new webpack.ProvidePlugin({ $: "jquery", jQuery: "jquery" }) ], debug: false }` is the entire config object that i pass to `wpStream`. It still isn't working though. Any ideas? – 1252748 May 16 '16 at 16:03
  • Also note that I'm not using npm to include jQuery. I have it in a local file. `files` array contains the directory it is in. – 1252748 May 16 '16 at 16:10
  • Are you using the $ or jQuery var in your modules or in the browser console? The ProvidePlugin will only resolve these global variables at build time in your modules that are bundled with webpack. https://webpack.github.io/docs/shimming-modules.html#plugin-provideplugin – Steffen May 16 '16 at 16:31
  • I use `jQuery`, not `$`. – 1252748 May 16 '16 at 16:39
  • It might also help to add resolve configuration for jquery.js and angular.js modules based on your folder structure: `var path = require('path'); // ... resolve: { root: path.resolve('./vendor'), extensions: ['', '.js'] }` and remove those files from the array – Steffen May 16 '16 at 16:45
  • where would that go in the config? within plugins? – 1252748 May 16 '16 at 17:03