amphora-html 6.0.0-4 → 6.0.0-7

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