@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
package/CHANGELOG.md
CHANGED
|
@@ -1,39 +1,46 @@
|
|
|
1
1
|
# @modern-js/plugin-data-loader
|
|
2
2
|
|
|
3
|
-
## 0.0.0-next-
|
|
3
|
+
## 0.0.0-next-20221209080421
|
|
4
4
|
|
|
5
5
|
### Major Changes
|
|
6
6
|
|
|
7
|
-
-
|
|
7
|
+
- dda38c9c3e: chore: v2
|
|
8
8
|
|
|
9
9
|
### Minor Changes
|
|
10
10
|
|
|
11
|
-
-
|
|
11
|
+
- b710adb843: feat: extract the data loader
|
|
12
12
|
feat: 提取 data loader
|
|
13
|
-
-
|
|
13
|
+
- 543be9558e: feat: compile server loader and support handle loader request
|
|
14
14
|
feat: 编译 server loader 并支持处理 loader 的请求
|
|
15
15
|
|
|
16
16
|
### Patch Changes
|
|
17
17
|
|
|
18
|
-
-
|
|
18
|
+
- 6604f1b8b3: feat: support router basename
|
|
19
|
+
feat: router 插件支持设置 basename
|
|
20
|
+
- 2e6031955e: fix: some optimizations for router and loader
|
|
19
21
|
fix: 一些 router 和 loader 的优化
|
|
20
22
|
q
|
|
21
|
-
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
- Updated dependencies [
|
|
26
|
-
- Updated dependencies [
|
|
27
|
-
- Updated dependencies [
|
|
28
|
-
- Updated dependencies [
|
|
29
|
-
- Updated dependencies [
|
|
30
|
-
- Updated dependencies [
|
|
31
|
-
- Updated dependencies [
|
|
32
|
-
- Updated dependencies [
|
|
33
|
-
- Updated dependencies [
|
|
34
|
-
- Updated dependencies [
|
|
35
|
-
- Updated dependencies [
|
|
36
|
-
- Updated dependencies [
|
|
37
|
-
- Updated dependencies [
|
|
38
|
-
|
|
39
|
-
|
|
23
|
+
- ea7cf06257: chore: bump webpack/babel-loader/postcss-loader/tsconfig-paths
|
|
24
|
+
|
|
25
|
+
chore: 升级 webpack/babel-loader/postcss-loader/tsconfig-paths 版本
|
|
26
|
+
|
|
27
|
+
- Updated dependencies [6aca875011]
|
|
28
|
+
- Updated dependencies [2e6031955e]
|
|
29
|
+
- Updated dependencies [92f0eade39]
|
|
30
|
+
- Updated dependencies [edd1cfb1af]
|
|
31
|
+
- Updated dependencies [cc971eabfc]
|
|
32
|
+
- Updated dependencies [5b9049f2e9]
|
|
33
|
+
- Updated dependencies [92004d1906]
|
|
34
|
+
- Updated dependencies [b8bbe036c7]
|
|
35
|
+
- Updated dependencies [d5a31df781]
|
|
36
|
+
- Updated dependencies [dda38c9c3e]
|
|
37
|
+
- Updated dependencies [3bbea92b2a]
|
|
38
|
+
- Updated dependencies [b710adb843]
|
|
39
|
+
- Updated dependencies [ea7cf06257]
|
|
40
|
+
- Updated dependencies [bbe4c4ab64]
|
|
41
|
+
- Updated dependencies [e4558a0bc4]
|
|
42
|
+
- Updated dependencies [abf3421a75]
|
|
43
|
+
- Updated dependencies [543be9558e]
|
|
44
|
+
- Updated dependencies [14b712da84]
|
|
45
|
+
- @modern-js/utils@0.0.0-next-20221209080421
|
|
46
|
+
- @modern-js/babel-compiler@0.0.0-next-20221209080421
|
|
@@ -1,26 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
}
|
|
9
|
-
};
|
|
10
|
-
var rejected = (value) => {
|
|
11
|
-
try {
|
|
12
|
-
step(generator.throw(value));
|
|
13
|
-
} catch (e) {
|
|
14
|
-
reject(e);
|
|
15
|
-
}
|
|
16
|
-
};
|
|
17
|
-
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
18
|
-
step((generator = generator.apply(__this, __arguments)).next());
|
|
19
|
-
});
|
|
20
|
-
};
|
|
21
|
-
import { compile } from "path-to-regexp";
|
|
22
|
-
import { redirect } from "react-router-dom";
|
|
23
|
-
const getRequestUrl = ({
|
|
1
|
+
// eslint-disable-next-line eslint-comments/disable-enable-pair
|
|
2
|
+
/* eslint-disable node/prefer-global/url */
|
|
3
|
+
// eslint-disable-next-line eslint-comments/disable-enable-pair
|
|
4
|
+
/* eslint-disable node/no-unsupported-features/node-builtins */
|
|
5
|
+
import { compile } from 'path-to-regexp';
|
|
6
|
+
import { redirect } from 'react-router-dom';
|
|
7
|
+
export const getRequestUrl = ({
|
|
24
8
|
params,
|
|
25
9
|
request,
|
|
26
10
|
routeId
|
|
@@ -31,25 +15,31 @@ const getRequestUrl = ({
|
|
|
31
15
|
});
|
|
32
16
|
const newPathName = toPath(params);
|
|
33
17
|
url.pathname = newPathName;
|
|
34
|
-
url.searchParams.append(
|
|
18
|
+
url.searchParams.append('_loader', routeId);
|
|
35
19
|
return url;
|
|
36
20
|
};
|
|
37
|
-
const handleRedirectResponse =
|
|
38
|
-
const {
|
|
39
|
-
|
|
21
|
+
const handleRedirectResponse = res => {
|
|
22
|
+
const {
|
|
23
|
+
headers
|
|
24
|
+
} = res;
|
|
25
|
+
const location = headers.get('X-Modernjs-Redirect');
|
|
40
26
|
if (location) {
|
|
41
27
|
return redirect(location);
|
|
42
28
|
}
|
|
43
29
|
return res;
|
|
44
30
|
};
|
|
45
|
-
const createRequest = (routeId, method =
|
|
46
|
-
return
|
|
31
|
+
export const createRequest = (routeId, method = 'get') => {
|
|
32
|
+
return async ({
|
|
47
33
|
params,
|
|
48
34
|
request
|
|
49
|
-
}) {
|
|
50
|
-
const url = getRequestUrl({
|
|
35
|
+
}) => {
|
|
36
|
+
const url = getRequestUrl({
|
|
37
|
+
params,
|
|
38
|
+
request,
|
|
39
|
+
routeId
|
|
40
|
+
});
|
|
51
41
|
try {
|
|
52
|
-
const res =
|
|
42
|
+
const res = await fetch(url, {
|
|
53
43
|
method,
|
|
54
44
|
signal: request.signal
|
|
55
45
|
});
|
|
@@ -58,9 +48,5 @@ const createRequest = (routeId, method = "get") => {
|
|
|
58
48
|
console.error(error);
|
|
59
49
|
throw error;
|
|
60
50
|
}
|
|
61
|
-
}
|
|
62
|
-
};
|
|
63
|
-
export {
|
|
64
|
-
createRequest,
|
|
65
|
-
getRequestUrl
|
|
66
|
-
};
|
|
51
|
+
};
|
|
52
|
+
};
|
|
@@ -1,18 +1,20 @@
|
|
|
1
|
-
import path from
|
|
2
|
-
const generateClient = ({
|
|
1
|
+
import path from 'path';
|
|
2
|
+
export const generateClient = ({
|
|
3
|
+
mapFile
|
|
4
|
+
}) => {
|
|
3
5
|
const loadersMap = require(mapFile);
|
|
4
|
-
const requestCode = Object.keys(loadersMap).map(
|
|
6
|
+
const requestCode = Object.keys(loadersMap).map(loaderId => {
|
|
5
7
|
const routeId = loadersMap[loaderId];
|
|
6
8
|
return `
|
|
7
9
|
const ${loaderId} = createRequest('${routeId}');
|
|
8
10
|
`;
|
|
9
|
-
}).join(
|
|
11
|
+
}).join('');
|
|
10
12
|
let exportsCode = `export {`;
|
|
11
13
|
for (const loader of Object.keys(loadersMap)) {
|
|
12
14
|
exportsCode += `${loader},`;
|
|
13
15
|
}
|
|
14
|
-
exportsCode +=
|
|
15
|
-
const requestCreatorPath = path.join(__dirname,
|
|
16
|
+
exportsCode += '}';
|
|
17
|
+
const requestCreatorPath = path.join(__dirname, './create-request').replace('node', 'treeshaking').replace(/\\/g, '/');
|
|
16
18
|
const importCode = `
|
|
17
19
|
import { createRequest } from '${requestCreatorPath}';
|
|
18
20
|
`;
|
|
@@ -21,7 +23,4 @@ const generateClient = ({ mapFile }) => {
|
|
|
21
23
|
${requestCode}
|
|
22
24
|
${exportsCode}
|
|
23
25
|
`;
|
|
24
|
-
};
|
|
25
|
-
export {
|
|
26
|
-
generateClient
|
|
27
|
-
};
|
|
26
|
+
};
|
|
@@ -1,39 +1,17 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
try {
|
|
5
|
-
step(generator.next(value));
|
|
6
|
-
} catch (e) {
|
|
7
|
-
reject(e);
|
|
8
|
-
}
|
|
9
|
-
};
|
|
10
|
-
var rejected = (value) => {
|
|
11
|
-
try {
|
|
12
|
-
step(generator.throw(value));
|
|
13
|
-
} catch (e) {
|
|
14
|
-
reject(e);
|
|
15
|
-
}
|
|
16
|
-
};
|
|
17
|
-
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
18
|
-
step((generator = generator.apply(__this, __arguments)).next());
|
|
19
|
-
});
|
|
20
|
-
};
|
|
1
|
+
// eslint-disable-next-line eslint-comments/disable-enable-pair
|
|
2
|
+
/* eslint-disable @babel/no-invalid-this */
|
|
3
|
+
|
|
21
4
|
import { generateClient } from "./generate-client";
|
|
22
|
-
function loader(source) {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
mapFile: options.mapFile
|
|
33
|
-
});
|
|
34
|
-
return code;
|
|
5
|
+
export default async function loader(source) {
|
|
6
|
+
var _this$_compiler;
|
|
7
|
+
this.cacheable();
|
|
8
|
+
const target = (_this$_compiler = this._compiler) === null || _this$_compiler === void 0 ? void 0 : _this$_compiler.options.target;
|
|
9
|
+
if (target === 'node') {
|
|
10
|
+
return source;
|
|
11
|
+
}
|
|
12
|
+
const options = this.getOptions();
|
|
13
|
+
const code = generateClient({
|
|
14
|
+
mapFile: options.mapFile
|
|
35
15
|
});
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
loader as default
|
|
39
|
-
};
|
|
16
|
+
return code;
|
|
17
|
+
}
|
|
@@ -1,85 +1,47 @@
|
|
|
1
|
-
var
|
|
2
|
-
var
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
var __spreadValues = (a, b) => {
|
|
9
|
-
for (var prop in b || (b = {}))
|
|
10
|
-
if (__hasOwnProp.call(b, prop))
|
|
11
|
-
__defNormalProp(a, prop, b[prop]);
|
|
12
|
-
if (__getOwnPropSymbols)
|
|
13
|
-
for (var prop of __getOwnPropSymbols(b)) {
|
|
14
|
-
if (__propIsEnum.call(b, prop))
|
|
15
|
-
__defNormalProp(a, prop, b[prop]);
|
|
16
|
-
}
|
|
17
|
-
return a;
|
|
18
|
-
};
|
|
19
|
-
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
20
|
-
var __async = (__this, __arguments, generator) => {
|
|
21
|
-
return new Promise((resolve, reject) => {
|
|
22
|
-
var fulfilled = (value) => {
|
|
23
|
-
try {
|
|
24
|
-
step(generator.next(value));
|
|
25
|
-
} catch (e) {
|
|
26
|
-
reject(e);
|
|
27
|
-
}
|
|
28
|
-
};
|
|
29
|
-
var rejected = (value) => {
|
|
30
|
-
try {
|
|
31
|
-
step(generator.throw(value));
|
|
32
|
-
} catch (e) {
|
|
33
|
-
reject(e);
|
|
34
|
-
}
|
|
35
|
-
};
|
|
36
|
-
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
37
|
-
step((generator = generator.apply(__this, __arguments)).next());
|
|
38
|
-
});
|
|
39
|
-
};
|
|
40
|
-
import {
|
|
41
|
-
installGlobals,
|
|
42
|
-
writeReadableStreamToWritable,
|
|
43
|
-
Response as NodeResponse
|
|
44
|
-
} from "@remix-run/node";
|
|
45
|
-
import {
|
|
46
|
-
matchRoutes
|
|
47
|
-
} from "react-router-dom";
|
|
48
|
-
import { LOADER_ROUTES_DIR } from "@modern-js/utils";
|
|
1
|
+
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; }
|
|
2
|
+
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; }
|
|
3
|
+
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; }
|
|
4
|
+
import { installGlobals, writeReadableStreamToWritable, Response as NodeResponse } from '@remix-run/node';
|
|
5
|
+
import { matchRoutes } from 'react-router-dom';
|
|
6
|
+
import { LOADER_ROUTES_DIR } from '@modern-js/utils';
|
|
7
|
+
// Polyfill Web Fetch API
|
|
49
8
|
installGlobals();
|
|
50
|
-
const LOADER_SEARCH_PARAM =
|
|
51
|
-
const redirectStatusCodes =
|
|
52
|
-
function isRedirectResponse(response) {
|
|
9
|
+
const LOADER_SEARCH_PARAM = '_loader';
|
|
10
|
+
const redirectStatusCodes = new Set([301, 302, 303, 307, 308]);
|
|
11
|
+
export function isRedirectResponse(response) {
|
|
53
12
|
return redirectStatusCodes.has(response.status);
|
|
54
13
|
}
|
|
55
|
-
function isResponse(value) {
|
|
56
|
-
return value != null && typeof value.status ===
|
|
14
|
+
export function isResponse(value) {
|
|
15
|
+
return value != null && typeof value.status === 'number' && typeof value.statusText === 'string' && typeof value.headers === 'object' && typeof value.body !== 'undefined';
|
|
57
16
|
}
|
|
58
17
|
const json = (data, init = {}) => {
|
|
59
|
-
const responseInit = typeof init ===
|
|
18
|
+
const responseInit = typeof init === 'number' ? {
|
|
19
|
+
status: init
|
|
20
|
+
} : init;
|
|
60
21
|
const headers = new Headers(responseInit.headers);
|
|
61
|
-
if (!headers.has(
|
|
62
|
-
headers.set(
|
|
22
|
+
if (!headers.has('Content-Type')) {
|
|
23
|
+
headers.set('Content-Type', 'application/json; charset=utf-8');
|
|
63
24
|
}
|
|
64
|
-
return new NodeResponse(JSON.stringify(data),
|
|
25
|
+
return new NodeResponse(JSON.stringify(data), _objectSpread(_objectSpread({}, responseInit), {}, {
|
|
65
26
|
headers
|
|
66
27
|
}));
|
|
67
28
|
};
|
|
68
|
-
|
|
29
|
+
|
|
30
|
+
// TODO: 添加 context
|
|
31
|
+
const callRouteLoader = async ({
|
|
69
32
|
routeId,
|
|
70
33
|
loader,
|
|
71
34
|
params,
|
|
72
35
|
request,
|
|
36
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
73
37
|
loadContext
|
|
74
|
-
}) {
|
|
38
|
+
}) => {
|
|
75
39
|
if (!loader) {
|
|
76
|
-
throw new Error(
|
|
77
|
-
`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.`
|
|
78
|
-
);
|
|
40
|
+
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.`);
|
|
79
41
|
}
|
|
80
42
|
let result;
|
|
81
43
|
try {
|
|
82
|
-
result =
|
|
44
|
+
result = await loader({
|
|
83
45
|
request,
|
|
84
46
|
params
|
|
85
47
|
});
|
|
@@ -89,14 +51,12 @@ const callRouteLoader = (_0) => __async(void 0, [_0], function* ({
|
|
|
89
51
|
}
|
|
90
52
|
result = error;
|
|
91
53
|
}
|
|
92
|
-
if (result ===
|
|
93
|
-
throw new Error(
|
|
94
|
-
`You defined a loader for route "${routeId}" but didn't return anything from your \`loader\` function. Please return a value or \`null\`.`
|
|
95
|
-
);
|
|
54
|
+
if (result === undefined) {
|
|
55
|
+
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\`.`);
|
|
96
56
|
}
|
|
97
57
|
return isResponse(result) ? result : json(result);
|
|
98
|
-
}
|
|
99
|
-
const createLoaderHeaders =
|
|
58
|
+
};
|
|
59
|
+
const createLoaderHeaders = requestHeaders => {
|
|
100
60
|
const headers = new Headers();
|
|
101
61
|
for (const [key, values] of Object.entries(requestHeaders)) {
|
|
102
62
|
if (values) {
|
|
@@ -111,8 +71,9 @@ const createLoaderHeaders = (requestHeaders) => {
|
|
|
111
71
|
}
|
|
112
72
|
return headers;
|
|
113
73
|
};
|
|
114
|
-
const createLoaderRequest =
|
|
74
|
+
const createLoaderRequest = context => {
|
|
115
75
|
const origin = `${context.protocol}://${context.host}`;
|
|
76
|
+
// eslint-disable-next-line node/no-unsupported-features/node-builtins, node/prefer-global/url
|
|
116
77
|
const url = new URL(context.url, origin);
|
|
117
78
|
const controller = new AbortController();
|
|
118
79
|
const init = {
|
|
@@ -122,46 +83,53 @@ const createLoaderRequest = (context) => {
|
|
|
122
83
|
};
|
|
123
84
|
return new Request(url.href, init);
|
|
124
85
|
};
|
|
125
|
-
const sendLoaderResponse = (res, nodeResponse) =>
|
|
86
|
+
const sendLoaderResponse = async (res, nodeResponse) => {
|
|
126
87
|
res.statusMessage = nodeResponse.statusText;
|
|
127
88
|
res.statusCode = nodeResponse.status;
|
|
128
89
|
for (const [key, value] of nodeResponse.headers.entries()) {
|
|
129
90
|
res.setHeader(key, value);
|
|
130
91
|
}
|
|
131
92
|
if (nodeResponse.body) {
|
|
132
|
-
|
|
93
|
+
await writeReadableStreamToWritable(nodeResponse.body, res);
|
|
133
94
|
} else {
|
|
134
95
|
res.end();
|
|
135
96
|
}
|
|
136
|
-
}
|
|
137
|
-
const getPathWithoutEntry = (pathname, entryPath) => {
|
|
138
|
-
if (entryPath ===
|
|
97
|
+
};
|
|
98
|
+
export const getPathWithoutEntry = (pathname, entryPath) => {
|
|
99
|
+
if (entryPath === '/') {
|
|
139
100
|
return pathname;
|
|
140
101
|
}
|
|
141
|
-
return pathname.replace(entryPath,
|
|
102
|
+
return pathname.replace(entryPath, '');
|
|
142
103
|
};
|
|
143
104
|
const matchEntry = (pathname, entries) => {
|
|
144
|
-
return entries.find(
|
|
105
|
+
return entries.find(entry => pathname.startsWith(entry.urlPath));
|
|
145
106
|
};
|
|
146
|
-
const handleRequest =
|
|
107
|
+
export const handleRequest = async ({
|
|
147
108
|
context,
|
|
148
109
|
serverRoutes,
|
|
149
110
|
distDir
|
|
150
|
-
}) {
|
|
151
|
-
const {
|
|
111
|
+
}) => {
|
|
112
|
+
const {
|
|
113
|
+
method,
|
|
114
|
+
query
|
|
115
|
+
} = context;
|
|
152
116
|
const routeId = query[LOADER_SEARCH_PARAM];
|
|
153
|
-
if (!routeId || method.toLowerCase() !==
|
|
117
|
+
if (!routeId || method.toLowerCase() !== 'get') {
|
|
154
118
|
return;
|
|
155
119
|
}
|
|
156
120
|
const entry = matchEntry(context.path, serverRoutes);
|
|
157
121
|
if (!entry) {
|
|
158
122
|
return;
|
|
159
123
|
}
|
|
160
|
-
const {
|
|
124
|
+
const {
|
|
125
|
+
routes
|
|
126
|
+
} = await import(`${distDir}/${LOADER_ROUTES_DIR}/${entry.entryName}`);
|
|
161
127
|
if (!routes) {
|
|
162
128
|
return;
|
|
163
129
|
}
|
|
164
|
-
const {
|
|
130
|
+
const {
|
|
131
|
+
res
|
|
132
|
+
} = context;
|
|
165
133
|
const pathname = getPathWithoutEntry(context.path, entry.urlPath);
|
|
166
134
|
const matches = matchRoutes(routes, pathname);
|
|
167
135
|
if (!matches) {
|
|
@@ -169,7 +137,7 @@ const handleRequest = (_0) => __async(void 0, [_0], function* ({
|
|
|
169
137
|
res.end(`Route ${pathname} was not matched`);
|
|
170
138
|
return;
|
|
171
139
|
}
|
|
172
|
-
const match = matches
|
|
140
|
+
const match = matches === null || matches === void 0 ? void 0 : matches.find(match => match.route.id === routeId);
|
|
173
141
|
if (!match) {
|
|
174
142
|
res.statusCode = 403;
|
|
175
143
|
res.end(`Route ${routeId} does not match URL ${context.path}`);
|
|
@@ -178,7 +146,7 @@ const handleRequest = (_0) => __async(void 0, [_0], function* ({
|
|
|
178
146
|
const request = createLoaderRequest(context);
|
|
179
147
|
let response;
|
|
180
148
|
try {
|
|
181
|
-
response =
|
|
149
|
+
response = await callRouteLoader({
|
|
182
150
|
loader: match.route.loader,
|
|
183
151
|
routeId: match.route.id,
|
|
184
152
|
params: match.params,
|
|
@@ -187,8 +155,8 @@ const handleRequest = (_0) => __async(void 0, [_0], function* ({
|
|
|
187
155
|
});
|
|
188
156
|
if (isRedirectResponse(response)) {
|
|
189
157
|
const headers = new Headers(response.headers);
|
|
190
|
-
headers.set(
|
|
191
|
-
headers.delete(
|
|
158
|
+
headers.set('X-Modernjs-Redirect', headers.get('Location'));
|
|
159
|
+
headers.delete('Location');
|
|
192
160
|
response = new NodeResponse(null, {
|
|
193
161
|
status: 204,
|
|
194
162
|
headers
|
|
@@ -199,33 +167,26 @@ const handleRequest = (_0) => __async(void 0, [_0], function* ({
|
|
|
199
167
|
response = new NodeResponse(message, {
|
|
200
168
|
status: 500,
|
|
201
169
|
headers: {
|
|
202
|
-
|
|
170
|
+
'Content-Type': 'text/plain'
|
|
203
171
|
}
|
|
204
172
|
});
|
|
205
173
|
}
|
|
206
174
|
sendLoaderResponse(res, response);
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
name:
|
|
175
|
+
};
|
|
176
|
+
export default (() => ({
|
|
177
|
+
name: '@modern-js/plugin-data-loader',
|
|
210
178
|
setup: () => ({
|
|
211
179
|
preparebeforeRouteHandler({
|
|
212
180
|
serverRoutes,
|
|
213
181
|
distDir
|
|
214
182
|
}) {
|
|
215
|
-
return
|
|
183
|
+
return async context => {
|
|
216
184
|
return handleRequest({
|
|
217
185
|
serverRoutes,
|
|
218
186
|
distDir,
|
|
219
187
|
context
|
|
220
188
|
});
|
|
221
|
-
}
|
|
189
|
+
};
|
|
222
190
|
}
|
|
223
191
|
})
|
|
224
|
-
});
|
|
225
|
-
export {
|
|
226
|
-
server_default as default,
|
|
227
|
-
getPathWithoutEntry,
|
|
228
|
-
handleRequest,
|
|
229
|
-
isRedirectResponse,
|
|
230
|
-
isResponse
|
|
231
|
-
};
|
|
192
|
+
}));
|
|
@@ -1,78 +1,53 @@
|
|
|
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
|
-
createRequest: () => createRequest,
|
|
21
|
-
getRequestUrl: () => getRequestUrl
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
22
5
|
});
|
|
23
|
-
|
|
24
|
-
var
|
|
25
|
-
var
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
} catch (e) {
|
|
32
|
-
reject(e);
|
|
33
|
-
}
|
|
34
|
-
};
|
|
35
|
-
var rejected = (value) => {
|
|
36
|
-
try {
|
|
37
|
-
step(generator.throw(value));
|
|
38
|
-
} catch (e) {
|
|
39
|
-
reject(e);
|
|
40
|
-
}
|
|
41
|
-
};
|
|
42
|
-
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
43
|
-
step((generator = generator.apply(__this, __arguments)).next());
|
|
44
|
-
});
|
|
45
|
-
};
|
|
6
|
+
exports.getRequestUrl = exports.createRequest = void 0;
|
|
7
|
+
var _pathToRegexp = require("path-to-regexp");
|
|
8
|
+
var _reactRouterDom = require("react-router-dom");
|
|
9
|
+
// eslint-disable-next-line eslint-comments/disable-enable-pair
|
|
10
|
+
/* eslint-disable node/prefer-global/url */
|
|
11
|
+
// eslint-disable-next-line eslint-comments/disable-enable-pair
|
|
12
|
+
/* eslint-disable node/no-unsupported-features/node-builtins */
|
|
13
|
+
|
|
46
14
|
const getRequestUrl = ({
|
|
47
15
|
params,
|
|
48
16
|
request,
|
|
49
17
|
routeId
|
|
50
18
|
}) => {
|
|
51
19
|
const url = new URL(request.url);
|
|
52
|
-
const toPath = (0,
|
|
20
|
+
const toPath = (0, _pathToRegexp.compile)(url.pathname, {
|
|
53
21
|
encode: encodeURIComponent
|
|
54
22
|
});
|
|
55
23
|
const newPathName = toPath(params);
|
|
56
24
|
url.pathname = newPathName;
|
|
57
|
-
url.searchParams.append(
|
|
25
|
+
url.searchParams.append('_loader', routeId);
|
|
58
26
|
return url;
|
|
59
27
|
};
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
const
|
|
28
|
+
exports.getRequestUrl = getRequestUrl;
|
|
29
|
+
const handleRedirectResponse = res => {
|
|
30
|
+
const {
|
|
31
|
+
headers
|
|
32
|
+
} = res;
|
|
33
|
+
const location = headers.get('X-Modernjs-Redirect');
|
|
63
34
|
if (location) {
|
|
64
|
-
return (0,
|
|
35
|
+
return (0, _reactRouterDom.redirect)(location);
|
|
65
36
|
}
|
|
66
37
|
return res;
|
|
67
38
|
};
|
|
68
|
-
const createRequest = (routeId, method =
|
|
69
|
-
return
|
|
39
|
+
const createRequest = (routeId, method = 'get') => {
|
|
40
|
+
return async ({
|
|
70
41
|
params,
|
|
71
42
|
request
|
|
72
|
-
}) {
|
|
73
|
-
const url = getRequestUrl({
|
|
43
|
+
}) => {
|
|
44
|
+
const url = getRequestUrl({
|
|
45
|
+
params,
|
|
46
|
+
request,
|
|
47
|
+
routeId
|
|
48
|
+
});
|
|
74
49
|
try {
|
|
75
|
-
const res =
|
|
50
|
+
const res = await fetch(url, {
|
|
76
51
|
method,
|
|
77
52
|
signal: request.signal
|
|
78
53
|
});
|
|
@@ -81,5 +56,6 @@ const createRequest = (routeId, method = "get") => {
|
|
|
81
56
|
console.error(error);
|
|
82
57
|
throw error;
|
|
83
58
|
}
|
|
84
|
-
}
|
|
59
|
+
};
|
|
85
60
|
};
|
|
61
|
+
exports.createRequest = createRequest;
|