Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can you compile angular templates with webpack?

The idea is to have 1 JS file out of all html files. and after just to use it via require:

templateUrl: require('./views/user-list.html')

Could you share you experience please? I was googling for it and found several loaders for webpack but not sure what to use.

like image 885
Stepan Suvorov Avatar asked Oct 21 '15 20:10

Stepan Suvorov


People also ask

How does Angular integrate with webpack?

In order to use a custom webpack config, you will need to add @angular-builders/custom-webpack and @angular-builders/dev-server to your project as devDependency packages: npm install --save-dev @angular-builders/custom-webpack @10.0.

Does webpack compile?

Webpack is used to compile JavaScript modules. Once installed, you can interact with webpack either from its CLI or API. If you're still new to webpack, please read through the core concepts and this comparison to learn why you might use it over the other tools that are out in the community.

Does ng build use webpack?

The application builder uses the webpack build tool, with default configuration options specified in the workspace configuration file ( angular.json ) or with a named alternative configuration.

What are the alternatives to webpack and what is your recommended approach for building Angular projects?

There are some alternatives to Webpack that provide the same functionality as webpack. These alternatives are gulp, babel, parcel, browserify, grunt, npm, and requireJS.


2 Answers

I decided to use ng-cache loader for webpack together with ES2015 syntax: it means import from instead of require.

Part of my webpack config:

module: {
    loaders: [
      { test: /\.js$/, loader: 'babel', include: APP },
      { test: /\.html$/, loader: 'ng-cache?prefix=[dir]/[dir]' },
    ]
  },

and example of directive with template:

import avatarTemplate from './views/avatar.html';

const avatar = function() {
  return {
    restrict: 'E',
    replace: true,
    scope: {
      user: '='
    },
    template: avatarTemplate
  }
};

export default avatar;
like image 114
Stepan Suvorov Avatar answered Sep 22 '22 09:09

Stepan Suvorov


your answer is right. However just offering alternative for it:

const avatar = function() {
  return {
    restrict: 'E',
    replace: true,
    scope: {
      user: '='
    },
    template: require("../path/to/file.html"),
  }
};

export default avatar;
like image 44
Andurit Avatar answered Sep 20 '22 09:09

Andurit