@modern-js/plugin-data-loader 0.0.0-next-20221209071922 → 0.0.0-next-20221209080421
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.
- package/CHANGELOG.md +31 -24
- package/dist/js/modern/cli/create-request.js +24 -38
- package/dist/js/modern/cli/generate-client.js +9 -10
- package/dist/js/modern/cli/loader.js +15 -37
- package/dist/js/modern/server/index.js +62 -101
- package/dist/js/node/cli/create-request.js +32 -56
- package/dist/js/node/cli/generate-client.js +15 -33
- package/dist/js/node/cli/loader.js +21 -56
- package/dist/js/node/server/index.js +74 -129
- package/dist/js/treeshaking/cli/create-request.js +68 -195
- package/dist/js/treeshaking/cli/generate-client.js +18 -35
- package/dist/js/treeshaking/cli/loader.js +31 -145
- package/dist/js/treeshaking/server/index.js +294 -554
- package/dist/types/server/index.d.ts +1 -3
- package/package.json +10 -10
|
@@ -1,46 +1,27 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
-
var __export = (target, all) => {
|
|
8
|
-
for (var name in all)
|
|
9
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
10
|
-
};
|
|
11
|
-
var __copyProps = (to, from, except, desc) => {
|
|
12
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
13
|
-
for (let key of __getOwnPropNames(from))
|
|
14
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
15
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
16
|
-
}
|
|
17
|
-
return to;
|
|
18
|
-
};
|
|
19
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
20
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
21
|
-
mod
|
|
22
|
-
));
|
|
23
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
24
|
-
var stdin_exports = {};
|
|
25
|
-
__export(stdin_exports, {
|
|
26
|
-
generateClient: () => generateClient
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
27
5
|
});
|
|
28
|
-
|
|
29
|
-
var
|
|
30
|
-
|
|
6
|
+
exports.generateClient = void 0;
|
|
7
|
+
var _path = _interopRequireDefault(require("path"));
|
|
8
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
9
|
+
const generateClient = ({
|
|
10
|
+
mapFile
|
|
11
|
+
}) => {
|
|
31
12
|
const loadersMap = require(mapFile);
|
|
32
|
-
const requestCode = Object.keys(loadersMap).map(
|
|
13
|
+
const requestCode = Object.keys(loadersMap).map(loaderId => {
|
|
33
14
|
const routeId = loadersMap[loaderId];
|
|
34
15
|
return `
|
|
35
16
|
const ${loaderId} = createRequest('${routeId}');
|
|
36
17
|
`;
|
|
37
|
-
}).join(
|
|
18
|
+
}).join('');
|
|
38
19
|
let exportsCode = `export {`;
|
|
39
20
|
for (const loader of Object.keys(loadersMap)) {
|
|
40
21
|
exportsCode += `${loader},`;
|
|
41
22
|
}
|
|
42
|
-
exportsCode +=
|
|
43
|
-
const requestCreatorPath =
|
|
23
|
+
exportsCode += '}';
|
|
24
|
+
const requestCreatorPath = _path.default.join(__dirname, './create-request').replace('node', 'treeshaking').replace(/\\/g, '/');
|
|
44
25
|
const importCode = `
|
|
45
26
|
import { createRequest } from '${requestCreatorPath}';
|
|
46
27
|
`;
|
|
@@ -50,3 +31,4 @@ const generateClient = ({ mapFile }) => {
|
|
|
50
31
|
${exportsCode}
|
|
51
32
|
`;
|
|
52
33
|
};
|
|
34
|
+
exports.generateClient = generateClient;
|
|
@@ -1,58 +1,23 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
var __export = (target, all) => {
|
|
6
|
-
for (var name in all)
|
|
7
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
-
};
|
|
9
|
-
var __copyProps = (to, from, except, desc) => {
|
|
10
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
-
for (let key of __getOwnPropNames(from))
|
|
12
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
-
}
|
|
15
|
-
return to;
|
|
16
|
-
};
|
|
17
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
-
var stdin_exports = {};
|
|
19
|
-
__export(stdin_exports, {
|
|
20
|
-
default: () => loader
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
21
5
|
});
|
|
22
|
-
|
|
23
|
-
var
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
reject(e);
|
|
38
|
-
}
|
|
39
|
-
};
|
|
40
|
-
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
41
|
-
step((generator = generator.apply(__this, __arguments)).next());
|
|
42
|
-
});
|
|
43
|
-
};
|
|
44
|
-
function loader(source) {
|
|
45
|
-
return __async(this, null, function* () {
|
|
46
|
-
var _a;
|
|
47
|
-
this.cacheable();
|
|
48
|
-
const target = (_a = this._compiler) == null ? void 0 : _a.options.target;
|
|
49
|
-
if (target === "node") {
|
|
50
|
-
return source;
|
|
51
|
-
}
|
|
52
|
-
const options = this.getOptions();
|
|
53
|
-
const code = (0, import_generate_client.generateClient)({
|
|
54
|
-
mapFile: options.mapFile
|
|
55
|
-
});
|
|
56
|
-
return code;
|
|
6
|
+
exports.default = loader;
|
|
7
|
+
var _generateClient = require("./generate-client");
|
|
8
|
+
// eslint-disable-next-line eslint-comments/disable-enable-pair
|
|
9
|
+
/* eslint-disable @babel/no-invalid-this */
|
|
10
|
+
|
|
11
|
+
async function loader(source) {
|
|
12
|
+
var _this$_compiler;
|
|
13
|
+
this.cacheable();
|
|
14
|
+
const target = (_this$_compiler = this._compiler) === null || _this$_compiler === void 0 ? void 0 : _this$_compiler.options.target;
|
|
15
|
+
if (target === 'node') {
|
|
16
|
+
return source;
|
|
17
|
+
}
|
|
18
|
+
const options = this.getOptions();
|
|
19
|
+
const code = (0, _generateClient.generateClient)({
|
|
20
|
+
mapFile: options.mapFile
|
|
57
21
|
});
|
|
58
|
-
|
|
22
|
+
return code;
|
|
23
|
+
}
|
|
@@ -1,121 +1,57 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
var __export = (target, all) => {
|
|
6
|
-
for (var name in all)
|
|
7
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
-
};
|
|
9
|
-
var __copyProps = (to, from, except, desc) => {
|
|
10
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
-
for (let key of __getOwnPropNames(from))
|
|
12
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
-
}
|
|
15
|
-
return to;
|
|
16
|
-
};
|
|
17
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
-
var stdin_exports = {};
|
|
19
|
-
__export(stdin_exports, {
|
|
20
|
-
default: () => server_default,
|
|
21
|
-
getPathWithoutEntry: () => getPathWithoutEntry,
|
|
22
|
-
handleRequest: () => handleRequest,
|
|
23
|
-
isRedirectResponse: () => isRedirectResponse,
|
|
24
|
-
isResponse: () => isResponse
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
25
5
|
});
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
var
|
|
30
|
-
var
|
|
31
|
-
var
|
|
32
|
-
var
|
|
33
|
-
var
|
|
34
|
-
var
|
|
35
|
-
var
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
var __spreadValues = (a, b) => {
|
|
42
|
-
for (var prop in b || (b = {}))
|
|
43
|
-
if (__hasOwnProp2.call(b, prop))
|
|
44
|
-
__defNormalProp(a, prop, b[prop]);
|
|
45
|
-
if (__getOwnPropSymbols)
|
|
46
|
-
for (var prop of __getOwnPropSymbols(b)) {
|
|
47
|
-
if (__propIsEnum.call(b, prop))
|
|
48
|
-
__defNormalProp(a, prop, b[prop]);
|
|
49
|
-
}
|
|
50
|
-
return a;
|
|
51
|
-
};
|
|
52
|
-
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
53
|
-
var __copyProps2 = (to, from, except, desc) => {
|
|
54
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
55
|
-
for (let key of __getOwnPropNames2(from))
|
|
56
|
-
if (!__hasOwnProp2.call(to, key) && key !== except)
|
|
57
|
-
__defProp2(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc2(from, key)) || desc.enumerable });
|
|
58
|
-
}
|
|
59
|
-
return to;
|
|
60
|
-
};
|
|
61
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps2(
|
|
62
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp2(target, "default", { value: mod, enumerable: true }) : target,
|
|
63
|
-
mod
|
|
64
|
-
));
|
|
65
|
-
var __async = (__this, __arguments, generator) => {
|
|
66
|
-
return new Promise((resolve, reject) => {
|
|
67
|
-
var fulfilled = (value) => {
|
|
68
|
-
try {
|
|
69
|
-
step(generator.next(value));
|
|
70
|
-
} catch (e) {
|
|
71
|
-
reject(e);
|
|
72
|
-
}
|
|
73
|
-
};
|
|
74
|
-
var rejected = (value) => {
|
|
75
|
-
try {
|
|
76
|
-
step(generator.throw(value));
|
|
77
|
-
} catch (e) {
|
|
78
|
-
reject(e);
|
|
79
|
-
}
|
|
80
|
-
};
|
|
81
|
-
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
82
|
-
step((generator = generator.apply(__this, __arguments)).next());
|
|
83
|
-
});
|
|
84
|
-
};
|
|
85
|
-
(0, import_node.installGlobals)();
|
|
86
|
-
const LOADER_SEARCH_PARAM = "_loader";
|
|
87
|
-
const redirectStatusCodes = /* @__PURE__ */ new Set([301, 302, 303, 307, 308]);
|
|
6
|
+
exports.handleRequest = exports.getPathWithoutEntry = exports.default = void 0;
|
|
7
|
+
exports.isRedirectResponse = isRedirectResponse;
|
|
8
|
+
exports.isResponse = isResponse;
|
|
9
|
+
var _node = require("@remix-run/node");
|
|
10
|
+
var _reactRouterDom = require("react-router-dom");
|
|
11
|
+
var _utils = require("@modern-js/utils");
|
|
12
|
+
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
13
|
+
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
14
|
+
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
15
|
+
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
|
16
|
+
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; }
|
|
17
|
+
// Polyfill Web Fetch API
|
|
18
|
+
(0, _node.installGlobals)();
|
|
19
|
+
const LOADER_SEARCH_PARAM = '_loader';
|
|
20
|
+
const redirectStatusCodes = new Set([301, 302, 303, 307, 308]);
|
|
88
21
|
function isRedirectResponse(response) {
|
|
89
22
|
return redirectStatusCodes.has(response.status);
|
|
90
23
|
}
|
|
91
24
|
function isResponse(value) {
|
|
92
|
-
return value != null && typeof value.status ===
|
|
25
|
+
return value != null && typeof value.status === 'number' && typeof value.statusText === 'string' && typeof value.headers === 'object' && typeof value.body !== 'undefined';
|
|
93
26
|
}
|
|
94
27
|
const json = (data, init = {}) => {
|
|
95
|
-
const responseInit = typeof init ===
|
|
28
|
+
const responseInit = typeof init === 'number' ? {
|
|
29
|
+
status: init
|
|
30
|
+
} : init;
|
|
96
31
|
const headers = new Headers(responseInit.headers);
|
|
97
|
-
if (!headers.has(
|
|
98
|
-
headers.set(
|
|
32
|
+
if (!headers.has('Content-Type')) {
|
|
33
|
+
headers.set('Content-Type', 'application/json; charset=utf-8');
|
|
99
34
|
}
|
|
100
|
-
return new
|
|
35
|
+
return new _node.Response(JSON.stringify(data), _objectSpread(_objectSpread({}, responseInit), {}, {
|
|
101
36
|
headers
|
|
102
37
|
}));
|
|
103
38
|
};
|
|
104
|
-
|
|
39
|
+
|
|
40
|
+
// TODO: 添加 context
|
|
41
|
+
const callRouteLoader = async ({
|
|
105
42
|
routeId,
|
|
106
43
|
loader,
|
|
107
44
|
params,
|
|
108
45
|
request,
|
|
46
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
109
47
|
loadContext
|
|
110
|
-
}) {
|
|
48
|
+
}) => {
|
|
111
49
|
if (!loader) {
|
|
112
|
-
throw new Error(
|
|
113
|
-
`You made a ${request.method} request to ${request.url} but did not provide a default component or \`loader\` for route "${routeId}", so there is no way to handle the request.`
|
|
114
|
-
);
|
|
50
|
+
throw new Error(`You made a ${request.method} request to ${request.url} but did not provide ` + `a default component or \`loader\` for route "${routeId}", ` + `so there is no way to handle the request.`);
|
|
115
51
|
}
|
|
116
52
|
let result;
|
|
117
53
|
try {
|
|
118
|
-
result =
|
|
54
|
+
result = await loader({
|
|
119
55
|
request,
|
|
120
56
|
params
|
|
121
57
|
});
|
|
@@ -125,14 +61,12 @@ const callRouteLoader = (_0) => __async(void 0, [_0], function* ({
|
|
|
125
61
|
}
|
|
126
62
|
result = error;
|
|
127
63
|
}
|
|
128
|
-
if (result ===
|
|
129
|
-
throw new Error(
|
|
130
|
-
`You defined a loader for route "${routeId}" but didn't return anything from your \`loader\` function. Please return a value or \`null\`.`
|
|
131
|
-
);
|
|
64
|
+
if (result === undefined) {
|
|
65
|
+
throw new Error(`You defined a loader for route "${routeId}" but didn't return ` + `anything from your \`loader\` function. Please return a value or \`null\`.`);
|
|
132
66
|
}
|
|
133
67
|
return isResponse(result) ? result : json(result);
|
|
134
|
-
}
|
|
135
|
-
const createLoaderHeaders =
|
|
68
|
+
};
|
|
69
|
+
const createLoaderHeaders = requestHeaders => {
|
|
136
70
|
const headers = new Headers();
|
|
137
71
|
for (const [key, values] of Object.entries(requestHeaders)) {
|
|
138
72
|
if (values) {
|
|
@@ -147,8 +81,9 @@ const createLoaderHeaders = (requestHeaders) => {
|
|
|
147
81
|
}
|
|
148
82
|
return headers;
|
|
149
83
|
};
|
|
150
|
-
const createLoaderRequest =
|
|
84
|
+
const createLoaderRequest = context => {
|
|
151
85
|
const origin = `${context.protocol}://${context.host}`;
|
|
86
|
+
// eslint-disable-next-line node/no-unsupported-features/node-builtins, node/prefer-global/url
|
|
152
87
|
const url = new URL(context.url, origin);
|
|
153
88
|
const controller = new AbortController();
|
|
154
89
|
const init = {
|
|
@@ -158,54 +93,62 @@ const createLoaderRequest = (context) => {
|
|
|
158
93
|
};
|
|
159
94
|
return new Request(url.href, init);
|
|
160
95
|
};
|
|
161
|
-
const sendLoaderResponse = (res, nodeResponse) =>
|
|
96
|
+
const sendLoaderResponse = async (res, nodeResponse) => {
|
|
162
97
|
res.statusMessage = nodeResponse.statusText;
|
|
163
98
|
res.statusCode = nodeResponse.status;
|
|
164
99
|
for (const [key, value] of nodeResponse.headers.entries()) {
|
|
165
100
|
res.setHeader(key, value);
|
|
166
101
|
}
|
|
167
102
|
if (nodeResponse.body) {
|
|
168
|
-
|
|
103
|
+
await (0, _node.writeReadableStreamToWritable)(nodeResponse.body, res);
|
|
169
104
|
} else {
|
|
170
105
|
res.end();
|
|
171
106
|
}
|
|
172
|
-
}
|
|
107
|
+
};
|
|
173
108
|
const getPathWithoutEntry = (pathname, entryPath) => {
|
|
174
|
-
if (entryPath ===
|
|
109
|
+
if (entryPath === '/') {
|
|
175
110
|
return pathname;
|
|
176
111
|
}
|
|
177
|
-
return pathname.replace(entryPath,
|
|
112
|
+
return pathname.replace(entryPath, '');
|
|
178
113
|
};
|
|
114
|
+
exports.getPathWithoutEntry = getPathWithoutEntry;
|
|
179
115
|
const matchEntry = (pathname, entries) => {
|
|
180
|
-
return entries.find(
|
|
116
|
+
return entries.find(entry => pathname.startsWith(entry.urlPath));
|
|
181
117
|
};
|
|
182
|
-
const handleRequest =
|
|
118
|
+
const handleRequest = async ({
|
|
183
119
|
context,
|
|
184
120
|
serverRoutes,
|
|
185
121
|
distDir
|
|
186
|
-
}) {
|
|
187
|
-
const {
|
|
122
|
+
}) => {
|
|
123
|
+
const {
|
|
124
|
+
method,
|
|
125
|
+
query
|
|
126
|
+
} = context;
|
|
188
127
|
const routeId = query[LOADER_SEARCH_PARAM];
|
|
189
|
-
if (!routeId || method.toLowerCase() !==
|
|
128
|
+
if (!routeId || method.toLowerCase() !== 'get') {
|
|
190
129
|
return;
|
|
191
130
|
}
|
|
192
131
|
const entry = matchEntry(context.path, serverRoutes);
|
|
193
132
|
if (!entry) {
|
|
194
133
|
return;
|
|
195
134
|
}
|
|
196
|
-
const {
|
|
135
|
+
const {
|
|
136
|
+
routes
|
|
137
|
+
} = await Promise.resolve(`${distDir}/${_utils.LOADER_ROUTES_DIR}/${entry.entryName}`).then(s => _interopRequireWildcard(require(s)));
|
|
197
138
|
if (!routes) {
|
|
198
139
|
return;
|
|
199
140
|
}
|
|
200
|
-
const {
|
|
141
|
+
const {
|
|
142
|
+
res
|
|
143
|
+
} = context;
|
|
201
144
|
const pathname = getPathWithoutEntry(context.path, entry.urlPath);
|
|
202
|
-
const matches = (0,
|
|
145
|
+
const matches = (0, _reactRouterDom.matchRoutes)(routes, pathname);
|
|
203
146
|
if (!matches) {
|
|
204
147
|
res.statusCode = 403;
|
|
205
148
|
res.end(`Route ${pathname} was not matched`);
|
|
206
149
|
return;
|
|
207
150
|
}
|
|
208
|
-
const match = matches
|
|
151
|
+
const match = matches === null || matches === void 0 ? void 0 : matches.find(match => match.route.id === routeId);
|
|
209
152
|
if (!match) {
|
|
210
153
|
res.statusCode = 403;
|
|
211
154
|
res.end(`Route ${routeId} does not match URL ${context.path}`);
|
|
@@ -214,7 +157,7 @@ const handleRequest = (_0) => __async(void 0, [_0], function* ({
|
|
|
214
157
|
const request = createLoaderRequest(context);
|
|
215
158
|
let response;
|
|
216
159
|
try {
|
|
217
|
-
response =
|
|
160
|
+
response = await callRouteLoader({
|
|
218
161
|
loader: match.route.loader,
|
|
219
162
|
routeId: match.route.id,
|
|
220
163
|
params: match.params,
|
|
@@ -223,38 +166,40 @@ const handleRequest = (_0) => __async(void 0, [_0], function* ({
|
|
|
223
166
|
});
|
|
224
167
|
if (isRedirectResponse(response)) {
|
|
225
168
|
const headers = new Headers(response.headers);
|
|
226
|
-
headers.set(
|
|
227
|
-
headers.delete(
|
|
228
|
-
response = new
|
|
169
|
+
headers.set('X-Modernjs-Redirect', headers.get('Location'));
|
|
170
|
+
headers.delete('Location');
|
|
171
|
+
response = new _node.Response(null, {
|
|
229
172
|
status: 204,
|
|
230
173
|
headers
|
|
231
174
|
});
|
|
232
175
|
}
|
|
233
176
|
} catch (error) {
|
|
234
177
|
const message = String(error);
|
|
235
|
-
response = new
|
|
178
|
+
response = new _node.Response(message, {
|
|
236
179
|
status: 500,
|
|
237
180
|
headers: {
|
|
238
|
-
|
|
181
|
+
'Content-Type': 'text/plain'
|
|
239
182
|
}
|
|
240
183
|
});
|
|
241
184
|
}
|
|
242
185
|
sendLoaderResponse(res, response);
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
|
|
186
|
+
};
|
|
187
|
+
exports.handleRequest = handleRequest;
|
|
188
|
+
var _default = () => ({
|
|
189
|
+
name: '@modern-js/plugin-data-loader',
|
|
246
190
|
setup: () => ({
|
|
247
191
|
preparebeforeRouteHandler({
|
|
248
192
|
serverRoutes,
|
|
249
193
|
distDir
|
|
250
194
|
}) {
|
|
251
|
-
return
|
|
195
|
+
return async context => {
|
|
252
196
|
return handleRequest({
|
|
253
197
|
serverRoutes,
|
|
254
198
|
distDir,
|
|
255
199
|
context
|
|
256
200
|
});
|
|
257
|
-
}
|
|
201
|
+
};
|
|
258
202
|
}
|
|
259
203
|
})
|
|
260
204
|
});
|
|
205
|
+
exports.default = _default;
|