Skip to content

Commit

Permalink
Support Regexp externals (#654)
Browse files Browse the repository at this point in the history
* regex

* whitespaces

* flip regex in object form

* fixup async

* fixup unit tests

* fixup tests

* fixup test skip

Co-authored-by: Guy Bedford <guybedford@gmail.com>
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
  • Loading branch information
3 people committed Mar 21, 2021
1 parent 8aa2a42 commit dd68f72
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 5 deletions.
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

0 comments on commit dd68f72

Please sign in to comment.