New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Webpack 4 chunking different runtime behaviour compared to Webpack 3 #6626
Comments
You removed Read more: https://gist.github.com/sokra/1522d586b8e5c0f5072d7565c2bee693 |
Well I've tried various different config options with Maybe some futher tweaks to the config would make it work, but in any case I think a lot of people will stumble into this same problem. optimization: {
splitChunks: {
cacheGroups: {
default: false,
commons: {
test: /[\\/]node_modules[\\/]/,
name: "vendor",
chunks: "all"
}
}
}
}, /***/ "./main.js":
/*!*****************!*\
!*** ./main.js ***!
\*****************/
/*! no exports provided */
/***/ (function(module, __webpack_exports__, __webpack_require__) {
"use strict";
eval("__webpack_require__.r(__webpack_exports__);\n/* harmony import */ var jquery__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! jquery */ \"./node_modules/jquery/dist/jquery.js\");\n/* harmony import */ var jquery__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(jquery__WEBPACK_IMPORTED_MODULE_0__);\n\r\n\r\njquery__WEBPACK_IMPORTED_MODULE_0___default.a.fn.sayHi();\r\n\n\n//# sourceURL=webpack:///./main.js?");
/***/ })
/******/ }); (window["webpackJsonp"] = window["webpackJsonp"] || []).push([["vendor"],{
/***/ "./node_modules/jquery/dist/jquery.js":
/*!********************************************!*\
!*** ./node_modules/jquery/dist/jquery.js ***!
\********************************************/
/*! no static exports found */
/***/ (function(module, exports, __webpack_require__) {
eval(".....JQUERY CODE HERE.......")
/***/ }),
/***/ "./vendor.js":
/*!*******************!*\
!*** ./vendor.js ***!
\*******************/
/*! no static exports found */
/***/ (function(module, exports, __webpack_require__) {
eval("/* WEBPACK VAR INJECTION */(function($) {$.fn.sayHi = function() {\r\n alert('Hi');\r\n};\r\n\n/* WEBPACK VAR INJECTION */}.call(this, __webpack_require__(/*! jquery */ \"./node_modules/jquery/dist/jquery.js\")))\n\n//# sourceURL=webpack:///./vendor.js?");
/***/ })
}]); |
Well, thats not an code splitting problem anymore. When code is splitted correctly i would shift my focus to this last error - maybe you should use I migrated my project yesterday and ProvidePlugin didn't make any issues, with config extremely similar to yours:
I would try wrapping the plugin code (vendor.js) into class, exporting it as ES module and try to instantiate it when appropriate. Hope something from above makes positive effect. |
Nope. For the life of me I cannot get it working as it did in Webpack 3 - the new So this is what I've got now: var webpack = require('webpack');
var path = require('path');
module.exports = {
mode: 'production',
entry: {
main: './main.js',
vendor: './vendor.js'
},
output: {
path: path.resolve(__dirname, './dist'),
filename: '[name].js',
},
optimization: {
minimize: false,
splitChunks: {
cacheGroups: {
default: false,
commons: {
test: /jquery/,
name: "vendor",
chunks: "initial",
minSize: 1,
reuseExistingChunk: true
}
}
}
},
plugins: [
new webpack.ProvidePlugin({
'$' : './jquery.js',
jQuery: './jquery.js'
})
]
}; jquery.js( function( global, factory ) {
"use strict";
if ( typeof module === "object" && typeof module.exports === "object" ) {
module.exports = global.document ?
factory( global, true ) :
function( w ) {
if ( !w.document ) {
throw new Error( "jQuery requires a window with a document" );
}
return factory( w );
};
} else {
factory( global );
}
} )( typeof window !== "undefined" ? window : this, function( window, noGlobal ) {
"use strict";
var
version = "3.3.1",
jQuery = function( selector, context ) {
return new jQuery.fn.init( selector, context );
};
jQuery.fn = jQuery.prototype = {
};
var
_jQuery = window.jQuery,
_$ = window.$;
if ( !noGlobal ) {
window.jQuery = window.$ = jQuery;
}
return jQuery;
} ); (this is a split down version for easier testing) This gives me a runtime in After some debugging it looks like I've tried fiddling around with the new I know Webpack 4 is in it's early days - but looking at the comments on that Gist I'm not the only one having trouble migrating. @pavelloz you mention you had no troubles with migrating - do you use jQuery and any plugins (and chunk to vendor.js) by any chance? Have you tried just creating a very basic setup where you have a) jQuery, b) some jQuery plugin, c) Use the jQuery plugin in |
Ok I've managed to get the runtime to go to a seperate file by doing: optimization: {
runtimeChunk: true
.... This creates a Webpack 3Webpack 4 |
I know its cryptic ;) Anyway, i think your webpack config looks suspicious, the things i mentioned and now...
I have never seen anyone using ProvidePlugin that way ('./jquery.js') |
That's a perfectly valid way to use the plugin. Also I only did it that way to try to understand / debug what's happening easier with Webpack rather than pulling down the whole of jQuery and looking at huge build outputs. If you shim jQuery library itself, you get the same result. |
I am having the same issue. The gist of the issue is that you'd expect the common chunk to contain the runtime, as it contains the dependencies of the other chunks, but instead the dependent code contains the runtime chunk. Since it is a complex subject, and is black magic for the uninitiated, the best solution would IMHO be an option to be able to specify in which output(s) the runtime should be placed. Or consider it a bug as it does follow behavior essentially preventing this (imho) common code splitting scenario. Fairly large example for a SPA / Smaller example of multiple-pages/entry points. |
So it seems you can change where the runtime goes by naming the entry: optimization: {
runtimeChunk: {
name: 'vendor'
}, However in my case this doesn't change what order the files are executed in, it seems |
I'm opening a new issue as this one went off track a bit at the start - #6647 |
Continuing off track.. Multi-Page-Application I have a question regarding "controlling the bundle output using webpack.config.js" With webpack v3 we used What I want: I have a vendor.build.js which imports all vendor libs, output should be: vendor.bundle.js. In the webpack.config I use one instance of the I also load modules dynamically in
How do I have to configure the optimization config part to get the following results in dist folder:
|
The Read this and tell me if it solves your problem. |
I guess, the main problem is, that we still think the |
@message |
Do you want to request a feature or report a bug?
Bug / Change in behaviour compared to Webpack 3
What is the current behavior?
When telling Webpack which chunk the runtime should be in, it doesn't appear to change the order of excecution - see below example.
If the current behavior is a bug, please provide the steps to reproduce.
vendor.js
main.js
index.htm
Webpack 3 config
Webpack 4 Result
As you can see,
main run
fires before vendor, even though the runtime has been put inside vendor and it's loaded first.Webpack 3 Result
As you can see
vendor run
fires first, which means plugin is successfully registered and the alert dialog pops up.~~
Original issue below - fixed by providing custom
splitChunks
. Above is now the current issue.~~
Do you want to request a feature or report a bug?
Bug
What is the current behavior?
Webpack 4 will add jQuery to both
vendor.js
andmain.js
when you useProvidePlugin
- this has a side effect that if you have a vendor plugin that is registering itself as a jQuery plugin invendor.js
thru$.fn
it is only available for use invendor.js
and won't work inmain.js
If the current behavior is a bug, please provide the steps to reproduce.
vendor.js
main.js
Webpack 3 config
The webpack 4 config is exactly the same, except minus the
CommonChunkPlugin
This is the output:
Webpack 3 with ProvidePlugin
Webpack 4 with ProvidePlugin
As you can see, Webpack 4 has added
jQuery
to both entries, though Webpack 3 hasn't.What is the expected behavior?
To only include one version of jQuery.
If this is a feature request, what is motivation or use case for changing the behavior?
This is likely down to the new "rules" surrounding when to chunk and it also not playing nicely with the ProvidePlugin. This is something that will likely catch a lot of people out, if they use the default chunking algorithm in Webpack 4 along with ProvidePlugin. Maybe there is a way to default to the old behaviour as Webpack 3, but it's not immediately obvious to me how to do this.
Example Repository
https://github.com/garygreen/webpack4-chunks-prob
~~
The text was updated successfully, but these errors were encountered: