Search code examples
webpackwebpack-provide-plugin

Choose for which files to provide the selection module webpack.ProvidePlugin


Is it possible that in my config I have chosen to serve the react package for specific files with the .jsx extension.

Now I have react imported for both .js and .jsx, but can I restrict it only for .jsx files

new webpack.ProvidePlugin({
    "React": "react",
}),

Solution

  • This is currently not possible using the webpack ProviderPlugin.

    As a temporary solution, you could modify the ProviderPlugin in node_modules/webpack/lib/ProviderPlugin.js and add the following code below lines 56 and 47:

    parser.hooks.expression.for(name).tap("ProvidePlugin", expr => {
      if (name === 'React' && !parser.state.current.resource.endsWith('.jsx')) {
        return true;
      }
    
      // ...
    });
    

    You could even fork the plugin and publish it under your NPM account as a modified version.