This code has been working until the upgrade to Webpack 5.
I reduced the example to its minimum:
import { x } from './x';
const { y } = require('./y');
console.log(y, x); // OK
define([], () => {
console.log(y, x); // X IS NOT DEFINED
})
x.js and y.js are one line files:
Default values for target and output in webpack.config.js.
Once compiled to test.js this code is used from a requirejs application, like this:
require([
'test'
], function(test) {
test();
})
The invocation of test() throws a ReferenceError: x is not defined. There's no problem with y.
I reviewed the official v4 to v5 migration guide, but I found nothing.
EDIT: content of the bundle in both versions
EDIT AGAIN: I think the relevant part is at the end of the bundle, and the problem is related to concatenate modules feature
Webpack 4 (last fragment)
/***/ (function(module, __webpack_exports__, __webpack_require__) {
"use strict";
// ESM COMPAT FLAG
__webpack_require__.r(__webpack_exports__);
// CONCATENATED MODULE: ./app/code/Ungento/ShopByQR/view/frontend/web.src/js/view/x.js
var x = 'x';
// CONCATENATED MODULE: ./app/code/Ungento/ShopByQR/view/frontend/web.src/js/view/component.mount.js
var _require = __webpack_require__(51),
y = _require.y;
var component_mount_component = function component() {
return function () {
console.log(x, y);
};
};
define([], component_mount_component);
/***/ })
Webpack 5 (last fragment)
(() => {
var _require = __webpack_require__(452),
y = _require.y;
var component = function component() {
return function () {
console.log(x, y);
};
};
define([], component);
})();
Webpack 4
/******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId]) {
/******/ return installedModules[moduleId].exports;
/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ i: moduleId,
/******/ l: false,
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/ // Flag the module as loaded
/******/ module.l = true;
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/******/
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = modules;
/******/
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/
/******/ // define getter function for harmony exports
/******/ __webpack_require__.d = function(exports, name, getter) {
/******/ if(!__webpack_require__.o(exports, name)) {
/******/ Object.defineProperty(exports, name, { enumerable: true, get: getter });
/******/ }
/******/ };
/******/
/******/ // define __esModule on exports
/******/ __webpack_require__.r = function(exports) {
/******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
/******/ }
/******/ Object.defineProperty(exports, '__esModule', { value: true });
/******/ };
/******/
/******/ // create a fake namespace object
/******/ // mode & 1: value is a module id, require it
/******/ // mode & 2: merge all properties of value into the ns
/******/ // mode & 4: return value when already ns object
/******/ // mode & 8|1: behave like require
/******/ __webpack_require__.t = function(value, mode) {
/******/ if(mode & 1) value = __webpack_require__(value);
/******/ if(mode & 8) return value;
/******/ if((mode & 4) && typeof value === 'object' && value && value.__esModule) return value;
/******/ var ns = Object.create(null);
/******/ __webpack_require__.r(ns);
/******/ Object.defineProperty(ns, 'default', { enumerable: true, value: value });
/******/ if(mode & 2 && typeof value != 'string') for(var key in value) __webpack_require__.d(ns, key, function(key) { return value[key]; }.bind(null, key));
/******/ return ns;
/******/ };
/******/
/******/ // getDefaultExport function for compatibility with non-harmony modules
/******/ __webpack_require__.n = function(module) {
/******/ var getter = module && module.__esModule ?
/******/ function getDefault() { return module['default']; } :
/******/ function getModuleExports() { return module; };
/******/ __webpack_require__.d(getter, 'a', getter);
/******/ return getter;
/******/ };
/******/
/******/ // Object.prototype.hasOwnProperty.call
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
/******/
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "/";
/******/
/******/
/******/ // Load entry module and return exports
/******/ return __webpack_require__(__webpack_require__.s = 53);
/******/ })
/************************************************************************/
/******/ ({
/***/ 51:
/***/ (function(module, __webpack_exports__, __webpack_require__) {
"use strict";
__webpack_require__.r(__webpack_exports__);
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "y", function() { return y; });
var y = 'y';
/***/ }),
/***/ 53:
/***/ (function(module, __webpack_exports__, __webpack_require__) {
"use strict";
// ESM COMPAT FLAG
__webpack_require__.r(__webpack_exports__);
// CONCATENATED MODULE: ./app/code/Ungento/ShopByQR/view/frontend/web.src/js/view/x.js
var x = 'x';
// CONCATENATED MODULE: ./app/code/Ungento/ShopByQR/view/frontend/web.src/js/view/component.mount.js
var _require = __webpack_require__(51),
y = _require.y;
var component_mount_component = function component() {
return function () {
console.log(x, y);
};
};
define([], component_mount_component);
/***/ })
/******/ });
//# sourceMappingURL=component.mount.js.map
Webpack 5
/******/ (() => { // webpackBootstrap
/******/ "use strict";
/******/ var __webpack_modules__ = ({
/***/ 452:
/***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {
__webpack_require__.r(__webpack_exports__);
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */ "y": () => (/* binding */ y)
/* harmony export */ });
var y = 'y';
/***/ })
/******/ });
/************************************************************************/
/******/ // The module cache
/******/ var __webpack_module_cache__ = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/ // Check if module is in cache
/******/ var cachedModule = __webpack_module_cache__[moduleId];
/******/ if (cachedModule !== undefined) {
/******/ return cachedModule.exports;
/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = __webpack_module_cache__[moduleId] = {
/******/ // no module.id needed
/******/ // no module.loaded needed
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ __webpack_modules__[moduleId](module, module.exports, __webpack_require__);
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/************************************************************************/
/******/ /* webpack/runtime/define property getters */
/******/ (() => {
/******/ // define getter functions for harmony exports
/******/ __webpack_require__.d = (exports, definition) => {
/******/ for(var key in definition) {
/******/ if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
/******/ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
/******/ }
/******/ }
/******/ };
/******/ })();
/******/
/******/ /* webpack/runtime/hasOwnProperty shorthand */
/******/ (() => {
/******/ __webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))
/******/ })();
/******/
/******/ /* webpack/runtime/make namespace object */
/******/ (() => {
/******/ // define __esModule on exports
/******/ __webpack_require__.r = (exports) => {
/******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
/******/ }
/******/ Object.defineProperty(exports, '__esModule', { value: true });
/******/ };
/******/ })();
/******/
/************************************************************************/
var __webpack_exports__ = {};
// This entry need to be wrapped in an IIFE because it need to be isolated against other modules in the chunk.
(() => {
var _require = __webpack_require__(452),
y = _require.y;
var component = function component() {
return function () {
console.log(x, y);
};
};
define([], component);
})();
/******/ })()
;
//# sourceMappingURL=component.mount.js.map
This code has been working until the upgrade to Webpack 5.
I reduced the example to its minimum:
x.jsandy.jsare one line files:Default values for target and output in
webpack.config.js.Once compiled to
test.jsthis code is used from arequirejsapplication, like this:The invocation of test() throws a
ReferenceError: x is not defined. There's no problem withy.I reviewed the official v4 to v5 migration guide, but I found nothing.
EDIT: content of the bundle in both versions
EDIT AGAIN: I think the relevant part is at the end of the bundle, and the problem is related to concatenate modules feature
Webpack 4 (last fragment)
Webpack 5 (last fragment)
Webpack 4
Webpack 5