问题
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