webapi-parser icon indicating copy to clipboard operation
webapi-parser copied to clipboard

Do not mangle classes names in WebApi Model

Open postatum opened this issue 6 years ago • 6 comments

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:

  1. 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.
  2. Mangle/minimize everything else as much as possible (what fullOptJS does).
  3. Output JS size should be affected as little as possible.
  4. Still produce working JS and Java packages.

postatum avatar Feb 12 '19 18:02 postatum

+1 It would be great

artemkaint avatar Jul 30 '19 07:07 artemkaint

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

yungcheng avatar Apr 08 '20 21:04 yungcheng

It seems you just need to provide a build time configuration to do fastOptJS instead of fullOptJS. Prior to this commit efe220d, the webapi-parser was using fastOptJS

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:

  1. 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.
  2. Mangle/minimize everything else as much as possible (what fullOptJS does).
  3. Output JS size should be affected as little as possible.
  4. Still produce working JS and Java packages.

PS. Added these points to the issue description.

postatum avatar Apr 09 '20 06:04 postatum

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?

yungcheng avatar Apr 09 '20 14:04 yungcheng

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.

postatum avatar Apr 13 '20 06:04 postatum

~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)
      },
    })
  })
}

mmmveggies avatar Aug 06 '20 12:08 mmmveggies