Skip to content

Commit

Permalink
Support .js imports from typescript (#510)
Browse files Browse the repository at this point in the history
This resolves #508 in allowing the pattern for TypeScript code using `.js` extensions to reference other TypeScript files.

This is currently recommended by the TypeScript team (microsoft/TypeScript#16577 (comment)) as an approach for writing TypeScript that will include the file extensions in the output files when using `tsc` manually, resulting in Node.js and browser compatibility for the output.

With this PR, ncc doesn't have to break enabling this workflow for users, while remaining backwards compatible with the previous behaviour so it would be fine to release this as a minor.
  • Loading branch information
guybedford authored Feb 21, 2020
1 parent 9a29955 commit e3e34b5
Show file tree
Hide file tree
Showing 7 changed files with 187 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,15 @@ module.exports = (
if (!err) return callback(null, result);
if (!err.missing || !err.missing.length)
return callback(err);
// Allow .js resolutions to .tsx? from .tsx?
if (request.endsWith('.js') && context.issuer && (context.issuer.endsWith('.ts') || context.issuer.endsWith('.tsx')))
return resolve.call(resolver, context, path, request.slice(0, -3), resolveContext, function (err, result) {
if (!err) return callback(null, result);
if (!err.missing || !err.missing.length)
return callback(err);
// make not found errors runtime errors
callback(null, __dirname + '/@@notfound.js' + '?' + (externalMap.get(request) || request));
});
// make not found errors runtime errors
callback(null, __dirname + '/@@notfound.js' + '?' + (externalMap.get(request) || request));
});
Expand Down
1 change: 1 addition & 0 deletions test/unit/ts-exts/dep-dep.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default {};
2 changes: 2 additions & 0 deletions test/unit/ts-exts/dep.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { default } from './dep-dep.js';

3 changes: 3 additions & 0 deletions test/unit/ts-exts/input.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import dep from './dep.js';

console.log(dep);
81 changes: 81 additions & 0 deletions test/unit/ts-exts/output-coverage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
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;
/******/ }
/******/
/******/
/******/ __webpack_require__.ab = __dirname + "/";
/******/
/******/ // the startup function
/******/ function startup() {
/******/ // Load entry module and return exports
/******/ return __webpack_require__(368);
/******/ };
/******/
/******/ // run startup
/******/ return startup();
/******/ })
/************************************************************************/
/******/ ({

/***/ 43:
/***/ (function(__unusedmodule, exports, __webpack_require__) {

"use strict";

exports.__esModule = true;
var dep_dep_js_1 = __webpack_require__(119);
exports["default"] = dep_dep_js_1["default"];


/***/ }),

/***/ 119:
/***/ (function(__unusedmodule, exports) {

"use strict";

exports.__esModule = true;
exports["default"] = {};


/***/ }),

/***/ 368:
/***/ (function(__unusedmodule, exports, __webpack_require__) {

"use strict";

exports.__esModule = true;
var dep_js_1 = __webpack_require__(43);
console.log(dep_js_1["default"]);


/***/ })

/******/ });
81 changes: 81 additions & 0 deletions test/unit/ts-exts/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
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;
/******/ }
/******/
/******/
/******/ __webpack_require__.ab = __dirname + "/";
/******/
/******/ // the startup function
/******/ function startup() {
/******/ // Load entry module and return exports
/******/ return __webpack_require__(578);
/******/ };
/******/
/******/ // run startup
/******/ return startup();
/******/ })
/************************************************************************/
/******/ ({

/***/ 578:
/***/ (function(__unusedmodule, exports, __webpack_require__) {

"use strict";

exports.__esModule = true;
var dep_js_1 = __webpack_require__(975);
console.log(dep_js_1["default"]);


/***/ }),

/***/ 668:
/***/ (function(__unusedmodule, exports) {

"use strict";

exports.__esModule = true;
exports["default"] = {};


/***/ }),

/***/ 975:
/***/ (function(__unusedmodule, exports, __webpack_require__) {

"use strict";

exports.__esModule = true;
var dep_dep_js_1 = __webpack_require__(668);
exports["default"] = dep_dep_js_1["default"];


/***/ })

/******/ });
10 changes: 10 additions & 0 deletions test/unit/ts-exts/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"compilerOptions": {
"module": "commonjs",
"moduleResolution": "node",
"baseUrl": ".",
"paths": {
"@*": ["./*"]
}
}
}

0 comments on commit e3e34b5

Please sign in to comment.