From 48d369263dcc5818db6169993b7525ebe813ae9b Mon Sep 17 00:00:00 2001 From: Yichi Yang Date: Fri, 18 Mar 2022 20:15:50 -0700 Subject: [PATCH 1/2] fix: handle Windows style path in generateWildcardRequire --- src/asset-relocator.js | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/asset-relocator.js b/src/asset-relocator.js index 631ab1b..0c2c740 100644 --- a/src/asset-relocator.js +++ b/src/asset-relocator.js @@ -285,17 +285,21 @@ const BOUND_REQUIRE = Symbol(); function generateWildcardRequire(dir, wildcardPath, wildcardParam, wildcardBlocks, log) { const wildcardBlockIndex = wildcardBlocks.length; - const trailingWildcard = wildcardPath.endsWith(WILDCARD); - const wildcardIndex = wildcardPath.indexOf(WILDCARD); + const wildcardPathNormalized = wildcardPath.split(path.sep).join(path.posix.sep) + const dirNormalized = dir.split(path.sep).join(path.posix.sep) - const wildcardPrefix = wildcardPath.substr(0, wildcardIndex); - const wildcardSuffix = wildcardPath.substr(wildcardIndex + 1); + const trailingWildcard = wildcardPathNormalized.endsWith(WILDCARD); + + const wildcardIndex = wildcardPathNormalized.indexOf(WILDCARD); + + const wildcardPrefix = wildcardPathNormalized.substr(0, wildcardIndex); + const wildcardSuffix = wildcardPathNormalized.substr(wildcardIndex + 1); const endPattern = wildcardSuffix ? '?(.@(js|json|node))' : '.@(js|json|node)'; // sync to support no emission case if (log) - console.log('Generating wildcard requires for ' + wildcardPath.replace(WILDCARD, '*')); + console.log('Generating wildcard requires for ' + wildcardPathNormalized.replace(WILDCARD, '*')); let options = glob.sync(wildcardPrefix + '**' + wildcardSuffix + endPattern, { mark: true, ignore: 'node_modules/**/*' }); if (!options.length) @@ -303,7 +307,7 @@ function generateWildcardRequire(dir, wildcardPath, wildcardParam, wildcardBlock const optionConditions = options.map((file, index) => { const arg = JSON.stringify(file.substring(wildcardPrefix.length, file.lastIndexOf(wildcardSuffix))); - let relPath = path.relative(dir, file).replace(/\\/g, '/'); + let relPath = path.posix.relative(dirNormalized, file); if (!relPath.startsWith('../')) relPath = './' + relPath; let condition = index === 0 ? ' ' : ' else '; From a7225b8cfde91b70695c9526709078b3a2c9372d Mon Sep 17 00:00:00 2001 From: Yichi Yang Date: Mon, 21 Mar 2022 20:06:11 -0700 Subject: [PATCH 2/2] test: add unit test to cover #154 --- test/unit/wildcard-require2/input.js | 5 ++ .../wildcard-require2/modules/path1/index.js | 1 + .../wildcard-require2/modules/path2/index.js | 1 + .../wildcard-require2/modules/path3/index.js | 1 + .../unit/wildcard-require2/output-coverage.js | 75 +++++++++++++++++++ test/unit/wildcard-require2/output.js | 75 +++++++++++++++++++ 6 files changed, 158 insertions(+) create mode 100644 test/unit/wildcard-require2/input.js create mode 100644 test/unit/wildcard-require2/modules/path1/index.js create mode 100644 test/unit/wildcard-require2/modules/path2/index.js create mode 100644 test/unit/wildcard-require2/modules/path3/index.js create mode 100644 test/unit/wildcard-require2/output-coverage.js create mode 100644 test/unit/wildcard-require2/output.js diff --git a/test/unit/wildcard-require2/input.js b/test/unit/wildcard-require2/input.js new file mode 100644 index 0000000..f815f25 --- /dev/null +++ b/test/unit/wildcard-require2/input.js @@ -0,0 +1,5 @@ +const num = Math.ceil(Math.random() * 3, 0); + +const path = `path${num}`; +const m = require(`./modules/${path}/index.js`); +console.log(m); diff --git a/test/unit/wildcard-require2/modules/path1/index.js b/test/unit/wildcard-require2/modules/path1/index.js new file mode 100644 index 0000000..aec857f --- /dev/null +++ b/test/unit/wildcard-require2/modules/path1/index.js @@ -0,0 +1 @@ +module.exports = 'module1'; \ No newline at end of file diff --git a/test/unit/wildcard-require2/modules/path2/index.js b/test/unit/wildcard-require2/modules/path2/index.js new file mode 100644 index 0000000..5c5b13f --- /dev/null +++ b/test/unit/wildcard-require2/modules/path2/index.js @@ -0,0 +1 @@ +module.exports = 'module2'; diff --git a/test/unit/wildcard-require2/modules/path3/index.js b/test/unit/wildcard-require2/modules/path3/index.js new file mode 100644 index 0000000..f5c0d20 --- /dev/null +++ b/test/unit/wildcard-require2/modules/path3/index.js @@ -0,0 +1 @@ +module.exports = 'module3'; diff --git a/test/unit/wildcard-require2/output-coverage.js b/test/unit/wildcard-require2/output-coverage.js new file mode 100644 index 0000000..34da1a9 --- /dev/null +++ b/test/unit/wildcard-require2/output-coverage.js @@ -0,0 +1,75 @@ +/******/ (() => { // webpackBootstrap +/******/ var __webpack_modules__ = ({ + +/***/ 52: +/***/ ((module) => { + +module.exports = 'module1'; + +/***/ }), + +/***/ 139: +/***/ ((module) => { + +module.exports = 'module2'; + + +/***/ }), + +/***/ 446: +/***/ ((module) => { + +module.exports = 'module3'; + + +/***/ }) + +/******/ }); +/************************************************************************/ +/******/ // 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/compat */ +/******/ +/******/ if (typeof __webpack_require__ !== 'undefined') __webpack_require__.ab = __dirname + "/";/************************************************************************/ +var __webpack_exports__ = {}; +// This entry need to be wrapped in an IIFE because it need to be isolated against other modules in the chunk. +(() => { +function __ncc_wildcard$0 (arg) { + if (arg === "path1") return __webpack_require__(52); + else if (arg === "path2") return __webpack_require__(139); + else if (arg === "path3") return __webpack_require__(446); +} +const num = Math.ceil(Math.random() * 3, 0); + +const path = `path${num}`; +const m = __ncc_wildcard$0(path); +console.log(m); + +})(); + +module.exports = __webpack_exports__; +/******/ })() +; \ No newline at end of file diff --git a/test/unit/wildcard-require2/output.js b/test/unit/wildcard-require2/output.js new file mode 100644 index 0000000..58f4506 --- /dev/null +++ b/test/unit/wildcard-require2/output.js @@ -0,0 +1,75 @@ +/******/ (() => { // webpackBootstrap +/******/ var __webpack_modules__ = ({ + +/***/ 805: +/***/ ((module) => { + +module.exports = 'module1'; + +/***/ }), + +/***/ 387: +/***/ ((module) => { + +module.exports = 'module2'; + + +/***/ }), + +/***/ 455: +/***/ ((module) => { + +module.exports = 'module3'; + + +/***/ }) + +/******/ }); +/************************************************************************/ +/******/ // 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/compat */ +/******/ +/******/ if (typeof __webpack_require__ !== 'undefined') __webpack_require__.ab = __dirname + "/";/************************************************************************/ +var __webpack_exports__ = {}; +// This entry need to be wrapped in an IIFE because it need to be isolated against other modules in the chunk. +(() => { +function __ncc_wildcard$0 (arg) { + if (arg === "path1") return __webpack_require__(805); + else if (arg === "path2") return __webpack_require__(387); + else if (arg === "path3") return __webpack_require__(455); +} +const num = Math.ceil(Math.random() * 3, 0); + +const path = `path${num}`; +const m = __ncc_wildcard$0(path); +console.log(m); + +})(); + +module.exports = __webpack_exports__; +/******/ })() +; \ No newline at end of file