Skip to content

Templates

Anton edited this page Aug 10, 2019 · 1 revision

After Closure compilation, the source code will loose all annotated types.

Templates/lostTypes.png

To enable types again for developer experience of those who will use the package, the types need to be overridden. This can be achieved with the template feature of Typal. The process is the following:

  1. Create the src/depack.js file that imports APIs from the source, and assigns them to module.exports object using quoted props notation, so that the properties don't get renamed, for example:
    import '../types/externs'
    import { Type, Property, Method, getNameWithDefault, parseFile, getLinks } from './'
    
    module.exports = {
      '_Type': Type,
      '_Property': Property,
      '_Method': Method,
      '_parseFile': parseFile,
      '_getLinks': getLinks,
    }
  2. Compile the source code into compile/package-name.js file using the depack command:
    {
      "compile": "depack src/depack -c -o compile/package-name.js -a -p -s -O 2018 --source_map_include_content"
    }
  3. Create a template file compile/template.js, that requires the compiled file: %EXAMPLE: src/template.js%
  4. Run Typal to include the correct annotations, with -t argument pointing to the folder with all types (each XML file will be read recursively):
    typal compile/template.js -T compile/index.js -t types

The tags supported by the template are @methodType and @fnType, where the method type is used to enrich methods with JSDoc, and the function type is used to place JSDoc above methods of a class.

Clone this wiki locally