Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle Windows style path in generateWildcardRequire #153

Merged
merged 2 commits into from Mar 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 10 additions & 6 deletions src/asset-relocator.js
Expand Up @@ -285,25 +285,29 @@ 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)
return;

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 ';
Expand Down
5 changes: 5 additions & 0 deletions 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);
1 change: 1 addition & 0 deletions test/unit/wildcard-require2/modules/path1/index.js
@@ -0,0 +1 @@
module.exports = 'module1';
1 change: 1 addition & 0 deletions test/unit/wildcard-require2/modules/path2/index.js
@@ -0,0 +1 @@
module.exports = 'module2';
1 change: 1 addition & 0 deletions test/unit/wildcard-require2/modules/path3/index.js
@@ -0,0 +1 @@
module.exports = 'module3';
75 changes: 75 additions & 0 deletions 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__;
/******/ })()
;
75 changes: 75 additions & 0 deletions 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__;
/******/ })()
;