amphora-html 6.0.0-1 → 6.0.0-5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.envrc +1 -1
- package/lib/media.js +7 -22
- package/lib/media.test.js +112 -1
- package/package.json +2 -1
- package/.vscode/.history/amphora-html/lib/media_20210422170004.js +0 -418
- package/.vscode/.history/amphora-html/lib/media_20210423114100.js +0 -419
- package/.vscode/.history/amphora-html/lib/media_20210423114125.js +0 -420
- package/.vscode/.history/amphora-html/lib/media_20210423121821.js +0 -420
- package/.vscode/.history/amphora-html/lib/media_20210423121839.js +0 -420
- package/.vscode/.history/amphora-html/lib/media_20210423133939.js +0 -420
- package/.vscode/.history/amphora-html/lib/media_20210423143343.js +0 -420
- package/.vscode/.history/amphora-html/lib/media_20210423143445.js +0 -420
- package/.vscode/.history/amphora-html/lib/media_20210430141402.js +0 -423
- package/.vscode/.history/amphora-html/lib/media_20210430142038.js +0 -432
- package/.vscode/.history/amphora-html/lib/media_20210430142153.js +0 -434
- package/.vscode/.history/amphora-html/lib/media_20210430142400.js +0 -434
- package/.vscode/.history/amphora-html/lib/media_20210430142437.js +0 -436
- package/.vscode/.history/amphora-html/lib/media_20210430142934.js +0 -436
- package/.vscode/.history/amphora-html/lib/media_20210430143110.js +0 -437
- package/.vscode/.history/amphora-html/lib/media_20210430143347.js +0 -441
- package/.vscode/.history/amphora-html/lib/media_20210430143354.js +0 -441
- package/.vscode/.history/amphora-html/lib/media_20210430143517.js +0 -442
- package/.vscode/.history/amphora-html/lib/media_20210430143831.js +0 -446
- package/.vscode/.history/amphora-html/lib/media_20210430144135.js +0 -446
- package/.vscode/.history/amphora-html/lib/media_20210430144206.js +0 -447
- package/.vscode/.history/amphora-html/lib/media_20210430144435.js +0 -458
- package/.vscode/.history/amphora-html/lib/media_20210503102940.js +0 -449
- package/.vscode/.history/amphora-html/lib/media_20210503103055.js +0 -435
- package/.vscode/.history/amphora-html/lib/media_20210503105800.js +0 -435
- package/.vscode/.history/amphora-html/lib/media_20210503115000.js +0 -435
- package/.vscode/.history/amphora-html/lib/media_20210503120729.js +0 -455
- package/.vscode/.history/amphora-html/lib/media_20210503120737.js +0 -455
- package/.vscode/.history/amphora-html/lib/media_20210503120859.js +0 -455
- package/.vscode/.history/amphora-html/lib/media_20210503121446.js +0 -455
- package/.vscode/.history/amphora-html/lib/media_20210503123023.js +0 -455
- package/.vscode/.history/amphora-html/lib/media_20210503123146.js +0 -435
- package/.vscode/.history/amphora-html/lib/media_20210503123207.js +0 -435
- package/.vscode/.history/amphora-html/lib/media_20210503123232.js +0 -458
- package/.vscode/.history/amphora-html/lib/media_20210503123433.js +0 -435
- package/.vscode/.history/amphora-html/lib/media_20210503124026.js +0 -435
- package/.vscode/.history/amphora-html/lib/media_20210503124152.js +0 -444
- package/.vscode/.history/amphora-html/lib/media_20210503124256.js +0 -455
- package/.vscode/.history/amphora-html/lib/media_20210503124650.js +0 -462
- package/.vscode/.history/amphora-html/lib/media_20210503124855.js +0 -453
- package/.vscode/.history/amphora-html/lib/media_20210503130202.js +0 -453
- package/.vscode/.history/amphora-html/lib/media_20210503130230.js +0 -453
- package/.vscode/.history/amphora-html/lib/media_20210503130932.js +0 -456
- package/.vscode/.history/amphora-html/lib/media_20210503131700.js +0 -457
- package/.vscode/.history/amphora-html/lib/media_20210503131800.js +0 -457
- package/.vscode/.history/amphora-html/lib/media_20210503131943.js +0 -457
- package/.vscode/.history/amphora-html/lib/media_20210503132029.js +0 -457
- package/.vscode/.history/amphora-html/lib/media_20210503135814.js +0 -457
- package/.vscode/.history/amphora-html/lib/media_20210503140648.js +0 -457
- package/.vscode/.history/amphora-html/lib/media_20210503161154.js +0 -427
- package/.vscode/.history/amphora-html/lib/media_20210503161242.js +0 -427
- package/.vscode/.history/amphora-html/lib/media_20210503165324.js +0 -427
- package/.vscode/.history/amphora-html/lib/media_20210503171517.js +0 -427
- package/.vscode/.history/amphora-html/lib/media_20210503173501.js +0 -427
- package/.vscode/.history/amphora-html/lib/media_20210504114616.js +0 -427
- package/.vscode/.history/amphora-html/lib/media_20210504121950.js +0 -427
- package/.vscode/.history/amphora-html/lib/media_20210504122005.js +0 -427
- package/.vscode/.history/amphora-html/lib/media_20210504122119.js +0 -427
- package/.vscode/.history/amphora-html/lib/media_20210504122215.js +0 -427
- package/.vscode/.history/amphora-html/lib/media_20210504161319.js +0 -427
- package/.vscode/.history/amphora-html/lib/media_20210504163440.js +0 -427
- package/.vscode/.history/amphora-html/lib/media_20210505105706.js +0 -427
- package/.vscode/.history/amphora-html/lib/media_20210505112458.js +0 -427
- package/.vscode/.history/amphora-html/lib/media_20210511135806.js +0 -434
- package/.vscode/.history/amphora-html/lib/media_20210512105948.js +0 -434
- package/.vscode/.history/amphora-html/lib/media_20210512110245.js +0 -434
- package/.vscode/.history/amphora-html/lib/media_20210512114913.js +0 -434
- package/.vscode/.history/amphora-html/lib/media_20210512114917.js +0 -434
- package/.vscode/.history/amphora-html/lib/media_20210512122725.js +0 -429
- package/.vscode/.history/amphora-html/lib/media_20210512123057.js +0 -429
- package/.vscode/.history/amphora-html/lib/media_20210512123102.js +0 -429
- package/.vscode/.history/amphora-html/lib/media_20210512123201.js +0 -429
- package/.vscode/.history/amphora-html/lib/media_20210512152244.js +0 -429
- package/.vscode/.history/amphora-html/lib/media_20210512152536.js +0 -433
- package/.vscode/.history/amphora-html/lib/media_20210512152541.js +0 -433
- package/.vscode/.history/amphora-html/lib/media_20210512152717.js +0 -433
- package/.vscode/.history/amphora-html/lib/media_20210512152931.js +0 -419
- package/.vscode/.history/amphora-html/lib/media_20210512152949.js +0 -420
- package/.vscode/.history/amphora-html/lib/media_20210512152958.js +0 -418
- package/.vscode/.history/amphora-html/lib/media_20210512155619.js +0 -418
- package/.vscode/.history/amphora-html/lib/media_20210512164028.js +0 -418
- package/.vscode/.history/amphora-html/lib/media_20210512170157.js +0 -418
- package/.vscode/.history/amphora-html/lib/media_20210512171030.js +0 -418
|
@@ -1,462 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
const _ = require('lodash'),
|
|
4
|
-
path = require('path'),
|
|
5
|
-
files = require('nymag-fs'),
|
|
6
|
-
bluebird = require('bluebird'),
|
|
7
|
-
setup = require('./setup'),
|
|
8
|
-
styleguide = require('./styleguide'),
|
|
9
|
-
STYLE_TAG = 'style',
|
|
10
|
-
SCRIPT_TAG = 'scripts',
|
|
11
|
-
ASYNC_SCRIPT_TAG = 'async',
|
|
12
|
-
DEFER_SCRIPT_TAG = 'defer',
|
|
13
|
-
ASYNC_DEFER_SCRIPT_TAG = 'async-defer',
|
|
14
|
-
MEDIA_DIRECTORY = path.join(process.cwd(), 'public');
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* @param {string} str
|
|
18
|
-
* @returns {number}
|
|
19
|
-
*/
|
|
20
|
-
function findBottom(str) {
|
|
21
|
-
var index = str.lastIndexOf('</body>');
|
|
22
|
-
|
|
23
|
-
if (index === -1) {
|
|
24
|
-
index = str.lastIndexOf('</');
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
return index;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* @param {string} str
|
|
32
|
-
* @returns {number}
|
|
33
|
-
*/
|
|
34
|
-
function findTop(str) {
|
|
35
|
-
var index = str.indexOf('</head>');
|
|
36
|
-
|
|
37
|
-
if (index === -1) {
|
|
38
|
-
index = str.indexOf('>') + 1;
|
|
39
|
-
}
|
|
40
|
-
return index;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* Put items at index in the very large target string.
|
|
45
|
-
*
|
|
46
|
-
* @param {string} str
|
|
47
|
-
* @param {number} index
|
|
48
|
-
* @param {[string]} items
|
|
49
|
-
* @returns {string}
|
|
50
|
-
*/
|
|
51
|
-
function splice(str, index, items) {
|
|
52
|
-
return str.substr(0, index) + items + str.substr(index);
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* Get the contents of a string that come after specified characters.
|
|
57
|
-
*
|
|
58
|
-
* @param {string} str Any string to split
|
|
59
|
-
* @param {string} dir The directory the file is in
|
|
60
|
-
* @return {string}
|
|
61
|
-
*/
|
|
62
|
-
function getFileName(str, dir) {
|
|
63
|
-
return str.split(dir)[1];
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* Retrieve the contents of a file
|
|
68
|
-
*
|
|
69
|
-
* @param {array} fileArray An array of file names
|
|
70
|
-
* @param {string} targetDir The directory to retrieve files from
|
|
71
|
-
* @param {string} filePrefix The string that comes right before the file name
|
|
72
|
-
* @return {Promise}
|
|
73
|
-
*/
|
|
74
|
-
function getContentsOfFiles(fileArray, targetDir, filePrefix) {
|
|
75
|
-
var allPromises = [],
|
|
76
|
-
currentDir = process.cwd();
|
|
77
|
-
|
|
78
|
-
fileArray.forEach(file => {
|
|
79
|
-
allPromises.push(files.readFilePromise(path.join(currentDir, targetDir, getFileName(file, filePrefix))));
|
|
80
|
-
});
|
|
81
|
-
|
|
82
|
-
return Promise.all(allPromises)
|
|
83
|
-
.catch(err => {
|
|
84
|
-
throw err;
|
|
85
|
-
});
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* Wraps a string with HTML tags
|
|
90
|
-
* @param {string} string The string to wrap
|
|
91
|
-
* @param {string} tag The HTML tag to use
|
|
92
|
-
* @return {string}
|
|
93
|
-
*/
|
|
94
|
-
function wrapWithTags(string, tag) {
|
|
95
|
-
if (tag === SCRIPT_TAG) {
|
|
96
|
-
return `<script type="text/javascript">
|
|
97
|
-
// <![CDATA[
|
|
98
|
-
${string}
|
|
99
|
-
// ]]
|
|
100
|
-
</script>`;
|
|
101
|
-
} else {
|
|
102
|
-
return `<style>${string}</style>`;
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
/**
|
|
107
|
-
* Concatenates an array of files into one string.
|
|
108
|
-
*
|
|
109
|
-
* @param {array} fileArray An array of files
|
|
110
|
-
* @param {string} directory The directory in which `fs` will look for the file
|
|
111
|
-
* @param {string} filePrefix The directory path before the filename
|
|
112
|
-
* @param {string|null} tag The type of tag to wrap the contents in.
|
|
113
|
-
* @return {string}
|
|
114
|
-
*/
|
|
115
|
-
function combineFileContents(fileArray, directory, filePrefix, tag) {
|
|
116
|
-
if (!fileArray || !fileArray.length) {
|
|
117
|
-
return false;
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
// If there are files, retrieve contents
|
|
121
|
-
return getContentsOfFiles(fileArray, directory, filePrefix)
|
|
122
|
-
.then(function (fileContents) {
|
|
123
|
-
return wrapWithTags(fileContents.join(''), tag);
|
|
124
|
-
});
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
/**
|
|
128
|
-
* Append at the bottom of the head tag, or if no head tag, then the top of root tag.
|
|
129
|
-
*
|
|
130
|
-
* @param {array} styles
|
|
131
|
-
* @param {string} html
|
|
132
|
-
* @returns {string}
|
|
133
|
-
*/
|
|
134
|
-
function appendMediaToTop(styles, html) {
|
|
135
|
-
var index = findTop(html);
|
|
136
|
-
|
|
137
|
-
return splice(html, index, styles);
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
/**
|
|
141
|
-
* Append at the bottom of the body tag, or if no body tag, then the bottom of the root tag.
|
|
142
|
-
* @param {Array} scripts
|
|
143
|
-
* @param {string} html
|
|
144
|
-
* @returns {string}
|
|
145
|
-
*/
|
|
146
|
-
function appendMediaToBottom(scripts, html) {
|
|
147
|
-
var index = findBottom(html);
|
|
148
|
-
|
|
149
|
-
return splice(html, index, scripts);
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
function injectTags(fileArray, site, tag) {
|
|
153
|
-
var buster = module.exports.cacheBuster ? `?version=${module.exports.cacheBuster}` : '';
|
|
154
|
-
|
|
155
|
-
return bluebird.resolve(_.map(fileArray, function (file) {
|
|
156
|
-
if (tag === STYLE_TAG) {
|
|
157
|
-
return `<link rel="stylesheet" type="text/css" href="${file}${buster}">`;
|
|
158
|
-
} else if (tag === ASYNC_SCRIPT_TAG) {
|
|
159
|
-
return `<script async="async" crossorigin="anonymous" src="${file}" type="text/javascript"></script>`;
|
|
160
|
-
} else if (tag === DEFER_SCRIPT_TAG) {
|
|
161
|
-
return `<script defer="defer" crossorigin="anonymous" src="${file}" type="text/javascript"></script>`;
|
|
162
|
-
} else if (tag === ASYNC_DEFER_SCRIPT_TAG) {
|
|
163
|
-
return `<script async="async" defer="defer" crossorigin="anonymous" src="${file}" type="text/javascript"></script>`;
|
|
164
|
-
} else {
|
|
165
|
-
return `<script crossorigin="anonymous" type="text/javascript" src="${file}${buster}"></script>`;
|
|
166
|
-
}
|
|
167
|
-
}).join('\n'));
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
/**
|
|
171
|
-
* Gets a list of all css and js needed for the components listed.
|
|
172
|
-
* @param {Object} state
|
|
173
|
-
* @returns {{styles: Array, scripts: Array}}
|
|
174
|
-
*/
|
|
175
|
-
function getMediaMap(state) {
|
|
176
|
-
return {
|
|
177
|
-
packs: module.exports.getManifestScripts(state),
|
|
178
|
-
styles: module.exports.getStyleFiles(state),
|
|
179
|
-
scripts: module.exports.getScriptFiles(state)
|
|
180
|
-
};
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
/* eslint-disable complexity */
|
|
184
|
-
/**
|
|
185
|
-
* Get the proper styles for the component. If a styleguide is configured, use
|
|
186
|
-
* that css. Defaults to the site slug
|
|
187
|
-
*
|
|
188
|
-
* @param {Object} state
|
|
189
|
-
* @returns {array}
|
|
190
|
-
*/
|
|
191
|
-
function getStyleFiles(state) {
|
|
192
|
-
const { site } = state.locals,
|
|
193
|
-
assetDir = site && site.assetDir || MEDIA_DIRECTORY,
|
|
194
|
-
assetPath = site && site.assetPath || '',
|
|
195
|
-
assetHost = site && site.assetHost || '',
|
|
196
|
-
siteStyleguide = site && site.styleguide,
|
|
197
|
-
layoutName = _.get(state, '_layoutData.name');
|
|
198
|
-
let cssFilePaths = [], layoutBasePath;
|
|
199
|
-
|
|
200
|
-
// legacy behavior: load <component>.css and <component>.<site>.css
|
|
201
|
-
if (!site.styleguide) {
|
|
202
|
-
const siteSlug = site && site.slug;
|
|
203
|
-
|
|
204
|
-
cssFilePaths = state._components
|
|
205
|
-
.reduce((acc, curr) => {
|
|
206
|
-
acc.push(
|
|
207
|
-
path.join(assetDir, 'css', `${curr}.css`),
|
|
208
|
-
path.join(assetDir, 'css', `${curr}.${siteSlug}.css`)
|
|
209
|
-
);
|
|
210
|
-
return acc;
|
|
211
|
-
}, []);
|
|
212
|
-
// in edit mode, grab all variations
|
|
213
|
-
} else if (state.locals.edit) {
|
|
214
|
-
const availableVariations = styleguide.getVariations(siteStyleguide);
|
|
215
|
-
|
|
216
|
-
cssFilePaths = state._components
|
|
217
|
-
.reduce((acc, cmptName) => {
|
|
218
|
-
acc.push(cmptName, ...availableVariations[cmptName] || []);
|
|
219
|
-
return acc;
|
|
220
|
-
}, [])
|
|
221
|
-
.map(baseName => {
|
|
222
|
-
const basePath = path.join(assetDir, 'css', `${baseName}.${siteStyleguide}.css`);
|
|
223
|
-
|
|
224
|
-
return files.fileExists(basePath) ?
|
|
225
|
-
basePath :
|
|
226
|
-
path.join(assetDir, 'css', `${baseName}._default.css`);
|
|
227
|
-
});
|
|
228
|
-
// in view mode, grab only used variations
|
|
229
|
-
} else if (state._usedVariations) {
|
|
230
|
-
cssFilePaths = state._usedVariations
|
|
231
|
-
.map(variation => {
|
|
232
|
-
const styleguideVariationPath = path.join(assetDir, 'css', `${variation}.${siteStyleguide}.css`);
|
|
233
|
-
|
|
234
|
-
return files.fileExists(styleguideVariationPath) ?
|
|
235
|
-
styleguideVariationPath :
|
|
236
|
-
path.join(assetDir, 'css', `${variation}._default.css`);
|
|
237
|
-
});
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
// add the layout css
|
|
241
|
-
if (layoutName) {
|
|
242
|
-
layoutBasePath = path.join(assetDir, 'css', `${layoutName}.${siteStyleguide}.css`);
|
|
243
|
-
|
|
244
|
-
if (files.fileExists(layoutBasePath)) {
|
|
245
|
-
cssFilePaths.push(layoutBasePath);
|
|
246
|
-
} else {
|
|
247
|
-
cssFilePaths.push(path.join(assetDir, 'css', `${layoutName}._default.css`));
|
|
248
|
-
}
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
return cssFilePaths
|
|
252
|
-
.filter(files.fileExists)
|
|
253
|
-
.map(pathJoin(assetHost, assetPath, assetDir));
|
|
254
|
-
}
|
|
255
|
-
/* eslint-enable complexity */
|
|
256
|
-
|
|
257
|
-
/**
|
|
258
|
-
* Get the proper scripts for the component
|
|
259
|
-
*
|
|
260
|
-
* @param {string} name
|
|
261
|
-
* @param {object} site
|
|
262
|
-
* @returns {array}
|
|
263
|
-
*/
|
|
264
|
-
|
|
265
|
-
function getScriptFiles(state) {
|
|
266
|
-
const site = state.locals.site,
|
|
267
|
-
slug = site.slug,
|
|
268
|
-
assetDir = site && site.assetDir || MEDIA_DIRECTORY,
|
|
269
|
-
assetPath = site && site.assetPath || '',
|
|
270
|
-
assetHost = site && site.assetHost || '';
|
|
271
|
-
|
|
272
|
-
return state._components
|
|
273
|
-
.reduce((prev, name) => {
|
|
274
|
-
prev.push(
|
|
275
|
-
path.join(assetDir, 'js', `${name}.js`),
|
|
276
|
-
path.join(assetDir, 'js', `${name}.${slug}.js`),
|
|
277
|
-
path.join(assetDir, 'js', `${name}.client.js`)
|
|
278
|
-
);
|
|
279
|
-
return prev;
|
|
280
|
-
}, [])
|
|
281
|
-
.filter(files.fileExists)
|
|
282
|
-
.map(pathJoin(assetHost, assetPath, assetDir));
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
/**
|
|
286
|
-
* Read the asset manifest generated by the `clay pack` bundling command and
|
|
287
|
-
* check for scripts which match the rendered component names. The manifest for
|
|
288
|
-
* each asset lists its complete set of dependencies, so we dedupe them.
|
|
289
|
-
* Downstream globals and Webpack's runtime are added to the beginning of the
|
|
290
|
-
* list, Clay CLI's initializer to the end.
|
|
291
|
-
*
|
|
292
|
-
* @param {Object} state - Amphora's render state.
|
|
293
|
-
* @return {string[]} A list of component assets.
|
|
294
|
-
*/
|
|
295
|
-
function getManifestScripts(state) {
|
|
296
|
-
const site = state.locals.site,
|
|
297
|
-
assetDir = site && site.assetDir || MEDIA_DIRECTORY,
|
|
298
|
-
assetHost = site && site.assetHost || '/',
|
|
299
|
-
manifestName = site && site.manifestName || 'public/assets-manifest.json',
|
|
300
|
-
manifestFile = path.resolve(path.join(assetDir, manifestName)),
|
|
301
|
-
manifest = files.tryRequire(manifestFile);
|
|
302
|
-
|
|
303
|
-
if (!manifest) return [];
|
|
304
|
-
|
|
305
|
-
const componentsAndDependencies = state._components.reduce((acc, component) => {
|
|
306
|
-
const allDependencies = _.get(manifest, ['entrypoints', component, 'assets', 'js'], []),
|
|
307
|
-
componentScripts = allDependencies.filter(script => script.includes(component)),
|
|
308
|
-
dependencyScripts = _.without(allDependencies, componentScripts);
|
|
309
|
-
|
|
310
|
-
acc.components = acc.components.concat(componentScripts);
|
|
311
|
-
acc.dependencies = acc.dependencies.concat(dependencyScripts);
|
|
312
|
-
return acc;
|
|
313
|
-
}, {
|
|
314
|
-
components: [],
|
|
315
|
-
dependencies: [],
|
|
316
|
-
});
|
|
317
|
-
|
|
318
|
-
const runtime = _.get(manifest, 'runtime.js', '');
|
|
319
|
-
const globalScripts = _.get(manifest, 'entrypoints.__clay_globals.assets.js', []);
|
|
320
|
-
const initScripts = _.get(manifest, 'entrypoints.__clay_client_init.assets.js', []);
|
|
321
|
-
|
|
322
|
-
const scripts = {
|
|
323
|
-
components: [
|
|
324
|
-
...componentsAndDependencies.dependencies,
|
|
325
|
-
...componentsAndDependencies.components
|
|
326
|
-
],
|
|
327
|
-
global: globalScripts,
|
|
328
|
-
init: initScripts,
|
|
329
|
-
runtime: [runtime],
|
|
330
|
-
};
|
|
331
|
-
|
|
332
|
-
const scriptsWithHost = _.mapValues(scripts, list => {
|
|
333
|
-
const realList = list && list.filter(Boolean);
|
|
334
|
-
|
|
335
|
-
if (!realList || !realList.length) return [];
|
|
336
|
-
|
|
337
|
-
return Array.from(new Set(realList))
|
|
338
|
-
.map(script => path.join(assetHost, script));
|
|
339
|
-
});
|
|
340
|
-
|
|
341
|
-
return scriptsWithHost;
|
|
342
|
-
}
|
|
343
|
-
|
|
344
|
-
/**
|
|
345
|
-
* Trim the path on the file system bythe link of the
|
|
346
|
-
* asset directory and then join it with either the assetHost
|
|
347
|
-
* value of the site or the assetPath value (found in the
|
|
348
|
-
* site's config file)
|
|
349
|
-
*
|
|
350
|
-
* @param {String} assetHost
|
|
351
|
-
* @param {String} assetPath
|
|
352
|
-
* @param {String} assetDir
|
|
353
|
-
* @return {Function}
|
|
354
|
-
*/
|
|
355
|
-
function pathJoin(assetHost, assetPath, assetDir) {
|
|
356
|
-
var assetDirLen = assetDir.length;
|
|
357
|
-
|
|
358
|
-
return function (filePath) {
|
|
359
|
-
var pathToFile = filePath.substr(assetDirLen);
|
|
360
|
-
|
|
361
|
-
if (assetHost) return `${assetHost}${pathToFile}`;
|
|
362
|
-
return path.join(assetPath, pathToFile);
|
|
363
|
-
};
|
|
364
|
-
}
|
|
365
|
-
|
|
366
|
-
/**
|
|
367
|
-
* Set the values for inlining vs tagging edit mode styles/scripts
|
|
368
|
-
*
|
|
369
|
-
* @param {object|boolean} options
|
|
370
|
-
* @param {string} [cacheBuster]
|
|
371
|
-
*/
|
|
372
|
-
function configure(options, cacheBuster = '') {
|
|
373
|
-
if (options && _.isObject(options)) {
|
|
374
|
-
module.exports.editStylesTags = options.styles || false;
|
|
375
|
-
module.exports.editScriptsTags = options.scripts || false;
|
|
376
|
-
} else {
|
|
377
|
-
module.exports.editStylesTags = options;
|
|
378
|
-
module.exports.editScriptsTags = options;
|
|
379
|
-
}
|
|
380
|
-
// Assign cacheBuster
|
|
381
|
-
module.exports.cacheBuster = cacheBuster;
|
|
382
|
-
}
|
|
383
|
-
|
|
384
|
-
/**
|
|
385
|
-
* Given the state and html string...
|
|
386
|
-
*
|
|
387
|
-
* 1. Find all associated scripts/styles
|
|
388
|
-
* 2. Run `resolveMedia`
|
|
389
|
-
* 3. Stitch in the assets' contents
|
|
390
|
-
* 4. Return the HTML string
|
|
391
|
-
*
|
|
392
|
-
* @param {Object} state
|
|
393
|
-
* @return {Function}
|
|
394
|
-
*/
|
|
395
|
-
function injectScriptsAndStyles(state) {
|
|
396
|
-
const { locals } = state;
|
|
397
|
-
|
|
398
|
-
return function (html) {
|
|
399
|
-
var mediaMap = {};
|
|
400
|
-
|
|
401
|
-
// Check that HTML is string
|
|
402
|
-
if (typeof html !== 'string') {
|
|
403
|
-
throw new Error('Missing html parameter');
|
|
404
|
-
}
|
|
405
|
-
|
|
406
|
-
// We've got a string so let's actually get the map of styles
|
|
407
|
-
mediaMap = module.exports.getMediaMap(state);
|
|
408
|
-
|
|
409
|
-
// allow site to change the media map before applying it
|
|
410
|
-
if (setup.resolveMedia) mediaMap = setup.resolveMedia(mediaMap, locals) || mediaMap;
|
|
411
|
-
|
|
412
|
-
if (!!mediaMap.packs && !!mediaMap.packs.runtime) {
|
|
413
|
-
mediaMap.packs.runtime = injectTags(mediaMap.packs.runtime, locals.site, SCRIPT_TAG);
|
|
414
|
-
}
|
|
415
|
-
if (!!mediaMap.packs && !!mediaMap.packs.global) {
|
|
416
|
-
mediaMap.packs.global = injectTags(mediaMap.packs.global, locals.site, ASYNC_SCRIPT_TAG);
|
|
417
|
-
}
|
|
418
|
-
|
|
419
|
-
if (!!mediaMap.packs && !!mediaMap.packs.components) {
|
|
420
|
-
mediaMap.packs.components = injectTags(mediaMap.packs.components, locals.site, ASYNC_DEFER_SCRIPT_TAG);
|
|
421
|
-
}
|
|
422
|
-
|
|
423
|
-
if (!!mediaMap.packs && !!mediaMap.packs.init) {
|
|
424
|
-
mediaMap.packs.init = injectTags(mediaMap.packs.init, locals.site, ASYNC_DEFER_SCRIPT_TAG);
|
|
425
|
-
}
|
|
426
|
-
|
|
427
|
-
if (!locals.edit) {
|
|
428
|
-
mediaMap.styles = combineFileContents(mediaMap.styles, 'public/css', '/css/', STYLE_TAG);
|
|
429
|
-
mediaMap.scripts = combineFileContents(mediaMap.scripts, 'public/js', '/js/', SCRIPT_TAG);
|
|
430
|
-
} else {
|
|
431
|
-
mediaMap.styles = module.exports.editStylesTags ? injectTags(mediaMap.styles, locals.site, STYLE_TAG) : combineFileContents(mediaMap.styles, 'public/css', '/css/', STYLE_TAG);
|
|
432
|
-
mediaMap.scripts = module.exports.editScriptsTags ? injectTags(mediaMap.scripts, locals.site, SCRIPT_TAG) : combineFileContents(mediaMap.scripts, 'public/js', '/js/', SCRIPT_TAG);
|
|
433
|
-
}
|
|
434
|
-
|
|
435
|
-
return bluebird.props(mediaMap)
|
|
436
|
-
.then(combinedFiles => {
|
|
437
|
-
html = combinedFiles.styles ? appendMediaToTop(combinedFiles.styles, html) : html; // If there are styles, append them
|
|
438
|
-
html = combinedFiles.scripts ? appendMediaToBottom(combinedFiles.scripts, html) : html; // If there are scripts, append them
|
|
439
|
-
return html; // Return the compiled HTML
|
|
440
|
-
}).then(html => {
|
|
441
|
-
return bluebird.props(mediaMap.packs).then(pack => {
|
|
442
|
-
html = pack.runtime ? appendMediaToBottom(pack.runtime, html) : html;
|
|
443
|
-
html = pack.global ? appendMediaToBottom(pack.global, html) : html;
|
|
444
|
-
html = pack.components ? appendMediaToBottom(pack.components, html) : html;
|
|
445
|
-
html = pack.init ? appendMediaToBottom(pack.init, html) : html;
|
|
446
|
-
return html;
|
|
447
|
-
});
|
|
448
|
-
});
|
|
449
|
-
};
|
|
450
|
-
}
|
|
451
|
-
|
|
452
|
-
module.exports.injectScriptsAndStyles = injectScriptsAndStyles;
|
|
453
|
-
module.exports.getMediaMap = getMediaMap;
|
|
454
|
-
module.exports.cacheBuster = '';
|
|
455
|
-
module.exports.configure = configure;
|
|
456
|
-
module.exports.editStylesTags = false;
|
|
457
|
-
module.exports.editScriptsTags = false;
|
|
458
|
-
|
|
459
|
-
// For testing
|
|
460
|
-
module.exports.getManifestScripts = getManifestScripts;
|
|
461
|
-
module.exports.getScriptFiles = getScriptFiles;
|
|
462
|
-
module.exports.getStyleFiles = getStyleFiles;
|