-
-
Notifications
You must be signed in to change notification settings - Fork 32
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
Missing __importStart and __importDefault calls when synthetic is required #113
Comments
Hi there. I think a little explainer is needed first: You might already know this, but internally, Rollup builds up a module graph of ES modules and requires everything to be modules. Plugins like And, given that under these circumstances, TypeScript is a client of Rollup, it is Rollup - not TypeScript - that is the decider of which module format(s) to target based on your Rollup config. Because of this, no matter what module format you've decided to target in your That is why you are experiencing differences in behavior between That said, I'm actually somewhat surprised to learn that you're using Rollup v2.26.5, because since v2.24.0 Rollup has made interoperability with CommonJS a little more flexible and arguably better, and here's the output I get from Rollup v2.26.5 with no customizations to the 'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
function _interopNamespace(e) {
if (e && e.__esModule) { return e; } else {
var n = Object.create(null);
if (e) {
Object.keys(e).forEach(function (k) {
if (k !== 'default') {
var d = Object.getOwnPropertyDescriptor(e, k);
Object.defineProperty(n, k, d.get ? d : {
enumerable: true,
get: function () {
return e[k];
}
});
}
});
}
n['default'] = e;
return Object.freeze(n);
}
}
async function importFoo(path) {
const bindings = await Promise.resolve().then(function () { return /*#__PURE__*/_interopNamespace(require('bindings')); });
const addon = bindings.default('addon');
return addon;
} I think this should be documented, so I've added an explainer for this in the README. I hope it makes sense |
Thank you @wessberg , The explanation is very clear. I got it now, I thought that was maybe the plugin was changing the result and the behavior of typescript when esModuleInterop is specified. After your explanation, I found the issue. By mistake in some packages, the building scripts were setting the output.interop variable to 'false'. |
tsc
(if applicable): YesSome CommonJS modules are developed exporting function or other objects as export.default (for example 'bindings'). Typescript synthesize a 'default' property to make it compliant with es6 module specification: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-7.html#support-for-import-d-from-cjs-from-commonjs-modules-with---esmoduleinterop
For example:
generates this using 'tsc':
However, this rollup plugin is generating this:
Expected Behavior
The esModuleInterop functions (__importStar and __importDefault) are preserved and default import are possible without workaround or direct invocation to 'require' function.
Actual Behavior
This produces a runtime error due to the fact that 'default' is not defined. It's defined in the code generated by 'tsc' in the call to
tslib_1.__importStar
.The text was updated successfully, but these errors were encountered: