Conditionally set externals for a specific Webpack bundle

时间秒杀一切 提交于 2020-01-16 11:38:09

问题


I want to exclude jQuery from all but one of my bundles. I see I can pass a function to externals, but I'm having trouble getting it working correctly. TBH, I'm not sure if it's possible to do this.

I'd like to exclude jQuery from core.js only.

// Relevant Webpacker config

environment.config.set('externals', function(context, request, callback) {
  // This makes core.js an external bundle
  if (!/core\.js$/.test(request)) {
    return callback(null, 'commonjs ' + request);
  }

  // This makes jQuery external for all bundles
  if (/^(jquery|\$)$/i.test(request)) {
    return callback(null, 'commonjs ' + request);
  }

  // How do I make jQuery external for all except for core?
  // This doesn't work because request is the filename as a string
  if (/^(jquery|\$)$/i.test(request) && !/core\.js$/.test(request)) {
    return callback(null, 'commonjs ' + request);
  }

  callback();
});

I thought request would contain more dependency information, but it seems to be strings of entry points and packages. Anyone know if it's possible to get that information?

For context: I'm working in a Rails app, migrating from Sprockets to Webpack. Our current JavaScript relies on libraries globally assigned to window. In order to migrate over, I'd like to maintain global assignment until we address it at a later date. Setting jQuery to an external forces a single global instance of jQuery, which is what I need at this point in time.

来源:https://stackoverflow.com/questions/58933900/conditionally-set-externals-for-a-specific-webpack-bundle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!