Skip to content

Commit 8cfcd08

Browse files
authored
Merge pull request #25 from jrjohnson/inline-scripts
Support Inline scripts
2 parents 7eb5546 + b2f3819 commit 8cfcd08

File tree

6 files changed

+119
-7
lines changed

6 files changed

+119
-7
lines changed

README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,45 @@ The root directory where the file matching `fileInputPattern` will be searched f
6767

6868
*Default:* `context.distDir`
6969

70+
### jsonBlueprint
71+
72+
The blueprint indicating what fields to read from the HTML file and convert into JSON.
73+
74+
*Default:*
75+
```javascript
76+
base: {
77+
selector: 'base',
78+
attributes: ['href']
79+
},
80+
meta: {
81+
selector: 'meta[name*="/config/environment"]',
82+
attributes: ['name', 'content']
83+
},
84+
link: {
85+
selector: 'link',
86+
attributes: ['rel', 'href', 'integrity']
87+
},
88+
script: {
89+
selector: 'script',
90+
attributes: ['src', 'integrity'],
91+
includeContent: false,
92+
}
93+
```
94+
95+
### includeContent
96+
By default only the attributes of `<script>` tags are read and included in the
97+
JSON output. If you wish to include the content of the script tag you must specify that.
98+
```javascript
99+
ENV['json-config'] = {
100+
jsonBlueprint(context, pluginHelper) {
101+
var jsonBlueprint = pluginHelper.readConfigDefault('jsonBlueprint');
102+
jsonBlueprint.script.includeContent = true;
103+
104+
return jsonBlueprint;
105+
}
106+
};
107+
```
108+
70109
## What does a converted index.html file look like?
71110

72111
The basic index.html file built by ember-cli will look soemething like this:

index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ module.exports = {
4444
},
4545
script: {
4646
selector: 'script',
47-
attributes: ['src', 'integrity']
47+
attributes: ['src', 'integrity'],
48+
includeContent: false,
4849
}
4950
}
5051
},

lib/utilities/extract-index-config.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
var cheerio = require('cheerio');
22
var RSVP = require('rsvp');
33

4-
function _get($, selector, attributes) {
4+
function _get($, selector, attributes, includeContent) {
55
attributes = attributes || [];
6+
includeContent = includeContent || false;
67
var config = [];
78
var $tags = $(selector);
89

@@ -15,6 +16,11 @@ function _get($, selector, attributes) {
1516
return data;
1617
}, {});
1718

19+
var content = $tag.text().trim();
20+
if (includeContent && content.length) {
21+
data['content'] = content;
22+
}
23+
1824
config.push(data);
1925
});
2026

@@ -29,8 +35,10 @@ module.exports = function(data) {
2935
for(var prop in blueprint) {
3036
var value = blueprint[prop];
3137

32-
if (value.selector && value.attributes) {
33-
json[prop] = _get($, value.selector, value.attributes);
38+
if ('selector' in value && ('attributes' in value || 'includeContent' in value)) {
39+
var attributes = 'attributes' in value?value.attributes:[];
40+
var includeContent = 'includeContent' in value?value.includeContent:false;
41+
json[prop] = _get($, value.selector, value.attributes, value.includeContent);
3442
}
3543
}
3644

tests/fixtures/dist/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@
1717
<body>
1818
<script src="assets/vendor.js" integrity="sha256-oqVuAfXRKap7fdgcCY5uykM6+R9GqQ8K/uxy9rx7HNQlGYl1kPzQho1wx4JwY8wC"></script>
1919
<script src="assets/app.js"></script>
20+
<script>var a = 'foo';</script>
2021
</body>
2122
</html>

tests/unit/index-nodetest.js

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,13 @@ describe('the deploy plugin object', function() {
99
var plugin;
1010
var promise;
1111
var distDir;
12+
var jsonBlueprint;
13+
var context;
1214

1315
before(function() {
1416
fakeRoot = process.cwd() + '/tests/fixtures';
1517
distDir = 'dist';
18+
jsonBlueprint = false;
1619
});
1720

1821
beforeEach(function() {
@@ -28,7 +31,7 @@ describe('the deploy plugin object', function() {
2831
name: 'json-config'
2932
});
3033

31-
var context = {
34+
context = {
3235
ui: mockUi,
3336
config: {
3437
'json-config': {
@@ -44,6 +47,10 @@ describe('the deploy plugin object', function() {
4447
}
4548
};
4649

50+
if (jsonBlueprint) {
51+
context.config['json-config'].jsonBlueprint = jsonBlueprint;
52+
}
53+
4754
plugin.beforeHook(context);
4855
plugin.configure(context);
4956

@@ -63,7 +70,8 @@ describe('the deploy plugin object', function() {
6370
it('generates index.json from index.html', function() {
6471
return assert.isFulfilled(promise)
6572
.then(function() {
66-
var json = require(fakeRoot + '/dist/index.json');
73+
var contents = fs.readFileSync(fakeRoot + '/dist/index.json');
74+
var json = JSON.parse(contents);
6775

6876
assert.equal(Object.keys(json).length, 4);
6977

@@ -73,6 +81,7 @@ describe('the deploy plugin object', function() {
7381
assert.deepEqual(json.link[1], { rel: 'stylesheet', href: 'assets/app.css' });
7482
assert.deepEqual(json.script[0], { src: 'assets/vendor.js', integrity: 'sha256-oqVuAfXRKap7fdgcCY5uykM6+R9GqQ8K/uxy9rx7HNQlGYl1kPzQho1wx4JwY8wC' });
7583
assert.deepEqual(json.script[1], { src: 'assets/app.js' });
84+
assert.deepEqual(json.script[2], { });
7685
});
7786
});
7887

@@ -97,5 +106,31 @@ describe('the deploy plugin object', function() {
97106
});
98107
});
99108
});
109+
110+
describe('when we ask for script tag contents', function() {
111+
before(function() {
112+
jsonBlueprint = {
113+
script: {
114+
selector: 'script',
115+
attributes: [],
116+
includeContent: true,
117+
}
118+
};
119+
});
120+
121+
it('provides the contents of the script tag', function() {
122+
return assert.isFulfilled(promise)
123+
.then(function() {
124+
var contents = fs.readFileSync(fakeRoot + '/dist/index.json');
125+
var json = JSON.parse(contents);
126+
127+
assert.equal(Object.keys(json).length, 1);
128+
129+
assert.deepEqual(json.script[0], {});
130+
assert.deepEqual(json.script[1], {});
131+
assert.deepEqual(json.script[2], { content: "var a = 'foo';"});
132+
});
133+
});
134+
});
100135
});
101136
});

tests/unit/lib/utilities/extract-index-config-nodetest.js

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ describe('extract-index-config', function() {
1111
subject = require('../../../../lib/utilities/extract-index-config');
1212
});
1313

14-
it('extracts the correct config', function() {
14+
it('extracts the correct default config', function() {
1515
var contents = fs.readFileSync(process.cwd() + '/tests/fixtures/dist/index.html');
1616

1717
var plugin = {
@@ -38,6 +38,34 @@ describe('extract-index-config', function() {
3838
assert.deepEqual(json.base[0], { href: '/' });
3939
assert.deepEqual(json.script[0], { src: 'assets/vendor.js' });
4040
assert.deepEqual(json.script[1], { src: 'assets/app.js' });
41+
assert.deepEqual(json.script[2], { });
42+
});
43+
});
44+
45+
it('extracts script contents when specified', function() {
46+
var contents = fs.readFileSync(process.cwd() + '/tests/fixtures/dist/index.html');
47+
48+
var plugin = {
49+
readConfig: function(key) {
50+
return {
51+
script: {
52+
selector: 'script',
53+
attributes: ['src'],
54+
includeContent: true,
55+
}
56+
};
57+
}
58+
};
59+
60+
return assert.isFulfilled(subject.call(plugin, contents))
61+
.then(function(config) {
62+
var json = JSON.parse(config);
63+
64+
assert.equal(Object.keys(json).length, 1);
65+
66+
assert.deepEqual(json.script[0], { src: 'assets/vendor.js' });
67+
assert.deepEqual(json.script[1], { src: 'assets/app.js' });
68+
assert.deepEqual(json.script[2], { content: "var a = 'foo';" });
4169
});
4270
});
4371
});

0 commit comments

Comments
 (0)