-
Notifications
You must be signed in to change notification settings - Fork 0
/
renderer.js
205 lines (164 loc) · 4.7 KB
/
renderer.js
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
'use strict';
const webpack = require('webpack');
const MemoryFS = require('memory-fs');
const { tmpdir } = require('os');
const { join, basename, relative } = require('path');
const { tiferr } = require('iferr');
const TMP_PATH = tmpdir();
const mfs = new MemoryFS();
/**
* @param {string} target
* @param {string|string[]} entrys
*/
const isEntry = (target, entrys) => {
if (typeof entrys === 'string') {
return entrys === target;
}
return entrys.includes(target);
};
/**
* @template T
* @template R
* @template U
* @param {Iterable<T>|ArrayLike<T>|Object.<string, T>} obj
* @param {(this: U, value: T) => R} callback
* @param {U} thisArg
* @return {R[]}
*/
const objectMap = (obj, callback, thisArg = undefined) => {
if (obj == null) {
throw new TypeError();
}
if (Array.isArray(obj)) {
return obj.map(callback, thisArg);
}
const type = typeof obj;
if (type !== 'object' && type !== 'string') {
throw new TypeError();
}
// maybe iterable(or string)
if (typeof obj[Symbol.iterator] === 'function') {
return Array.from(obj, callback, thisArg);
}
// maybe ArrayLike
if (typeof obj.length === 'number') {
return Array.from(obj, callback, thisArg);
}
return Object.values(obj).map(callback, thisArg);
};
/**
* @param {string|Iterable<string>|ArrayLike<string>|Object.<string, string>} targets
* @param {string} base
* @return {string|string[]}
*/
const toAbsolutePath = (targets, base) => {
if (targets == null) {
return [];
}
if (typeof targets === 'string') {
return join(base, targets);
}
// Convert config of the entry from object.
return objectMap(targets, x => join(base, x));
};
/**
* @typedef {object} Hexo
*/
/**
* @param {Hexo} ctx
*/
const getRawWebpackConfig = ctx => {
return Object.assign({}, ctx.config, ctx.theme.config, ctx.config.theme_config).webpack;
};
const resolveEntryPath = (path, config, ctx) => {
if (!config.entry || !isEntry(path, toAbsolutePath(config.entry, ctx.source_dir))) {
return null;
}
return Object.assign({}, config, { entry: path });
};
/**
* @param {string} path
* @param {Hexo} ctx
*/
const getWebpackConfig = (path, ctx) => {
const raw = getRawWebpackConfig(ctx);
if (Array.isArray(raw)) {
return raw.map(item => resolveEntryPath(path, item, ctx)).filter(item => item != null);
}
return resolveEntryPath(path, raw, ctx);
};
/**
* @param {webpack.Configuration} config
* @param {Hexo} ctx
*/
const webpackInmemoryRenderAsync = (config, ctx) => {
const output = {
path: TMP_PATH,
filename: basename(config.entry)
};
const outputPath = join(output.path, output.filename);
config.output = config.output || {};
Object.assign(config.output, output);
const compiler = webpack(config);
compiler.outputFileSystem = mfs;
let resolve, reject;
const promise = new Promise((_resolve, _reject) => { resolve = _resolve; reject = _reject; });
compiler.run(tiferr(reject, stats => {
if (stats.hasErrors()) {
ctx.log.error(stats.toString());
throw new Error(stats.toJson('errors-only').errors.join('\n'));
}
if (stats.hasWarnings()) {
ctx.log.warn(stats.toString());
}
resolve(mfs.readFileSync(outputPath, 'utf8'));
}));
return promise;
};
/**
* @param {webpack.Configuration[]} configs
* @param {Hexo} ctx
*/
const webpackMultiRenderAsync = (configs, ctx) => {
if (!configs.every(config => config.output)) {
return Promise.reject(new Error());
}
return Promise.all(configs.map(config => {
const { output } = config;
const { path, filename } = output;
output.path = path ? join(TMP_PATH, path) : TMP_PATH;
const outputPath = join(output.path, output.filename);
const routePath = filename || relative(ctx.source_dir, config.entry);
const compiler = webpack(config);
compiler.outputFileSystem = mfs;
let resolve, reject;
const promise = new Promise((_resolve, _reject) => { resolve = _resolve; reject = _reject; });
compiler.run(tiferr(reject, stats => {
if (stats.hasErrors()) {
ctx.log.error(stats.toString());
throw new Error(stats.toJson('errors-only').errors.join('\n'));
}
if (stats.hasWarnings()) {
ctx.log.warn(stats.toString());
}
const result = mfs.readFileSync(outputPath, 'utf8');
ctx.route.set(routePath, result);
resolve();
}));
return promise;
}));
};
function renderer({path, text}) {
const config = getWebpackConfig(path, this);
if (config == null) {
return text;
}
if (Array.isArray(config)) {
if (config.length === 0) {
return text;
}
return webpackMultiRenderAsync(config, this).then(_ => '');
}
return webpackInmemoryRenderAsync(config, this);
}
module.exports = renderer;