Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bundled index JS with webpack or esbuild unrunnable #620

Open
sudo-alexslater opened this issue Nov 12, 2024 · 3 comments
Open

Bundled index JS with webpack or esbuild unrunnable #620

sudo-alexslater opened this issue Nov 12, 2024 · 3 comments

Comments

@sudo-alexslater
Copy link

sudo-alexslater commented Nov 12, 2024

Hey there,

First of all, great package. Made things a lot easier for our API project.

I'm facing some issues using this package because of a dependency - specifically @seriousme/openapi-schema-validator. I believe looking at the code this is using a URL object and building a path to select a version of openapi to validate against (a JSON that is read in at runtime).

Problem with this is that if you'd like to bundle all your code into one file / runtime like we do, this file will not come with it. In fact, bundlers seem to replace the URL with an absolute path pointing at the module which is a bit weird for sure (see code at bottom taken from the compiled code).

Do you have any advice on this / perhaps we could consider importing it into this file in some way to make the connection easier for bundlers to understand? I will continue to look at options as I may be able to assist with a merge request on this library

`const openApiVersions = new Set(["2.0", "3.0", "3.1"]);
const ajvVersions = {
"http://json-schema.org/draft-04/schema#": dist,
"https://json-schema.org/draft/2020-12/schema": _2020,
};
const inlinedRefs = "x-inlined-refs";

function localFile(fileName) {
return (0,external_node_url_.fileURLToPath)(new external_node_url_.URL(fileName, "file:///Users/XXXX/Documents/projects/XXXX/node_modules/@seriousme/openapi-schema-validator/index.js"));
}

function importJSON(file) {
return JSON.parse((0,external_node_fs_namespaceObject.readFileSync)(localFile(file)));
}`

@seriousme
Copy link
Owner

Hi,

thanks for asking.
Sorry to read that your bundler is having an issue with @seriousme/openapi-schema-validator.

To me it sounds like a bug in the bundler, but since I'm also the author of @seriousme/openapi-schema-validator. I will take a look to see if I can fix it.

Kind regards,
Hans

@seriousme
Copy link
Owner

I just released [email protected] which contains @seriousme/[email protected]
@seriousme/[email protected] has removed the dependency on node:url so it should work with your bundler.

Hope this helps.
Kind regards,
Hans

@seriousme
Copy link
Owner

Looking at it again I think that although the node:URL has been fixed,you will still run into issues with:

function importJSON(file) {
     return JSON.parse((0,external_node_fs_namespaceObject.readFileSync)(localFile(file)));
}

The reason for that is that build tools that are intended for browsers have no notion of filesystem and @seriousme/[email protected] needs to read its schema definitions from file.

I might be able to fix this using import "file" with {type: 'json'} syntax, but I can't guarantee that this will work, nor that there will never be any updates that won't be able to work in a browser.

Just out of curiosity: why do you bundle serverside code using browser bundlers?

Kind regards,
Hans

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants