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,414 +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 === SCRIPT_TAG){
157
- return `<script type="text/javascript" src="${file}${buster}"></script>`;
158
- }
159
- }).join('\n'));
160
- }
161
-
162
- /**
163
- * Filter the components rendered in this request, removing Webpack-managed
164
- * assets so that we can fall back on Browserify.
165
- *
166
- * @param {Object} state - Amphora render state.
167
- * @returns {string[]} - A list of components not available through Webpack.
168
- */
169
- function filterManifestComponents(state) {
170
- const site = state.locals.site,
171
- assetDir = site && site.assetDir || MEDIA_DIRECTORY,
172
- manifestName = site && site.manifestName || 'assets-manifest.json',
173
- manifestFile = path.resolve(path.join(assetDir, manifestName));
174
- let manifest;
175
-
176
- try {
177
- manifest = files.tryRequire(manifestFile);
178
- } catch (err) {
179
- return state._components;
180
- }
181
-
182
-
183
- return state._components
184
- .filter(componentName => !manifest.hasOwnProperty(`${ componentName }.js`));
185
- }
186
-
187
- /**
188
- * Gets a list of all css and js needed for the components listed.
189
- * @param {Object} state
190
- * @returns {{styles: Array, scripts: Array}}
191
- */
192
- function getMediaMap(state) {
193
- return {
194
- styles: module.exports.getStyleFiles(state),
195
- scripts: module.exports.getScriptFiles(state)
196
- };
197
- }
198
-
199
- /* eslint-disable complexity */
200
- /**
201
- * Get the proper styles for the component. If a styleguide is configured, use
202
- * that css. Defaults to the site slug
203
- *
204
- * @param {Object} state
205
- * @returns {array}
206
- */
207
- function getStyleFiles(state) {
208
- const { site } = state.locals,
209
- assetDir = site && site.assetDir || MEDIA_DIRECTORY,
210
- assetPath = site && site.assetPath || '',
211
- assetHost = site && site.assetHost || '',
212
- siteStyleguide = site && site.styleguide,
213
- layoutName = _.get(state, '_layoutData.name');
214
- let cssFilePaths = [], layoutBasePath;
215
-
216
- // legacy behavior: load <component>.css and <component>.<site>.css
217
- if (!site.styleguide) {
218
- const siteSlug = site && site.slug;
219
-
220
- cssFilePaths = state._components
221
- .reduce((acc, curr) => {
222
- acc.push(
223
- path.join(assetDir, 'css', `${curr}.css`),
224
- path.join(assetDir, 'css', `${curr}.${siteSlug}.css`)
225
- );
226
- return acc;
227
- }, []);
228
- // in edit mode, grab all variations
229
- } else if (state.locals.edit) {
230
- const availableVariations = styleguide.getVariations(siteStyleguide);
231
-
232
- cssFilePaths = state._components
233
- .reduce((acc, cmptName) => {
234
- acc.push(cmptName, ...availableVariations[cmptName] || []);
235
- return acc;
236
- }, [])
237
- .map(baseName => {
238
- const basePath = path.join(assetDir, 'css', `${baseName}.${siteStyleguide}.css`);
239
-
240
- return files.fileExists(basePath) ?
241
- basePath :
242
- path.join(assetDir, 'css', `${baseName}._default.css`);
243
- });
244
- // in view mode, grab only used variations
245
- } else if (state._usedVariations) {
246
- cssFilePaths = state._usedVariations
247
- .map(variation => {
248
- const styleguideVariationPath = path.join(assetDir, 'css', `${variation}.${siteStyleguide}.css`);
249
-
250
- return files.fileExists(styleguideVariationPath) ?
251
- styleguideVariationPath :
252
- path.join(assetDir, 'css', `${variation}._default.css`);
253
- });
254
- }
255
-
256
- // add the layout css
257
- if (layoutName) {
258
- layoutBasePath = path.join(assetDir, 'css', `${layoutName}.${siteStyleguide}.css`);
259
-
260
- if (files.fileExists(layoutBasePath)) {
261
- cssFilePaths.push(layoutBasePath);
262
- } else {
263
- cssFilePaths.push(path.join(assetDir, 'css', `${layoutName}._default.css`));
264
- }
265
- }
266
-
267
- return cssFilePaths
268
- .filter(files.fileExists)
269
- .map(pathJoin(assetHost, assetPath, assetDir));
270
- }
271
- /* eslint-enable complexity */
272
-
273
- /**
274
- * Get the proper scripts for the component
275
- *
276
- * @param {string} name
277
- * @param {object} site
278
- * @returns {array}
279
- */
280
-
281
- function getScriptFiles(state) {
282
- const site = state.locals.site,
283
- slug = site.slug,
284
- assetDir = site && site.assetDir || MEDIA_DIRECTORY,
285
- assetPath = site && site.assetPath || '',
286
- assetHost = site && site.assetHost || '',
287
- manifestName = site && site.manifestName || 'assets-manifest.json',
288
- manifestFile = path.resolve(path.join(assetDir, manifestName)),
289
- manifestData = files.readFile(manifestFile);
290
-
291
- if (manifestData) {
292
- const manifest = JSON.parse(manifestData),
293
- entries = _.get(manifest, 'entrypoints', {}),
294
- scripts = _.flatMap(entries, entry => _.get(entry, 'assets.js', []));
295
-
296
- return _.uniq(scripts);
297
- }
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
- * Trim the path on the file system bythe link of the
314
- * asset directory and then join it with either the assetHost
315
- * value of the site or the assetPath value (found in the
316
- * site's config file)
317
- *
318
- * @param {String} assetHost
319
- * @param {String} assetPath
320
- * @param {String} assetDir
321
- * @return {Function}
322
- */
323
- function pathJoin(assetHost, assetPath, assetDir) {
324
- var assetDirLen = assetDir.length;
325
-
326
- return function (filePath) {
327
- var pathToFile = filePath.substr(assetDirLen);
328
-
329
- if (assetHost) return `${assetHost}${pathToFile}`;
330
- return path.join(assetPath, pathToFile);
331
- };
332
- }
333
-
334
- /**
335
- * Set the values for inlining vs tagging edit mode styles/scripts
336
- *
337
- * @param {object|boolean} options
338
- * @param {string} [cacheBuster]
339
- */
340
- function configure(options, cacheBuster = '') {
341
- if (options && _.isObject(options)) {
342
- module.exports.editStylesTags = options.styles || false;
343
- module.exports.editScriptsTags = options.scripts || false;
344
- } else {
345
- module.exports.editStylesTags = options;
346
- module.exports.editScriptsTags = options;
347
- }
348
- // Assign cacheBuster
349
- module.exports.cacheBuster = cacheBuster;
350
- }
351
-
352
- /**
353
- * Given the state and html string...
354
- *
355
- * 1. Find all associated scripts/styles
356
- * 2. Run `resolveMedia`
357
- * 3. Stitch in the assets' contents
358
- * 4. Return the HTML string
359
- *
360
- * @param {Object} state
361
- * @return {Function}
362
- */
363
- function injectScriptsAndStyles(state) {
364
- const { locals } = state;
365
-
366
- return function (html) {
367
- var mediaMap = {};
368
-
369
- // Check that HTML is string
370
- if (typeof html !== 'string') {
371
- throw new Error('Missing html parameter');
372
- }
373
-
374
- // We've got a string so let's actually get the map of styles
375
- mediaMap = module.exports.getMediaMap(state);
376
-
377
- // allow site to change the media map before applying it
378
- if (setup.resolveMedia) mediaMap = setup.resolveMedia(mediaMap, locals) || mediaMap;
379
-
380
- if (!locals.edit) {
381
- mediaMap.styles = combineFileContents(mediaMap.styles, 'public/css', '/css/', STYLE_TAG);
382
- mediaMap.scripts = combineFileContents(mediaMap.scripts, 'public/js', '/js/', SCRIPT_TAG);
383
- } else {
384
- mediaMap.styles = module.exports.editStylesTags ? injectTags(mediaMap.styles, locals.site, STYLE_TAG) : combineFileContents(mediaMap.styles, 'public/css', '/css/', STYLE_TAG);
385
- mediaMap.scripts = module.exports.editScriptsTags ? injectTags(mediaMap.scripts, locals.site, SCRIPT_TAG) : combineFileContents(mediaMap.scripts, 'public/js', '/js/', SCRIPT_TAG);
386
-
387
- // FIXME: Some of the logic which excludes client-side scripts from edit
388
- // mode sits with nymag/sites, in the `resolve-media` module, and some
389
- // lives here. Since we're currently not using Webpack for Kiln and model
390
- // assets, this feels like the best place to null out client scripts.
391
- mediaMap.manifestAssets = null;
392
- }
393
-
394
- return bluebird.props(mediaMap)
395
- .then(combinedFiles => {
396
- html = combinedFiles.styles ? appendMediaToTop(combinedFiles.styles, html) : html; // If there are styles, append them
397
- html = combinedFiles.manifestAssets ? appendMediaToBottom(combinedFiles.manifestAssets, html) : html; // If there are assets registered with the manifest, append them
398
- html = combinedFiles.scripts ? appendMediaToBottom(combinedFiles.scripts, html) : html; // If there are scripts, append them
399
- return html; // Return the compiled HTML
400
- });
401
- };
402
- }
403
-
404
- module.exports.injectScriptsAndStyles = injectScriptsAndStyles;
405
- module.exports.getMediaMap = getMediaMap;
406
- module.exports.cacheBuster = '';
407
- module.exports.configure = configure;
408
- module.exports.editStylesTags = false;
409
- module.exports.editScriptsTags = false;
410
-
411
- // For testing
412
- module.exports.getManifestAssets = getManifestAssets;
413
- module.exports.getScriptFiles = getScriptFiles;
414
- module.exports.getStyleFiles = getStyleFiles;