Open cjscheller opened 1 year ago
FYI: This is only a problem for JSON Schema generation. If I instead import Ajv from "ajv/dist/jtd.js"
for JTD schemas, the code compiles fine.
Any news on this one? This is a big issue that actually prevents using AJV to generate ESM validation modules.
It's not just ucs2length. I also noticed it's requiring the equal
module to be used when you have a schema like this:
{
"type": "string",
"const": "my_value"
}
const func1 = require('ajv/dist/runtime/ucs2length').default;
+ const func0 = require('ajv/dist/runtime/equal').default;
Any news on this one? This is a big issue that actually prevents using AJV to generate ESM validation modules.
@ericmorand
Our fix for now is to configure Vite to transform the standalone validation files so that CJS Require statements get modified to look like ES import syntax.
import commonjs from 'vite-plugin-commonjs';
const config = defineConfig({
plugins: [
commonjs({
filter: (id) => {
const fileRegex = /precompiled.*validations\.js$/;
return fileRegex.test(id);
},
}),
],
// ...
The same approach can be done for Rollup users.
I solved this issue in vite using this in vite.config.js
:
import commonjs from '@rollup/plugin-commonjs';
const config = {
build: {
rollupOptions: {
//... other options
plugins: [commonjs({ transformMixedEsModules: true })],
}
}
};
As mentioned beforehand, you could also use { transformMixedEsModules: true }
in a rollup config
The workaround with the "commonjs" plugin did not work for me, maybe because I don't compile the validation code to a file, but in-memory via the "virtual" plugin. I had to monkey-patch the generated code and replace the require() lines with proper imports:
import func2 from 'ajv/dist/runtime/ucs2length'
import {fullFormats} from 'ajv-formats/dist/formats'
const formats0 = fullFormats.date
Actual code may vary depending on used features. Needless to say, this is a fragile hack and no substitute for a proper fix.
The version of Ajv you are using 8.12
The environment you have the problem with node v16.16.0, ESM project (
{type: module}
)Your code (please make it as small as possible to reproduce the issue)
Code to compile AJV schema:
Results in node.js v8+ Able to successfully compile standalone code; unable to import in ESM project
Results and error messages in your platform
I am using the standalone validation code functionality to generate compiled schema files at build time. I am compiling the code with the AJV library in a JS script and specifying
esm: true
to compile the standalone code for ESM. The compilation works and I am writing the compiled schema to a JS file, but I am unable to import the standalone code from this schema file in my ESM project because it imports theucs2length
utility via arequire
statement (const func2 = require("ajv/dist/runtime/ucs2length").default
).Here is an example schema compiled via standalone for ESM support (generated with the above script:
I'm able to import and use the schema file by manually replacing the
require
with animport
statement. Am I overlooking something to generate standalone validation code that is fully ESM-compatible?