How to get ngtemplate-loader working with Webpack 3, Angular 1, TypeScript and ES 6 modules?

Viewed 2476

I am trying configure ngtemplate-loader to be able to use my angular template in a TypeScript file like so:

import myTemplateUrl from './hello-angular2-template.thtml';
angular
    .module('pd')
    .component('helloAngular2', {
        templateUrl: myTemplateUrl,
    });

Loader definition in webpack.config:

module: {
    rules: [
        // Angular Template HTML
        {
            test: /\.thtml$/,
            use: [
                {
                    loader: 'ngtemplate-loader',
                },
                {
                    loader: 'html-loader',
                }
            ],
        },

(The strange *.ththml suffix is just so, that no standard html-loader can interfere.)

However the template never gets loaded (is undefined after import).

I tried to add

options: {
    exportAsEs6Default: true
}

to the chained html-loader, but that did not work out, too.

Full example project: https://github.com/eekboom/ng-webpack

4 Answers

I made it work with this settings:

{
  test: /\.html$/,
  use: [
    {
      loader: 'ngtemplate-loader',
      options: {
        angular: true,
      },
    },
    {
      loader: 'raw-loader',
    },
  ],
};

The packages versions:

"webpack": "^3.5.0",
"angular": "^1.6.5",
"ngtemplate-loader": "^2.0.1",
"raw-loader": "^0.5.1",
Related