html-Loader is not working with Webpack 5 + AngularJs (appropriate loader)

1.9k Views Asked by At

I'm trying to load AngularJs app templates(html) using html-loader + webpack 5. Many people junked templateCache favoring html-loader. But in my case I can't really get it work.

I even tried raw-loader, angularjs-template-loader and ngtemplate-loader too. But webpack always complaining about the loader.

Error: "Module parse failed: Unexpected token (1:0) You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders"

  > <div class="panel">
  |     <div class="panelContent gridContainer">
  |         <div class="dataGridHeader">

Here is is my weback.config.js:

module:{
  rules: [
           {
                test: /\.html$i/,
                loader: 'html-loader',
                // use: [
                //     // {
                //     //     loader: "ngtemplate-loader",
                //     //     options: {
                //     //         relativeTo: "src/",
                //     //     },
                //     // },

                //     // "ngtemplate-loader",
                //     // "raw-loader",
                //     {
                //         loader: "html-loader",
                //         options: {
                //             minimize: true,
                //         },
                //     },
                // ],
         },
  ]
}

Directive:

const directive = {
        restrict: "E",
        template: require("ROOT/app/views/templates/dataGrid.html"),
        controller: dataGridController,
        controllerAs: "vm",
        bindToController: true
  }
 return directive;

I tried commented options in webpack config file above, but all resulting the same error.

My package.json:

Portion of devDependencies:

"devDependencies": {
    "@babel/cli": "^7.0.0",
    "@babel/core": "^7.11.1",
    "@babel/plugin-syntax-dynamic-import": "^7.8.3",
    "@babel/preset-env": "^7.11.0",
    "angularjs-template-loader": "^0.1.2",
    "babel-loader": "^8.0.0",
    "html-loader": "^2.1.2",
    "ngtemplate-loader": "^2.1.0",
    "raw-loader": "^4.0.2",
    "speed-measure-webpack-plugin": "^1.5.0",
    "webpack": "^5.36.2",
    "webpack-cli": "^4.7.0",
    "webpack-dev-server": "^3.11.2",
    "webpack-merge": "^5.7.3"
  }

Anybody facing this issue?

1

There are 1 best solutions below

0
On

We were able to resolve this issue by replacing all imports like import * as templateName from './template.html' with import templateName from './template.html. Hopefully this will help you.