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

Support Regexp externals #654

Merged
merged 9 commits into from
Mar 21, 2021
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
27 changes: 23 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,30 @@ function ncc (
}
});

const externalMap = new Map();
const externalMap = (() => {
const regexps = [];
const aliasMap = new Map();

function set(key, value) {
if (key instanceof RegExp)
regexps.push(key);
aliasMap.set(key, value);
}

function get(key) {
if (aliasMap.has(key)) return aliasMap.get(key);

const matchedRegex = regexps.find(regex => regex.test(key));
return matchedRegex !== null ? aliasMap.get(matchedRegex) : null;
}

return { get, set };
})();

if (Array.isArray(externals))
externals.forEach(external => externalMap.set(external, external));
else if (typeof externals === 'object')
Object.keys(externals).forEach(external => externalMap.set(external, externals[external]));
Object.keys(externals).forEach(external => externalMap.set(external[0] === '/' && external[external.length - 1] === '/' ? new RegExp(external.slice(1, -1)) : external, externals[external]));

let watcher, watchHandler, rebuildHandler;

Expand Down Expand Up @@ -260,8 +278,9 @@ function ncc (
},
// https://github.com/vercel/ncc/pull/29#pullrequestreview-177152175
node: false,
externals: ({ context, request }, callback) => {
if (externalMap.has(request)) return callback(null, `commonjs ${externalMap.get(request)}`);
externals ({ context, request }, callback) {
const external = externalMap.get(request);
if (external) return callback(null, `commonjs ${external}`);
return callback();
},
module: {
Expand Down
3 changes: 2 additions & 1 deletion test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ for (const unitTest of fs.readdirSync(`${__dirname}/unit`)) {
},
externals: {
'piscina': 'piscina',
'externaltest': 'externalmapped'
'externaltest': 'externalmapped',
'/\\w+-regex/': 'regexexternal',
}
}, opts)).then(
async ({ code, assets, map }) => {
Expand Down
3 changes: 3 additions & 0 deletions test/unit/externals/input.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
const external = require('externaltest');
const regexpExternal = require('external-regex');

console.log(external);
console.log(regexpExternal);

11 changes: 11 additions & 0 deletions test/unit/externals/output-coverage.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@
"use strict";
module.exports = require("externalmapped");

/***/ }),

/***/ 196:
/***/ ((module) => {

"use strict";
module.exports = require("regexexternal");

/***/ })

/******/ });
Expand Down Expand Up @@ -49,8 +57,11 @@ var __webpack_exports__ = {};
// This entry need to be wrapped in an IIFE because it need to be isolated against other modules in the chunk.
(() => {
const external = __nccwpck_require__(306);
const regexpExternal = __nccwpck_require__(196);

console.log(external);
console.log(regexpExternal);


})();

Expand Down
11 changes: 11 additions & 0 deletions test/unit/externals/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@
"use strict";
module.exports = require("externalmapped");

/***/ }),

/***/ 196:
/***/ ((module) => {

"use strict";
module.exports = require("regexexternal");

/***/ })

/******/ });
Expand Down Expand Up @@ -49,8 +57,11 @@ var __webpack_exports__ = {};
// This entry need to be wrapped in an IIFE because it need to be isolated against other modules in the chunk.
(() => {
const external = __nccwpck_require__(306);
const regexpExternal = __nccwpck_require__(196);

console.log(external);
console.log(regexpExternal);


})();

Expand Down