amphora-html 6.0.0-4 → 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 (40) hide show
  1. package/lib/media.js +1 -1
  2. package/package.json +1 -1
  3. package/.vscode/.history/amphora-html/lib/media.test_20210527151433.js +0 -797
  4. package/.vscode/.history/amphora-html/lib/media.test_20210607095503.js +0 -815
  5. package/.vscode/.history/amphora-html/lib/media.test_20210607095544.js +0 -815
  6. package/.vscode/.history/amphora-html/lib/media.test_20210607100025.js +0 -817
  7. package/.vscode/.history/amphora-html/lib/media.test_20210607100402.js +0 -817
  8. package/.vscode/.history/amphora-html/lib/media.test_20210607100635.js +0 -829
  9. package/.vscode/.history/amphora-html/lib/media.test_20210607105251.js +0 -852
  10. package/.vscode/.history/amphora-html/lib/media.test_20210607120734.js +0 -839
  11. package/.vscode/.history/amphora-html/lib/media.test_20210607121735.js +0 -873
  12. package/.vscode/.history/amphora-html/lib/media.test_20210608203514.js +0 -908
  13. package/.vscode/.history/amphora-html/lib/media_20210803152635.js +0 -456
  14. package/.vscode/.history/amphora-html/lib/media_20210803154356.js +0 -459
  15. package/.vscode/.history/amphora-html/lib/media_20210803154416.js +0 -458
  16. package/.vscode/.history/amphora-html/lib/media_20210803154428.js +0 -429
  17. package/.vscode/.history/amphora-html/lib/media_20210803154955.js +0 -426
  18. package/.vscode/.history/amphora-html/lib/media_20210803155434.js +0 -414
  19. package/.vscode/.history/amphora-html/lib/media_20210803155459.js +0 -413
  20. package/.vscode/.history/amphora-html/lib/media_20210803155524.js +0 -413
  21. package/.vscode/.history/amphora-html/lib/media_20210803161841.js +0 -412
  22. package/.vscode/.history/amphora-html/lib/media_20210803163103.js +0 -411
  23. package/.vscode/.history/amphora-html/lib/media_20210818123839.js +0 -449
  24. package/.vscode/.history/amphora-html/lib/media_20210818124154.js +0 -463
  25. package/.vscode/.history/amphora-html/lib/media_20210818124158.js +0 -463
  26. package/.vscode/.history/amphora-html/lib/media_20210818124413.js +0 -458
  27. package/.vscode/.history/amphora-html/lib/media_20210818124810.js +0 -459
  28. package/.vscode/.history/amphora-html/lib/media_20210818124903.js +0 -454
  29. package/.vscode/.history/amphora-html/lib/media_20210819115533.js +0 -454
  30. package/.vscode/.history/amphora-html/lib/media_20210819115825.js +0 -454
  31. package/.vscode/.history/amphora-html/lib/media_20210819120153.js +0 -454
  32. package/.vscode/.history/amphora-html/lib/media_20210819120237.js +0 -455
  33. package/.vscode/.history/amphora-html/lib/media_20210819120300.js +0 -454
  34. package/.vscode/.history/amphora-html/lib/media_20210819120500.js +0 -455
  35. package/.vscode/.history/amphora-html/lib/media_20210819120508.js +0 -453
  36. package/.vscode/.history/amphora-html/lib/media_20210819120525.js +0 -437
  37. package/.vscode/.history/amphora-html/lib/media_20210819172211.js +0 -437
  38. package/.vscode/.history/amphora-html/lib/media_20210819172412.js +0 -438
  39. package/.vscode/.history/amphora-html/package_20210825103846.json +0 -62
  40. package/.vscode/.history/amphora-html/package_20210825104039.json +0 -62
