webapi-parser
webapi-parser copied to clipboard
Do not mangle classes names in WebApi Model
Find a way to not mangle names of classes when building JS from Scala with fullOptJS
.
Also note how .js
file size changes.
This should make easier for users to explore/navigate AMF model.
Ideal solution should:
- Leave names exported from Scala to JS un-mangled. At least the part which is displayed when logging webapi-parser Model objects in NodeJS console.
- Mangle/minimize everything else as much as possible (what fullOptJS does).
- Output JS size should be affected as little as possible.
- Still produce working JS and Java packages.
+1 It would be great
It seems you just need to provide a build time configuration to do fastOptJS
instead of fullOptJS
. Prior to this commit https://github.com/raml-org/webapi-parser/commit/efe220d502bb81122de5ea7137cf6cba1f301dec, the webapi-parser was using fastOptJS
It seems you just need to provide a build time configuration to do
fastOptJS
instead offullOptJS
. Prior to this commit efe220d, the webapi-parser was usingfastOptJS
Hi @yungcheng. Thank you for helping us address this issue!
Though, simply switching fullOptJS
to fastOptJS
doesn't fully fix the issue, because fastOptJS
adds too much weight to the output JS file (it explodes from 3-4mb to 20.1mb).
Ideal solution should:
- Leave names exported from Scala to JS un-mangled. At least the part which is displayed when logging webapi-parser Model objects in NodeJS console.
- Mangle/minimize everything else as much as possible (what fullOptJS does).
- Output JS size should be affected as little as possible.
- Still produce working JS and Java packages.
PS. Added these points to the issue description.
Scala js uses Google Closure Compiler, in order to do full optimization but keep the function name intact, you’d need to have the ability to configure how closure compiler is run. Also if I understand correctly, what you want is not an artifact for development time but a prod artifact with functions unobfuscated?
Scala js uses Google Closure Compiler, in order to do full optimization but keep the function name intact, you’d need to have the ability to configure how closure compiler is run. Also if I understand correctly, what you want is not an artifact for development time but a prod artifact with functions unobfuscated?
Correct. I want WebApi Model functions and properties names unobfuscated when logging object in a Nodejs console.
~I can't inspect objects (mangled props) and the type definitions don't match the example~
This seems to get the correct typings:
import { model } from 'amf-client-js'
import { WebApiParser, WebApiBaseUnitWithDeclaresModelAndEncodesModel } from 'webapi-parser'
declare abstract class ParsedRaml extends WebApiBaseUnitWithDeclaresModelAndEncodesModel {
encodes: model.domain.WebApi
}
type ConcreteElements = Omit<typeof model.domain, 'DomainElement' | 'Linkable'>
type Visitors = {
[K in keyof ConcreteElements]: (concrete: InstanceType<ConcreteElements[K]>) => any
}
/**
* Find a matching implementation handler in `visitors` and call it with `element`.
* The purpose here is to get some type safety while reflecting.
*/
function doWhen(element: model.domain.DomainElement, visitors: Partial<Visitors>) {
for (const typeId of element.graph().types()) {
const typeKey = typeId.split('#').pop() as keyof Visitors
const fn = visitors[typeKey]
if (fn) {
return fn(element as any)
}
}
return undefined
}
async function example() {
// parse some file
const parsed = await WebApiParser.raml10.parse('...') as ParsedRaml
// look for an `AnyShape` implementor (object, scalar, etc) for each `declare`
parsed.declares.forEach((declare) => {
doWhen(declare, {
AnyShape: (shape) => {
console.log(shape.name.value(), shape.toJsonSchema)
},
})
})
}