From e768650cb582c48bb655e35236e616d70224f575 Mon Sep 17 00:00:00 2001 From: Soon Date: Thu, 2 Jan 2025 18:29:19 +0800 Subject: [PATCH] fix(modern-module): skip the asset entry module concaten for Rslib (#8837) * chore: update * chore: update * chore: update snap --- .../src/modern_module_library_plugin.rs | 13 ++++- .../tests/__snapshots__/Config.test.js.snap | 45 ++++++++++++++++++ .../modern-module-force-concaten/h/file.png | Bin 0 -> 95 bytes .../rspack.config.js | 12 ++++- 4 files changed, 68 insertions(+), 2 deletions(-) create mode 100644 packages/rspack-test-tools/tests/configCases/library/modern-module-force-concaten/h/file.png diff --git a/crates/rspack_plugin_library/src/modern_module_library_plugin.rs b/crates/rspack_plugin_library/src/modern_module_library_plugin.rs index f539922abd4..5f3aaa00bce 100644 --- a/crates/rspack_plugin_library/src/modern_module_library_plugin.rs +++ b/crates/rspack_plugin_library/src/modern_module_library_plugin.rs @@ -7,7 +7,7 @@ use rspack_core::{ CodeGenerationExportsFinalNames, Compilation, CompilationFinishModules, CompilationOptimizeChunkModules, CompilationParams, CompilerCompilation, CompilerOptions, ConcatenatedModule, ConcatenatedModuleExportsDefinitions, DependenciesBlock, Dependency, - DependencyId, LibraryOptions, ModuleGraph, ModuleIdentifier, Plugin, PluginContext, + DependencyId, LibraryOptions, ModuleGraph, ModuleIdentifier, Plugin, PluginContext, SourceType, }; use rspack_error::{error_bail, Result}; use rspack_hash::RspackHash; @@ -113,6 +113,17 @@ impl ModernModuleLibraryPlugin { .collect::>(); for module_id in unconcatenated_module_ids.into_iter() { + // skip the asset module when the entry is asset module + let module_graph = compilation.get_module_graph(); + let Some(module) = module_graph.module_by_identifier(module_id) else { + continue; + }; + let source_types = module.source_types(); + let is_asset_module = source_types.contains(&SourceType::Asset); + if is_asset_module { + continue; + } + let chunk_runtime = compilation .chunk_graph .get_module_runtimes(*module_id, &compilation.chunk_by_ukey) diff --git a/packages/rspack-test-tools/tests/__snapshots__/Config.test.js.snap b/packages/rspack-test-tools/tests/__snapshots__/Config.test.js.snap index 4bb73741011..8b80ffe3bde 100644 --- a/packages/rspack-test-tools/tests/__snapshots__/Config.test.js.snap +++ b/packages/rspack-test-tools/tests/__snapshots__/Config.test.js.snap @@ -501,6 +501,51 @@ const a = 'a' export { a }; `; +exports[`config config/library/modern-module-force-concaten step should pass: asset as entry should not be concatenated 1`] = ` +var __webpack_modules__ = ({ +"273": (function (module, __unused_webpack_exports, __webpack_require__) { +module.exports = __webpack_require__.p + "fc90103f80b6abc6.png"; + +}), + +}); +/************************************************************************/ +// 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] = { +exports: {} +}); +// Execute the module function +__webpack_modules__[moduleId](module, module.exports, __webpack_require__); + +// Return the exports of the module +return module.exports; + +} + +/************************************************************************/ +// webpack/runtime/public_path +(() => { +__webpack_require__.p = ""; + +})(); +/************************************************************************/ +// startup +// Load entry module and return exports +// This entry module doesn't tell about it's top-level declarations so it can't be inlined +var __webpack_exports__ = __webpack_require__("273"); +`; + exports[`config config/library/modern-module-force-concaten step should pass: external module should bail out when bundling 1`] = ` import { createRequire as __WEBPACK_EXTERNAL_createRequire } from "node:module"; var __webpack_modules__ = ({ diff --git a/packages/rspack-test-tools/tests/configCases/library/modern-module-force-concaten/h/file.png b/packages/rspack-test-tools/tests/configCases/library/modern-module-force-concaten/h/file.png new file mode 100644 index 0000000000000000000000000000000000000000..1914264c08781d1f30ee0b8482bccf44586f2dc1 GIT binary patch literal 95 zcmeAS@N?(olHy`uVBq!ia0vp^j3CU&3?x-=hn)ga%mF?ju0VQumF+E%TuG2$FoVOh l8)-lem#2$k2*>s01R$Gz9%CSj!PC{xWt~$(697H@6ZHT9 literal 0 HcmV?d00001 diff --git a/packages/rspack-test-tools/tests/configCases/library/modern-module-force-concaten/rspack.config.js b/packages/rspack-test-tools/tests/configCases/library/modern-module-force-concaten/rspack.config.js index 4fd8d01b26a..39d98d250c4 100644 --- a/packages/rspack-test-tools/tests/configCases/library/modern-module-force-concaten/rspack.config.js +++ b/packages/rspack-test-tools/tests/configCases/library/modern-module-force-concaten/rspack.config.js @@ -7,7 +7,16 @@ module.exports = { "d": "./d.mjs", "e": "./e/index.js", "f": "./f/index.js", - "g": "./g/index.js" + "g": "./g/index.js", + "h": "./h/file.png", + }, + module: { + rules: [ + { + test: /\.png$/, + type: "asset/resource", + } + ] }, externals: { path: 'node-commonjs path', @@ -42,6 +51,7 @@ module.exports = { expect(assets['e.js']._value).toMatchSnapshot(".cjs should bail out when bundling"); expect(assets['f.js']._value).toMatchSnapshot("external module should bail out when bundling"); expect(assets['g.js']._value).toMatchSnapshot("harmony export should concat, even with bailout reason"); + expect(assets['h.js']._value).toMatchSnapshot("asset as entry should not be concatenated"); }); }; this.hooks.compilation.tap("testcase", handler);