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,463 +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.
173
- *
174
- * @param {Object} state - The Amphora state object.
175
- * @returns {boolean} - Whether the manifest file exists.
176
- */
177
- function checkWebpackManifest(state) {
178
- const site = state.locals.site,
179
- assetDir = site && site.assetDir || MEDIA_DIRECTORY,
180
- manifestName = site && site.manifestName || 'assets-manifest.json',
181
- manifestFile = path.resolve(path.join(assetDir, manifestName));
182
-
183
- return (files.fileExists(manifestFile));
184
- }
185
-
186
- /**
187
- * Filter the components rendered in this request, removing Webpack-managed
188
- * assets so that we can fall back on Browserify.
189
- *
190
- * @param {Object} state - Amphora render state.
191
- * @returns {string[]} - A list of components not available through Webpack.
192
- */
193
- function filterManifestComponents(state) {
194
- const site = state.locals.site,
195
- assetDir = site && site.assetDir || MEDIA_DIRECTORY,
196
- manifestName = site && site.manifestName || 'assets-manifest.json',
197
- manifestFile = path.resolve(path.join(assetDir, manifestName));
198
- let manifest;
199
-
200
- try {
201
- manifest = files.tryRequire(manifestFile);
202
- } catch (err) {
203
- return state._components;
204
- }
205
-
206
-
207
- return state._components
208
- .filter(componentName => !manifest.hasOwnProperty(`${ componentName }.js`));
209
- }
210
-
211
- /**
212
- * Gets a list of all css and js needed for the components listed.
213
- * @param {Object} state
214
- * @returns {{styles: Array, scripts: Array}}
215
- */
216
- function getMediaMap(state) {
217
- return {
218
- styles: module.exports.getStyleFiles(state),
219
- scripts: [],
220
- manifestAssets: module.exports.getManifestAssets(state)
221
- };
222
- }
223
-
224
- /* eslint-disable complexity */
225
- /**
226
- * Get the proper styles for the component. If a styleguide is configured, use
227
- * that css. Defaults to the site slug
228
- *
229
- * @param {Object} state
230
- * @returns {array}
231
- */
232
- function getStyleFiles(state) {
233
- const { site } = state.locals,
234
- assetDir = site && site.assetDir || MEDIA_DIRECTORY,
235
- assetPath = site && site.assetPath || '',
236
- assetHost = site && site.assetHost || '',
237
- siteStyleguide = site && site.styleguide,
238
- layoutName = _.get(state, '_layoutData.name');
239
- let cssFilePaths = [], layoutBasePath;
240
-
241
- // legacy behavior: load <component>.css and <component>.<site>.css
242
- if (!site.styleguide) {
243
- const siteSlug = site && site.slug;
244
-
245
- cssFilePaths = state._components
246
- .reduce((acc, curr) => {
247
- acc.push(
248
- path.join(assetDir, 'css', `${curr}.css`),
249
- path.join(assetDir, 'css', `${curr}.${siteSlug}.css`)
250
- );
251
- return acc;
252
- }, []);
253
- // in edit mode, grab all variations
254
- } else if (state.locals.edit) {
255
- const availableVariations = styleguide.getVariations(siteStyleguide);
256
-
257
- cssFilePaths = state._components
258
- .reduce((acc, cmptName) => {
259
- acc.push(cmptName, ...availableVariations[cmptName] || []);
260
- return acc;
261
- }, [])
262
- .map(baseName => {
263
- const basePath = path.join(assetDir, 'css', `${baseName}.${siteStyleguide}.css`);
264
-
265
- return files.fileExists(basePath) ?
266
- basePath :
267
- path.join(assetDir, 'css', `${baseName}._default.css`);
268
- });
269
- // in view mode, grab only used variations
270
- } else if (state._usedVariations) {
271
- cssFilePaths = state._usedVariations
272
- .map(variation => {
273
- const styleguideVariationPath = path.join(assetDir, 'css', `${variation}.${siteStyleguide}.css`);
274
-
275
- return files.fileExists(styleguideVariationPath) ?
276
- styleguideVariationPath :
277
- path.join(assetDir, 'css', `${variation}._default.css`);
278
- });
279
- }
280
-
281
- // add the layout css
282
- if (layoutName) {
283
- layoutBasePath = path.join(assetDir, 'css', `${layoutName}.${siteStyleguide}.css`);
284
-
285
- if (files.fileExists(layoutBasePath)) {
286
- cssFilePaths.push(layoutBasePath);
287
- } else {
288
- cssFilePaths.push(path.join(assetDir, 'css', `${layoutName}._default.css`));
289
- }
290
- }
291
-
292
- return cssFilePaths
293
- .filter(files.fileExists)
294
- .map(pathJoin(assetHost, assetPath, assetDir));
295
- }
296
- /* eslint-enable complexity */
297
-
298
- /**
299
- * Get the proper scripts for the component
300
- *
301
- * @param {string} name
302
- * @param {object} site
303
- * @returns {array}
304
- */
305
-
306
- function getScriptFiles(state) {
307
- const site = state.locals.site,
308
- slug = site.slug,
309
- assetDir = site && site.assetDir || MEDIA_DIRECTORY,
310
- assetPath = site && site.assetPath || '',
311
- assetHost = site && site.assetHost || '';
312
-
313
- return filterManifestComponents(state)
314
- .reduce((prev, name) => {
315
- prev.push(
316
- path.join(assetDir, 'js', `${name}.js`),
317
- path.join(assetDir, 'js', `${name}.${slug}.js`),
318
- path.join(assetDir, 'js', `${name}.client.js`)
319
- );
320
- return prev;
321
- }, [])
322
- .filter(files.fileExists)
323
- .map(pathJoin(assetHost, assetPath, assetDir));
324
- }
325
-
326
- /**
327
- * Read the asset manifest generated by the `clay pack` bundling command and
328
- * check for scripts which match the rendered component names. The manifest for
329
- * each asset lists its complete set of dependencies, so we dedupe them.
330
- * Webpack's runtime is added to the beginning of the list, downstream legacy
331
- * globals and Clay CLI's init script to the end.
332
- *
333
- * @param {Object} state - Amphora's render state.
334
- * @return {string[]} A list of component assets.
335
- */
336
- function getManifestAssets(state) {
337
- const site = state.locals.site,
338
- assetDir = site && site.assetDir || MEDIA_DIRECTORY,
339
- assetHost = site && site.assetHost || '/',
340
- manifestName = site && site.manifestName || 'assets-manifest.json',
341
- manifestFile = path.resolve(path.join(assetDir, manifestName));
342
- let manifest;
343
-
344
- try {
345
- manifest = files.tryRequire(manifestFile);
346
- } catch (err) {
347
- return [];
348
- }
349
-
350
- const scriptFiles = _.get(manifest, ['entrypoints', 'main', 'assets', 'js'], []);
351
-
352
- return scriptFiles.map(script => url.resolve(assetHost, script));
353
- }
354
-
355
- /**
356
- * Trim the path on the file system bythe link of the
357
- * asset directory and then join it with either the assetHost
358
- * value of the site or the assetPath value (found in the
359
- * site's config file)
360
- *
361
- * @param {String} assetHost
362
- * @param {String} assetPath
363
- * @param {String} assetDir
364
- * @return {Function}
365
- */
366
- function pathJoin(assetHost, assetPath, assetDir) {
367
- var assetDirLen = assetDir.length;
368
-
369
- return function (filePath) {
370
- var pathToFile = filePath.substr(assetDirLen);
371
-
372
- if (assetHost) return `${assetHost}${pathToFile}`;
373
- return path.join(assetPath, pathToFile);
374
- };
375
- }
376
-
377
- /**
378
- * Set the values for inlining vs tagging edit mode styles/scripts
379
- *
380
- * @param {object|boolean} options
381
- * @param {string} [cacheBuster]
382
- */
383
- function configure(options, cacheBuster = '') {
384
- if (options && _.isObject(options)) {
385
- module.exports.editStylesTags = options.styles || false;
386
- module.exports.editScriptsTags = options.scripts || false;
387
- } else {
388
- module.exports.editStylesTags = options;
389
- module.exports.editScriptsTags = options;
390
- }
391
- // Assign cacheBuster
392
- module.exports.cacheBuster = cacheBuster;
393
- }
394
-
395
- /**
396
- * Given the state and html string...
397
- *
398
- * 1. Find all associated scripts/styles
399
- * 2. Run `resolveMedia`
400
- * 3. Stitch in the assets' contents
401
- * 4. Return the HTML string
402
- *
403
- * @param {Object} state
404
- * @return {Function}
405
- */
406
- function injectScriptsAndStyles(state) {
407
- const { locals } = state;
408
-
409
- return function (html) {
410
- var mediaMap = {};
411
-
412
- // Check that HTML is string
413
- if (typeof html !== 'string') {
414
- throw new Error('Missing html parameter');
415
- }
416
-
417
- // We've got a string so let's actually get the map of styles
418
- mediaMap = module.exports.getMediaMap(state);
419
-
420
- // allow site to change the media map before applying it
421
- if (setup.resolveMedia) mediaMap = setup.resolveMedia(mediaMap, locals) || mediaMap;
422
-
423
- if (!locals.edit) {
424
- mediaMap.styles = combineFileContents(mediaMap.styles, 'public/css', '/css/', STYLE_TAG);
425
- mediaMap.scripts = combineFileContents(mediaMap.scripts, 'public/js', '/js/', SCRIPT_TAG);
426
-
427
- // TODO: Ideally, we'd load these scripts asynchronously or after the DOM
428
- // is ready, setting the `async` attribute, the `defer` attribute, or
429
- // both. Because the order in which they're parsed and executed currently
430
- // matters, we can't do that safely.
431
- mediaMap.manifestAssets = injectTags(mediaMap.manifestAssets, locals.site, SCRIPT_TAG);
432
- } else {
433
- mediaMap.styles = module.exports.editStylesTags ? injectTags(mediaMap.styles, locals.site, STYLE_TAG) : combineFileContents(mediaMap.styles, 'public/css', '/css/', STYLE_TAG);
434
- mediaMap.scripts = module.exports.editScriptsTags ? injectTags(mediaMap.scripts, locals.site, SCRIPT_TAG) : combineFileContents(mediaMap.scripts, 'public/js', '/js/', SCRIPT_TAG);
435
-
436
- // FIXME: Some of the logic which excludes client-side scripts from edit
437
- // mode sits with nymag/sites, in the `resolve-media` module, and some
438
- // lives here. Since we're currently not using Webpack for Kiln and model
439
- // assets, this feels like the best place to null out client scripts.
440
- mediaMap.manifestAssets = null;
441
- }
442
-
443
- return bluebird.props(mediaMap)
444
- .then(combinedFiles => {
445
- html = combinedFiles.styles ? appendMediaToTop(combinedFiles.styles, html) : html; // If there are styles, append them
446
- html = combinedFiles.manifestAssets ? appendMediaToBottom(combinedFiles.manifestAssets, html) : html; // If there are assets registered with the manifest, append them
447
- html = combinedFiles.scripts ? appendMediaToBottom(combinedFiles.scripts, html) : html; // If there are scripts, append them
448
- return html; // Return the compiled HTML
449
- });
450
- };
451
- }
452
-
453
- module.exports.injectScriptsAndStyles = injectScriptsAndStyles;
454
- module.exports.getMediaMap = getMediaMap;
455
- module.exports.cacheBuster = '';
456
- module.exports.configure = configure;
457
- module.exports.editStylesTags = false;
458
- module.exports.editScriptsTags = false;
459
-
460
- // For testing
461
- module.exports.getManifestAssets = getManifestAssets;
462
- module.exports.getScriptFiles = getScriptFiles;
463
- module.exports.getStyleFiles = getStyleFiles;