json-diff-kit icon indicating copy to clipboard operation
json-diff-kit copied to clipboard

fix import react error in some cases

Open pgarr opened this issue 1 year ago • 5 comments

Fixes #12. Based on below resources, I changed "jsx": "react-jsx" in tsconfig.json. It allows proper jsx compilation without import React from 'react'. https://stackoverflow.com/questions/64656055/react-refers-to-a-umd-global-but-the-current-file-is-a-module/65539274#65539274 https://devblogs.microsoft.com/typescript/announcing-typescript-4-1/#jsx-factories

pgarr avatar Apr 11 '23 05:04 pgarr

I think it's okay to change the createElement to _jsx, but here are two things we should consider:

  1. We should modify the version of react and react-dom in peerDependencies. According to the article in React blog, the minimum version below 17 and support _jsx call is 16.14.0, but currently it's ^16.0.0 in this repo.
  2. ~~It's better to have a file tsconfig.dev.json with "jsx": "react-jsxdev" option.~~ This library uses ESBuild to compile, maybe you should change rollup.config.js and esbuild.mjs instead of tsconfig.json.

RexSkz avatar Apr 11 '23 06:04 RexSkz

Also, I'm curious why this will fix the "Cannot use import statement outside a module" issue. The compiled files are still in ESM, if your project doesn't use ESM and there's no transpiler (e.g. Webpack), you'll still get this error.

Maybe json-diff-kit should provide CJS dist as well since ESM is not widely used as I expected...

RexSkz avatar Apr 11 '23 06:04 RexSkz

I'd definitely love it if this had a CommonJS distribution; I'm currently struggling to get this to import using babel in a CommonJS-based project.

jpage-godaddy avatar Jul 06 '23 20:07 jpage-godaddy

I'd definitely love it if this had a CommonJS distribution; I'm currently struggling to get this to import using babel in a CommonJS-based project.

CJS is on the way, but there're some issues to solve. A new version with CJS support will be released once I finish my company work (in a day or two I think).

RexSkz avatar Jul 07 '23 07:07 RexSkz

For what it's worth, I forked your main branch and published what you have to a private package repository, and the CommonJS export is working great. Looking forward to when it's published.

jpage-godaddy avatar Jul 07 '23 22:07 jpage-godaddy