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