amphora-html 6.0.0-3 → 6.0.0-6

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 (43) hide show
  1. package/.envrc +1 -1
  2. package/lib/media.js +6 -22
  3. package/package.json +1 -1
  4. package/.vscode/.history/amphora-html/lib/media.test_20210527151433.js +0 -797
  5. package/.vscode/.history/amphora-html/lib/media.test_20210607095503.js +0 -815
  6. package/.vscode/.history/amphora-html/lib/media.test_20210607095544.js +0 -815
  7. package/.vscode/.history/amphora-html/lib/media.test_20210607100025.js +0 -817
  8. package/.vscode/.history/amphora-html/lib/media.test_20210607100402.js +0 -817
  9. package/.vscode/.history/amphora-html/lib/media.test_20210607100635.js +0 -829
  10. package/.vscode/.history/amphora-html/lib/media.test_20210607105251.js +0 -852
  11. package/.vscode/.history/amphora-html/lib/media.test_20210607120734.js +0 -839
  12. package/.vscode/.history/amphora-html/lib/media.test_20210607121735.js +0 -873
  13. package/.vscode/.history/amphora-html/lib/media.test_20210608203514.js +0 -908
  14. package/.vscode/.history/amphora-html/lib/media_20210511135806.js +0 -434
  15. package/.vscode/.history/amphora-html/lib/media_20210512105948.js +0 -434
  16. package/.vscode/.history/amphora-html/lib/media_20210512110245.js +0 -434
  17. package/.vscode/.history/amphora-html/lib/media_20210512114913.js +0 -434
  18. package/.vscode/.history/amphora-html/lib/media_20210512114917.js +0 -434
  19. package/.vscode/.history/amphora-html/lib/media_20210512122725.js +0 -429
  20. package/.vscode/.history/amphora-html/lib/media_20210512123057.js +0 -429
  21. package/.vscode/.history/amphora-html/lib/media_20210512123102.js +0 -429
  22. package/.vscode/.history/amphora-html/lib/media_20210512123201.js +0 -429
  23. package/.vscode/.history/amphora-html/lib/media_20210512152244.js +0 -429
  24. package/.vscode/.history/amphora-html/lib/media_20210512152536.js +0 -433
  25. package/.vscode/.history/amphora-html/lib/media_20210512152541.js +0 -433
  26. package/.vscode/.history/amphora-html/lib/media_20210512152717.js +0 -433
  27. package/.vscode/.history/amphora-html/lib/media_20210512152931.js +0 -419
  28. package/.vscode/.history/amphora-html/lib/media_20210512152949.js +0 -420
  29. package/.vscode/.history/amphora-html/lib/media_20210512152958.js +0 -418
  30. package/.vscode/.history/amphora-html/lib/media_20210512155619.js +0 -418
  31. package/.vscode/.history/amphora-html/lib/media_20210512164028.js +0 -418
  32. package/.vscode/.history/amphora-html/lib/media_20210512170157.js +0 -418
  33. package/.vscode/.history/amphora-html/lib/media_20210512171030.js +0 -418
  34. package/.vscode/.history/amphora-html/lib/media_20210607085451.js +0 -454
  35. package/.vscode/.history/amphora-html/lib/media_20210607085550.js +0 -454
  36. package/.vscode/.history/amphora-html/lib/media_20210607095350.js +0 -454
  37. package/.vscode/.history/amphora-html/lib/media_20210607121609.js +0 -455
  38. package/.vscode/.history/amphora-html/lib/media_20210607121656.js +0 -455
  39. package/.vscode/.history/amphora-html/lib/media_20210607121742.js +0 -455
  40. package/.vscode/.history/amphora-html/lib/media_20210608203406.js +0 -454
  41. package/.vscode/.history/amphora-html/package_20210607131602.json +0 -62
  42. package/.vscode/.history/amphora-html/package_20210607131617.json +0 -62
  43. package/.vscode/.history/amphora-html/package_20210608210629.json +0 -62
