UNPKG

2.66 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7var _path = require('path');
8
9var _path2 = _interopRequireDefault(_path);
10
11function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
12
13/**
14 * Create the base template, returning a top and a bottom part
15 *
16 * @param {String} dest The URL carte-blanche should be at
17 * @return {Object} The template split into two parts
18 */
19var createBaseTemplate = function createBaseTemplate(dest, commonsChunkFilename) {
20 return {
21 top: '\n<!DOCTYPE html>\n<html>\n <head>\n <meta charset="UTF-8">\n <title>CarteBlanche</title>\n <link rel="stylesheet" type="text/css" href="' + (dest ? '/' + _path2.default.join(dest, 'client-bundle.css') : 'client-bundle.css') + '" />\n </head>\n <body>\n <div id=\'carte-blanche-root\'></div>\n',
22 bottom: (commonsChunkFilename ? ' <script src="/' + commonsChunkFilename + '"></script>' : '') + '\n <script src="' + (dest ? '/' + _path2.default.join(dest, 'client-bundle.js') : 'client-bundle.js') + '"></script>\n <script src="' + (dest ? '/' + _path2.default.join(dest, 'user-bundle.js') : 'user-bundle.js') + '"></script>\n </body>\n</html>'
23 };
24};
25
26/**
27 * Create HTML from a base template with injected styles and scripts and the
28 * common chunk
29 *
30 * @param {Object} [options] The options
31 * @param {String} [options.dest] The subfolder where the bundles are
32 * @param {Array} [options.extraScripts] An array of strings filled with JS code
33 * @param {Array} [options.extraStyles] --“-- CSS code
34 * @param {Array} [options.commonsChunkFilename] The common chunk filename
35 *
36 * @return {String} The finished HTML
37 */
38/* eslint-disable max-len */
39var createHTML = function createHTML(options) {
40 var _ref = options || {};
41
42 var dest = _ref.dest;
43 var extraScripts = _ref.extraScripts;
44 var extraStyles = _ref.extraStyles;
45 var commonsChunkFilename = _ref.commonsChunkFilename;
46
47 var baseTemplate = createBaseTemplate(dest || '', commonsChunkFilename || '');
48 // If there's no extraScripts or extraStyles return the basetemplate
49 if (!extraScripts && !extraStyles) {
50 return baseTemplate.top + '\n' + baseTemplate.bottom;
51 }
52
53 // Put together the injected content
54 var injectedContent = '';
55 if (extraScripts) {
56 injectedContent += ' <script>' + extraScripts.join('\n') + '</script>\n';
57 }
58 if (extraStyles) {
59 injectedContent += ' <style>' + extraStyles.join('\n') + '</style>\n';
60 }
61 return '' + baseTemplate.top + injectedContent + baseTemplate.bottom;
62};
63
64exports.default = createHTML;
\No newline at end of file