-
Notifications
You must be signed in to change notification settings - Fork 34
CJS interop? #12
Comments
I bring this up because, whew boy are we in enough of the weeds due to incompatibilities of existing CJS interop w/ babel and ESM |
Example requested by https://twitter.com/dan_abramov/status/806506649544491008 This plugin maps
CJS interop with ESM cannot create a ModuleNamespaceObject that can proxy to all possible shapes of Due to this all proposals for interop starting with the original EP have used wrapping mechanics to produce ModuleNamespaceObjects. The exact behavior of wrapping mechanics is still in discussion from talks with VM implementors and TC39 started in September. However, all proposals require the wrapping mechanics to provide a A good example of breakage should people expect exact mapping to (await import('fs')).readFile; // may not work depending on wrapping mechanics However, if we use the (await import('fs')).default.readFile; // will work in all proposals To note: this is a babel plugin and should detect non-CJS using |
ah, conversely in the current code: (await import('fs')).default.readFile; Does not work, since it doesn't shadow nor wrap |
Sounds like migrating to that is going to be particularly painful for TypeScript... it maps commonjs module.exports directly to the namespace, and requires that you do fun things such as call a namespace import as a function if the commonjs exports was a function, etc... I think that solution will be a good thing (you can't statically verify the imported symbols otherwise), but sounds like it'll take time 😅 |
@Kovensky I've had talks w/ typescript in the past about this, they are aware. This behavior also affects |
I had actually expected implementers of a dynamic import plugin for webpack to actually map to System.import, which on webpack's implementation should already have the wrap-in-default behavior for non-ESM (...I think). I presume this went with require.ensure for webpack 1 compatibility... |
Yes the import() we have implemented is more similar to System.import |
@Kovensky just in case you were unaware, TypeScript has a flag to model the synthesis of a default export from a CommonJS module. |
@bmeck at the moment I'm operating under the assumption/belief/hope that it will indeed allow named property access. Once the interop story gets more concrete, we can ship a breaking change to this transform that implements the proper semantics. |
@ljharb can we at least properly set the |
With the way it's implemented right now, won't Babel do the correctish thing wrt default and named exports? |
@ljharb no, you need something like: (new Promise((resolve) => {
require.ensure([], (require) => {
var ns = require(SOURCE);
if (ns && ns.__esModule) {
resolve(ns);
} else {
var wrapper = Object.create(ns);
wrapper.__esModule = true;
wrapper.default = ns;
Object.freeze(wrapper);
resolve(wrapper);
}
});
})) to get close-ish |
Right, but if the |
@ljharb existing shim in babel below: function _interopRequireWildcard(obj) {
if (obj && obj.__esModule) { return obj; }
else {
var newObj = {};
if (obj != null) {
for (var key in obj) {
if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key];
}
}
newObj.default = obj; return newObj;
}
}
|
@bmeck How does something like this sound? function _specInteropRequireWildcard(obj) {
if (obj && obj.__esModule) {
return obj;
} else {
var newObj = Object.create
? Object.create(null, {
default: {
value: obj,
writable: true,
enumerable: true
},
__esModule: {
value: true
}
})
: {
default: obj,
__esModule: true
};
if (typeof Symbol !== "undefined" && Symbol.toStringTag) {
Object.defineProperty(newObj, Symbol.toStringTag, { value: "Module" });
}
return (Object.freeze || Object)(newObj);
}
} I've been discussing with @loganfsmyth on slack about implementing a possible |
@Kovensky sounds good. eventually would be interested in test262 being brought into all this to see % compliance for babel: TEST_FILES="$(find test262/test -name \*.js)"
mkdir compiled-test262
for FILE in $TEST_FILES; do
babel -o compiled-$FILE < $FILE
done
// with .babelrc for transform-es2015-modules-commonjs
for FILE in $TEST_FILES; do
node --require add-assert-global.js compiled-$FILE
done but not really going to push that right now since it just checks the runtime behavior of ModuleNamespace, not the actual interop properties. |
@aluanhaddad does enabling |
@Kovensky // test.ts
import {version} from 'express';
import express from 'express';
import * as expressNS from 'express';
import expressAssign = require('express');
console.log(version);
express();
console.log(expressNS.version);
console.log(expressAssign.version);
export default function defaultExport() {}
export let letExport = 0; // out.js
"use strict";
var express_1 = require("express");
var express_2 = require("express");
var expressNS = require("express");
var expressAssign = require("express");
console.log(express_1.version);
express_2["default"]();
console.log(expressNS.version);
console.log(expressAssign.version);
function defaultExport() { }
exports.__esModule = true;
exports["default"] = defaultExport;
exports.letExport = 0; {
"compilerOptions": {
"module": "commonjs",
"allowSyntheticDefaultImports": false,
"moduleResolution": "node"
}
} I actually don't see any impact of swapping |
The TypeScript defaults |
To my understanding this will load CJS files and have named property access on them? There is ongoing discussion if this is going to be implemented or not. I am hesitant to encourage named properties working. In all proposed interopoperability scenarios however, there is a
default
export that maps directly tomodule.exports
. Something to bear in mind, and perhaps remove named imports until interop is ironed out.The text was updated successfully, but these errors were encountered: