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