Skip to content

Commit

Permalink
Externals mapping support (#402)
Browse files Browse the repository at this point in the history
  • Loading branch information
guybedford authored May 24, 2019
1 parent f0b0911 commit 31448a0
Show file tree
Hide file tree
Showing 10 changed files with 147 additions and 67 deletions.
10 changes: 1 addition & 9 deletions src/@@notfound.js
Original file line number Diff line number Diff line change
@@ -1,9 +1 @@
const id = 'UNKNOWN';
if (id.startsWith('./') || id.startsWith('../')) {
const e = new Error('Cannot find module "' + id + '".');
e.code = 'MODULE_NOT_FOUND';
throw e;
}
else {
__non_webpack_require__(id);
}
__non_webpack_require__('UNKNOWN');
11 changes: 8 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,18 @@ module.exports = (
if (!err.missing || !err.missing.length)
return callback(err);
// make not found errors runtime errors
callback(null, __dirname + '/@@notfound.js' + '?' + request);
callback(null, __dirname + '/@@notfound.js' + '?' + (externalMap.get(request) || request));
});
};
}
});

const externalSet = new Set(externals);
const externalMap = new Map();

if (externals instanceof Array)
externals.forEach(external => externalMap.set(external, external));
else if (typeof externals === 'object')
Object.keys(externals).forEach(external => externalMap.set(external, externals[external]));

let watcher, watchHandler, rebuildHandler;

Expand Down Expand Up @@ -131,7 +136,7 @@ module.exports = (
// https://github.com/zeit/ncc/pull/29#pullrequestreview-177152175
node: false,
externals: async ({ context, request }, callback) => {
if (externalSet.has(request)) return callback(null, `commonjs ${request}`);
if (externalMap.has(request)) return callback(null, `commonjs ${externalMap.get(request)}`);
return callback();
},
module: {
Expand Down
6 changes: 5 additions & 1 deletion test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ for (const unitTest of fs.readdirSync(`${__dirname}/unit`)) {
process.env.TS_NODE_PROJECT = `${testDir}/tsconfig.json`;
// find the name of the input file (e.g input.ts)
const inputFile = fs.readdirSync(testDir).find(file => file.includes("input"));
await ncc(`${testDir}/${inputFile}`).then(
await ncc(`${testDir}/${inputFile}`, {
externals: {
'externaltest': 'externalmapped'
}
}).then(
async ({ code, assets }) => {
const actual = code
.trim()
Expand Down
3 changes: 3 additions & 0 deletions test/unit/externals/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const external = require('externaltest');

console.log(external);
62 changes: 62 additions & 0 deletions test/unit/externals/output-coverage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
module.exports =
/******/ (function(modules, runtime) { // webpackBootstrap
/******/ "use strict";
/******/ // The module cache
/******/ var installedModules = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId]) {
/******/ return installedModules[moduleId].exports;
/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ i: moduleId,
/******/ l: false,
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/ // Flag the module as loaded
/******/ module.l = true;
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/******/
/******/
/******/ // the startup function
/******/ function startup() {
/******/ // Load entry module and return exports
/******/ return __webpack_require__(613);
/******/ };
/******/
/******/ // run startup
/******/ return startup();
/******/ })
/************************************************************************/
/******/ ({

/***/ 306:
/***/ (function(module) {

module.exports = require("externalmapped");

/***/ }),

/***/ 613:
/***/ (function(__unusedmodule, __unusedexports, __webpack_require__) {

const external = __webpack_require__(306);

console.log(external);


/***/ })

/******/ });
62 changes: 62 additions & 0 deletions test/unit/externals/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
module.exports =
/******/ (function(modules, runtime) { // webpackBootstrap
/******/ "use strict";
/******/ // The module cache
/******/ var installedModules = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId]) {
/******/ return installedModules[moduleId].exports;
/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ i: moduleId,
/******/ l: false,
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/ // Flag the module as loaded
/******/ module.l = true;
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/******/
/******/
/******/ // the startup function
/******/ function startup() {
/******/ // Load entry module and return exports
/******/ return __webpack_require__(586);
/******/ };
/******/
/******/ // run startup
/******/ return startup();
/******/ })
/************************************************************************/
/******/ ({

/***/ 306:
/***/ (function(module) {

module.exports = require("externalmapped");

/***/ }),

/***/ 586:
/***/ (function(__unusedmodule, __unusedexports, __webpack_require__) {

const external = __webpack_require__(306);

console.log(external);


/***/ })

/******/ });
20 changes: 2 additions & 18 deletions test/unit/runtime-notfound/output-coverage.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,31 +53,15 @@ __webpack_require__(548);
/***/ 548:
/***/ (function() {

const id = "./not-foud2.js";
if (id.startsWith('./') || id.startsWith('../')) {
const e = new Error('Cannot find module "' + id + '".');
e.code = 'MODULE_NOT_FOUND';
throw e;
}
else {
eval("require")(id);
}
eval("require")("./not-foud2.js");


/***/ }),

/***/ 590:
/***/ (function() {

const id = "./not-found.js";
if (id.startsWith('./') || id.startsWith('../')) {
const e = new Error('Cannot find module "' + id + '".');
e.code = 'MODULE_NOT_FOUND';
throw e;
}
else {
eval("require")(id);
}
eval("require")("./not-found.js");


/***/ })
Expand Down
20 changes: 2 additions & 18 deletions test/unit/runtime-notfound/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,7 @@ module.exports =
/***/ 663:
/***/ (function() {

const id = "./not-foud2.js";
if (id.startsWith('./') || id.startsWith('../')) {
const e = new Error('Cannot find module "' + id + '".');
e.code = 'MODULE_NOT_FOUND';
throw e;
}
else {
eval("require")(id);
}
eval("require")("./not-foud2.js");


/***/ }),
Expand All @@ -69,15 +61,7 @@ __webpack_require__(663);
/***/ 925:
/***/ (function() {

const id = "./not-found.js";
if (id.startsWith('./') || id.startsWith('../')) {
const e = new Error('Cannot find module "' + id + '".');
e.code = 'MODULE_NOT_FOUND';
throw e;
}
else {
eval("require")(id);
}
eval("require")("./not-found.js");


/***/ })
Expand Down
10 changes: 1 addition & 9 deletions test/unit/tsconfig-paths-conflicting-external/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,7 @@ console.log(_module_1["default"]);
/***/ 545:
/***/ (function() {

const id = "@module";
if (id.startsWith('./') || id.startsWith('../')) {
const e = new Error('Cannot find module "' + id + '".');
e.code = 'MODULE_NOT_FOUND';
throw e;
}
else {
eval("require")(id);
}
eval("require")("@module");


/***/ })
Expand Down
10 changes: 1 addition & 9 deletions test/unit/tsconfig-paths/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,7 @@ console.log(_module_1["default"]);
/***/ 545:
/***/ (function() {

const id = "@module";
if (id.startsWith('./') || id.startsWith('../')) {
const e = new Error('Cannot find module "' + id + '".');
e.code = 'MODULE_NOT_FOUND';
throw e;
}
else {
eval("require")(id);
}
eval("require")("@module");


/***/ })
Expand Down

0 comments on commit 31448a0

Please sign in to comment.