-
-
Notifications
You must be signed in to change notification settings - Fork 208
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: export standalone dependencies (#617)
- Loading branch information
1 parent
895892c
commit 703ced8
Showing
6 changed files
with
58 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,31 @@ | ||
const fs = require('fs') | ||
const path = require('path') | ||
|
||
function buildStandaloneCode (options, validator, isValidatorUsed, contextFunctionCode) { | ||
const serializerCode = fs.readFileSync(path.join(__dirname, 'serializer.js')).toString() | ||
let buildAjvCode = '' | ||
let ajvSchemasCode = '' | ||
|
||
if (isValidatorUsed) { | ||
let defaultAjvSchema = '' | ||
// we need to export the custom json schema | ||
const defaultMeta = validator.ajv.defaultMeta() | ||
if (typeof defaultMeta === 'string') { | ||
defaultAjvSchema = defaultMeta | ||
} else { | ||
defaultAjvSchema = defaultMeta.$id || defaultMeta.id | ||
} | ||
|
||
ajvSchemasCode += `const validator = new Validator(${JSON.stringify(options.ajv || {})})\n` | ||
for (const [id, schema] of Object.entries(validator.ajv.schemas)) { | ||
// should skip ajv default schema | ||
if (id === defaultAjvSchema) continue | ||
ajvSchemasCode += `validator.ajv.addSchema(${JSON.stringify(schema.schema)}, "${id}")\n` | ||
} | ||
buildAjvCode = fs.readFileSync(path.join(__dirname, 'validator.js')).toString() | ||
buildAjvCode = buildAjvCode.replace("'use strict'", '').replace('module.exports = SchemaValidator', '') | ||
'use strict' | ||
|
||
function buildStandaloneCode (contextFunc, context, serializer, validator) { | ||
let ajvDependencyCode = '' | ||
if (context.validatorSchemasIds.size > 0) { | ||
ajvDependencyCode += `const validatorState = ${JSON.stringify(validator.getState())}\n` | ||
ajvDependencyCode += 'const validator = Validator.restoreFromState(validatorState)\n' | ||
} else { | ||
ajvDependencyCode += 'const validator = null\n' | ||
} | ||
|
||
const serializerOptions = options && options.rounding ? JSON.stringify({ options: options.rounding }) : '' | ||
return ` | ||
'use strict' | ||
${serializerCode.replace("'use strict'", '').replace('module.exports = ', '')} | ||
${buildAjvCode} | ||
const serializer = new Serializer(${serializerOptions}) | ||
${ajvSchemasCode} | ||
const { dependencies } = require('fast-json-stringify/lib/standalone') | ||
const { Serializer, Validator } = dependencies | ||
const serializerState = ${JSON.stringify(serializer.getState())} | ||
const serializer = Serializer.restoreFromState(serializerState) | ||
${contextFunctionCode.replace('return main', '')} | ||
${ajvDependencyCode} | ||
module.exports = main` | ||
module.exports = ${contextFunc.toString()}(validator, serializer)` | ||
} | ||
|
||
module.exports = buildStandaloneCode | ||
|
||
module.exports.dependencies = { | ||
Serializer: require('./serializer'), | ||
Validator: require('./validator') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters