forked from gpoitch/ember-cli-inline-content
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
93 lines (77 loc) · 2.73 KB
/
index.js
File metadata and controls
93 lines (77 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
var fs = require('fs');
var path = require('path');
var UglifyJS = require('uglify-js');
var CleanCSS = require('clean-css');
function InlineContentRenderer(project) {
this.name = 'ember-cli-inline-content';
this.project = project;
}
InlineContentRenderer.prototype.included = function(app) {
this.options = app.options || {};
};
InlineContentRenderer.prototype.contentFor = function(type, config) {
var inlineContent = this.options.inlineContent;
var inlineContentForType = inlineContent && inlineContent[type];
var contentOptions, filePath, content;
if(inlineContentForType) {
contentOptions = ('object' === typeof inlineContentForType) && inlineContentForType;
if (contentOptions && contentOptions.enabled !== undefined && Boolean(contentOptions.enabled) === false) {
return;
}
if (contentOptions && contentOptions.content) {
content = contentOptions.content;
} else {
filePath = contentOptions && contentOptions.file || inlineContentForType;
content = this.readFile(filePath);
}
if ('function' === typeof contentOptions.postProcess) {
content = contentOptions.postProcess(content);
}
if (filePath) {
switch (path.extname(filePath)) {
case '.js':
return this.renderScript(content, contentOptions);
case '.css':
return this.renderStyle(content, contentOptions);
}
}
return content;
}
};
InlineContentRenderer.prototype.readFile = function(filePath) {
if (!filePath) {
return console.log(this.name + ' error: file path not defined');
}
var fullPath = path.join(this.project.root, filePath);
try {
return fs.readFileSync(fullPath, 'utf8');
} catch(e){
return console.log(this.name + ' error: file not found: ' + fullPath);
}
};
InlineContentRenderer.prototype.renderContentWithTag = function(content, tag, options) {
var attrs = options && options.attrs;
var openTag = '<' + tag;
var closeTag = '</' + tag + '>';
for (var attr in attrs) {
if (attrs.hasOwnProperty(attr)) {
openTag += ' ' + attr + '="' + attrs[attr] + '"';
}
}
openTag += '>';
return [openTag, content, closeTag].join('\n');
};
InlineContentRenderer.prototype.renderScript = function(content, options) {
if (this.options.minifyJS.enabled) {
var uglifyOptions = { fromString: true };
content = UglifyJS.minify(content, uglifyOptions).code;
}
return this.renderContentWithTag(content, 'script', options);
};
InlineContentRenderer.prototype.renderStyle = function(content, options) {
if (this.options.minifyCSS.enabled) {
content = new CleanCSS(this.options.minifyCSS.options).minify(content);
}
return this.renderContentWithTag(content, 'style', options);
};
module.exports = InlineContentRenderer;