Comments (9)
OK, I found a solution by myself.
If other people get the same issue, that's what I did:
- Import
react-color
classes from'@uiw/react-color/esm'
- Add
@uiw/.*
,colors-named
, andcolors-named-hex
patterns to your Jest configuration intransformIgnorePatterns
value (eg:transformIgnorePatterns: ['/node_modules/(?!@uiw|colors-named|colors-named-hex)'],
from react-color.
I have a NextJS project and faced the same issue.
Just found out that importing components from @uiw/react-color/src/index
, solved the issue.
For example:
import { Circle } from "@uiw/react-color/src/index";
Importing components from @uiw/react-color/esm
raised another error.
from react-color.
I've attempted to use the 'next-remove-imports', but I'm still getting 'Cannot redefine property: render' error.
Am I doing something incorrectly?
My next.config.js:
const { withSentryConfig } = require('@sentry/nextjs');
const removeImports = require("next-remove-imports")();
const withPlugins = require('next-compose-plugins');
let config = {
reactStrictMode: true,
experimental: { esmExternals: true },
eslint: {
// Warning: This allows production builds to successfully complete even if
// your project has ESLint errors.
ignoreDuringBuilds: true,
},
};
module.exports = withPlugins([
removeImports,
[ withSentryConfig,
{ silent: true ,
hideSourcemaps: true },
]
], config);
from react-color.
Hi there, it looks like the same error occurs on Jest test suites ran on a pure React project (no NextJS involved here).
I use the commonJS bundle and tracked the error down to @uiw/react-color-colorful
lib import.
In the built source (in @uiw/react-color/cjs/index.js
file), it happens here (line 188):
![image](https://private-user-images.githubusercontent.com/1149128/269273986-2df1debb-a271-4b69-8e05-ea702089b071.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MTA2Njc4NjUsIm5iZiI6MTcxMDY2NzU2NSwicGF0aCI6Ii8xMTQ5MTI4LzI2OTI3Mzk4Ni0yZGYxZGViYi1hMjcxLTRiNjktOGUwNS1lYTcwMjA4OWIwNzEucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI0MDMxNyUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNDAzMTdUMDkyNjA1WiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9MWM0ZGViNTlmMjQ2YWYxM2NjZWIwZjIzZDFkOTA1ZDRlODM5N2ZiOGMyZTkyZWU4NDhjNzY3OTM0ZDI4NjMxNiZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QmYWN0b3JfaWQ9MCZrZXlfaWQ9MCZyZXBvX2lkPTAifQ.ibUj3BjR2dip8XXO08SYRJBiOiCjkM5RB_rk9b0Waao)
I'm not sure that the next-remove-imports
lib is a solution in this case. Do you have any other solution in mind ?
Thanks !
from react-color.
https://github.com/uiwjs/next-remove-imports
from react-color.
from react-color.
// next.config.js
const removeImports = require('next-remove-imports')();
module.exports = (phase, { defaultConfig }) => {
return removeImports({
...defaultConfig
});
};
// next.config.js
const removeImports = require('next-remove-imports')({
test: /node_modules([\s\S]*?)\.(tsx|ts|js|mjs|jsx)$/,
matchImports: "\\.(less|css|scss|sass|styl)$"
});
module.exports = removeImports({
webpack(config, options) {
return config
},
});
// next.config.js
const removeImports = require('next-remove-imports')()
module.exports = removeImports({
webpack(config, options) {
return config
},
});
from react-color.
Had this same issue when using Astro. Changing imports to @uiw/react-color/src/index
worked
import { Sketch } from "@uiw/react-color/src/index";
from react-color.
Just to clarify, this problem is nothing to do with Next.js or Jest. The problem is that the CommonJS build is broken, so anything which imports the CommonJS build will get this error.
This can be confirmed by creating the following simple CommonJS file, and seeing the error.
const reactColor = require("@uiw/react-color");
console.log(reactColor);
// Uncaught TypeError: Cannot redefine property: render
The reason this breaks is because in the package export, every sub package is exported with export * from "foo"
. The problem with that is multiple packages define a render
export.
When this compiles to CommonJS, it ends up like this. Each of these Object.keys
blocks is looping over all the exports from the sub package, and attaching them to exports
using Object.defineProperty
.
var _reactColorSwatch = _interopRequireWildcard(require("@uiw/react-color-swatch"));
Object.keys(_reactColorSwatch).forEach(function (key) {
if (key === "default" || key === "__esModule") return;
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
if (key in exports && exports[key] === _reactColorSwatch[key]) return;
Object.defineProperty(exports, key, {
enumerable: true,
get: function get() {
return _reactColorSwatch[key];
}
});
});
var _reactColorWheel = _interopRequireWildcard(require("@uiw/react-color-wheel"));
Object.keys(_reactColorWheel).forEach(function (key) {
if (key === "default" || key === "__esModule") return;
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
if (key in exports && exports[key] === _reactColorWheel[key]) return;
Object.defineProperty(exports, key, {
enumerable: true,
get: function get() {
return _reactColorWheel[key];
}
});
});
You cannot use Object.defineProperty
to redefine an existing property. So the first block defines a render
property on exports
, and then subsequent packages try to set the same property, causing the error.
The solution is to avoid exporting sub packages like this, or at least being careful that each sub package defines unique exports. I have added a PR (#140) which addresses this.
As a super hacky fix, I have added a patch to my project which adds the configurable
option to each Object.defineProperty
in the CommonJS file. This allows subsequent packages to overwrite the render
export without the error, but it would be preferable if this package did not add duplicate exports at all.
var _reactColorWheel = _interopRequireWildcard(require("@uiw/react-color-wheel"));
Object.keys(_reactColorWheel).forEach(function (key) {
if (key === "default" || key === "__esModule") return;
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
if (key in exports && exports[key] === _reactColorWheel[key]) return;
Object.defineProperty(exports, key, {
enumerable: true,
+ configurable: true,
get: function get() {
return _reactColorWheel[key];
}
});
});
from react-color.
Related Issues (20)
- Wheel using HEX instead of HSVA HOT 2
- Wheel shape width/height adaption HOT 2
- onChange 用useCallback 包裹 ,onChange里面无法修改父组件的状态!把useCallback 干掉可以吗? HOT 10
- Disable Alpha for Sketch picker HOT 3
- Shade slider (white color) HOT 1
- Colorful component works incorrectly on mobiles HOT 1
- hex is #00000000 bug
- Direction hue "vertical" HOT 2
- Null/undefined color value HOT 10
- Support hex input with leading # HOT 2
- Typo in Github picker placement enum: "Botton" instead of "Bottom" HOT 1
- Sometimes Wheel don't react to onChange HOT 2
- Change Value for Wheel HOT 7
- Color picking dark color results in jumpy behavior HOT 3
- [color-convert] Circular dependencies error HOT 2
- Direction/angle color issue HOT 1
- Circle with margin HOT 8
- Feature request: Ability to disable alpha HOT 1
- Color wheel gets stuck at V = 0 HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-color.