From 57a8ebd7611e32a02fdf75afc494b5b72735d492 Mon Sep 17 00:00:00 2001 From: Xiao <784487301@qq.com> Date: Fri, 5 Sep 2025 17:31:05 +0800 Subject: [PATCH] fix: keep renderModule order consistent (#19867) --- lib/ExternalModule.js | 62 ++- .../ConfigCacheTestCases.longtest.js.snap | 524 ++++++++++++++++-- .../ConfigTestCases.basictest.js.snap | 131 ++++- .../externals/module-import/index.js | 2 +- .../library/render-order-issue/bar.js | 2 + .../library/render-order-issue/entry.js | 2 + .../library/render-order-issue/entry1.js | 2 + .../library/render-order-issue/entry2.js | 2 + .../library/render-order-issue/foo.js | 2 + .../library/render-order-issue/index.js | 3 + .../library/render-order-issue/loader.js | 22 + .../library/render-order-issue/test.config.js | 7 + .../render-order-issue/webpack.config.js | 58 ++ types.d.ts | 2 + 14 files changed, 741 insertions(+), 80 deletions(-) create mode 100644 test/configCases/library/render-order-issue/bar.js create mode 100644 test/configCases/library/render-order-issue/entry.js create mode 100644 test/configCases/library/render-order-issue/entry1.js create mode 100644 test/configCases/library/render-order-issue/entry2.js create mode 100644 test/configCases/library/render-order-issue/foo.js create mode 100644 test/configCases/library/render-order-issue/index.js create mode 100644 test/configCases/library/render-order-issue/loader.js create mode 100644 test/configCases/library/render-order-issue/test.config.js create mode 100644 test/configCases/library/render-order-issue/webpack.config.js diff --git a/lib/ExternalModule.js b/lib/ExternalModule.js index d356198d2..313100f52 100644 --- a/lib/ExternalModule.js +++ b/lib/ExternalModule.js @@ -258,7 +258,6 @@ class ModuleExternalInitFragment extends InitFragment { } } - const identifier = `__WEBPACK_EXTERNAL_MODULE_${ident}__`; super( "", InitFragment.STAGE_HARMONY_IMPORTS, @@ -268,8 +267,8 @@ class ModuleExternalInitFragment extends InitFragment { this._ident = ident; this._request = request; this._dependencyMeta = dependencyMeta; - this._imported = imported; - this._identifier = identifier; + this._identifier = this.buildIdentifier(ident); + this._imported = this.buildImported(imported); } /** @@ -332,6 +331,28 @@ class ModuleExternalInitFragment extends InitFragment { getNamespaceIdentifier() { return this._identifier; } + + /** + * @param {string} ident ident + * @returns {string} identifier + */ + buildIdentifier(ident) { + return `__WEBPACK_EXTERNAL_MODULE_${ident}__`; + } + + /** + * @param {Imported} imported imported + * @returns {Imported} normalized imported + */ + buildImported(imported) { + if (Array.isArray(imported)) { + return imported.map(([name]) => { + const ident = `${this._ident}_${name}`; + return [name, this.buildIdentifier(ident)]; + }); + } + return imported; + } } register( @@ -428,21 +449,7 @@ const getSourceForModuleExternal = ( imported = []; break; default: - imported = []; - if (exportsInfo.isUsed(runtime) === false) { - // no used, only - } - for (const [name] of usedExports.entries()) { - let counter = 0; - let finalName = name; - - if (concatenationScope) { - while (!concatenationScope.registerUsedName(finalName)) { - finalName = `${name}_${counter++}`; - } - } - imported.push([name, finalName]); - } + imported = [...usedExports.entries()]; } } @@ -453,7 +460,24 @@ const getSourceForModuleExternal = ( dependencyMeta, runtimeTemplate.outputOptions.hashFunction ); - const specifiers = imported === true ? undefined : imported; + const normalizedImported = initFragment.getImported(); + const specifiers = + normalizedImported === true + ? undefined + : /** @type {[string, string][]} */ ( + normalizedImported.map(([name, rawFinalName]) => { + let finalName = rawFinalName; + let counter = 0; + + if (concatenationScope) { + while (!concatenationScope.registerUsedName(finalName)) { + finalName = `${finalName}_${counter++}`; + } + } + return [name, finalName]; + }) + ); + const baseAccess = `${initFragment.getNamespaceIdentifier()}${propertyAccess( moduleAndSpecifiers, 1 diff --git a/test/__snapshots__/ConfigCacheTestCases.longtest.js.snap b/test/__snapshots__/ConfigCacheTestCases.longtest.js.snap index b4552e87f..3aca78e1c 100644 --- a/test/__snapshots__/ConfigCacheTestCases.longtest.js.snap +++ b/test/__snapshots__/ConfigCacheTestCases.longtest.js.snap @@ -10150,9 +10150,9 @@ exports[`ConfigCacheTestCases css webpack-ignore exported tests should compile 1 `; exports[`ConfigCacheTestCases library concatenate-modules-named-import-externals concatenate-modules-named-import-externals should compile 1`] = ` -"import { HomeLayout as lib_HomeLayout_0, a } from \\"externals0\\"; -import { HomeLayout as HomeLayout_0, a as a_0 } from \\"externals1\\"; -import { default as default_0 } from \\"externals2\\"; +"import { HomeLayout as __WEBPACK_EXTERNAL_MODULE_externals0_HomeLayout__, a as __WEBPACK_EXTERNAL_MODULE_externals0_a__ } from \\"externals0\\"; +import { HomeLayout as __WEBPACK_EXTERNAL_MODULE_externals1_HomeLayout__, a as __WEBPACK_EXTERNAL_MODULE_externals1_a__ } from \\"externals1\\"; +import { default as __WEBPACK_EXTERNAL_MODULE_externals2_default__ } from \\"externals2\\"; import * as __WEBPACK_EXTERNAL_MODULE_externals3__ from \\"externals3\\"; import \\"externals4\\"; /******/ // The require scope @@ -10195,15 +10195,15 @@ const external_externals3_namespaceObject = __WEBPACK_EXTERNAL_MODULE_externals3 ;// ./lib.js -const { HomeLayout: lib_HomeLayout = 123 } = {}; -console.log({ HomeLayout: lib_HomeLayout }); +const { HomeLayout = 123 } = {}; +console.log({ HomeLayout }); { - const { HomeLayout = lib_HomeLayout_0 } = {}; + const { HomeLayout = __WEBPACK_EXTERNAL_MODULE_externals0_HomeLayout__ } = {}; console.log({ HomeLayout }); } (() => { { - const { HomeLayout = lib_HomeLayout_0 } = {}; + const { HomeLayout = __WEBPACK_EXTERNAL_MODULE_externals0_HomeLayout__ } = {}; console.log({ HomeLayout }); } })() @@ -10237,19 +10237,19 @@ console.log({ HomeLayout: lib_HomeLayout }); const HomeLayout_0 = 'HomeLayout_0'; HomeLayout_0; } -HomeLayout_0; -a; -a_0; -default_0; +__WEBPACK_EXTERNAL_MODULE_externals1_HomeLayout__; +__WEBPACK_EXTERNAL_MODULE_externals0_a__; +__WEBPACK_EXTERNAL_MODULE_externals1_a__; +__WEBPACK_EXTERNAL_MODULE_externals2_default__; external_externals3_namespaceObject; -export { lib_HomeLayout as HomeLayout, a }; +export { HomeLayout, __WEBPACK_EXTERNAL_MODULE_externals0_a__ as a }; " `; exports[`ConfigCacheTestCases library concatenate-modules-named-import-externals concatenate-modules-named-import-externals should compile 2`] = ` -"import { HomeLayout as lib_HomeLayout_0, a } from \\"externals0\\"; -import { HomeLayout as HomeLayout_0, a as a_0 } from \\"externals1\\"; -import { default as default_0 } from \\"externals2\\"; +"import { HomeLayout as __WEBPACK_EXTERNAL_MODULE_externals0_HomeLayout__, a as __WEBPACK_EXTERNAL_MODULE_externals0_a__ } from \\"externals0\\"; +import { HomeLayout as __WEBPACK_EXTERNAL_MODULE_externals1_HomeLayout__, a as __WEBPACK_EXTERNAL_MODULE_externals1_a__ } from \\"externals1\\"; +import { default as __WEBPACK_EXTERNAL_MODULE_externals2_default__ } from \\"externals2\\"; import * as __WEBPACK_EXTERNAL_MODULE_externals3__ from \\"externals3\\"; import \\"externals4\\"; /******/ // The require scope @@ -10292,15 +10292,15 @@ const external_externals3_namespaceObject = __WEBPACK_EXTERNAL_MODULE_externals3 ;// ./lib.js -const { HomeLayout: lib_HomeLayout = 123 } = {}; -console.log({ HomeLayout: lib_HomeLayout }); +const { HomeLayout = 123 } = {}; +console.log({ HomeLayout }); { - const { HomeLayout = lib_HomeLayout_0 } = {}; + const { HomeLayout = __WEBPACK_EXTERNAL_MODULE_externals0_HomeLayout__ } = {}; console.log({ HomeLayout }); } (() => { { - const { HomeLayout = lib_HomeLayout_0 } = {}; + const { HomeLayout = __WEBPACK_EXTERNAL_MODULE_externals0_HomeLayout__ } = {}; console.log({ HomeLayout }); } })() @@ -10334,19 +10334,19 @@ console.log({ HomeLayout: lib_HomeLayout }); const HomeLayout_0 = 'HomeLayout_0'; HomeLayout_0; } -HomeLayout_0; -a; -a_0; -default_0; +__WEBPACK_EXTERNAL_MODULE_externals1_HomeLayout__; +__WEBPACK_EXTERNAL_MODULE_externals0_a__; +__WEBPACK_EXTERNAL_MODULE_externals1_a__; +__WEBPACK_EXTERNAL_MODULE_externals2_default__; external_externals3_namespaceObject; -export { lib_HomeLayout as HomeLayout, a }; +export { HomeLayout, __WEBPACK_EXTERNAL_MODULE_externals0_a__ as a }; " `; exports[`ConfigCacheTestCases library concatenate-modules-named-import-externals concatenate-modules-named-import-externals should pre-compile to fill disk cache (1st) 1`] = ` -"import { HomeLayout as lib_HomeLayout_0, a } from \\"externals0\\"; -import { HomeLayout as HomeLayout_0, a as a_0 } from \\"externals1\\"; -import { default as default_0 } from \\"externals2\\"; +"import { HomeLayout as __WEBPACK_EXTERNAL_MODULE_externals0_HomeLayout__, a as __WEBPACK_EXTERNAL_MODULE_externals0_a__ } from \\"externals0\\"; +import { HomeLayout as __WEBPACK_EXTERNAL_MODULE_externals1_HomeLayout__, a as __WEBPACK_EXTERNAL_MODULE_externals1_a__ } from \\"externals1\\"; +import { default as __WEBPACK_EXTERNAL_MODULE_externals2_default__ } from \\"externals2\\"; import * as __WEBPACK_EXTERNAL_MODULE_externals3__ from \\"externals3\\"; import \\"externals4\\"; /******/ // The require scope @@ -10389,15 +10389,15 @@ const external_externals3_namespaceObject = __WEBPACK_EXTERNAL_MODULE_externals3 ;// ./lib.js -const { HomeLayout: lib_HomeLayout = 123 } = {}; -console.log({ HomeLayout: lib_HomeLayout }); +const { HomeLayout = 123 } = {}; +console.log({ HomeLayout }); { - const { HomeLayout = lib_HomeLayout_0 } = {}; + const { HomeLayout = __WEBPACK_EXTERNAL_MODULE_externals0_HomeLayout__ } = {}; console.log({ HomeLayout }); } (() => { { - const { HomeLayout = lib_HomeLayout_0 } = {}; + const { HomeLayout = __WEBPACK_EXTERNAL_MODULE_externals0_HomeLayout__ } = {}; console.log({ HomeLayout }); } })() @@ -10431,19 +10431,19 @@ console.log({ HomeLayout: lib_HomeLayout }); const HomeLayout_0 = 'HomeLayout_0'; HomeLayout_0; } -HomeLayout_0; -a; -a_0; -default_0; +__WEBPACK_EXTERNAL_MODULE_externals1_HomeLayout__; +__WEBPACK_EXTERNAL_MODULE_externals0_a__; +__WEBPACK_EXTERNAL_MODULE_externals1_a__; +__WEBPACK_EXTERNAL_MODULE_externals2_default__; external_externals3_namespaceObject; -export { lib_HomeLayout as HomeLayout, a }; +export { HomeLayout, __WEBPACK_EXTERNAL_MODULE_externals0_a__ as a }; " `; exports[`ConfigCacheTestCases library concatenate-modules-named-import-externals concatenate-modules-named-import-externals should pre-compile to fill disk cache (2nd) 1`] = ` -"import { HomeLayout as lib_HomeLayout_0, a } from \\"externals0\\"; -import { HomeLayout as HomeLayout_0, a as a_0 } from \\"externals1\\"; -import { default as default_0 } from \\"externals2\\"; +"import { HomeLayout as __WEBPACK_EXTERNAL_MODULE_externals0_HomeLayout__, a as __WEBPACK_EXTERNAL_MODULE_externals0_a__ } from \\"externals0\\"; +import { HomeLayout as __WEBPACK_EXTERNAL_MODULE_externals1_HomeLayout__, a as __WEBPACK_EXTERNAL_MODULE_externals1_a__ } from \\"externals1\\"; +import { default as __WEBPACK_EXTERNAL_MODULE_externals2_default__ } from \\"externals2\\"; import * as __WEBPACK_EXTERNAL_MODULE_externals3__ from \\"externals3\\"; import \\"externals4\\"; /******/ // The require scope @@ -10486,15 +10486,15 @@ const external_externals3_namespaceObject = __WEBPACK_EXTERNAL_MODULE_externals3 ;// ./lib.js -const { HomeLayout: lib_HomeLayout = 123 } = {}; -console.log({ HomeLayout: lib_HomeLayout }); +const { HomeLayout = 123 } = {}; +console.log({ HomeLayout }); { - const { HomeLayout = lib_HomeLayout_0 } = {}; + const { HomeLayout = __WEBPACK_EXTERNAL_MODULE_externals0_HomeLayout__ } = {}; console.log({ HomeLayout }); } (() => { { - const { HomeLayout = lib_HomeLayout_0 } = {}; + const { HomeLayout = __WEBPACK_EXTERNAL_MODULE_externals0_HomeLayout__ } = {}; console.log({ HomeLayout }); } })() @@ -10528,12 +10528,12 @@ console.log({ HomeLayout: lib_HomeLayout }); const HomeLayout_0 = 'HomeLayout_0'; HomeLayout_0; } -HomeLayout_0; -a; -a_0; -default_0; +__WEBPACK_EXTERNAL_MODULE_externals1_HomeLayout__; +__WEBPACK_EXTERNAL_MODULE_externals0_a__; +__WEBPACK_EXTERNAL_MODULE_externals1_a__; +__WEBPACK_EXTERNAL_MODULE_externals2_default__; external_externals3_namespaceObject; -export { lib_HomeLayout as HomeLayout, a }; +export { HomeLayout, __WEBPACK_EXTERNAL_MODULE_externals0_a__ as a }; " `; @@ -11333,6 +11333,434 @@ export { __webpack_exports__HomeLayout as HomeLayout, __webpack_exports__a as a " `; +exports[`ConfigCacheTestCases library render-order-issue render-order-issue should compile 1`] = ` +"import { v as __WEBPACK_EXTERNAL_MODULE_externals0_v__ } from \\"externals0\\"; +import { v as __WEBPACK_EXTERNAL_MODULE_externals1_v__ } from \\"externals1\\"; +/******/ var __webpack_modules__ = ({ + +/***/ \\"./bar.js\\": +/*!****************************!*\\\\ + !*** ./bar.js + 1 modules ***! + \\\\****************************/ +/***/ (() => { + + +;// external \\"externals0\\" + +;// ./bar.js + +__WEBPACK_EXTERNAL_MODULE_externals0_v__; + +/***/ }), + +/***/ \\"./entry.js\\": +/*!******************!*\\\\ + !*** ./entry.js ***! + \\\\******************/ +/***/ ((module, __unused_webpack_exports, __webpack_require__) => { + +module.exports.entry1 = __webpack_require__(/*! ./entry1.js */ \\"./entry1.js\\") +module.exports.entry2 = __webpack_require__(/*! ./entry2.js */ \\"./entry2.js\\") + +/***/ }), + +/***/ \\"./entry1.js\\": +/*!*******************!*\\\\ + !*** ./entry1.js ***! + \\\\*******************/ +/***/ ((module, __unused_webpack_exports, __webpack_require__) => { + +module.exports = __webpack_require__(/*! ./foo */ \\"./foo.js\\") +module.exports.name = \\"entry1\\" + +/***/ }), + +/***/ \\"./entry2.js\\": +/*!*******************!*\\\\ + !*** ./entry2.js ***! + \\\\*******************/ +/***/ ((module, __unused_webpack_exports, __webpack_require__) => { + +module.exports = __webpack_require__(/*! ./bar */ \\"./bar.js\\") +module.exports.name = \\"entry2\\" + +/***/ }), + +/***/ \\"./foo.js\\": +/*!****************************!*\\\\ + !*** ./foo.js + 1 modules ***! + \\\\****************************/ +/***/ (() => { + + +;// external \\"externals1\\" + +;// ./foo.js + +__WEBPACK_EXTERNAL_MODULE_externals1_v__; + +/***/ }) + +/******/ }); +/************************************************************************/ +/******/ // 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; +/******/ } +/******/ +/************************************************************************/ +/******/ +/******/ // startup +/******/ // Load entry module and return exports +/******/ // This entry module used 'module' so it can't be inlined +/******/ var __webpack_exports__ = __webpack_require__(\\"./entry.js\\"); +/******/ const __webpack_exports__entry1 = __webpack_exports__.entry1; +/******/ const __webpack_exports__entry2 = __webpack_exports__.entry2; +/******/ export { __webpack_exports__entry1 as entry1, __webpack_exports__entry2 as entry2, __webpack_exports__ as default }; +/******/ +" +`; + +exports[`ConfigCacheTestCases library render-order-issue render-order-issue should compile 2`] = ` +"import { v as __WEBPACK_EXTERNAL_MODULE_externals0_v__ } from \\"externals0\\"; +import { v as __WEBPACK_EXTERNAL_MODULE_externals1_v__ } from \\"externals1\\"; +/******/ var __webpack_modules__ = ({ + +/***/ \\"./bar.js\\": +/*!****************************!*\\\\ + !*** ./bar.js + 1 modules ***! + \\\\****************************/ +/***/ (() => { + + +;// external \\"externals0\\" + +;// ./bar.js + +__WEBPACK_EXTERNAL_MODULE_externals0_v__; + +/***/ }), + +/***/ \\"./entry.js\\": +/*!******************!*\\\\ + !*** ./entry.js ***! + \\\\******************/ +/***/ ((module, __unused_webpack_exports, __webpack_require__) => { + +module.exports.entry1 = __webpack_require__(/*! ./entry1.js */ \\"./entry1.js\\") +module.exports.entry2 = __webpack_require__(/*! ./entry2.js */ \\"./entry2.js\\") + +/***/ }), + +/***/ \\"./entry1.js\\": +/*!*******************!*\\\\ + !*** ./entry1.js ***! + \\\\*******************/ +/***/ ((module, __unused_webpack_exports, __webpack_require__) => { + +module.exports = __webpack_require__(/*! ./foo */ \\"./foo.js\\") +module.exports.name = \\"entry1\\" + +/***/ }), + +/***/ \\"./entry2.js\\": +/*!*******************!*\\\\ + !*** ./entry2.js ***! + \\\\*******************/ +/***/ ((module, __unused_webpack_exports, __webpack_require__) => { + +module.exports = __webpack_require__(/*! ./bar */ \\"./bar.js\\") +module.exports.name = \\"entry2\\" + +/***/ }), + +/***/ \\"./foo.js\\": +/*!****************************!*\\\\ + !*** ./foo.js + 1 modules ***! + \\\\****************************/ +/***/ (() => { + + +;// external \\"externals1\\" + +;// ./foo.js + +__WEBPACK_EXTERNAL_MODULE_externals1_v__; + +/***/ }) + +/******/ }); +/************************************************************************/ +/******/ // 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; +/******/ } +/******/ +/************************************************************************/ +/******/ +/******/ // startup +/******/ // Load entry module and return exports +/******/ // This entry module used 'module' so it can't be inlined +/******/ var __webpack_exports__ = __webpack_require__(\\"./entry.js\\"); +/******/ const __webpack_exports__entry1 = __webpack_exports__.entry1; +/******/ const __webpack_exports__entry2 = __webpack_exports__.entry2; +/******/ export { __webpack_exports__entry1 as entry1, __webpack_exports__entry2 as entry2, __webpack_exports__ as default }; +/******/ +" +`; + +exports[`ConfigCacheTestCases library render-order-issue render-order-issue should pre-compile to fill disk cache (1st) 1`] = ` +"import { v as __WEBPACK_EXTERNAL_MODULE_externals0_v__ } from \\"externals0\\"; +import { v as __WEBPACK_EXTERNAL_MODULE_externals1_v__ } from \\"externals1\\"; +/******/ var __webpack_modules__ = ({ + +/***/ \\"./bar.js\\": +/*!****************************!*\\\\ + !*** ./bar.js + 1 modules ***! + \\\\****************************/ +/***/ (() => { + + +;// external \\"externals0\\" + +;// ./bar.js + +__WEBPACK_EXTERNAL_MODULE_externals0_v__; + +/***/ }), + +/***/ \\"./entry.js\\": +/*!******************!*\\\\ + !*** ./entry.js ***! + \\\\******************/ +/***/ ((module, __unused_webpack_exports, __webpack_require__) => { + +module.exports.entry1 = __webpack_require__(/*! ./entry1.js */ \\"./entry1.js\\") +module.exports.entry2 = __webpack_require__(/*! ./entry2.js */ \\"./entry2.js\\") + +/***/ }), + +/***/ \\"./entry1.js\\": +/*!*******************!*\\\\ + !*** ./entry1.js ***! + \\\\*******************/ +/***/ ((module, __unused_webpack_exports, __webpack_require__) => { + +module.exports = __webpack_require__(/*! ./foo */ \\"./foo.js\\") +module.exports.name = \\"entry1\\" + +/***/ }), + +/***/ \\"./entry2.js\\": +/*!*******************!*\\\\ + !*** ./entry2.js ***! + \\\\*******************/ +/***/ ((module, __unused_webpack_exports, __webpack_require__) => { + +module.exports = __webpack_require__(/*! ./bar */ \\"./bar.js\\") +module.exports.name = \\"entry2\\" + +/***/ }), + +/***/ \\"./foo.js\\": +/*!****************************!*\\\\ + !*** ./foo.js + 1 modules ***! + \\\\****************************/ +/***/ (() => { + + +;// external \\"externals1\\" + +;// ./foo.js + +__WEBPACK_EXTERNAL_MODULE_externals1_v__; + +/***/ }) + +/******/ }); +/************************************************************************/ +/******/ // 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; +/******/ } +/******/ +/************************************************************************/ +/******/ +/******/ // startup +/******/ // Load entry module and return exports +/******/ // This entry module used 'module' so it can't be inlined +/******/ var __webpack_exports__ = __webpack_require__(\\"./entry.js\\"); +/******/ const __webpack_exports__entry1 = __webpack_exports__.entry1; +/******/ const __webpack_exports__entry2 = __webpack_exports__.entry2; +/******/ export { __webpack_exports__entry1 as entry1, __webpack_exports__entry2 as entry2, __webpack_exports__ as default }; +/******/ +" +`; + +exports[`ConfigCacheTestCases library render-order-issue render-order-issue should pre-compile to fill disk cache (2nd) 1`] = ` +"import { v as __WEBPACK_EXTERNAL_MODULE_externals0_v__ } from \\"externals0\\"; +import { v as __WEBPACK_EXTERNAL_MODULE_externals1_v__ } from \\"externals1\\"; +/******/ var __webpack_modules__ = ({ + +/***/ \\"./bar.js\\": +/*!****************************!*\\\\ + !*** ./bar.js + 1 modules ***! + \\\\****************************/ +/***/ (() => { + + +;// external \\"externals0\\" + +;// ./bar.js + +__WEBPACK_EXTERNAL_MODULE_externals0_v__; + +/***/ }), + +/***/ \\"./entry.js\\": +/*!******************!*\\\\ + !*** ./entry.js ***! + \\\\******************/ +/***/ ((module, __unused_webpack_exports, __webpack_require__) => { + +module.exports.entry1 = __webpack_require__(/*! ./entry1.js */ \\"./entry1.js\\") +module.exports.entry2 = __webpack_require__(/*! ./entry2.js */ \\"./entry2.js\\") + +/***/ }), + +/***/ \\"./entry1.js\\": +/*!*******************!*\\\\ + !*** ./entry1.js ***! + \\\\*******************/ +/***/ ((module, __unused_webpack_exports, __webpack_require__) => { + +module.exports = __webpack_require__(/*! ./foo */ \\"./foo.js\\") +module.exports.name = \\"entry1\\" + +/***/ }), + +/***/ \\"./entry2.js\\": +/*!*******************!*\\\\ + !*** ./entry2.js ***! + \\\\*******************/ +/***/ ((module, __unused_webpack_exports, __webpack_require__) => { + +module.exports = __webpack_require__(/*! ./bar */ \\"./bar.js\\") +module.exports.name = \\"entry2\\" + +/***/ }), + +/***/ \\"./foo.js\\": +/*!****************************!*\\\\ + !*** ./foo.js + 1 modules ***! + \\\\****************************/ +/***/ (() => { + + +;// external \\"externals1\\" + +;// ./foo.js + +__WEBPACK_EXTERNAL_MODULE_externals1_v__; + +/***/ }) + +/******/ }); +/************************************************************************/ +/******/ // 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; +/******/ } +/******/ +/************************************************************************/ +/******/ +/******/ // startup +/******/ // Load entry module and return exports +/******/ // This entry module used 'module' so it can't be inlined +/******/ var __webpack_exports__ = __webpack_require__(\\"./entry.js\\"); +/******/ const __webpack_exports__entry1 = __webpack_exports__.entry1; +/******/ const __webpack_exports__entry2 = __webpack_exports__.entry2; +/******/ export { __webpack_exports__entry1 as entry1, __webpack_exports__entry2 as entry2, __webpack_exports__ as default }; +/******/ +" +`; + exports[`ConfigCacheTestCases source-map extract-source-map exported tests should extract source map - 1 1`] = ` Array [ "webpack:///./external-source-map.txt", diff --git a/test/__snapshots__/ConfigTestCases.basictest.js.snap b/test/__snapshots__/ConfigTestCases.basictest.js.snap index 854f642ee..b6cef95aa 100644 --- a/test/__snapshots__/ConfigTestCases.basictest.js.snap +++ b/test/__snapshots__/ConfigTestCases.basictest.js.snap @@ -10150,9 +10150,9 @@ exports[`ConfigTestCases css webpack-ignore exported tests should compile 1`] = `; exports[`ConfigTestCases library concatenate-modules-named-import-externals concatenate-modules-named-import-externals should compile 1`] = ` -"import { HomeLayout as lib_HomeLayout_0, a } from \\"externals0\\"; -import { HomeLayout as HomeLayout_0, a as a_0 } from \\"externals1\\"; -import { default as default_0 } from \\"externals2\\"; +"import { HomeLayout as __WEBPACK_EXTERNAL_MODULE_externals0_HomeLayout__, a as __WEBPACK_EXTERNAL_MODULE_externals0_a__ } from \\"externals0\\"; +import { HomeLayout as __WEBPACK_EXTERNAL_MODULE_externals1_HomeLayout__, a as __WEBPACK_EXTERNAL_MODULE_externals1_a__ } from \\"externals1\\"; +import { default as __WEBPACK_EXTERNAL_MODULE_externals2_default__ } from \\"externals2\\"; import * as __WEBPACK_EXTERNAL_MODULE_externals3__ from \\"externals3\\"; import \\"externals4\\"; /******/ // The require scope @@ -10195,15 +10195,15 @@ const external_externals3_namespaceObject = __WEBPACK_EXTERNAL_MODULE_externals3 ;// ./lib.js -const { HomeLayout: lib_HomeLayout = 123 } = {}; -console.log({ HomeLayout: lib_HomeLayout }); +const { HomeLayout = 123 } = {}; +console.log({ HomeLayout }); { - const { HomeLayout = lib_HomeLayout_0 } = {}; + const { HomeLayout = __WEBPACK_EXTERNAL_MODULE_externals0_HomeLayout__ } = {}; console.log({ HomeLayout }); } (() => { { - const { HomeLayout = lib_HomeLayout_0 } = {}; + const { HomeLayout = __WEBPACK_EXTERNAL_MODULE_externals0_HomeLayout__ } = {}; console.log({ HomeLayout }); } })() @@ -10237,12 +10237,12 @@ console.log({ HomeLayout: lib_HomeLayout }); const HomeLayout_0 = 'HomeLayout_0'; HomeLayout_0; } -HomeLayout_0; -a; -a_0; -default_0; +__WEBPACK_EXTERNAL_MODULE_externals1_HomeLayout__; +__WEBPACK_EXTERNAL_MODULE_externals0_a__; +__WEBPACK_EXTERNAL_MODULE_externals1_a__; +__WEBPACK_EXTERNAL_MODULE_externals2_default__; external_externals3_namespaceObject; -export { lib_HomeLayout as HomeLayout, a }; +export { HomeLayout, __WEBPACK_EXTERNAL_MODULE_externals0_a__ as a }; " `; @@ -10445,6 +10445,113 @@ export { __webpack_exports__HomeLayout as HomeLayout, __webpack_exports__a as a " `; +exports[`ConfigTestCases library render-order-issue render-order-issue should compile 1`] = ` +"import { v as __WEBPACK_EXTERNAL_MODULE_externals0_v__ } from \\"externals0\\"; +import { v as __WEBPACK_EXTERNAL_MODULE_externals1_v__ } from \\"externals1\\"; +/******/ var __webpack_modules__ = ({ + +/***/ \\"./bar.js\\": +/*!****************************!*\\\\ + !*** ./bar.js + 1 modules ***! + \\\\****************************/ +/***/ (() => { + + +;// external \\"externals0\\" + +;// ./bar.js + +__WEBPACK_EXTERNAL_MODULE_externals0_v__; + +/***/ }), + +/***/ \\"./entry.js\\": +/*!******************!*\\\\ + !*** ./entry.js ***! + \\\\******************/ +/***/ ((module, __unused_webpack_exports, __webpack_require__) => { + +module.exports.entry1 = __webpack_require__(/*! ./entry1.js */ \\"./entry1.js\\") +module.exports.entry2 = __webpack_require__(/*! ./entry2.js */ \\"./entry2.js\\") + +/***/ }), + +/***/ \\"./entry1.js\\": +/*!*******************!*\\\\ + !*** ./entry1.js ***! + \\\\*******************/ +/***/ ((module, __unused_webpack_exports, __webpack_require__) => { + +module.exports = __webpack_require__(/*! ./foo */ \\"./foo.js\\") +module.exports.name = \\"entry1\\" + +/***/ }), + +/***/ \\"./entry2.js\\": +/*!*******************!*\\\\ + !*** ./entry2.js ***! + \\\\*******************/ +/***/ ((module, __unused_webpack_exports, __webpack_require__) => { + +module.exports = __webpack_require__(/*! ./bar */ \\"./bar.js\\") +module.exports.name = \\"entry2\\" + +/***/ }), + +/***/ \\"./foo.js\\": +/*!****************************!*\\\\ + !*** ./foo.js + 1 modules ***! + \\\\****************************/ +/***/ (() => { + + +;// external \\"externals1\\" + +;// ./foo.js + +__WEBPACK_EXTERNAL_MODULE_externals1_v__; + +/***/ }) + +/******/ }); +/************************************************************************/ +/******/ // 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; +/******/ } +/******/ +/************************************************************************/ +/******/ +/******/ // startup +/******/ // Load entry module and return exports +/******/ // This entry module used 'module' so it can't be inlined +/******/ var __webpack_exports__ = __webpack_require__(\\"./entry.js\\"); +/******/ const __webpack_exports__entry1 = __webpack_exports__.entry1; +/******/ const __webpack_exports__entry2 = __webpack_exports__.entry2; +/******/ export { __webpack_exports__entry1 as entry1, __webpack_exports__entry2 as entry2, __webpack_exports__ as default }; +/******/ +" +`; + exports[`ConfigTestCases source-map extract-source-map exported tests should extract source map - 1 1`] = ` Array [ "webpack:///./external-source-map.txt", diff --git a/test/configCases/externals/module-import/index.js b/test/configCases/externals/module-import/index.js index 90246878f..583439742 100644 --- a/test/configCases/externals/module-import/index.js +++ b/test/configCases/externals/module-import/index.js @@ -3,7 +3,7 @@ const path = require("path"); it("module-import should correctly get fallback type", function() { const content = fs.readFileSync(path.resolve(__dirname, "a.js"), "utf-8"); - expect(content).toContain(`import { default as default_0 } from "external0"`); // module + expect(content).toContain(`import { default as __WEBPACK_EXTERNAL_MODULE_external0_default__ } from "external0"`); // module expect(content).toContain(`import * as __WEBPACK_EXTERNAL_MODULE_external1__ from "external1"`); // module expect(content).toContain(`module.exports = __WEBPACK_EXTERNAL_createRequire(import.meta.url)("external2")`); // node-commonjs expect(content).toContain(`import * as __WEBPACK_EXTERNAL_MODULE_external3__ from "external3"`); // module diff --git a/test/configCases/library/render-order-issue/bar.js b/test/configCases/library/render-order-issue/bar.js new file mode 100644 index 000000000..1e0580830 --- /dev/null +++ b/test/configCases/library/render-order-issue/bar.js @@ -0,0 +1,2 @@ +import { v } from "externals0"; +v; \ No newline at end of file diff --git a/test/configCases/library/render-order-issue/entry.js b/test/configCases/library/render-order-issue/entry.js new file mode 100644 index 000000000..b15f9ad3c --- /dev/null +++ b/test/configCases/library/render-order-issue/entry.js @@ -0,0 +1,2 @@ +module.exports.entry1 = require('./entry1.js') +module.exports.entry2 = require('./entry2.js') \ No newline at end of file diff --git a/test/configCases/library/render-order-issue/entry1.js b/test/configCases/library/render-order-issue/entry1.js new file mode 100644 index 000000000..7f9697bc4 --- /dev/null +++ b/test/configCases/library/render-order-issue/entry1.js @@ -0,0 +1,2 @@ +module.exports = require('./foo') +module.exports.name = "entry1" \ No newline at end of file diff --git a/test/configCases/library/render-order-issue/entry2.js b/test/configCases/library/render-order-issue/entry2.js new file mode 100644 index 000000000..7232e0516 --- /dev/null +++ b/test/configCases/library/render-order-issue/entry2.js @@ -0,0 +1,2 @@ +module.exports = require('./bar') +module.exports.name = "entry2" \ No newline at end of file diff --git a/test/configCases/library/render-order-issue/foo.js b/test/configCases/library/render-order-issue/foo.js new file mode 100644 index 000000000..d8dcbf742 --- /dev/null +++ b/test/configCases/library/render-order-issue/foo.js @@ -0,0 +1,2 @@ +import { v } from "externals1"; +v; \ No newline at end of file diff --git a/test/configCases/library/render-order-issue/index.js b/test/configCases/library/render-order-issue/index.js new file mode 100644 index 000000000..5ee095b92 --- /dev/null +++ b/test/configCases/library/render-order-issue/index.js @@ -0,0 +1,3 @@ +it('should work', () => { + expect(true).toBe(true); +}); \ No newline at end of file diff --git a/test/configCases/library/render-order-issue/loader.js b/test/configCases/library/render-order-issue/loader.js new file mode 100644 index 000000000..80f631ae3 --- /dev/null +++ b/test/configCases/library/render-order-issue/loader.js @@ -0,0 +1,22 @@ +"use strict"; + +/** @typedef {import("../../../../types").LoaderDefinition} LoaderDefinition */ + +/** + * @type {LoaderDefinition} + */ +module.exports = function loader(code) { + const request = this.resourcePath; + const callback = this.async(); + + // You can simulate an unstable async operation by switching the waiting module + // uncomment this 👇 and run again + // if (request.includes("entry1")) { + if (request.includes("entry2")) { + setTimeout(() => { + callback(null, code); + }, 2000); + } else { + callback(null, code); + } +}; \ No newline at end of file diff --git a/test/configCases/library/render-order-issue/test.config.js b/test/configCases/library/render-order-issue/test.config.js new file mode 100644 index 000000000..9100dc599 --- /dev/null +++ b/test/configCases/library/render-order-issue/test.config.js @@ -0,0 +1,7 @@ +"use strict"; + +module.exports = { + findBundle() { + return ["main.mjs"]; + } +}; diff --git a/test/configCases/library/render-order-issue/webpack.config.js b/test/configCases/library/render-order-issue/webpack.config.js new file mode 100644 index 000000000..b25c397f7 --- /dev/null +++ b/test/configCases/library/render-order-issue/webpack.config.js @@ -0,0 +1,58 @@ +"use strict"; + +/** @type {import("../../../../types").Configuration} */ +module.exports = { + mode: "development", + devtool: false, + optimization: { + minimize: false, + moduleIds: "named", + concatenateModules: true, + usedExports: true + }, + entry: { + main: "./index.js", + entry: "./entry.js" + }, + output: { + clean: true, + filename: "[name].mjs", + library: { + type: "module" + } + }, + externalsType: "module", + externals: ["externals0", "externals1"], + experiments: { + outputModule: true + }, + module: { + rules: [ + { + test: /\.js$/, + loader: "./loader", + sideEffects: true + } + ] + }, + plugins: [ + (compiler) => { + compiler.hooks.compilation.tap( + "testcase", + ( + /** @type {import("../../../../types").Compilation} */ compilation + ) => { + compilation.hooks.afterProcessAssets.tap( + "testcase", + ( + /** @type {Record} */ assets + ) => { + const source = assets["entry.mjs"].source(); + expect(source).toMatchSnapshot(); + } + ); + } + ); + } + ] +}; diff --git a/types.d.ts b/types.d.ts index 734d60c3e..cb9ea1523 100644 --- a/types.d.ts +++ b/types.d.ts @@ -10097,6 +10097,8 @@ declare class ModuleExternalInitFragment extends InitFragment { getImported(): Imported; setImported(imported: Imported): void; getNamespaceIdentifier(): string; + buildIdentifier(ident: string): string; + buildImported(imported: Imported): Imported; static addToSource( source: Source, initFragments: MaybeMergeableInitFragment[],