Skip to content

Commit 9705d23

Browse files
committed
Merge pull request Urigo#1348 from kamilkisiela/chore/templates-runtime
[WIP] chore(templates): Move common things to angular-templates-runtime
2 parents b3ab343 + edaea0e commit 9705d23

File tree

11 files changed

+186
-460
lines changed

11 files changed

+186
-460
lines changed

packages/angular-blaze-templates-compiler/package.js

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,22 @@
11
Package.describe({
2-
name: "angular-blaze-templates-compiler",
3-
summary: "Compile angular templates into the template cache for .ng extensions",
4-
version: "0.0.1",
5-
git: "https://github.com/Urigo/angular-meteor.git",
2+
name: 'angular-blaze-templates-compiler',
3+
summary: 'Compile angular templates into the template cache for .ng extensions',
4+
version: '0.0.1',
5+
git: 'https://github.com/Urigo/angular-meteor.git',
66
documentation: null
77
});
88

99
Package.registerBuildPlugin({
10-
name: "compileNGTemplate",
10+
name: 'compileNGTemplate',
1111
sources: [
12-
"plugin/ng-caching-html-compiler.js",
13-
"plugin/ng-html-scanner.js",
14-
"plugin/ng-template-compiler.js",
15-
"plugin/ng-annotate.js"
12+
'plugin/ng-caching-html-compiler.js',
13+
'plugin/ng-template-compiler.js',
14+
'plugin/ng-annotate.js'
1615
],
1716
use: [
18-
19-
20-
21-
22-
17+
2318
],
2419
npmDependencies : {
25-
'cheerio': '0.19.0',
26-
'html-minifier' : '0.6.9',
27-
'uglify-js': '2.4.24',
2820
'ng-annotate': '0.15.4'
2921
}
3022
});

