简体   繁体   中英

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",
}),

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM