applyHtmlWebpackPlugin.js
6.49 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
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.fixRoutePathInWindows = fixRoutePathInWindows;
exports.default = _default;
function _react() {
const data = _interopRequireDefault(require("react"));
_react = function _react() {
return data;
};
return data;
}
function _types() {
const data = require("@umijs/types");
_types = function _types() {
return data;
};
return data;
}
function _fs() {
const data = require("fs");
_fs = function _fs() {
return data;
};
return data;
}
function _os() {
const data = require("os");
_os = function _os() {
return data;
};
return data;
}
function _path() {
const data = require("path");
_path = function _path() {
return data;
};
return data;
}
var _constants = require("../../features/ssr/constants");
var _htmlUtils = require("../htmlUtils");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
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 fixRoutePathInWindows(path) {
// window 下 : 不是一个合法路径,所以需要处理一下
// 不直接删除是为了保证 render 可以生效
if (!path || !(path === null || path === void 0 ? void 0 : path.includes(':')) || (0, _os().platform)() !== 'win32') {
return path;
}
return path.replace(/:/g, '.');
}
function _default(api) {
// maybe hack but useful
function ensureServerFileExisted() {
return new Promise(resolve => {
const interval = setInterval(() => {
if ((0, _fs().existsSync)((0, _path().join)(api.paths.absOutputPath, _constants.OUTPUT_SERVER_FILENAME))) {
clearInterval(interval);
resolve({});
}
}, 300);
});
}
class HtmlWebpackPlugin {
apply(compiler) {
compiler.hooks.emit.tapPromise('UmiHtmlGeneration', /*#__PURE__*/function () {
var _ref = _asyncToGenerator(function* (compilation) {
if (api.config.ssr) {
// waiting umi.server.js emited
yield ensureServerFileExisted();
}
const html = (0, _htmlUtils.getHtmlGenerator)({
api
});
const routeMap = yield api.applyPlugins({
key: 'modifyExportRouteMap',
type: api.ApplyPluginsType.modify,
initialValue: [{
route: {
path: '/'
},
file: 'index.html'
}],
args: {
html
}
});
var _iterator = _createForOfIteratorHelper(routeMap),
_step;
try {
for (_iterator.s(); !(_step = _iterator.n()).done;) {
const _step$value = _step.value,
route = _step$value.route,
file = _step$value.file;
const defaultContent = yield html.getContent({
route,
assets: compilation.assets,
chunks: compilation.chunks
});
const content = yield api.applyPlugins({
key: 'modifyProdHTMLContent',
type: api.ApplyPluginsType.modify,
initialValue: defaultContent,
args: {
route,
file
}
});
compilation.assets[fixRoutePathInWindows(file)] = {
source: () => content,
size: () => content.length
};
}
} catch (err) {
_iterator.e(err);
} finally {
_iterator.f();
}
return true;
});
return function (_x) {
return _ref.apply(this, arguments);
};
}());
}
}
api.modifyBundleConfig((bundleConfig, {
env,
mfsu,
type,
bundler: {
id
}
}) => {
const enableWriteToDisk = api.config.devServer && api.config.devServer.writeToDisk;
if (!mfsu && (env === 'production' || enableWriteToDisk) && id === 'webpack' && process.env.HTML !== 'none' && // avoid ssr bundler build override index.html
type === _types().BundlerConfigType.csr) {
var _bundleConfig$plugins;
(_bundleConfig$plugins = bundleConfig.plugins) === null || _bundleConfig$plugins === void 0 ? void 0 : _bundleConfig$plugins.unshift(new HtmlWebpackPlugin());
}
return bundleConfig;
});
}