• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

laxa1986/gulp-angular-embed-templates: gulp plugin to inject angular templates i ...

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

laxa1986/gulp-angular-embed-templates

开源软件地址(OpenSource Url):

https://github.com/laxa1986/gulp-angular-embed-templates

开源编程语言(OpenSource Language):

JavaScript 93.7%

开源软件介绍(OpenSource Introduction):

gulp-angular-embed-templates

gulp plugin to include the contents of angular templates inside directive's code


Plugin searches for templateUrl: {template url} and replace it with template: {minified template content}. To archive this template first minified with minimize

Nearest neighbours are:

  • gulp-angular-templates - good for single page applications, combine all templates in one module. gulp-angular-embed-templates is better for multi page applications, where different pages use different set of angular directives so combining all templates in one is not an option. For single page applications they are similar but angular-inject-templates doesn't forces you to change your code for using some additional module: just replace template reference with the template code.
  • gulp-include-file - can be used for the same purpose (file include) with minimize plugin as transform functions. gulp-angular-embed-templates do all of this out of the box.

Versions / Release Notes

CHANGELOG on GitHub

Install

npm install --save-dev gulp-angular-embed-templates

Usage (Angular 1.x)

Given the following file structure

src
+-hello-world
  |-hello-world-directive.js
  +-hello-world-template.html

hello-world-directive.js:

angular.module('test').directive('helloWorld', function () {
    return {
        restrict: 'E',
        // relative path to template
        templateUrl: 'hello-world-template.html'
    };
});

hello-world-template.html:

<strong>
    Hello world!
</strong>

gulpfile.js:

var gulp = require('gulp');
var embedTemplates = require('gulp-angular-embed-templates');

gulp.task('js:build', function () {
    gulp.src('src/scripts/**/*.js')
        .pipe(embedTemplates())
        .pipe(gulp.dest('./dist'));
});

gulp-angular-embed-templates will generate the following file:

angular.module('test').directive('helloWorld', function () {
    return {
        restrict: 'E',
        template:'<strong>Hello world!</strong>'
    };
});

Usage (Angular 2.0)

Given the following file structure

src
+-hello-world
  |-hello-world-component.ts
  +-hello-world-template.html

hello-world-component.ts:

class Component extends Directive {
  restrict: string = "E";
  controller: Controller;
  controllerAs: string = "vm";
  templateUrl: string = "angular2-template.html";
}
// or
@View({
    ...
    templateUrl: 'angular2-template.html'
})

angular2-template.html:

<task-cmp [model]="task" (complete)="onCmpl(task)">
    {{index}}
</task-cmp>

gulpfile.js:

var gulp = require('gulp');
var embedTemplates = require('gulp-angular-embed-templates');

gulp.task('js:build', function () {
    gulp.src('src/scripts/**/*.ts') // also can use *.js files
        .pipe(embedTemplates({sourceType:'ts'}))
        .pipe(gulp.dest('./dist'));
});

gulp-angular-embed-templates will generate the following file:

class Component extends Directive {
  restrict: string = "E";
  controller: Controller;
  controllerAs: string = "vm";
  template:string='<task-cmp [model]="task" (complete)="onCmpl(task)">{{index}}</task-cmp>';
}
// or
@View({
    ...
    template:'<task-cmp [model]="task" (complete)="onCmpl(task)">{{index}}</task-cmp>'
})

Note: call embedTemplates before source maps initialization.

API

embedTemplates(options)

options.sourceType

Type: String. Default value: 'js'. Available values:

  • 'js' both for Angular 1.x syntax templateUrl: 'path' and Angular 2.x syntax @View({templateUrl: 'path'})
  • 'ts' additionally support Angular 2.x TypeScript syntax class Component {templateUrl: string = 'path'}

options.basePath

Type: String. By default plugin use path specified in 'templateUrl' as a relative path to corresponding '.js' file (file with 'templateUrl'). This option allow to specify another basePath to search templates as 'basePath'+'templateUrl'

skip one template embedding

The easiest way to skip one concrete is just add some comment like /*!*/ between templateUrl and template path, like this: templateUrl: /*!*/ '/template-path.html'

options.skipFiles

Type: RegExp or Function. By default: do not skip any files. RegExp can test file name to skip template embedding, but this file still be passed in general gulp pipe and be visible for all follow plugins. Function can be used for more detail filtering. Example: function(file) {return file.path.endsWith('-skip-directive.js');}

options.skipTemplates

Type: RegExp or Function. By default: do not skip any templates. RegExp can test concrete templateUrl to skip it (like /\-large\.html$/). Function can be used for more detail filtering. Example: function(templatePath, fileContext) {return templatePath.endsWith('-large.html');}

options.minimize

Type: Object. Default value: {parser: customParser}

settings to pass in minimize plugin. Please see all settings on minimize official page. Please don't specify key 'parser' because it already used for internal purposes

options.skipErrors

Type: Boolean. Default value: 'false'

should plugin brake on errors (file not found, error in minification) or skip errors (warn in logs) and go to next template

options.jsEncoding

Type: String. Default value: 'utf-8'

js files encoding (angular directives)

options.templateEncoding

Type: String. Default value: 'utf-8'

angular template files encoding

options.maxSize

Type: Number. Not specified by default (templates of any size allowed)

define the max size limit in bytes for the template be embedded. Ignore templates which size exceed this limit

License

This module is released under the MIT license.




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap