Webpacker: ProvidePlugin not making `$` globally available

Created on 12 Jul 2018  路  2Comments  路  Source: rails/webpacker

I may be misunderstanding #1389 but if I have this in config/webpack/environment.js:

const { environment } = require('@rails/webpacker');
const webpack = require('webpack');

environment.plugins.prepend(
  'Provide',
  new webpack.ProvidePlugin({
    $: 'jquery',
    jQuery: 'jquery'
  })
);

module.exports = environment;

then I expect to have access to $ and jQuery globally, including in the console window. This isn't happening. If I add a debug statement to development.js I see that the plugin is being added:

#console.log(environment.plugins);

ConfigList [
  { key: 'ProvideJQuery',
    value: ProvidePlugin { definitions: [Object] } },
  { key: 'Environment',
    value: EnvironmentPlugin { keys: [Array], defaultValues: [Object] } },
  { key: 'CaseSensitivePaths',
    value:
     CaseSensitivePathsPlugin { options: {}, pathCache: {}, fsOperations: 0, primed: false } },
  { key: 'ExtractText',
    value:
     ExtractTextPlugin { filename: '[name]-[contenthash].css', id: 1, options: {} } },
  { key: 'Manifest', value: ManifestPlugin { opts: [Object] } } ]

But I have no $ or jQuery in the browser window. And while some jQuery-dependent packs work fine, others don't -- specifically I encounter this issue: OwlCarousel2/OwlCarousel2#2206

Most helpful comment

ProvidePlugin

module is loaded automatically and the identifier is filled with the exports of the loaded module

if I understand correctly global scope is not modified and $/jQuery could be accessible only inside a module, to make identifier available globally you could write something like window.$ = $ inside your application.js module

All 2 comments

ProvidePlugin

module is loaded automatically and the identifier is filled with the exports of the loaded module

if I understand correctly global scope is not modified and $/jQuery could be accessible only inside a module, to make identifier available globally you could write something like window.$ = $ inside your application.js module

I think you're probably looking for the Expose Loader; not the ProvidePlugin.

Was this page helpful?
0 / 5 - 0 ratings