packages/angular-blaze-templates-compiler/plugin/ng-caching-html-compiler.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
NgCachingHtmlCompiler = class NgCachingHtmlCompiler extends CachingHtmlCompiler {
22
compileOneFile(inputFile) {
33
const contents = inputFile.getContentsAsString();
4-
var packagePrefix = '';
4+
let packagePrefix = '';
55

66
if (inputFile.getPackageName()) {
7-
packagePrefix += '/packages/' + inputFile.getPackageName() + '/';
7+
packagePrefix += `/packages/${inputFile.getPackageName()}/`;
88
}
99

1010
const inputPath = packagePrefix + inputFile.getPathInPackage();
1111
try {
1212
const tags = this.tagScannerFunc({
1313
sourceName: inputPath,
1414
contents: contents,
15-
tagNames: ["body", "head", "template"]
15+
tagNames: ['body', 'head', 'template']
1616
});
1717

1818
return this.tagHandlerFunc(tags);
Lines changed: 1 addition & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -1,114 +1,5 @@
1-
var htmlMinifier = Npm.require('html-minifier');
2-
31
Plugin.registerCompiler({
42
extensions: ['ng.html'],
53
archMatching: 'web',
64
isTemplate: true
7-
}, () => new NgCachingHtmlCompiler("angular", scanHtmlForTags, compileTagsToStaticHtml));
8-
9-
// Same API as TutorialTools.compileTagsWithSpacebars, but instead of compiling
10-
// with Spacebars, it just returns static HTML
11-
function compileTagsToStaticHtml(tags) {
12-
var handler = new StaticHtmlTagHandler();
13-
14-
tags.forEach((tag) => {
15-
handler.addTagToResults(tag);
16-
});
17-
18-
return handler.getResults();
19-
}
20-
21-
class StaticHtmlTagHandler {
22-
constructor() {
23-
this.results = {
24-
head: '',
25-
body: '',
26-
js: '',
27-
bodyAttrs: {}
28-
};
29-
}
30-
31-
getResults() {
32-
return this.results;
33-
}
34-
35-
addTagToResults(tag) {
36-
this.tag = tag;
37-
38-
// do we have 1 or more attributes?
39-
const hasAttribs = ! _.isEmpty(this.tag.attribs);
40-
41-
if (this.tag.tagName === "head") {
42-
if (hasAttribs) {
43-
this.throwCompileError("Attributes on <head> not supported");
44-
}
45-
46-
this.results.head += this.tag.contents;
47-
return;
48-
}
49-
50-
51-
// <body> or <template>
52-
53-
try {
54-
if (this.tag.tagName === "body") {
55-
this.addBodyAttrs(this.tag.attribs);
56-
57-
// We may be one of many `<body>` tags.
58-
this.results.body += this.tag.contents;
59-
} else if (this.tag.tagName === 'template') {
60-
var contents = minifyHtml(this.tag.contents);
61-
62-
this.results.js += wrapAngularTemplate(this.tag.attribs.id, contents);
63-
}
64-
else {
65-
this.throwCompileError("Expected <head> or <body> tag", this.tag.tagStartIndex);
66-
}
67-
} catch (e) {
68-
if (e.scanner) {
69-
// The error came from Spacebars
70-
this.throwCompileError(e.message, this.tag.contentsStartIndex + e.offset);
71-
} else {
72-
throw e;
73-
}
74-
}
75-
}
76-
77-
addBodyAttrs(attrs) {
78-
Object.keys(attrs).forEach((attr) => {
79-
const val = attrs[attr];
80-
81-
// This check is for conflicting body attributes in the same file;
82-
// we check across multiple files in caching-html-compiler using the
83-
// attributes on results.bodyAttrs
84-
if (this.results.bodyAttrs.hasOwnProperty(attr) && this.results.bodyAttrs[attr] !== val) {
85-
this.throwCompileError(
86-
`<body> declarations have conflicting values for the '${attr}' attribute.`);
87-
}
88-
89-
this.results.bodyAttrs[attr] = val;
90-
});
91-
}
92-
93-
throwCompileError(message, overrideIndex) {
94-
TemplatingTools.throwCompileError(this.tag, message, overrideIndex);
95-
}
96-
}
97-
98-
var minifyHtml = function(html) {
99-
// Just parse the html to make sure it is correct before minifying
100-
HTMLTools.parseFragment(html);
101-
102-
return htmlMinifier.minify(html, {
103-
collapseWhitespace : true,
104-
conservativeCollapse : true,
105-
minifyCSS : true,
106-
minifyJS : true,
107-
processScripts : ['text/template']
108-
});
109-
};
110-
111-
function wrapAngularTemplate(id, contents) {
112-
return "angular.module('angular-meteor').run(['$templateCache', function($templateCache) { $templateCache.put('" +
113-
id + "'," + JSON.stringify(contents) + ");}]);";
114-
}
5+
}, () => new NgCachingHtmlCompiler('angular', AngularTemplates.scanner, AngularTemplates.handler));
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
AngularTemplates = {
2+
scanner: HtmlScan.init,
3+
handler: StaticHtmlTagHandler.init
4+
};

packages/angular-blaze-templates-compiler/plugin/ng-html-scanner.js renamed to packages/angular-templates-runtime/ng-html-scanner.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
scanHtmlForTags = function (options) {
2-
const scan = new HtmlScan(options);
3-
return scan.getTags();
4-
};
5-
61
/**
72
* Scan an HTML file for top-level tags and extract their contents. Pass them to
83
* a tag handler (an object with a handleTag method)
@@ -11,7 +6,7 @@ scanHtmlForTags = function (options) {
116
* top-level tags, which are allowed to have attributes,
127
* and ignores top-level HTML comments.
138
*/
14-
class HtmlScan {
9+
HtmlScan = class HtmlScan {
1510
/**
1611
* Initialize and run a scan of a single file
1712
* @param {String} sourceName The filename, used in errors only
@@ -197,3 +192,8 @@ class HtmlScan {
197192
return this.tags;
198193
}
199194
}
195+
196+
HtmlScan.init = function (options) {
197+
const scan = new HtmlScan(options);
198+
return scan.getTags();
199+
};
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
var htmlMinifier = Npm.require('html-minifier');
2+
// Same API as TutorialTools.compileTagsWithSpacebars, but instead of compiling
3+
// with Spacebars, it just returns static HTML
4+
function compileTagsToStaticHtml(tags) {
5+
var handler = new StaticHtmlTagHandler();
6+
7+
tags.forEach((tag) => {
8+
handler.addTagToResults(tag);
9+
});
10+
11+
return handler.getResults();
12+
}
13+
14+
StaticHtmlTagHandler = class StaticHtmlTagHandler {
15+
constructor() {
16+
this.results = {
17+
head: '',
18+
body: '',
19+
js: '',
20+
bodyAttrs: {}
21+
};
22+
}
23+
24+
getResults() {
25+
return this.results;
26+
}
27+
28+
addTagToResults(tag) {
29+
this.tag = tag;
30+
31+
// do we have 1 or more attributes?
32+
const hasAttribs = ! _.isEmpty(this.tag.attribs);
33+
34+
if (this.tag.tagName === "head") {
35+
if (hasAttribs) {
36+
this.throwCompileError("Attributes on <head> not supported");
37+
}
38+
39+
this.results.head += this.tag.contents;
40+
return;
41+
}
42+
43+
44+
// <body> or <template>
45+
46+
try {
47+
if (this.tag.tagName === "body") {
48+
this.addBodyAttrs(this.tag.attribs);
49+
50+
// We may be one of many `<body>` tags.
51+
this.results.body += this.tag.contents;
52+
} else if (this.tag.tagName === 'template') {
53+
var contents = minifyHtml(this.tag.contents);
54+
55+
this.results.js += wrapAngularTemplate(this.tag.attribs.id, contents);
56+
}
57+
else {
58+
this.throwCompileError("Expected <head> or <body> tag", this.tag.tagStartIndex);
59+
}
60+
} catch (e) {
61+
if (e.scanner) {
62+
// The error came from Spacebars
63+
this.throwCompileError(e.message, this.tag.contentsStartIndex + e.offset);
64+
} else {
65+
throw e;
66+
}
67+
}
68+
}
69+
70+
addBodyAttrs(attrs) {
71+
Object.keys(attrs).forEach((attr) => {
72+
const val = attrs[attr];
73+
74+
// This check is for conflicting body attributes in the same file;
75+
// we check across multiple files in caching-html-compiler using the
76+
// attributes on results.bodyAttrs
77+
if (this.results.bodyAttrs.hasOwnProperty(attr) && this.results.bodyAttrs[attr] !== val) {
78+
this.throwCompileError(
79+
`<body> declarations have conflicting values for the '${attr}' attribute.`);
80+
}
81+
82+
this.results.bodyAttrs[attr] = val;
83+
});
84+
}
85+
86+
throwCompileError(message, overrideIndex) {
87+
TemplatingTools.throwCompileError(this.tag, message, overrideIndex);
88+
}
89+
}
90+
91+
var minifyHtml = function(html) {
92+
// Just parse the html to make sure it is correct before minifying
93+
HTMLTools.parseFragment(html);
94+
95+
return htmlMinifier.minify(html, {
96+
collapseWhitespace : true,
97+
conservativeCollapse : true,
98+
minifyCSS : true,
99+
minifyJS : true,
100+
processScripts : ['text/template']
101+
});
102+
};
103+
104+
function wrapAngularTemplate(id, contents) {
105+
return "angular.module('angular-templates').run(['$templateCache', function($templateCache) { $templateCache.put('" +
106+
id + "'," + JSON.stringify(contents) + ");}]);";
107+
}
108+
109+
StaticHtmlTagHandler.init = function(tags) {
110+
var handler = new StaticHtmlTagHandler();
111+
112+
tags.forEach((tag) => {
113+
handler.addTagToResults(tag);
114+
});
115+
116+
return handler.getResults();
117+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
Package.describe({
2+
name: 'angular-templates-runtime',
3+
summary: 'Compile angular templates into the template cache',
4+
version: '0.0.1',
5+
git: 'https://github.com/Urigo/angular-meteor.git',
6+
documentation: null
7+
});
8+
9+
Npm.depends({
10+
'html-minifier' : '0.6.9',
11+
'uglify-js': '2.4.24',
12+
'cheerio': '0.19.0' // why do we keep it?
13+
});
14+
15+
Package.onUse(function(api) {
16+
api.versionsFrom('[email protected]');
17+
18+
api.use([
19+
20+
21+
22+
23+
], 'server');
24+
25+
api.imply([
26+
27+
28+
29+
], 'server');
30+
31+
api.addFiles([
32+
'ng-html-scanner.js',
33+
'ng-template-compiler.js',
34+
'index.js'
35+
], 'server');
36+
37+
api.export([
38+
'AngularTemplates'
39+
], 'server');
40+
});

packages/angular-templates/package.js

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,11 @@ Package.registerBuildPlugin({
1010
name: 'compileNGTemplate',
1111
sources: [
1212
'plugin/ng-caching-html-compiler.js',
13-
'plugin/ng-html-scanner.js',
1413
'plugin/ng-template-compiler.js'
1514
],
1615
use: [
17-
18-
19-
20-
21-
22-
],
23-
npmDependencies : {
24-
'cheerio': '0.19.0',
25-
'html-minifier' : '0.6.9',
26-
'uglify-js': '2.4.24'
27-
}
16+
17+
]
2818
});
2919

3020
Package.onUse(function(api) {

0 commit comments

Comments
 (0)