Skip to content

Commit

Permalink
Fix minimizer renaming functions (#339)
Browse files Browse the repository at this point in the history
  • Loading branch information
diwic committed Jun 25, 2021
1 parent b0ba65f commit ab95d2c
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 24 deletions.
34 changes: 11 additions & 23 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,21 +72,7 @@ function build (schema, options) {
`

code += `
${$pad2Zeros.toString()}
${$asAny.toString()}
${$asString.toString()}
${$asStringNullable.toString()}
${$asStringSmall.toString()}
${$asDatetime.toString()}
${$asDate.toString()}
${$asTime.toString()}
${$asNumber.toString()}
${$asNumberNullable.toString()}
${$asInteger.toString()}
${$asIntegerNullable.toString()}
${$asNull.toString()}
${$asBoolean.toString()}
${$asBooleanNullable.toString()}
${asFunctions}
var isLong = ${isLong ? isLong.toString() : false}
Expand Down Expand Up @@ -116,19 +102,19 @@ function build (schema, options) {
code = buildObject(location, code, main)
break
case 'string':
main = schema.nullable ? $asStringNullable.name : getStringSerializer(schema.format)
main = schema.nullable ? '$asStringNullable' : getStringSerializer(schema.format)
break
case 'integer':
main = schema.nullable ? $asIntegerNullable.name : $asInteger.name
main = schema.nullable ? '$asIntegerNullable' : '$asInteger'
break
case 'number':
main = schema.nullable ? $asNumberNullable.name : $asNumber.name
main = schema.nullable ? '$asNumberNullable' : '$asNumber'
break
case 'boolean':
main = schema.nullable ? $asBooleanNullable.name : $asBoolean.name
main = schema.nullable ? '$asBooleanNullable' : '$asBoolean'
break
case 'null':
main = $asNull.name
main = '$asNull'
break
case 'array':
main = '$main'
Expand Down Expand Up @@ -233,6 +219,7 @@ function getTestSerializer (format) {
return stringSerializerMap[format]
}

const asFunctions = `
function $pad2Zeros (num) {
const s = '00' + num
return s[s.length - 2] + s[s.length - 1]
Expand Down Expand Up @@ -372,7 +359,7 @@ function $asStringSmall (str) {
surrogateFound = true
}
if (point === 34 || point === 92) {
result += str.slice(last, i) + '\\'
result += str.slice(last, i) + '\\\\'
last = i
found = true
}
Expand All @@ -385,6 +372,7 @@ function $asStringSmall (str) {
}
return ((point < 32) || (surrogateFound === true)) ? JSON.stringify(str) : '"' + result + '"'
}
`

function addPatternProperties (location) {
const schema = location.schema
Expand Down Expand Up @@ -928,7 +916,7 @@ function buildObject (location, code, name) {
if (schema.nullable) {
code += `
if(input === null) {
return '${$asNull()}';
return 'null';
}
`
}
Expand Down Expand Up @@ -967,7 +955,7 @@ function buildArray (location, code, name, key = null) {
if (schema.nullable) {
code += `
if(obj === null) {
return '${$asNull()}';
return 'null';
}
`
}
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
"semver": "^7.1.0",
"standard": "^16.0.1",
"tap": "^15.0.0",
"typescript": "^4.0.2"
"typescript": "^4.0.2",
"webpack": "^5.40.0"
},
"dependencies": {
"ajv": "^6.11.0",
Expand Down
50 changes: 50 additions & 0 deletions test/webpack.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
'use strict'

const test = require('tap').test
const webpack = require('webpack')
const path = require('path')

test('the library should work with webpack', async (t) => {
t.plan(1)
const targetdir = path.resolve(__dirname, '..', '.cache')
const targetname = path.join(targetdir, 'webpacktest.js')
const wopts = {
entry: path.resolve(__dirname, '..', 'index.js'),
mode: 'production',
target: 'node',
output: {
path: targetdir,
filename: 'webpacktest.js',
library: {
name: 'fastJsonStringify',
type: 'umd'
}
}
}
await new Promise((resolve, reject) => {
webpack(wopts, (err, stats) => {
if (err) { reject(err) } else { resolve(stats) };
})
})
const build = require(targetname)
const stringify = build({
title: 'webpack should not rename code to be executed',
type: 'object',
properties: {
foo: {
type: 'string'
},
bar: {
type: 'boolean'
}
},
patternProperties: {
foo: {
type: 'number'
}
}
})

const obj = { foo: '42', bar: true }
t.equal(stringify(obj), '{"foo":"42","bar":true}')
})

0 comments on commit ab95d2c

Please sign in to comment.