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