@@ -1,453 +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
- url = require('url'),
10
- STYLE_TAG = 'style',
11
- SCRIPT_TAG = 'scripts',
12
- ASYNC_SCRIPT_TAG = 'async',
13
- DEFER_SCRIPT_TAG = 'defer',
14
- ASYNC_DEFER_SCRIPT_TAG = 'async-defer',
15
- MEDIA_DIRECTORY = path.join(process.cwd(), 'public');
16
-
17
- /**
18
- * @param {string} str
19
- * @returns {number}
20
- */
21
- function findBottom(str) {
22
- var index = str.lastIndexOf('</body>');
23
-
24
- if (index === -1) {
25
- index = str.lastIndexOf('</');
26
- }
27
-
28
- return index;
29
- }
30
-
31
- /**
32
- * @param {string} str
33
- * @returns {number}
34
- */
35
- function findTop(str) {
36
- var index = str.indexOf('</head>');
37
-
38
- if (index === -1) {
39
- index = str.indexOf('>') + 1;
40
- }
41
- return index;
42
- }
43
-
44
- /**
45
- * Put items at index in the very large target string.
46
- *
47
- * @param {string} str
48
- * @param {number} index
49
- * @param {[string]} items
50
- * @returns {string}
51
- */
52
- function splice(str, index, items) {
53
- return str.substr(0, index) + items + str.substr(index);
54
- }
55
-
56
- /**
57
- * Get the contents of a string that come after specified characters.
58
- *
59
- * @param {string} str Any string to split
60
- * @param {string} dir The directory the file is in
61
- * @return {string}
62
- */
63
- function getFileName(str, dir) {
64
- return str.split(dir)[1];
65
- }
66
-
67
- /**
68
- * Retrieve the contents of a file
69
- *
70
- * @param {array} fileArray An array of file names
71
- * @param {string} targetDir The directory to retrieve files from
72
- * @param {string} filePrefix The string that comes right before the file name
73
- * @return {Promise}
74
- */
75
- function getContentsOfFiles(fileArray, targetDir, filePrefix) {
76
- var allPromises = [],
77
- currentDir = process.cwd();
78
-
79
- fileArray.forEach(file => {
80
- allPromises.push(files.readFilePromise(path.join(currentDir, targetDir, getFileName(file, filePrefix))));
81
- });
82
-
83
- return Promise.all(allPromises)
84
- .catch(err => {
85
- throw err;
86
- });
87
- }
88
-
89
- /**
90
- * Wraps a string with HTML tags
91
- * @param {string} string The string to wrap
92
- * @param {string} tag The HTML tag to use
93
- * @return {string}
94
- */
95
- function wrapWithTags(string, tag) {
96
- if (tag === SCRIPT_TAG) {
97
- return `<script type="text/javascript">
98
- // <![CDATA[
99
- ${string}
100
- // ]]
101
- </script>`;
102
- } else {
103
- return `<style>${string}</style>`;
104
- }
105
- }
106
-
107
- /**
108
- * Concatenates an array of files into one string.
109
- *
110
- * @param {array} fileArray An array of files
111
- * @param {string} directory The directory in which `fs` will look for the file
112
- * @param {string} filePrefix The directory path before the filename
113
- * @param {string|null} tag The type of tag to wrap the contents in.
114
- * @return {string}
115
- */
116
- function combineFileContents(fileArray, directory, filePrefix, tag) {
117
- if (!fileArray || !fileArray.length) {
118
- return false;
119
- }
120
-
121
- // If there are files, retrieve contents
122
- return getContentsOfFiles(fileArray, directory, filePrefix)
123
- .then(function (fileContents) {
124
- return wrapWithTags(fileContents.join(''), tag);
125
- });
126
- }
127
-
128
- /**
129
- * Append at the bottom of the head tag, or if no head tag, then the top of root tag.
130
- *
131
- * @param {array} styles
132
- * @param {string} html
133
- * @returns {string}
134
- */
135
- function appendMediaToTop(styles, html) {
136
- var index = findTop(html);
137
-
138
- return splice(html, index, styles);
139
- }
140
-
141
- /**
142
- * Append at the bottom of the body tag, or if no body tag, then the bottom of the root tag.
143
- * @param {Array} scripts
144
- * @param {string} html
145
- * @returns {string}
146
- */
147
- function appendMediaToBottom(scripts, html) {
148
- var index = findBottom(html);
149
-
150
- return splice(html, index, scripts);
151
- }
152
-
153
- function injectTags(fileArray, site, tag) {
154
- var buster = module.exports.cacheBuster ? `?version=${module.exports.cacheBuster}` : '';
155
-
156
- return bluebird.resolve(_.map(fileArray, function (file) {
157
- if (tag === STYLE_TAG) {
158
- return `<link rel="stylesheet" type="text/css" href="${file}${buster}">`;
159
- } else if (tag === ASYNC_SCRIPT_TAG) {
160
- return `<script async src="${file}" type="text/javascript"></script>`;
161
- } else if (tag === DEFER_SCRIPT_TAG) {
162
- return `<script defer src="${file}" type="text/javascript"></script>`;
163
- } else if (tag === ASYNC_DEFER_SCRIPT_TAG) {
164
- return `<script async defer src="${file}" type="text/javascript"></script>`;
165
- } else {
166
- return `<script type="text/javascript" src="${file}${buster}"></script>`;
167
- }
168
- }).join('\n'));
169
- }
170
-
171
- /**
172
- * Check the asset directory for a Webpack manifest file. Avoiding a check for
173
- * CLAYCLI_WEBPACK_BUILD in the enivronment helps minimize coupling with Clay CLI.
174
- *
175
- * @param {Object} state - The Amphora state object.
176
- * @returns {boolean} - Whether the manifest file exists.
177
- */
178
- function assetManifestExists(state) {
179
- const site = state.locals.site,
180
- assetDir = site && site.assetDir || MEDIA_DIRECTORY,
181
- manifestName = site && site.manifestName || 'assets-manifest.json',
182
- manifestFile = path.resolve(path.join(assetDir, manifestName));
183
-
184
- return files.fileExists(manifestFile);
185
- }
186
-
187
- /**
188
- * Filter the components rendered in this request, removing Webpack-managed
189
- * assets so that we can fall back on Browserify.
190
- *
191
- * @param {Object} state - Amphora render state.
192
- * @returns {string[]} - A list of components not available through Webpack.
193
- */
194
- function filterManifestComponents(state) {
195
- const site = state.locals.site,
196
- assetDir = site && site.assetDir || MEDIA_DIRECTORY,
197
- manifestName = site && site.manifestName || 'assets-manifest.json',
198
- manifestFile = path.resolve(path.join(assetDir, manifestName));
199
- let manifest;
200
-
201
- try {
202
- manifest = files.tryRequire(manifestFile);
203
- } catch (err) {
204
- return state._components;
205
- }
206
-
207
-
208
- return state._components
209
- .filter(componentName => !manifest.hasOwnProperty(`${ componentName }.js`));
210
- }
211
-
212
- /**
213
- * Gets a list of all css and js needed for the components listed.
214
- * @param {Object} state
215
- * @returns {{styles: Array, scripts: Array}}
216
- */
217
- function getMediaMap(state) {
218
- return {
219
- styles: module.exports.getStyleFiles(state),
220
- scripts: module.exports.getScriptFiles(state),
221
- manifestAssets: module.exports.getManifestAssets(state)
222
- };
223
- }
224
-
225
- /* eslint-disable complexity */
226
- /**
227
- * Get the proper styles for the component. If a styleguide is configured, use
228
- * that css. Defaults to the site slug
229
- *
230
- * @param {Object} state
231
- * @returns {array}
232
- */
233
- function getStyleFiles(state) {
234
- const { site } = state.locals,
235
- assetDir = site && site.assetDir || MEDIA_DIRECTORY,
236
- assetPath = site && site.assetPath || '',
237
- assetHost = site && site.assetHost || '',
238
- siteStyleguide = site && site.styleguide,
239
- layoutName = _.get(state, '_layoutData.name');
240
- let cssFilePaths = [], layoutBasePath;
241
-
242
- // legacy behavior: load <component>.css and <component>.<site>.css
243
- if (!site.styleguide) {
244
- const siteSlug = site && site.slug;
245
-
246
- cssFilePaths = state._components
247
- .reduce((acc, curr) => {
248
- acc.push(
249
- path.join(assetDir, 'css', `${curr}.css`),
250
- path.join(assetDir, 'css', `${curr}.${siteSlug}.css`)
251
- );
252
- return acc;
253
- }, []);
254
- // in edit mode, grab all variations
255
- } else if (state.locals.edit) {
256
- const availableVariations = styleguide.getVariations(siteStyleguide);
257
-
258
- cssFilePaths = state._components
259
- .reduce((acc, cmptName) => {
260
- acc.push(cmptName, ...availableVariations[cmptName] || []);
261
- return acc;
262
- }, [])
263
- .map(baseName => {
264
- const basePath = path.join(assetDir, 'css', `${baseName}.${siteStyleguide}.css`);
265
-
266
- return files.fileExists(basePath) ?
267
- basePath :
268
- path.join(assetDir, 'css', `${baseName}._default.css`);
269
- });
270
- // in view mode, grab only used variations
271
- } else if (state._usedVariations) {
272
- cssFilePaths = state._usedVariations
273
- .map(variation => {
274
- const styleguideVariationPath = path.join(assetDir, 'css', `${variation}.${siteStyleguide}.css`);
275
-
276
- return files.fileExists(styleguideVariationPath) ?
277
- styleguideVariationPath :
278
- path.join(assetDir, 'css', `${variation}._default.css`);
279
- });
280
- }
281
-
282
- // add the layout css
283
- if (layoutName) {
284
- layoutBasePath = path.join(assetDir, 'css', `${layoutName}.${siteStyleguide}.css`);
285
-
286
- if (files.fileExists(layoutBasePath)) {
287
- cssFilePaths.push(layoutBasePath);
288
- } else {
289
- cssFilePaths.push(path.join(assetDir, 'css', `${layoutName}._default.css`));
290
- }
291
- }
292
-
293
- return cssFilePaths
294
- .filter(files.fileExists)
295
- .map(pathJoin(assetHost, assetPath, assetDir));
296
- }
297
- /* eslint-enable complexity */
298
-
299
- /**
300
- * Get the proper scripts for the component
301
- *
302
- * @param {string} name
303
- * @param {object} site
304
- * @returns {array}
305
- */
306
-
307
- function getScriptFiles(state) {
308
- const site = state.locals.site,
309
- slug = site.slug,
310
- assetDir = site && site.assetDir || MEDIA_DIRECTORY,
311
- assetPath = site && site.assetPath || '',
312
- assetHost = site && site.assetHost || '';
313
-
314
- return filterManifestComponents(state)
315
- .reduce((prev, name) => {
316
- prev.push(
317
- path.join(assetDir, 'js', `${name}.js`),
318
- path.join(assetDir, 'js', `${name}.${slug}.js`),
319
- path.join(assetDir, 'js', `${name}.client.js`)
320
- );
321
- return prev;
322
- }, [])
323
- .filter(files.fileExists)
324
- .map(pathJoin(assetHost, assetPath, assetDir));
325
- }
326
-
327
- /**
328
- * Read the asset manifest generated by the `clay pack` bundling command and
329
- * check for scripts which match the rendered component names. The manifest for
330
- * each asset lists its complete set of dependencies, so we dedupe them.
331
- * Webpack's runtime is added to the beginning of the list, downstream legacy
332
- * globals and Clay CLI's init script to the end.
333
- *
334
- * @param {Object} state - Amphora's render state.
335
- * @return {string[]} A list of component assets.
336
- */
337
- function getManifestAssets(state) {
338
- const site = state.locals.site,
339
- assetDir = site && site.assetDir || MEDIA_DIRECTORY,
340
- assetHost = site && site.assetHost || '/',
341
- manifestName = site && site.manifestName || 'assets-manifest.json',
342
- manifestFile = path.resolve(path.join(assetDir, manifestName));
343
- let manifest;
344
-
345
- try {
346
- manifest = files.tryRequire(manifestFile);
347
- } catch (err) {
348
- return [];
349
- }
350
-
351
- const scriptFiles = _.get(manifest, ['entrypoints', 'main', 'assets', 'js'], []);
352
-
353
- return scriptFiles.map(script => url.resolve(assetHost, script));
354
- }
355
-
356
- /**
357
- * Trim the path on the file system bythe link of the
358
- * asset directory and then join it with either the assetHost
359
- * value of the site or the assetPath value (found in the
360
- * site's config file)
361
- *
362
- * @param {String} assetHost
363
- * @param {String} assetPath
364
- * @param {String} assetDir
365
- * @return {Function}
366
- */
367
- function pathJoin(assetHost, assetPath, assetDir) {
368
- var assetDirLen = assetDir.length;
369
-
370
- return function (filePath) {
371
- var pathToFile = filePath.substr(assetDirLen);
372
-
373
- if (assetHost) return `${assetHost}${pathToFile}`;
374
- return path.join(assetPath, pathToFile);
375
- };
376
- }
377
-
378
- /**
379
- * Set the values for inlining vs tagging edit mode styles/scripts
380
- *
381
- * @param {object|boolean} options
382
- * @param {string} [cacheBuster]
383
- */
384
- function configure(options, cacheBuster = '') {
385
- if (options && _.isObject(options)) {
386
- module.exports.editStylesTags = options.styles || false;
387
- module.exports.editScriptsTags = options.scripts || false;
388
- } else {
389
- module.exports.editStylesTags = options;
390
- module.exports.editScriptsTags = options;
391
- }
392
- // Assign cacheBuster
393
- module.exports.cacheBuster = cacheBuster;
394
- }
395
-
396
- /**
397
- * Given the state and html string...
398
- *
399
- * 1. Find all associated scripts/styles
400
- * 2. Run `resolveMedia`
401
- * 3. Stitch in the assets' contents
402
- * 4. Return the HTML string
403
- *
404
- * @param {Object} state
405
- * @return {Function}
406
- */
407
- function injectScriptsAndStyles(state) {
408
- const { locals } = state;
409
-
410
- return function (html) {
411
- var mediaMap = {};
412
-
413
- // Check that HTML is string
414
- if (typeof html !== 'string') {
415
- throw new Error('Missing html parameter');
416
- }
417
-
418
- // We've got a string so let's actually get the map of styles
419
- mediaMap = module.exports.getMediaMap(state);
420
-
421
- // allow site to change the media map before applying it
422
- if (setup.resolveMedia) mediaMap = setup.resolveMedia(mediaMap, locals) || mediaMap;
423
-
424
- if (!locals.edit) {
425
- mediaMap.styles = combineFileContents(mediaMap.styles, 'public/css', '/css/', STYLE_TAG);
426
- mediaMap.scripts = !!mediaMap.manifestAssets && mediaMap.manifestAssets.length > 0
427
- ? injectTags(mediaMap.manifestAssets, locals.site, ASYNC_DEFER_SCRIPT_TAG)
428
- : combineFileContents(mediaMap.scripts, 'public/js', '/js/', SCRIPT_TAG);
429
- } else {
430
- mediaMap.styles = module.exports.editStylesTags ? injectTags(mediaMap.styles, locals.site, STYLE_TAG) : combineFileContents(mediaMap.styles, 'public/css', '/css/', STYLE_TAG);
431
- mediaMap.scripts = module.exports.editScriptsTags ? injectTags(mediaMap.scripts, locals.site, SCRIPT_TAG) : combineFileContents(mediaMap.scripts, 'public/js', '/js/', SCRIPT_TAG);
432
- }
433
-
434
- return bluebird.props(mediaMap)
435
- .then(combinedFiles => {
436
- html = combinedFiles.styles ? appendMediaToTop(combinedFiles.styles, html) : html; // If there are styles, append them
437
- html = combinedFiles.scripts ? appendMediaToBottom(combinedFiles.scripts, html) : html; // If there are scripts, append them
438
- return html; // Return the compiled HTML
439
- });
440
- };
441
- }
442
-
443
- module.exports.injectScriptsAndStyles = injectScriptsAndStyles;
444
- module.exports.getMediaMap = getMediaMap;
445
- module.exports.cacheBuster = '';
446
- module.exports.configure = configure;
447
- module.exports.editStylesTags = false;
448
- module.exports.editScriptsTags = false;
449
-
450
- // For testing
451
- module.exports.getManifestAssets = getManifestAssets;
452
- module.exports.getScriptFiles = getScriptFiles;
453
- module.exports.getStyleFiles = getStyleFiles;