question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

webpack.ProvidePlugin

See original GitHub issue

I need to inject React variable to each module. In pure webpack solution it would be like that:

plugins: [
        new webpack.ProvidePlugin({
            'React': 'react'
        })
    ],

but webpack-stream have no ProvidePlugin method. Also, I dont want to make another dependence on webpack just for global React var.

How can I use webpack.ProvidePlugin?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

5reactions
Kwapicommented, Aug 16, 2018

For future reference, I managed to solve my issue by changing my code to this:

const webpack = require('webpack'); 
const webpackStream = require('webpack-stream');
const compiler = require('webpack')

gulp.task('js', ['clean-js'], function () {
    return gulp.src(config.theme.path + 'src/js/app.js')
        .pipe(plumber())
        .pipe(webpackStream(
            {
                mode: 'development',
                devtool: 'source-map',
                plugins: [
                    new webpack.ProvidePlugin({
                        $: 'jquery',
                        jQuery: 'jquery',
                        'window.jQuery': 'jquery'
                    }),
                ],

                output: {
                    filename: '[name].[contenthash].js'
                },

                /* config */
            }, compiler, function(err, stats) {
                /* Use stats to do more things if needed */
            }))
        .pipe(gulp.dest(config.theme.path + 'assets/js/'))
})

The key was to rename the original webpack variable to webpackStream and additionally import a new webpack variable from webpack. That way I can access the ProvidePlugin class.

Now my scripts can see jQuery. I hope that helps someone who’s in a similar situation

0reactions
blachawkcommented, Mar 26, 2022
  • As of 2022, install webpack as a npm dependency!
  • Next, pipe in webpack stream as normal, but this time try setting the config value to require webpack.config.js like so…
...
  .pipe(gulpif(PRODUCTION, webpack({
            config: require('./webpack.config.js'),
            })
        ))
...

Then within webpack.config.js you can rock and roll…


// webpack.config.js
const webpack = require('webpack')

const paths = {
  scripts: {
      src: ['src/scripts/js_bundle.js'],
      dest: './pub/scripts'
  },
  scriptsFlexslider: {
      src: ['src/scripts/js_bundle_flexslider.js'],
      dest: './pub/scripts'
  }
};

module.exports = {
  entry: {
    js_bundle: './' + paths.scripts.src,
    js_bundle_flexslider: './' + paths.scriptsFlexslider.src
  },
  mode: 'development',
  devtool: 'source-map',
  output: { }, //using gulp to pipe my dest's
  plugins: [  //...or whatever plugin react logic you need here!
    new webpack.ProvidePlugin({
      'window.jQuery'    : 'jquery',
      'window.$'         : 'jquery',
      'jQuery'           : 'jquery',
      '$'                : 'jquery'
    })
  ]
}


I find myself doing it this way only if I need plugin support through webpack stream!

Read more comments on GitHub >

github_iconTop Results From Across the Web

ProvidePlugin - webpack
webpack is a module bundler. Its main purpose is to bundle JavaScript files for usage in a browser, yet it is also capable...
Read more >
How to use the webpack.ProvidePlugin function in ... - Snyk
ProvidePlugin function in webpack. To help you get started, we've selected a few webpack examples, based on popular ways it is used in...
Read more >
webpack.ProvidePlugin JavaScript and Node.js code examples
Best JavaScript code snippets using webpack.ProvidePlugin(Showing top 12 results out of 315) · Most used webpack functions · Popular in JavaScript.
Read more >
Webpack ProvidePlugin vs externals? - Stack Overflow
It's both possible: You can include libraries with a <script> (i. e. to use a library from a CDN) or include them into...
Read more >
ProvidePlugin & Global Vars - SymfonyCasts
Instead, go to webpack.js.org . This time, skip straight to the Documentation, Plugins, then find the ProvidePlugin. This plugin is crazy cool: it's...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found