vite-plugins icon indicating copy to clipboard operation
vite-plugins copied to clipboard

Unconditional default unwrapping causes problems

Open juanevp opened this issue 4 years ago • 4 comments

In line: https://github.com/originjs/vite-plugins/blob/74b73c3069dafa328a5d57da276d9d77f6b712ff/packages/vite-plugin-commonjs/src/lib.ts#L37

the transformation unconditionally unwraps the default property of exports if present. I use a library which assumes that require() returns an object with a default property and it is failing because the code ends up double-unwrapping which produces an undefined value. AFAIK, unwrapping the default export is not part of the require expected behavior.

Versions

  • originjs: 1.0.2
  • node: 16.13.1

Reproduction

This is the case I am referring to specifically. In this line que component is required: https://github.com/martinnov92/React-Splitters/blob/c9ca351426e55c41c2016c9cc1c647dd17974783/lib/Splitter.js#L20

In this line is it used: https://github.com/martinnov92/React-Splitters/blob/c9ca351426e55c41c2016c9cc1c647dd17974783/lib/Splitter.js#L288

See that the use expects to have the default property.

Additional Details

Steps to reproduce

What is Expected?

That the transformed code be:

import * as __require_for_vite_JvR9OZ from "./Pane";
/*...*/
var Pane_1 = __require_for_vite_JvR9OZ;
/*...*/
React.createElement(Pane_1.default, /*...*/),

What is actually happening?

import * as __require_for_vite_JvR9OZ from "./Pane";
/*...*/
var Pane_1 = __require_for_vite_JvR9OZ.default || __require_for_vite_JvR9OZ;
/*...*/
React.createElement(Pane_1.default, /*...*/),

Which produces the error:

React.createElement: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: undefined.

Given the require semantics I think this should be considered a bug,. The default behavior should not automatically unwrap the default property. Maybe this behavior could be specified through a setting.

juanevp avatar Feb 06 '22 23:02 juanevp

+1

hirokith avatar Feb 16 '22 03:02 hirokith

+1

matheusliraofficial avatar Mar 23 '22 05:03 matheusliraofficial

+1

LeeeeeeM avatar Apr 17 '22 02:04 LeeeeeeM

+1

aztack avatar Dec 05 '22 03:12 aztack