Coder Social home page Coder Social logo

moteesh-in2tive / auto-ngtemplate-loader Goto Github PK

View Code? Open in Web Editor NEW

This project forked from yashdalfthegray/auto-ngtemplate-loader

0.0 1.0 0.0 8.04 MB

Auto require AngularJS 1.x templates in Webpack style

License: MIT License

JavaScript 99.56% Shell 0.44%

auto-ngtemplate-loader's Introduction

npm version Test and lint CodeQL

auto-ngtemplate-loader

Auto require AngularJS 1.x templates in Webpack style

Usage

Install the package by running npm install auto-ngtemplate-loader. Once installed, you can add it to your Webpack config.

module.exports = {
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        use: ['babel-loader', 'other-loaders', 'auto-ngtemplate-loader'],
      },
    ],
  },
};

Note - It is recommended that this loader be run before any transpilation happens so it can operate on unchanged source code.

The next step is to add ngtemplate-loader and a loader that you want to handle your template code with. The most common one is html-loader. This will run every time Webpack encounters require('something.html').

module.exports = {
  module: {
    rules: [
      {
        test: /\.html$/,
        exclude: /node_modules/,
        use: [
          {
            loader: 'ngtemplate-loader',
            options: {
              relativeTo: 'src/',
            },
          },
          {
            loader: 'html-loader',
          },
        ],
      },
    ],
  },
};

It is a good idea to add the relativeTo option for ngtemplate-loader so that the templates aren't put into the Angular template cache with absolute paths which contain platform specific or user specific information. This will affect the portability of the bundled code.

Options

This module supports configuration through either the options object method or the query string method. The valid options are listed in the table below.

Name Type Default Value Details
variableName string autoNgTemplateLoaderTemplate The variable name that gets injected into the compiled code. This is included so that variable collisions can be prevented.
pathResolver (path: string) => string urlToRequest This function can be used to customize the require path in cases where templates don't use relative paths. This function is called with the path of the template and must return a string which is a valid path.

Webpack v1 Compatible Options

Since Webpack v1 only supports query strings for loaders and doesn't allow passing a function as an option, this loader has a useResolverFromConfig boolean option that can be passed in through the query string. The loader will look for the resolver in the Webpack configuration object under the key autoNgTemplateLoader. The only acceptable member of autoNgTemplateLoader is pathResolver which should be a function that returns a string for all cases. An example is below.

module.exports = {
  autoNgTemplateLoader: {
    pathResolver: (p) => p.replace(/src/, '..').substring(1),
  },
  module: {
    loaders: [
      {
        test: /\.js$/,
        exclude: /node_modules/,
        loader:
          'babel-loader!auto-ngtemplate-loader?variableName=testVar&useResolverFromConfig=true',
      },
    ],
  },
};

Note: The loader will throw an error if useResolverFromConfig is used in Webpack v2 or newer. The recommended way to pass the function is through the options in that case. This is because the Loader API v2 has deprecated a property that is used for the v1 workaround. The loader will check the version and report an error.

Development

Please follow the guidelines in the contribution guide for development.

Installation

Once the repository is cloned, run npm install to get all the dependencies. The examples in this package also depend on ngtemplate-loader and html-loader.

Running

There are a few example projects included. One that has one directive, another that has more, one that uses templates from a different folder, and one that uses absolute paths. You can run npm run one-directive, npm run many-directives, npm run multiple-directives, npm run separated-templates, or npm run absolute-paths to see the loader in action. Once successful, examining the build/bundle.js file under the respective examples folder will show the results.

Testing

The tests for this package are written in with ava. They can be run by running npm test.

Linting

This project uses ESLint. All the requisite files can be linted using npm run linter. The rules for this project are located in eslintrc.json. This will also report prettier errors.

Miscellaneous

This project also includes an .nvmrc. This is to tell nvm what version of Node.js to use for this project. It is set to v16 which is the current release. However, we run tests on node LTS versions 10, 12, and 14 as well.

Resources

Issue and PR templates derived from smhxx/atom-ts-transpiler.

auto-ngtemplate-loader's People

Contributors

yashdalfthegray avatar dependabot[bot] avatar muzazu avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.