-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
without __esModule property when format esm module cause load module error #1323
Comments
I am also experiencing the same problem |
Looking forward esbuild to solving this problem。 |
Any workaround ? |
Maybe this isn't esbuild bug. No matter TypeScript compiler or babel compiler, any esm output wouldn't create In your above code, esbuild output esm product, so it didn't included |
Babel and tsc will generate function _interopRequireDefault(obj) {
// 判断传入的对象是 ESModule 还是 CommonJS 模块
return obj && obj.__esModule // 这里就是之前说的为什么要加上 `__esModule` 属性
? obj // 如果是 ESModule,就直接返回传入的模块对象
: { 'default': obj }; // 如果不是 ESModule,则将传入的 `module.exports` 放置在一个新对象 `default` 上,来模拟 ESModule
} It cause an error, after vite use esbuild optimize var _vue = (init_vue_runtime_esm(), vue_runtime_esm_exports);
var _vue2 = _interopRequireDefault(_vue);
function _interopRequireDefault(obj) {
return obj && obj.__esModule ? obj : {default: obj};
} |
I said output syntax is es module, __esModule should be in commonjs product, not es module product. Your product is esm product, not commonjs. So __esModule isn't in it. _interopRequireDefault only work with commonjs syntax js file, not esm file Only file which is commonjs syntax should has a __esModule flag: exports.default = function foo() {}
exports.__esModule = true |
Those output codes have __esModule because they are commonjs syntax, not esm. |
var __markAsModule = (target) => __defProp(target, "__esModule", {value: true});
var require_vue_runtime_esm = __commonJS((exports) => {
__markAsModule(exports);
__export(exports, {
default: () => vue_runtime_esm_default
}); |
I have used the latest version esbuild(v0.12.5). It works very well if output format is cjs and esm, and it has an expected behavior you want. ~/codes/demo ❱❱❱ ls
a.js index.js node_modules package.json yarn.lock
[15:33:18] [cost 0.212s] ls
~/codes/demo ❱❱❱ cat a.js
export default function a () {}
[cost 0.182s] cat a.js
~/codes/demo ❱❱❱ yarn esbuild index.js --format=esm
yarn run v1.22.10
warning package.json: No license field
$ /xxxxxx/xxxxxx/codes/demo/node_modules/.bin/esbuild index.js --format=esm
import a from "./a.js";
a();
✨ Done in 0.10s.
[cost 0.488s] yarn esbuild index.js --format=esm
~/codes/demo ❱❱❱ yarn esbuild index.js --format=cjs
yarn run v1.22.10
warning package.json: No license field
$ /xxxxx/xxxxxx/codes/demo/node_modules/.bin/esbuild index.js --format=cjs
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
+ var __markAsModule = (target) => __defProp(target, "__esModule", { value: true });
var __reExport = (target, module2, desc) => {
if (module2 && typeof module2 === "object" || typeof module2 === "function") {
for (let key of __getOwnPropNames(module2))
if (!__hasOwnProp.call(target, key) && key !== "default")
__defProp(target, key, { get: () => module2[key], enumerable: !(desc = __getOwnPropDesc(module2, key)) || desc.enumerable });
}
return target;
};
var __toModule = (module2) => {
return __reExport(__markAsModule(__defProp(module2 != null ? __create(__getProtoOf(module2)) : {}, "default", module2 && module2.__esModule && "default" in module2 ? { get: () => module2.default, enumerable: true } : { value: module2, enumerable: true })), module2);
};
var import_a = __toModule(require("./a.js"));
(0, import_a.default)();
✨ Done in 0.07s.
[cost 0.437s] yarn esbuild index.js --format=cjs
~/codes/demo ❱❱❱ yarn esbuild --version
yarn run v1.22.10
warning package.json: No license field
$ /Users/xxxxxx/codes/demo/node_modules/.bin/esbuild --version
0.12.5
✨ Done in 0.10s.
[cost 0.504s] yarn esbuild --version |
-.- yes, i know here is the issue repo |
FYI, the |
demo:https://github.com/tnfe/vite-vue2-pro |
fix evanw#1333 evanw#1323 Signed-off-by: Liu Bowen <mr_lbw@outlook.com>
fix evanw#1333 evanw#1323 Signed-off-by: Liu Bowen <mr_lbw@outlook.com>
fix evanw#1333 evanw#1323 Signed-off-by: Liu Bowen <mr_lbw@outlook.com>
fix evanw#1333 evanw#1323 Signed-off-by: Liu Bowen <mr_lbw@outlook.com>
from vite issue i think it's esbuild problem
Describe the bug
when the thirdparty module is esm format, esbuild manage it without add
__esModule
property. but webpack will add__esModule
property.for examle, the source code is
After esbuild format ,will output
without
__esModule
property.it will be cause an error. if a module build by webpack, the source code as below
after webpack build , output code will add helper function
__importDefault
but because of
__esModule
is undefined,__importDefault
function will add default property once again cause the return value__importDefault(require("swiper")).default
is not the current object will be errorReproduction
https://github.com/zhangyuang/vite-react-swiper-error
System Info
vite/2.3.2 darwin-x64 node-v12.18.3
additional
when i add
__esModule
property in source code manually, it can be executed succeed likeIn this case,maybe vite can add
__esModule
in esbuild plugin . for exampleThe text was updated successfully, but these errors were encountered: