@@ -5,7 +5,7 @@ import addFormats, { FormatsPluginOptions } from "ajv-formats";
55import standaloneCode from "ajv/dist/standalone" ;
66import { mkdirSync , writeFileSync } from "fs" ;
77import * as path from "path" ;
8- import { ValidatorOutput } from "../GenerateOptions" ;
8+ import { GenerateOptions , ValidatorOutput } from "../GenerateOptions" ;
99import { createDecoderName , createValidatorName } from "./generation-utils" ;
1010
1111export function generateStandaloneDecoders (
@@ -14,6 +14,7 @@ export function generateStandaloneDecoders(
1414 addFormats : boolean ,
1515 formatOptions : FormatsPluginOptions | undefined ,
1616 output : ValidatorOutput ,
17+ esm : boolean ,
1718 outDirs : string [ ] ,
1819 prettierOptions : Options
1920) : void {
@@ -32,12 +33,9 @@ export function generateStandaloneDecoders(
3233 prettierOptions
3334 ) ;
3435
35- const validatorImportStatement =
36- output === "module"
37- ? `import { ${ validatorName } } from './validator'`
38- : `const { ${ validatorName } } = require("./validator")` ;
36+ const validatorImportStatement = createValidatorImportStatement ( validatorName , output , false , esm ) ;
3937
40- let rawDecoderOutput = decoderFileTemplate
38+ let rawDecoderOutput = decoderFileTemplate ( esm )
4139 . replace ( / \$ D e c o d e r N a m e / g, decoderName )
4240 . replace ( / \$ C l a s s / g, definitionName )
4341 . replace ( / \$ V a l i d a t o r I m p o r t s / g, validatorImportStatement )
@@ -51,7 +49,7 @@ export function generateStandaloneDecoders(
5149 ) ;
5250
5351 indexExports . push (
54- `export { ${ decoderName } } from './${ definitionName } /decoder';`
52+ `export { ${ decoderName } } from './${ definitionName } /decoder${ esm ? ".js" : "" } ';`
5553 ) ;
5654
5755 outDirs . forEach ( ( outDir ) => {
@@ -91,6 +89,7 @@ export function generateStandaloneMergedDecoders(
9189 addFormats : boolean ,
9290 formatOptions : FormatsPluginOptions | undefined ,
9391 output : ValidatorOutput ,
92+ esm : boolean ,
9493 outDirs : string [ ] ,
9594 prettierOptions : Options
9695) {
@@ -108,12 +107,9 @@ export function generateStandaloneMergedDecoders(
108107 . map ( ( d ) => createValidatorName ( d ) )
109108 . join ( ", " ) ;
110109
111- const validatorImportStatement =
112- output === "module"
113- ? `import { ${ validatorImports } } from './validators';`
114- : `const { ${ validatorImports } } = require("./validators")` ;
110+ const validatorImportStatement = createValidatorImportStatement ( validatorImports , output , true , esm ) ;
115111
116- const rawDecoderOutput = mergedDecodersFileTemplate
112+ const rawDecoderOutput = mergedDecodersFileTemplate ( esm )
117113 . replace ( / \$ V a l i d a t o r I m p o r t s / g, validatorImportStatement )
118114 . replace ( / \$ M o d e l I m p o r t s / g, definitionNames . join ( ", " ) )
119115 . replace ( / \$ D e c o d e r s / g, decoders ) ;
@@ -150,6 +146,22 @@ export function generateStandaloneMergedDecoders(
150146 } ) ;
151147}
152148
149+
150+ function createValidatorImportStatement ( validatorImportString : string , output : ValidatorOutput , merged : boolean , esm : boolean ) {
151+ const fileName = merged ? 'validators' : 'validator' ;
152+ switch ( output ) {
153+ case 'commonjs' :
154+ return `const { ${ validatorImportString } } = require("./${ fileName } ")`
155+ case 'module' :
156+ if ( esm ) {
157+ return `import { ${ validatorImportString } } from './${ fileName } .js'`
158+ } else {
159+ return `import { ${ validatorImportString } } from './${ fileName } '`
160+ }
161+ }
162+ }
163+
164+
153165function standAloneValidatorOutput (
154166 schema : ParsedSchema ,
155167 definitions : string [ ] ,
@@ -223,30 +235,36 @@ export const $DecoderName: Decoder<$Class> = {
223235}
224236` ;
225237
226- const decoderFileTemplate = `
227- /* eslint-disable */
238+ const decoderFileTemplate = ( esm : boolean ) => {
239+ const importExtension = esm ? ".js" : "" ;
240+ return `
241+ /* eslint-disable */
228242
229- import { Decoder } from '../../helpers';
230- import { validateJson, Validator } from '../../validate';
231- import { $Class } from '../../models';
232- $ValidatorImports
243+ import { Decoder } from '../../helpers${ importExtension } ';
244+ import { validateJson, Validator } from '../../validate${ importExtension } ';
245+ import { $Class } from '../../models${ importExtension } ';
246+ $ValidatorImports
233247
234- ${ decoderTemplate }
235- ` ;
248+ ${ decoderTemplate }
249+ `
250+ }
236251
237252const decodersFileTemplate = `
238253/* eslint-disable */
239254
240255$Exports
241256` ;
242257
243- const mergedDecodersFileTemplate = `
244- /* eslint-disable */
245-
246- import { Decoder } from './helpers';
247- import { validateJson, Validator } from './validate';
248- import { $ModelImports } from './models';
249- $ValidatorImports
250-
251- $Decoders
252- ` ;
258+ const mergedDecodersFileTemplate = ( esm : boolean ) => {
259+ const importExtension = esm ? ".js" : "" ;
260+ return `
261+ /* eslint-disable */
262+
263+ import { Decoder } from './helpers${ importExtension } ';
264+ import { validateJson, Validator } from './validate${ importExtension } ';
265+ import { $ModelImports } from './models${ importExtension } ';
266+ $ValidatorImports
267+
268+ $Decoders
269+ `
270+ } ;
0 commit comments