Skip to content
This repository has been archived by the owner on Sep 26, 2023. It is now read-only.

Object.defineProperty(NextImage, '__esModule', descriptor) is no more needed from next@12.1.7-canary.7 #95

Closed
kazukinagata opened this issue Jun 10, 2022 · 4 comments · Fixed by #98
Labels
bug Something isn't working released

Comments

@kazukinagata
Copy link

kazukinagata commented Jun 10, 2022

Describe the bug

Storybook will throw an error TypeError: Cannot redefine property: __esModule at this line with using next@12.1.7-canary.7 or later.

Your minimal, reproducible example

none

Steps to reproduce

  1. Use next version 12.1.7-canary.7 or later
  2. run storybook
  3. It gives the above error

Expected behavior

Working Storybook

How often does this bug happen?

Every time

Screenshots or Videos

No response

Platform

  • OS: Ubuntu 18.04.5
  • Browser: Chrome 102.0.5005.63
  • Node: v16.14.2
  • Relevant packages:
    • next: ~12.1.7-canary.7

storybook-addon-next version

v1.6.6

Additional context

I did some digging and found that the default import of next/image has been fixed by this PR.

The built code looks like following, so you need not to redefine __esModule property on next-image-stub.tsx.

if ((typeof exports.default === 'function' || (typeof exports.default === 'object' && exports.default !== null)) && typeof exports.default.__esModule === 'undefined') {
  Object.defineProperty(exports.default, '__esModule', { value: true });
  Object.assign(exports.default, exports);
  module.exports = exports.default;
}

Related issues

@RyanClementsHax
Copy link
Owner

Thanks for opening up this issue.

When next.js releases the next version, I'll happily update this repo or accept any PRs to make this library work with it

@RyanClementsHax RyanClementsHax added bug Something isn't working blocked labels Jun 25, 2022
tubbo added a commit to tubbo/storybook-addon-next that referenced this issue Jun 28, 2022
NextJS no longer requires a bit of code added in to patch a bug that
existed in Next v12.1.5 and above. As of v12.2.0, this patch is no
longer needed, and furthermore causes a `TypeError` to be thrown when
attempting to ugprade to Next 12.

Fixes: RyanClementsHax#95
Related: RyanClementsHax#72
@Otoris
Copy link

Otoris commented Jun 28, 2022

@tubbo

Do we need to make any changes to this snippet in our storybook preview.js file with this PR?

// De-optimize next.js images to work with storybook
const OriginalNextImage = NextImage.default;

Object.defineProperty(NextImage, "default", {
  configurable: true,
  value: (props) => typeof props.src === 'string' ? (
    <OriginalNextImage {...props} unoptimized blurDataURL={props.src} />
  ) : (
    <OriginalNextImage {...props} unoptimized />
  ),
});

Object.defineProperty(NextImage, "__esModule", {
  configurable: true,
  value: true
});

@github-actions
Copy link

🎉 This issue has been resolved in version 1.6.7 🎉

The release is available on:

Your semantic-release bot 📦🚀

@RyanClementsHax
Copy link
Owner

@Otoris I believe that is no longer needed. The addon should do everything for you. If you come by any other issues feel free to open another issue.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working released
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants