AngularDart Pitfalls pt.3 (Generated files?? What are they and why don’t they show up in my editor?)

J’aime l’œuf
Blood Engineering
Published in
3 min readSep 23, 2019
AngularDart

Hello and welcome back to the third article in AngularDart Pitfalls. Check out the other articles in the series if you missed it! Links are at the end of this article :) *Note the date of publishing, things may have changed as time passes, do double check again :)

The hell is ”some_component.template.dart” ???

Confused? So am I… Source

When you are building your AngularDart web application, you may have noticed that you are required to import a generated template file. Which for a component named my_component.dart a corresponding file named my_component.template.dart will also be generated.

When you need to use certain components or code from a file like my_component.dart, often times, you would be asked to import the my_component.template.dart file instead. This is because your component is only ready for import after the DDC (DartDevC, the AngularDart to JavaScript transpiler) has transpiled (transpilation is just like compilation but instead of compiling to machine code, it compiles to another language like JavaScript for example) it, into the template file.

On Component.template.dart files

  • The DDC (Dart Development Compiler) will create the some_component.template.dart file for the corresponding some_component.dart file, IN THE similarly named directory in the build parent directory!!!
  • So make sure that when you try to import it, make sure its from the same folder as the normal .dart file as the editor cannot intelligently tell you where is it!!!
  • Angular uses code generation to generate Dart code from Angular template syntax during compilation. Other components and routers can then import the generated code. The code contains factory methods that are required by the router to create component instances.
  • The generated dart codes for the templates are then transpiled into JS code to run in browsers with the dart2js compiler.
  • If you cannot import the template.dart file, it means that either you are not importing from the correct directory (check where’s your dart module) OR it means that there are errors in your .html template which causes the generation to fail silently in .dart_tools/build/generated Link
  • References. → as mentioned in this link, no official docs for this yet.
  • Issue opened → Seems like only certain IDE supports the finding of auto generated template.dart files, but still not supported by the dart-analyzer.

To go into more details, you can click on the link below to read the full article!

Link → The Angular compiler generates component factories behind the scenes when you build the app. To access the factory you need to import the generated component template file. Until you’ve built the app, the generated files don’t exist. Use the thing in the link to disable the errors to calm your linters and static analyzers down!

Outstanding! Source

Thanks for reading the articles from this series on AngularDart again! Hope you found this useful/helpful and saved you from pulling out all your hair! If you need more help, leave a comment below or reach out to me directly!

  • Link to part 1 of the series. (Missing components)
  • Link to part 2 of the series. (Template failures)

--

--

J’aime l’œuf
Blood Engineering

Writing down that constant flow of inspirations in my mind ☺️