Introduction
In this article we are going to see how to compile TypeScript files to JavaScript with the Angular 4 html template files by using a custom built toolchain. SharePoint Framework doesn’t provide a direct toolchain for Angular components but it will be announced in the near future.
SPFx build tool chain
The SharePoint Framework toolchain is the set of build tools, framework packages, and other items that manage building and deploying your client-side projects.
SPFx uses gulp as the build process runner and it enables us to integrate our custom gulp tasks to SPFx build pipeline. That’s the key for us to bundle the angular template files.
Normally gulp tasks are defined in gulpfile.js file. To view the available tasks in your SPFx project use the command
gulp --tasks
The custom tasks that can be run out of the SPFx build pipeline scope can be added anywhere and you can run it independently like running other gulp tasks (Ex; gulp {CustomTaskName}).
If you want to add any custom tasks that must run along with SPFx build tool chain it should be defined before initializing the global gulp instance, before this piece of code:
build.initialize(gulp)
What happen if we don’t use custom gulp tasks to bundle angular templates in SPFx?
Normally in Angular we will be giving the template URL’s in the component files like below in Angular’ s Component decorator.
- @Component({
- selector: 'my-spfx-app',
- templateUrl: `app.component.html`
- })
Code Snippet: 1
SharePoint framework doesn’t recognize the base URL of this template file “app.component.html” while executing your app through gulp serve command and we will end up with file not found error in the console window.
But at the same time if you intentionally hard code the base URL like the below code snippet in the component itself, the execution will work perfectly.
- @Component({
- selector: 'my-spfx-app',
- templateUrl: `src/webparts/hfCommunity/app/components/app.component.html`
- })
Code Snippet: 2
And it’s not good practice to mention the base path of the html file in each component that you use in the project.
Otherwise, you have to use the template directly in the typescript file like below which is not suitable for the bigger projects.
- @Component({
- selector: 'my-spfx-app',
- template: `<h1>Welcome to SPFx {{name}}!!</h1>`
- })
Code Snippet: 3
So, the solution is we should convert the template html files into an inline html template code and inject it into the component after compiling the Typescript file into a JavaScript file through SPFx build toolchain.
Adding custom tasks in SPFx build tool chain to bundle angular html templates
There are some gulp tasks already available which we just need to incorporate into the SPFx build took chain, which will help us to append the compiled template html into component. Add the below packages in your package.json file like below,
- "gulp-inline-ng2-template": "4.0.0",
- "gulp-typescript": "3.1.6",

Join the conversation! Your thoughts help the community grow.