diff --git a/crates/rspack_plugin_mf/src/sharing/consumesCommon.js b/crates/rspack_plugin_mf/src/sharing/consumesCommon.js index 4d2a7a6b1fb6..e7d03bb6696c 100644 --- a/crates/rspack_plugin_mf/src/sharing/consumesCommon.js +++ b/crates/rspack_plugin_mf/src/sharing/consumesCommon.js @@ -107,7 +107,16 @@ var parseRange = function(str) { if (items.length === 1) { const items = str .trim() - .split(/(?<=[-0-9A-Za-z])\s+/g) + .split('').reduce((pre, cur) => { + const lastIdx = pre.length-1; + const t = pre[lastIdx] || ''; + if(/\s/.test(cur) && /[-0-9A-Za-z]/.test(t[t.length-1])){ + pre.push('') + }else{ + pre[lastIdx] = t+cur + } + return pre + }, ['']).filter(t=>t) .map(parseSimple); return combine(items, 2); } diff --git a/tests/webpack-test/ConsumesCommon.test.js b/tests/webpack-test/ConsumesCommon.test.js new file mode 100644 index 000000000000..00abc001222b --- /dev/null +++ b/tests/webpack-test/ConsumesCommon.test.js @@ -0,0 +1,38 @@ +"use strict"; + +// refactor: replace Regex in consumesCommon.js +// Original source code str.trim().split(/(?<=[-0-9A-Za-z])\s+/g); + +const orgFunc = (str) => str.trim().split(/(?<=[-0-9A-Za-z])\s+/g); + +const newFunc = (str) => str.trim().split('').reduce((pre, cur) => { + const lastIdx = pre.length-1; + const t = pre[lastIdx] || ''; + if(/\s/.test(cur) && /[-0-9A-Za-z]/.test(t[t.length-1])){ + pre.push('') + }else{ + pre[lastIdx] = t+cur + } + return pre +}, ['']).filter(t=>t) + +describe("ConsumesCommonRegex", () => { + const cases = [ + "5 || 6 || 7.x.x", + "1 - 2", + "=3", + "=3.0", + "^3.4", + "3.4 - 6.5", + "<=3.4", + ">3.4", + "1.2.3-alpha.x.x", + "1.2.3-NaN" + ]; + for(let i=0; i { + expect(newFunc(str)).toStrictEqual(orgFunc(str)); + }); + } +}); diff --git a/tests/webpack-test/jest.config.js b/tests/webpack-test/jest.config.js index ccc46e29371e..dbaf7151de41 100644 --- a/tests/webpack-test/jest.config.js +++ b/tests/webpack-test/jest.config.js @@ -21,6 +21,7 @@ module.exports = { "/Compiler.test.js", // "/ConfigCacheTestCases.longtest.js", "/ConfigTestCases.basictest.js", + "/ConsumesCommon.test.js", // "/ContextModule.unittest.js", // "/ContextModuleFactory.unittest.js", // "/Defaults.unittest.js",