Skip to content

Commit 510324c

Browse files
author
SomaticIT
committed
Merge branch 'megawac-master'
2 parents 84a4f14 + 4b417d7 commit 510324c

File tree

3 files changed

+91
-71
lines changed

3 files changed

+91
-71
lines changed

index.html

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,24 @@
44
<!-- build:style bundle -->
55
<link rel="stylesheet" type="text/css" href="/path/to/css/dev.css" />
66
<!-- /build -->
7+
<!-- build:style bundle [media="screen and (max-width: 480px)"] -->
8+
<link rel="stylesheet" type="text/css" href="/path/to/css/dev.css" />
9+
<!-- /build -->
710
<!-- build:style inline test -->
811
<link rel="stylesheet" type="text/css" href="/path/to/css/dev-inline.css" />
912
<!-- /build -->
13+
<!-- build:style inline test [media="screen and (max-width: 480px)"] -->
14+
<link rel="stylesheet" type="text/css" href="/path/to/css/dev-inline.css" />
15+
<!-- /build -->
1016
</head>
1117
<body id="landing-page">
1218
<!-- build:section views -->
1319
<!-- /build -->
1420
<!-- build:section recursive views -->
1521
<!-- /build -->
16-
1722
<!-- build:remove -->
1823
<script type="text/javascript" src="/path/to/js/only-dev.js"></script>
1924
<!-- /build -->
20-
2125
<!-- build:script bundle -->
2226
<script type="text/javascript" src="/path/to/js/libs/jquery.js"></script>
2327
<script type="text/javascript" src="/path/to/js/libs/knockout.js"></script>
@@ -39,16 +43,15 @@
3943
title = "<%= title %>";
4044
</script>
4145
<!-- /build -->
42-
<!-- build:script defer main -->
46+
<!-- build:script main [defer] -->
4347
<!-- /build -->
44-
<!-- build:script async main -->
48+
<!-- build:script main [async] -->
4549
<!-- /build -->
4650
<!-- build:script inline main -->
4751
<script type="text/javascript">
4852
main();
4953
</script>
5054
<!-- /build -->
51-
5255
<!-- build:section optional test -->
5356
<!-- /build -->
5457
</body>

samples/index.html

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,19 @@
55
<link type="text/css" rel="stylesheet" href="../fixtures/css/libs.css" />
66
<link type="text/css" rel="stylesheet" href="../fixtures/css/dev.css" />
77
<link type="text/css" rel="stylesheet/less" href="../fixtures/css/another.less" />
8+
<link type="text/css" rel="stylesheet" media="screen and (max-width: 480px)" href="../fixtures/css/libs.css" />
9+
<link type="text/css" rel="stylesheet" media="screen and (max-width: 480px)" href="../fixtures/css/dev.css" />
10+
<link type="text/css" rel="stylesheet/less" media="screen and (max-width: 480px)" href="../fixtures/css/another.less" />
811
<style>
912
.this-is-inline {
1013
font-weight: bold;
1114
}
1215
</style>
16+
<style media="screen and (max-width: 480px)">
17+
.this-is-inline {
18+
font-weight: bold;
19+
}
20+
</style>
1321
</head>
1422

1523
<body id="landing-page">
@@ -34,8 +42,6 @@ <h1>test - 0.1.0</h1>
3442
<div id="view2"> ... </div>
3543
<div id="view3"> ... </div>
3644

37-
38-
3945
<script type="text/javascript" src="../fixtures/scripts/app.js"></script>
4046
<script type="text/javascript" src="../fixtures/scripts/libs.js"></script>
4147
<script type="text/javascript" src="//cdn.jsdelivr.net/jquery/2.1.0/jquery.min.js"></script>
@@ -52,13 +58,12 @@ <h1>test - 0.1.0</h1>
5258
title = "test";
5359
</script>
5460

55-
<script type="text/javascript" src="../fixtures/scripts/main.js" defer></script>
56-
<script type="text/javascript" src="../fixtures/scripts/main.js" async></script>
61+
<script type="text/javascript" defer src="../fixtures/scripts/main.js"></script>
62+
<script type="text/javascript" async src="../fixtures/scripts/main.js"></script>
5763
<script type="text/javascript">
5864
productionMain();
5965
</script>
6066

61-
6267
</body>
6368

6469
</html>

tasks/build-html.js

Lines changed: 73 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ module.exports = function (grunt) {
4040
},
4141

4242
// Tags Regular Expressions
43-
regexTagStartTemplate = "<!--\\s*%parseTag%:(\\w+)\\s*(inline)?\\s*(optional)?\\s*(recursive)?\\s*(noprocess)?\\s*(defer)?\\s*(async)?\\s*([^\\s]*)\\s*-->", // <!-- build:{type} [inline] [optional] [recursive] {name} --> {} required [] optional
43+
regexTagStartTemplate = "<!--\\s*%parseTag%:(\\w+)\\s*(inline)?\\s*(optional)?\\s*(recursive)?\\s*(noprocess)?\\s*([^\\s]*)\\s*(?:\\[(.*)\\])?\\s*-->", // <!-- build:{type} (inline) (optional) (recursive) {name} [attributes...] --> {} required () optional
4444
regexTagEndTemplate = "<!--\\s*\\/%parseTag%\\s*-->", // <!-- /build -->
4545
regexTagStart = "",
4646
regexTagEnd = "",
@@ -68,9 +68,8 @@ module.exports = function (grunt) {
6868
optional: !!tagStart[3],
6969
recursive: !!tagStart[4],
7070
noprocess: !!tagStart[5],
71-
defer: !!tagStart[6],
72-
async: !!tagStart[7],
73-
name: tagStart[8],
71+
name: tagStart[6],
72+
attributes: tagStart[7],
7473
lines: []
7574
};
7675
tags.push(last);
@@ -122,16 +121,16 @@ module.exports = function (grunt) {
122121
}
123122
else {
124123
// if paths are named, just take values
125-
files = _.values(src);
124+
files = _.values(src);
126125
}
127126
}
128127

129128
if (!Array.isArray(files)) {
130129
files = [files];
131-
}
130+
}
132131

133132
return params.processPath(files, params, opt);
134-
}
133+
}
135134
}
136135
function validateBlockAlways(tag) {
137136
return true;
@@ -146,46 +145,59 @@ module.exports = function (grunt) {
146145

147146
//#region Processors Methods
148147

149-
function createTemplateData(options, extend) {
148+
function createTemplateData(options, src, attrs) {
149+
var extend = {};
150+
151+
if (src) {
152+
extend.src = src;
153+
}
154+
155+
if (attrs) {
156+
extend.attributes = attrs;
157+
}
158+
150159
return {
151-
data: extend ? _.extend({}, options.data, extend) : options.data
160+
data: _.extend({}, options.data, extend)
152161
};
153162
}
154-
function processTemplate(template, options, extend) {
155-
return grunt.template.process(template, createTemplateData(options, extend));
163+
function processTemplate(template, options, src, attrs) {
164+
return grunt.template.process(template, createTemplateData(options, src, attrs));
156165
}
157-
function processHtmlTagTemplate(options, extend) {
158-
var template = templates[options.type + (options.inline ? "-inline" : "")];
159-
if (!options.inline) {
160-
var result = template.replace("<%= src %>", extend.src);
166+
function createAttributes(options, src) {
167+
var attrs = options.attributes || "";
161168

162-
if (options.type === "style" && path.extname(extend.src) === ".less") {
163-
result = result.replace('rel="stylesheet"', 'rel="stylesheet/less"');
169+
if (options.type === "script") {
170+
attrs = 'type="text/javascript" ' + attrs;
171+
}
172+
else if (options.type === "style" && !options.inline) {
173+
if (path.extname(src) === ".less") {
174+
attrs = 'type="text/css" rel="stylesheet/less" ' + attrs;
164175
}
165-
166-
if (options.type === "script") {
167-
if (options.async) {
168-
result = result.replace(">", " async>");
169-
}
170-
else if (options.defer) {
171-
result = result.replace(">", " defer>");
172-
}
176+
else {
177+
attrs = 'type="text/css" rel="stylesheet" ' + attrs;
173178
}
174-
175-
return result;
176179
}
177-
else if (options.noprocess) {
178-
return template.replace("<%= src %>", extend.src);
180+
181+
return attrs.trim();
182+
}
183+
function processHtmlTagTemplate(options, src) {
184+
var template = templates[options.type + (options.inline ? "-inline" : "")],
185+
attrs = createAttributes(options, src);
186+
187+
if (!options.inline || options.noprocess) {
188+
return template
189+
.replace("<%= src %>", src)
190+
.replace("<%= attributes %>", attrs);
179191
}
180192
else {
181-
return processTemplate(template, options, extend);
193+
return processTemplate(template, options, src, attrs);
182194
}
183195
}
184196

185197
function processHtmlTag(options) {
186198
if (options.inline) {
187199
var content = options.files.map(grunt.file.read).join(EOL);
188-
return processHtmlTagTemplate(options, { src: content });
200+
return processHtmlTagTemplate(options, content);
189201
}
190202
else {
191203
return options.files.map(function (f) {
@@ -196,8 +208,8 @@ module.exports = function (grunt) {
196208
if (options.prefix) {
197209
url = URL.resolve(options.prefix.replace(/\\/g, '/'), url);
198210
}
199-
200-
return processHtmlTagTemplate(options, { src: url });
211+
212+
return processHtmlTagTemplate(options, url);
201213
}).join(EOL);
202214
}
203215
}
@@ -208,10 +220,10 @@ module.exports = function (grunt) {
208220

209221
var
210222
templates = {
211-
'script': '<script type="text/javascript" src="<%= src %>"></script>',
212-
'script-inline': '<script type="text/javascript"><%= src %></script>',
213-
'style': '<link type="text/css" rel="stylesheet" href="<%= src %>" />',
214-
'style-inline': '<style><%= src %></style>'
223+
'script': '<script <%= attributes %> src="<%= src %>"></script>',
224+
'script-inline': '<script <%= attributes %>><%= src %></script>',
225+
'style': '<link <%= attributes %> href="<%= src %>" />',
226+
'style-inline': '<style <%= attributes %>><%= src %></style>'
215227
},
216228
validators = {
217229
script: validateBlockWithName,
@@ -273,45 +285,45 @@ module.exports = function (grunt) {
273285
var tags = getBuildTags(content),
274286
config = grunt.config();
275287

276-
tags.forEach(function (tag) {
277-
var raw = tag.lines.join(EOL),
278-
result = "",
279-
tagFiles = validators.validate(tag, params);
280-
281-
if (tagFiles) {
282-
var options = _.extend({}, tag, {
283-
data: _.extend({}, config, params.data),
284-
files: tagFiles,
285-
dest: dest,
286-
prefix: params.prefix,
288+
tags.forEach(function (tag) {
289+
var raw = tag.lines.join(EOL),
290+
result = "",
291+
tagFiles = validators.validate(tag, params);
292+
293+
if (tagFiles) {
294+
var options = _.extend({}, tag, {
295+
data: _.extend({}, config, params.data),
296+
files: tagFiles,
297+
dest: dest,
298+
prefix: params.prefix,
287299
relative: params.relative,
288300
params: params
289-
});
301+
});
290302

291-
result = processors.transform(options);
292-
}
303+
result = processors.transform(options);
304+
}
293305
else if (tagFiles === false) {
294306
grunt.log.warn("Unknown tag detected: '" + tag.type + "'");
295307

296308
if (!params.allowUnknownTags) {
297309
grunt.fail.warn("Use 'parseTag' or 'allowUnknownTags' options to avoid this issue");
298310
}
299311
}
300-
else if (tag.optional) {
312+
else if (tag.optional) {
301313
if (params.logOptionals) {
302314
grunt.log.warn("Tag with type: '" + tag.type + "' and name: '" + tag.name + "' is not configured in your Gruntfile.js but is set optional, deleting block !");
303315
}
304-
}
305-
else {
306-
grunt.fail.warn("Tag with type '" + tag.type + "' and name: '" + tag.name + "' is not configured in your Gruntfile.js !");
307-
}
316+
}
317+
else {
318+
grunt.fail.warn("Tag with type '" + tag.type + "' and name: '" + tag.name + "' is not configured in your Gruntfile.js !");
319+
}
308320

309-
content = content.replace(raw, function () { return result });
310-
});
321+
content = content.replace(raw, function () { return result });
322+
});
311323

312-
if (params.beautify) {
313-
content = beautify.html(content, _.isObject(params.beautify) ? params.beautify : {});
314-
}
324+
if (params.beautify) {
325+
content = beautify.html(content, _.isObject(params.beautify) ? params.beautify : {});
326+
}
315327

316328
return content;
317329
}

0 commit comments

Comments
 (0)