@@ -1,433 +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 crossorigin="anonymous" src="${file}" type="text/javascript"></script>`;
160
- } else if (tag === DEFER_SCRIPT_TAG) {
161
- return `<script defer crossorigin="anonymous" src="${file}" type="text/javascript"></script>`;
162
- } else if (tag === ASYNC_DEFER_SCRIPT_TAG) {
163
- return `<script async 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
- styles: module.exports.getStyleFiles(state),
178
- scripts: module.exports.getScriptFiles(state),
179
- manifestAssets: module.exports.getManifestAssets(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 getManifestAssets(state) {
296
- const site = state.locals.site,
297
- assetDir = site && site.assetDir || MEDIA_DIRECTORY,
298
- assetHost = site && site.assetHost || '/',
299
- manifestName = site && site.manifestName || 'assets-manifest.json',
300
- manifestFile = path.resolve(path.join(assetDir, manifestName)),
301
- manifest = files.tryRequire(manifestFile);
302
-
303
- if (!manifest) return [];
304
-
305
- const runtime = _.get(manifest, 'runtime.js', '');
306
-
307
- const componentsAndDependencies = [state._components, '__clay_client_init', '__clay_globals'].reduce((acc, component) => {
308
- const allDependencies = _.get(manifest, ['entrypoints', component, 'assets', 'js'], []),
309
- componentScripts = allDependencies.filter(script => script.includes(component)),
310
- dependencyScripts = _.difference(allDependencies, componentScripts);
311
-
312
- acc.components = acc.components.concat(componentScripts);
313
- acc.dependencies = acc.dependencies.concat(dependencyScripts);
314
- return acc;
315
- }, {
316
- components: [],
317
- dependencies: [],
318
- });
319
-
320
- const scripts =
321
- _.union(
322
- runtime,
323
- componentsAndDependencies.dependencies,
324
- componentsAndDependencies.components
325
- )
326
- .map(script => path.join(assetHost, script));
327
-
328
- return scripts;
329
- }
330
-
331
- /**
332
- * Trim the path on the file system bythe link of the
333
- * asset directory and then join it with either the assetHost
334
- * value of the site or the assetPath value (found in the
335
- * site's config file)
336
- *
337
- * @param {String} assetHost
338
- * @param {String} assetPath
339
- * @param {String} assetDir
340
- * @return {Function}
341
- */
342
- function pathJoin(assetHost, assetPath, assetDir) {
343
- var assetDirLen = assetDir.length;
344
-
345
- return function (filePath) {
346
- var pathToFile = filePath.substr(assetDirLen);
347
-
348
- if (assetHost) return `${assetHost}${pathToFile}`;
349
- return path.join(assetPath, pathToFile);
350
- };
351
- }
352
-
353
- /**
354
- * Set the values for inlining vs tagging edit mode styles/scripts
355
- *
356
- * @param {object|boolean} options
357
- * @param {string} [cacheBuster]
358
- */
359
- function configure(options, cacheBuster = '') {
360
- if (options && _.isObject(options)) {
361
- module.exports.editStylesTags = options.styles || false;
362
- module.exports.editScriptsTags = options.scripts || false;
363
- } else {
364
- module.exports.editStylesTags = options;
365
- module.exports.editScriptsTags = options;
366
- }
367
- // Assign cacheBuster
368
- module.exports.cacheBuster = cacheBuster;
369
- }
370
-
371
- /**
372
- * Given the state and html string...
373
- *
374
- * 1. Find all associated scripts/styles
375
- * 2. Run `resolveMedia`
376
- * 3. Stitch in the assets' contents
377
- * 4. Return the HTML string
378
- *
379
- * @param {Object} state
380
- * @return {Function}
381
- */
382
- function injectScriptsAndStyles(state) {
383
- const { locals } = state;
384
-
385
- return function (html) {
386
- var mediaMap = {};
387
-
388
- // Check that HTML is string
389
- if (typeof html !== 'string') {
390
- throw new Error('Missing html parameter');
391
- }
392
-
393
- // We've got a string so let's actually get the map of styles
394
- mediaMap = module.exports.getMediaMap(state);
395
-
396
- // allow site to change the media map before applying it
397
- if (setup.resolveMedia) mediaMap = setup.resolveMedia(mediaMap, locals) || mediaMap;
398
-
399
- if (!locals.edit) {
400
- mediaMap.styles = combineFileContents(mediaMap.styles, 'public/css', '/css/', STYLE_TAG);
401
- mediaMap.scripts = combineFileContents(mediaMap.scripts, 'public/js', '/js/', SCRIPT_TAG);
402
- } else {
403
- mediaMap.styles = module.exports.editStylesTags ? injectTags(mediaMap.styles, locals.site, STYLE_TAG) : combineFileContents(mediaMap.styles, 'public/css', '/css/', STYLE_TAG);
404
- mediaMap.scripts = module.exports.editScriptsTags ? injectTags(mediaMap.scripts, locals.site, SCRIPT_TAG) : combineFileContents(mediaMap.scripts, 'public/js', '/js/', SCRIPT_TAG);
405
- }
406
-
407
- // TODO: Ideally, we'd load these scripts asynchronously or after
408
- // the DOM is ready, setting the `async` or `defer` attribute.
409
- // Because the order in which they're parsed and executed currently
410
- // matters, we can't do that safely.
411
- mediaMap.manifestAssets = injectTags(mediaMap.manifestAssets, locals.site, SCRIPT_TAG);
412
-
413
- return bluebird.props(mediaMap)
414
- .then(combinedFiles => {
415
- html = combinedFiles.styles ? appendMediaToTop(combinedFiles.styles, html) : html; // If there are styles, append them
416
- html = combinedFiles.manifestAssets ? appendMediaToBottom(combinedFiles.manifestAssets, html) : html; // If there are assets registered with the manifest, append them
417
- html = combinedFiles.scripts ? appendMediaToBottom(combinedFiles.scripts, html) : html; // If there are scripts, append them
418
- return html; // Return the compiled HTML
419
- });
420
- };
421
- }
422
-
423
- module.exports.injectScriptsAndStyles = injectScriptsAndStyles;
424
- module.exports.getMediaMap = getMediaMap;
425
- module.exports.cacheBuster = '';
426
- module.exports.configure = configure;
427
- module.exports.editStylesTags = false;
428
- module.exports.editScriptsTags = false;
429
-
430
- // For testing
431
- module.exports.getManifestAssets = getManifestAssets;
432
- module.exports.getScriptFiles = getScriptFiles;
433
- module.exports.getStyleFiles = getStyleFiles;