npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

simple-ngtemplate-loader

v2.0.1

Published

Include AngularJS templates in the Webpack bundle WITHOUT preload the template cache. Forked from https://github.com/WearyMonkey/ngtemplate-loader

Downloads

3

Readme

AngularJS Template loader for webpack

Based on ngtemplate-loader

Includes your AngularJS templates into your webpack Javascript Bundle.

SimpleNgTemplate loader does not minify or process your HTML at all, and instead uses the standard loaders such as html-loader or raw-loader. This gives you the flexibility to pick and choose your HTML loaders.

Install

npm install simple-ngtemplate-loader --save-dev

Usage

Documentation: Using loaders

SimpleNgTemplate loader will export the path of the HTML file, so you can use require directly AngularJS with templateUrl parameters e.g.

var templateUrl = require('simple-ngtemplate-loader!html!./test.html');

app.directive('testDirective', function() {
    return {
        restrict: 'E',
        templateUrl: templateUrl
    }
});

To remove the extra require, check out the Baggage Example below.

Beware of requiring from the directive definition

The following code is wrong, Because it'll operate only after angular bootstraps:

app.directive('testDirective', function() {
    return {
        restrict: 'E',
        templateUrl: require('simple-ngtemplate-loader!html!./test.html') // <- WRONG !
    }
});

relativeTo and prefix

You can set the base path of your templates using relativeTo and prefix parameters. relativeTo is used to strip a matching prefix from the absolute path of the input html file. prefix is then appended to path.

The prefix of the path up to and including the first relativeTo match is stripped, e.g.

require('!simple-ngtemplate-loader?relativeTo=/src/!html!/test/src/test.html');
// c.put('test.html', ...)

To match the from the start of the absolute path prefix a '//', e.g.

require('!simple-ngtemplate-loader?relativeTo=//Users/WearyMonkey/project/test/!html!/test/src/test.html');
// c.put('src/test.html', ...)

You can combine relativeTo and prefix to replace the prefix in the absolute path, e.g.

require('!simple-ngtemplate-loader?relativeTo=src/&prefix=build/!html!/test/src/test.html');
// c.put('build/test.html', ...)

Parameter Interpolation

relativeTo and prefix parameters are interpolated using Webpack's standard interpolation rules. Interpolation regular expressions can be passed using the extra parameters relativeToRegExp and prefixRegExp which apply to single parameters, or regExp which will apply to all three parameters.

Path Separators (Or using on Windows)

By default, SimpleNgTemplate loader will assume you are using unix style path separators '/' for html paths in your project. e.g. templateUrl: '/views/app.html'. If however you want to use Window's style path separators '' e.g. templateUrl: '\\views\\app.html' you can override the separator by providing the pathSep parameter.

require('simple-ngtemplate-loader?pathSep=\\!html!.\\test.html')

Make sure you use the same path separator for the prefix and relativeTo parameters, all templateUrls and in your webpack.config.js file.

Webpack Config

It's recommended to adjust your webpack.config so simple-ngtemplate-loader!html! is applied automatically on all files ending with .html. For Webpack 1 this would be something like:

module.exports = {
  module: {
    loaders: [
      {
        test: /\.html$/,
        loader: 'simple-ngtemplate-loader?relativeTo=' + (path.resolve(__dirname, './app')) + '/!html'
      }
    ]
  }
};

For Webpack 2 this would be something like:

module.exports = {
  module: {
    rules: [
      {
        test: /\.html$/,
        use: [
          { loader:'simple-ngtemplate-loader?relativeTo=' + (path.resolve(__dirname, './app')) },
          { loader: 'html-loader' }
        ]
      }
    ]
  }
};

Make sure you already have html-loader installed. Then you only need to write: require('file.html').

Baggage Example

SimpleNgTemplate loader works well with the Baggage Loader to remove all those extra HTML and CSS requires. See an example of a directive and webpack.config.js below. Or take a look at more complete example in the examples/baggage folder.

With a folder structure:

app/
├── app.js
├── index.html
├── webpack.config.js
└── my-directive/
    ├── my-directive.js
    ├── my-directive.css
    └── my-directive.html

and a webpack.config.js for webpack 1 like:

module.exports = {
  module: {
    preLoaders: [
      { 
        test: /\.js$/, 
        loader: 'baggage?[file].html&[file].css' 
      }
    ],
    loaders: [
      {
        test: /\.html$/,
        loader: 'simple-ngtemplate-loader?relativeTo=' + __dirname + '/!html'
      }
    ]
  }
};

For webpack 2 like:

module.exports = {
  module: {
    rules: [
      { 
        test: /\.js$/, 
        enforce: 'pre',
        use: [{ loader:'baggage?[file].html&[file].css'  }]
      },
      {
        test: /\.html$/,
        use: [
          { loader: 'simple-ngtemplate-loader?relativeTo=' + __dirname + '/' },
          { loader: 'html-loader' }]
        ]
      }
    ]
  }
};

You can now skip the initial require of html and css like so:

app.directive('myDirective', function() {
    return {
        restrict: 'E',
        templateUrl: require('./my-directive.html')
    }
});

License

MIT (http://www.opensource.org/licenses/mit-license.php)