jsonnext / codemirror-json-schema

A JSONSchema enabled mode for codemirror 6, for json4 and json5, inspired by monaco-json
https://codemirror-json-schema.netlify.app
MIT License
56 stars 9 forks source link

Add ES modules (ESM) support? #88

Closed RabeeAbuBaker closed 1 month ago

RabeeAbuBaker commented 3 months ago

Trying to use with ES modules, I get this error

image
import { Draft07, isJsonError } from "json-schema-library";
         ^^^^^^^
SyntaxError: Named export 'Draft07' not found. The requested module 'json-schema-library' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

import pkg from 'json-schema-library';
const { Draft07, isJsonError } = pkg;

    at ModuleJob._instantiate (node:internal/modules/esm/module_job:124:21)
    at ModuleJob.run (node:internal/modules/esm/module_job:190:5)

Using the following:

"codemirror-json-schema": "^0.7.0"
"vite": "^5.1.5",

tsconfig compilerOptions:

  "compilerOptions": {
    "lib": ["DOM", "DOM.Iterable", "ES2022"],
    "types": ["vitest/globals"],
    "isolatedModules": true,
    "esModuleInterop": true,
    "jsx": "react-jsx",
    "moduleResolution": "Bundler",
    "resolveJsonModule": true,
    "target": "ES2022",
    "strict": true,
    "allowJs": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true,
    "baseUrl": ".",
    "paths": {
      "~/*": ["./app/*"]
    },
acao commented 3 months ago

hello, I think you are looking for this issue in our dependency json-schema-library. I reccomend subscribing for the fix. this is the codemirror-json-schema repo so we don't have control over that, but we might end up contributing the fix. I have full time and oss contract obligations in other repos but I hope to get to it in the coming months!

https://github.com/sagold/json-schema-library/issues/51

to note for anyone using next.js, we resolved this using transpileModules which is now built-in to next 13.

afaik, there is not a known workaround for vite/esbuild however, this requires adding full esm support to json-schema-library

RabeeAbuBaker commented 3 months ago

hello, I think you are looking for this issue in our dependency json-schema-library. I reccomend subscribing for the fix. this is the codemirror-json-schema repo so we don't have control over that, but we might end up contributing the fix. I have full time and oss contract obligations in other repos but I hope to get to it in the coming months!

sagold/json-schema-library#51

to note for anyone using next.js, we resolved this using transpileModules which is now built-in to next 13.

afaik, there is not a known workaround for vite/esbuild however, this requires adding full esm support to json-schema-library

Thanks a lot! I appreciate your work and efforts There's no workaround for esbuild AFAIK unfortunately, but there's on for vite (fix)

acao commented 3 months ago

our demo build is with vite actually, i forgot how we got around it?? i wonder if we switch back from moduleResolution: bundler to node if that would fix it

imolorhe commented 1 month ago

I think this can be closed now?

acao commented 1 month ago

yes i think it is resolved, please let us know if the latest releases do not resolve this and we can re-open

tmcw commented 3 weeks ago

Unfortunately if you're using native ESM, or esbuild, this doesn't work because the underlying json-schema-library doesn't support ESM: https://github.com/sagold/json-schema-library/issues/59