Html.js
9.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
function _react() {
const data = _interopRequireDefault(require("react"));
_react = function _react() {
return data;
};
return data;
}
function _ejs() {
const data = _interopRequireDefault(require("@umijs/deps/compiled/ejs"));
_ejs = function _ejs() {
return data;
};
return data;
}
function _prettier() {
const data = _interopRequireDefault(require("@umijs/deps/reexported/prettier"));
_prettier = function _prettier() {
return data;
};
return data;
}
function _utils() {
const data = require("@umijs/utils");
_utils = function _utils() {
return data;
};
return data;
}
function _assert() {
const data = _interopRequireDefault(require("assert"));
_assert = function _assert() {
return data;
};
return data;
}
function _fs() {
const data = require("fs");
_fs = function _fs() {
return data;
};
return data;
}
function _path() {
const data = require("path");
_path = function _path() {
return data;
};
return data;
}
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
class Html {
constructor(opts) {
this.config = opts.config;
this.tplPath = opts.tplPath;
}
getHtmlPath(path) {
if (path === '/') {
return 'index.html';
} // remove first and last slash
path = path.replace(/^\//, '');
path = path.replace(/\/$/, '');
if (this.config.exportStatic && this.config.exportStatic.htmlSuffix || path === 'index.html') {
return `${path}`;
} else {
return `${path}/index.html`;
}
}
getRelPathToPublicPath(path) {
const htmlPath = this.getHtmlPath(path);
const len = htmlPath.split('/').length;
return Array(this.config.exportStatic && this.config.exportStatic.htmlSuffix ? len : len - 1).join('../') || './';
}
getAsset(opts) {
if (/^https?:\/\//.test(opts.file)) {
return opts.file;
}
const file = opts.file.charAt(0) === '/' ? opts.file.slice(1) : opts.file;
if (this.config.exportStatic && this.config.exportStatic.dynamicRoot) {
return `${this.getRelPathToPublicPath(opts.path || '/')}${file}`;
} else {
return `${this.config.publicPath}${file}`;
}
}
getScriptsContent(scripts) {
return scripts.map(script => {
const content = script.content,
attrs = _objectWithoutProperties(script, ["content"]);
if (content && !attrs.src) {
const newAttrs = Object.keys(attrs).reduce((memo, key) => {
return [...memo, `${key}="${attrs[key]}"`];
}, []);
return [`<script${newAttrs.length ? ' ' : ''}${newAttrs.join(' ')}>`, content.split('\n').map(line => ` ${line}`).join('\n'), '</script>'].join('\n');
} else {
const newAttrs = Object.keys(attrs).reduce((memo, key) => {
return [...memo, `${key}="${attrs[key]}"`];
}, []);
return `<script ${newAttrs.join(' ')}></script>`;
}
}).join('\n');
}
getContent(args) {
var _this = this;
return _asyncToGenerator(function* () {
const route = args.route,
_args$tplPath = args.tplPath,
tplPath = _args$tplPath === void 0 ? _this.tplPath : _args$tplPath;
let _args$metas = args.metas,
metas = _args$metas === void 0 ? [] : _args$metas,
_args$links = args.links,
links = _args$links === void 0 ? [] : _args$links,
_args$styles = args.styles,
styles = _args$styles === void 0 ? [] : _args$styles,
_args$headJSFiles = args.headJSFiles,
headJSFiles = _args$headJSFiles === void 0 ? [] : _args$headJSFiles,
_args$headScripts = args.headScripts,
headScripts = _args$headScripts === void 0 ? [] : _args$headScripts,
_args$scripts = args.scripts,
scripts = _args$scripts === void 0 ? [] : _args$scripts,
_args$jsFiles = args.jsFiles,
jsFiles = _args$jsFiles === void 0 ? [] : _args$jsFiles,
_args$cssFiles = args.cssFiles,
cssFiles = _args$cssFiles === void 0 ? [] : _args$cssFiles;
const config = _this.config;
if (tplPath) {
(0, _assert().default)((0, _fs().existsSync)(tplPath), `getContent() failed, tplPath of ${tplPath} not exists.`);
}
const tpl = (0, _fs().readFileSync)(tplPath || (0, _path().join)(__dirname, 'document.ejs'), 'utf-8');
const context = _objectSpread({
config
}, config.exportStatic ? {
route
} : {});
let html = _ejs().default.render(tpl, context, {
_with: false,
localsName: 'context',
filename: 'document.ejs'
});
let $ = _utils().cheerio.load(html, {
// @ts-ignore
decodeEntities: false
}); // metas
metas.forEach(meta => {
$('head').append(['<meta', ...Object.keys(meta).reduce((memo, key) => {
return memo.concat(`${key}="${meta[key]}"`);
}, []), '/>'].join(' '));
}); // links
links.forEach(link => {
$('head').append(['<link', ...Object.keys(link).reduce((memo, key) => {
return memo.concat(`${key}="${link[key]}"`);
}, []), '/>'].join(' '));
}); // styles
styles.forEach(style => {
const _style$content = style.content,
content = _style$content === void 0 ? '' : _style$content,
attrs = _objectWithoutProperties(style, ["content"]);
const newAttrs = Object.keys(attrs).reduce((memo, key) => {
return memo.concat(`${key}="${attrs[key]}"`);
}, []);
$('head').append([`<style${newAttrs.length ? ' ' : ''}${newAttrs.join(' ')}>`, content.split('\n').map(line => ` ${line}`).join('\n'), '</style>'].join('\n'));
}); // css
cssFiles.forEach(file => {
$('head').append(`<link rel="stylesheet" href="${_this.getAsset({
file,
path: route.path
})}" />`);
}); // root element
const mountElementId = config.mountElementId || 'root';
if (!$(`#${mountElementId}`).length) {
const bodyEl = $('body');
(0, _assert().default)(bodyEl.length, `<body> not found in html template.`);
bodyEl.append(`<div id="${mountElementId}"></div>`);
} // js
if (headScripts.length) {
$('head').append(_this.getScriptsContent(headScripts));
}
headJSFiles.forEach(file => {
$('head').append(`<script src="${_this.getAsset({
file,
path: route.path
})}"></script>`);
});
if (scripts.length) {
$('body').append(_this.getScriptsContent(scripts));
}
jsFiles.forEach(file => {
$('body').append(`<script src="${_this.getAsset({
file,
path: route.path
})}"></script>`);
});
if (args.modifyHTML) {
$ = yield args.modifyHTML($, {
route
});
}
html = $.html(); // Node 8 not support prettier v2
// https://github.com/prettier/eslint-plugin-prettier/issues/278
try {
html = _prettier().default.format(html, {
parser: 'html'
});
} catch (_) {}
return html;
})();
}
}
var _default = Html;
exports.default = _default;