@modern-js/plugin-data-loader 2.35.0 → 2.35.2-alpha.0
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/dist/cjs/cli/createRequest.js +73 -25
- package/dist/cjs/cli/data.js +27 -10
- package/dist/cjs/cli/generateClient.js +49 -30
- package/dist/cjs/cli/loader.js +34 -23
- package/dist/cjs/common/constants.js +27 -18
- package/dist/cjs/runtime/index.js +68 -55
- package/dist/cjs/runtime/response.js +28 -11
- package/dist/cjs/server/index.js +39 -18
- package/dist/esm/cli/createRequest.js +125 -7
- package/dist/esm/cli/data.js +5 -2
- package/dist/esm/cli/generateClient.js +13 -36
- package/dist/esm/cli/loader.js +12 -11
- package/dist/esm/common/constants.js +8 -3
- package/dist/esm/runtime/index.js +29 -19
- package/dist/esm/runtime/response.js +5 -2
- package/dist/esm/server/index.js +8 -5
- package/dist/esm-node/cli/createRequest.js +44 -3
- package/dist/esm-node/cli/data.js +5 -2
- package/dist/esm-node/cli/generateClient.js +16 -20
- package/dist/esm-node/cli/loader.js +12 -11
- package/dist/esm-node/common/constants.js +8 -3
- package/dist/esm-node/runtime/index.js +25 -16
- package/dist/esm-node/runtime/response.js +5 -2
- package/dist/esm-node/server/index.js +5 -2
- package/dist/js/modern/cli/create-request.js +66 -0
- package/dist/js/modern/cli/generate-client.js +42 -0
- package/dist/js/modern/cli/loader.js +40 -0
- package/dist/js/modern/common/constants.js +6 -0
- package/dist/js/modern/server/index.js +237 -0
- package/dist/js/node/cli/create-request.js +90 -0
- package/dist/js/node/cli/generate-client.js +71 -0
- package/dist/js/node/cli/loader.js +61 -0
- package/dist/js/node/common/constants.js +30 -0
- package/dist/js/node/server/index.js +261 -0
- package/dist/js/treeshaking/cli/create-request.js +186 -0
- package/dist/js/treeshaking/cli/generate-client.js +45 -0
- package/dist/js/treeshaking/cli/loader.js +154 -0
- package/dist/js/treeshaking/common/constants.js +3 -0
- package/dist/js/treeshaking/server/index.js +578 -0
- package/dist/types/cli/createRequest.d.ts +9 -2
- package/dist/types/cli/data.d.ts +1 -1
- package/dist/types/cli/generateClient.d.ts +6 -4
- package/dist/types/cli/loader.d.ts +4 -1
- package/dist/types/runtime/index.d.ts +1 -1
- package/dist/types/runtime/response.d.ts +1 -1
- package/package.json +8 -7
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { compile } from "path-to-regexp";
|
|
2
|
-
import { redirect } from "@modern-js/utils/
|
|
2
|
+
import { redirect } from "@modern-js/runtime-utils/router";
|
|
3
3
|
import { LOADER_ID_PARAM, DIRECT_PARAM, CONTENT_TYPE_DEFERRED } from "../common/constants";
|
|
4
4
|
import { parseDeferredReadableStream } from "./data";
|
|
5
|
-
|
|
5
|
+
const getRequestUrl = ({ params, request, routeId }) => {
|
|
6
6
|
const url = new URL(request.url);
|
|
7
7
|
const toPath = compile(url.pathname, {
|
|
8
8
|
encode: encodeURIComponent
|
|
@@ -28,7 +28,7 @@ const handleDeferredResponse = async (res) => {
|
|
|
28
28
|
}
|
|
29
29
|
return res;
|
|
30
30
|
};
|
|
31
|
-
|
|
31
|
+
const createRequest = (routeId, method = "get") => {
|
|
32
32
|
return async ({ params, request }) => {
|
|
33
33
|
const url = getRequestUrl({
|
|
34
34
|
params,
|
|
@@ -48,3 +48,44 @@ export const createRequest = (routeId, method = "get") => {
|
|
|
48
48
|
return res;
|
|
49
49
|
};
|
|
50
50
|
};
|
|
51
|
+
const createActionRequest = (routeId) => {
|
|
52
|
+
return async ({ params, request }) => {
|
|
53
|
+
const url = getRequestUrl({
|
|
54
|
+
params,
|
|
55
|
+
request,
|
|
56
|
+
routeId
|
|
57
|
+
});
|
|
58
|
+
const init = {
|
|
59
|
+
signal: request.signal
|
|
60
|
+
};
|
|
61
|
+
if (request.method !== "GET") {
|
|
62
|
+
init.method = request.method;
|
|
63
|
+
const contentType = request.headers.get("Content-Type");
|
|
64
|
+
if (contentType && /\bapplication\/json\b/.test(contentType)) {
|
|
65
|
+
init.headers = {
|
|
66
|
+
"Content-Type": contentType
|
|
67
|
+
};
|
|
68
|
+
init.body = JSON.stringify(await request.json());
|
|
69
|
+
} else if (contentType && /\btext\/plain\b/.test(contentType)) {
|
|
70
|
+
init.headers = {
|
|
71
|
+
"Content-Type": contentType
|
|
72
|
+
};
|
|
73
|
+
init.body = await request.text();
|
|
74
|
+
} else if (contentType && /\bapplication\/x-www-form-urlencoded\b/.test(contentType)) {
|
|
75
|
+
init.body = new URLSearchParams(await request.text());
|
|
76
|
+
} else {
|
|
77
|
+
init.body = await request.formData();
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
const res = await fetch(url, init);
|
|
81
|
+
if (!res.ok) {
|
|
82
|
+
throw res;
|
|
83
|
+
}
|
|
84
|
+
return res;
|
|
85
|
+
};
|
|
86
|
+
};
|
|
87
|
+
export {
|
|
88
|
+
createActionRequest,
|
|
89
|
+
createRequest,
|
|
90
|
+
getRequestUrl
|
|
91
|
+
};
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { UNSAFE_DeferredData as DeferredData, AbortedDeferredError } from "@modern-js/utils/
|
|
1
|
+
import { UNSAFE_DeferredData as DeferredData, AbortedDeferredError } from "@modern-js/runtime-utils/remix-router";
|
|
2
2
|
const DEFERRED_VALUE_PLACEHOLDER_PREFIX = "__deferred_promise:";
|
|
3
|
-
|
|
3
|
+
async function parseDeferredReadableStream(stream) {
|
|
4
4
|
if (!stream) {
|
|
5
5
|
throw new Error("parseDeferredReadableStream requires stream argument");
|
|
6
6
|
}
|
|
@@ -135,3 +135,6 @@ function mergeArrays(...arrays) {
|
|
|
135
135
|
}
|
|
136
136
|
return out;
|
|
137
137
|
}
|
|
138
|
+
export {
|
|
139
|
+
parseDeferredReadableStream
|
|
140
|
+
};
|
|
@@ -1,36 +1,32 @@
|
|
|
1
1
|
import path from "path";
|
|
2
|
-
|
|
3
|
-
delete require.cache[mapFile];
|
|
4
|
-
const loadersMap = require(mapFile);
|
|
2
|
+
const generateClient = ({ inline, action, routeId }) => {
|
|
5
3
|
let requestCode = ``;
|
|
6
|
-
let exportsCode = ``;
|
|
7
4
|
const requestCreatorPath = path.join(__dirname, "./createRequest").replace("/cjs/cli/", "/esm/cli/").replace(/\\/g, "/");
|
|
8
5
|
const importCode = `
|
|
9
|
-
import { createRequest } from '${requestCreatorPath}';
|
|
6
|
+
import { createRequest, createActionRequest } from '${requestCreatorPath}';
|
|
10
7
|
`;
|
|
11
|
-
if (
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
const
|
|
8
|
+
if (inline) {
|
|
9
|
+
if (action) {
|
|
10
|
+
requestCode = `
|
|
11
|
+
export const loader = createRequest('${routeId}');
|
|
12
|
+
export const action = createActionRequest('${routeId}')
|
|
13
|
+
`;
|
|
14
|
+
} else {
|
|
15
|
+
requestCode = `
|
|
16
|
+
export const loader = createRequest('${routeId}');
|
|
16
17
|
`;
|
|
17
|
-
}).join("");
|
|
18
|
-
exportsCode = `export {`;
|
|
19
|
-
for (const loader of Object.keys(loadersMap)) {
|
|
20
|
-
exportsCode += `${loader},`;
|
|
21
18
|
}
|
|
22
|
-
exportsCode += "}";
|
|
23
19
|
} else {
|
|
24
|
-
const loader = loadersMap[loaderId];
|
|
25
20
|
requestCode = `
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
exportsCode = `export default loader;`;
|
|
21
|
+
export default createRequest('${routeId}');
|
|
22
|
+
`;
|
|
29
23
|
}
|
|
30
24
|
const generatedCode = `
|
|
31
25
|
${importCode}
|
|
32
26
|
${requestCode}
|
|
33
|
-
${exportsCode}
|
|
34
27
|
`;
|
|
35
28
|
return generatedCode;
|
|
36
29
|
};
|
|
30
|
+
export {
|
|
31
|
+
generateClient
|
|
32
|
+
};
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { promisify } from "util";
|
|
2
2
|
import { logger } from "@modern-js/utils/logger";
|
|
3
3
|
import { generateClient } from "./generateClient";
|
|
4
|
-
|
|
4
|
+
async function loader(source) {
|
|
5
5
|
var _this__compiler;
|
|
6
6
|
this.cacheable();
|
|
7
7
|
const target = (_this__compiler = this._compiler) === null || _this__compiler === void 0 ? void 0 : _this__compiler.options.target;
|
|
@@ -12,17 +12,14 @@ export default async function loader(source) {
|
|
|
12
12
|
return source;
|
|
13
13
|
}
|
|
14
14
|
const { resourceQuery } = this;
|
|
15
|
-
const options = resourceQuery.slice(1).split("&").
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
if (!key || !value) {
|
|
20
|
-
return pre;
|
|
15
|
+
const options = resourceQuery.slice(1).split("&").reduce((pre, cur) => {
|
|
16
|
+
const [key, value] = cur.split("=");
|
|
17
|
+
if (key && value) {
|
|
18
|
+
pre[key] = value === "true" ? true : value === "false" ? false : value;
|
|
21
19
|
}
|
|
22
|
-
pre[key] = value;
|
|
23
20
|
return pre;
|
|
24
21
|
}, {});
|
|
25
|
-
if (!options.
|
|
22
|
+
if (!options.loaderId) {
|
|
26
23
|
return source;
|
|
27
24
|
}
|
|
28
25
|
if (options.clientData) {
|
|
@@ -38,8 +35,12 @@ export default async function loader(source) {
|
|
|
38
35
|
}
|
|
39
36
|
}
|
|
40
37
|
const code = generateClient({
|
|
41
|
-
|
|
42
|
-
|
|
38
|
+
inline: options.inline,
|
|
39
|
+
action: options.action,
|
|
40
|
+
routeId: options.routeId
|
|
43
41
|
});
|
|
44
42
|
return code;
|
|
45
43
|
}
|
|
44
|
+
export {
|
|
45
|
+
loader as default
|
|
46
|
+
};
|
|
@@ -1,3 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
const LOADER_ID_PARAM = "__loader";
|
|
2
|
+
const DIRECT_PARAM = "__ssrDirect";
|
|
3
|
+
const CONTENT_TYPE_DEFERRED = "text/modernjs-deferred";
|
|
4
|
+
export {
|
|
5
|
+
CONTENT_TYPE_DEFERRED,
|
|
6
|
+
DIRECT_PARAM,
|
|
7
|
+
LOADER_ID_PARAM
|
|
8
|
+
};
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { installGlobals, writeReadableStreamToWritable, Response as NodeResponse } from "@remix-run/node";
|
|
2
|
-
import { createStaticHandler, ErrorResponse, UNSAFE_DEFERRED_SYMBOL as DEFERRED_SYMBOL } from "@modern-js/utils/
|
|
3
|
-
import { transformNestedRoutes } from "@modern-js/utils/
|
|
2
|
+
import { createStaticHandler, ErrorResponse, UNSAFE_DEFERRED_SYMBOL as DEFERRED_SYMBOL } from "@modern-js/runtime-utils/remix-router";
|
|
3
|
+
import { transformNestedRoutes } from "@modern-js/runtime-utils/browser";
|
|
4
4
|
import { isPlainObject } from "@modern-js/utils/lodash";
|
|
5
|
-
import { matchEntry, createRequestContext, reporterCtx } from "@modern-js/utils/
|
|
6
|
-
import { time } from "@modern-js/utils/
|
|
5
|
+
import { matchEntry, createRequestContext, reporterCtx } from "@modern-js/runtime-utils/node";
|
|
6
|
+
import { time } from "@modern-js/runtime-utils/time";
|
|
7
7
|
import { LOADER_REPORTER_NAME } from "@modern-js/utils/universal/constants";
|
|
8
8
|
import { CONTENT_TYPE_DEFERRED, LOADER_ID_PARAM } from "../common/constants";
|
|
9
9
|
import { createDeferredReadableStream } from "./response";
|
|
@@ -15,10 +15,10 @@ const redirectStatusCodes = /* @__PURE__ */ new Set([
|
|
|
15
15
|
307,
|
|
16
16
|
308
|
|
17
17
|
]);
|
|
18
|
-
|
|
18
|
+
function isRedirectResponse(status) {
|
|
19
19
|
return redirectStatusCodes.has(status);
|
|
20
20
|
}
|
|
21
|
-
|
|
21
|
+
function isResponse(value) {
|
|
22
22
|
return value != null && typeof value.status === "number" && typeof value.statusText === "string" && typeof value.headers === "object" && typeof value.body !== "undefined";
|
|
23
23
|
}
|
|
24
24
|
function convertModernRedirectResponse(headers, basename) {
|
|
@@ -49,7 +49,7 @@ const createLoaderHeaders = (requestHeaders) => {
|
|
|
49
49
|
}
|
|
50
50
|
return headers;
|
|
51
51
|
};
|
|
52
|
-
const
|
|
52
|
+
const createRequest = (context) => {
|
|
53
53
|
const origin = `${context.protocol}://${context.host}`;
|
|
54
54
|
const url = new URL(context.url, origin);
|
|
55
55
|
const controller = new AbortController();
|
|
@@ -58,7 +58,11 @@ const createLoaderRequest = (context) => {
|
|
|
58
58
|
headers: createLoaderHeaders(context.headers),
|
|
59
59
|
signal: controller.signal
|
|
60
60
|
};
|
|
61
|
-
|
|
61
|
+
if (context.method.toUpperCase() !== "GET" && context.method !== "HEAD") {
|
|
62
|
+
init.body = context.req;
|
|
63
|
+
}
|
|
64
|
+
const request = new Request(url.href, init);
|
|
65
|
+
return request;
|
|
62
66
|
};
|
|
63
67
|
const sendLoaderResponse = async (res, nodeResponse) => {
|
|
64
68
|
res.statusMessage = nodeResponse.statusText;
|
|
@@ -72,16 +76,13 @@ const sendLoaderResponse = async (res, nodeResponse) => {
|
|
|
72
76
|
res.end();
|
|
73
77
|
}
|
|
74
78
|
};
|
|
75
|
-
|
|
76
|
-
const {
|
|
79
|
+
const handleRequest = async ({ context, serverRoutes, routes: routesConfig }) => {
|
|
80
|
+
const { query } = context;
|
|
77
81
|
const routeId = query[LOADER_ID_PARAM];
|
|
78
82
|
const entry = matchEntry(context.path, serverRoutes);
|
|
79
83
|
if (!routeId || !entry) {
|
|
80
84
|
return;
|
|
81
85
|
}
|
|
82
|
-
if (method.toLowerCase() !== "get") {
|
|
83
|
-
throw new Error("CSR data loader request only support http GET method");
|
|
84
|
-
}
|
|
85
86
|
const basename = entry.urlPath;
|
|
86
87
|
const end = time();
|
|
87
88
|
const { res, logger, reporter } = context;
|
|
@@ -89,7 +90,7 @@ export const handleRequest = async ({ context, serverRoutes, routes: routesConfi
|
|
|
89
90
|
const { queryRoute } = createStaticHandler(routes, {
|
|
90
91
|
basename
|
|
91
92
|
});
|
|
92
|
-
const request =
|
|
93
|
+
const request = createRequest(context);
|
|
93
94
|
const requestContext = createRequestContext();
|
|
94
95
|
requestContext.set(reporterCtx, reporter);
|
|
95
96
|
let response;
|
|
@@ -123,9 +124,12 @@ export const handleRequest = async ({ context, serverRoutes, routes: routesConfi
|
|
|
123
124
|
});
|
|
124
125
|
}
|
|
125
126
|
} catch (error) {
|
|
126
|
-
var _logger;
|
|
127
127
|
const message = error instanceof ErrorResponse ? error.data : String(error);
|
|
128
|
-
(
|
|
128
|
+
if (error instanceof Error) {
|
|
129
|
+
logger === null || logger === void 0 ? void 0 : logger.error(error);
|
|
130
|
+
} else {
|
|
131
|
+
logger === null || logger === void 0 ? void 0 : logger.error(message);
|
|
132
|
+
}
|
|
129
133
|
response = new NodeResponse(message, {
|
|
130
134
|
status: 500,
|
|
131
135
|
headers: {
|
|
@@ -137,3 +141,8 @@ export const handleRequest = async ({ context, serverRoutes, routes: routesConfi
|
|
|
137
141
|
reporter.reportTiming(`${LOADER_REPORTER_NAME}-navigation`, cost);
|
|
138
142
|
await sendLoaderResponse(res, response);
|
|
139
143
|
};
|
|
144
|
+
export {
|
|
145
|
+
handleRequest,
|
|
146
|
+
isRedirectResponse,
|
|
147
|
+
isResponse
|
|
148
|
+
};
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { TextEncoder } from "util";
|
|
2
|
-
import { serializeJson } from "@modern-js/utils/
|
|
2
|
+
import { serializeJson } from "@modern-js/runtime-utils/node";
|
|
3
3
|
function isTrackedPromise(value) {
|
|
4
4
|
return value != null && typeof value.then === "function" && value._tracked === true;
|
|
5
5
|
}
|
|
6
6
|
const DEFERRED_VALUE_PLACEHOLDER_PREFIX = "__deferred_promise:";
|
|
7
|
-
|
|
7
|
+
function createDeferredReadableStream(deferredData, signal) {
|
|
8
8
|
const encoder = new TextEncoder();
|
|
9
9
|
const stream = new ReadableStream({
|
|
10
10
|
async start(controller) {
|
|
@@ -58,3 +58,6 @@ function enqueueTrackedPromise(controller, encoder, settledKey, promise) {
|
|
|
58
58
|
`));
|
|
59
59
|
}
|
|
60
60
|
}
|
|
61
|
+
export {
|
|
62
|
+
createDeferredReadableStream
|
|
63
|
+
};
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import path from "path";
|
|
2
2
|
import fs from "fs";
|
|
3
3
|
import { MAIN_ENTRY_NAME, SERVER_BUNDLE_DIRECTORY } from "@modern-js/utils";
|
|
4
|
-
import { matchEntry } from "@modern-js/utils/
|
|
5
|
-
|
|
4
|
+
import { matchEntry } from "@modern-js/runtime-utils/node";
|
|
5
|
+
var server_default = () => ({
|
|
6
6
|
name: "@modern-js/plugin-data-loader",
|
|
7
7
|
setup: () => ({
|
|
8
8
|
prepareLoaderHandler({ serverRoutes, distDir }) {
|
|
@@ -28,3 +28,6 @@ export default () => ({
|
|
|
28
28
|
}
|
|
29
29
|
})
|
|
30
30
|
});
|
|
31
|
+
export {
|
|
32
|
+
server_default as default
|
|
33
|
+
};
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
var __async = (__this, __arguments, generator) => {
|
|
2
|
+
return new Promise((resolve, reject) => {
|
|
3
|
+
var fulfilled = (value) => {
|
|
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
|
+
};
|
|
21
|
+
import { compile } from "path-to-regexp";
|
|
22
|
+
import { redirect } from "react-router-dom";
|
|
23
|
+
import { LOADER_ID_PARAM, DIRECT_PARAM } from "../common/constants";
|
|
24
|
+
const getRequestUrl = ({
|
|
25
|
+
params,
|
|
26
|
+
request,
|
|
27
|
+
routeId
|
|
28
|
+
}) => {
|
|
29
|
+
const url = new URL(request.url);
|
|
30
|
+
const toPath = compile(url.pathname, {
|
|
31
|
+
encode: encodeURIComponent
|
|
32
|
+
});
|
|
33
|
+
const newPathName = toPath(params);
|
|
34
|
+
url.pathname = newPathName;
|
|
35
|
+
url.searchParams.append(LOADER_ID_PARAM, routeId);
|
|
36
|
+
url.searchParams.append(DIRECT_PARAM, "true");
|
|
37
|
+
return url;
|
|
38
|
+
};
|
|
39
|
+
const handleRedirectResponse = (res) => {
|
|
40
|
+
const { headers } = res;
|
|
41
|
+
const location = headers.get("X-Modernjs-Redirect");
|
|
42
|
+
if (location) {
|
|
43
|
+
return redirect(location);
|
|
44
|
+
}
|
|
45
|
+
return res;
|
|
46
|
+
};
|
|
47
|
+
const createRequest = (routeId, method = "get") => {
|
|
48
|
+
return (_0) => __async(void 0, [_0], function* ({
|
|
49
|
+
params,
|
|
50
|
+
request
|
|
51
|
+
}) {
|
|
52
|
+
const url = getRequestUrl({ params, request, routeId });
|
|
53
|
+
const res = yield fetch(url, {
|
|
54
|
+
method,
|
|
55
|
+
signal: request.signal
|
|
56
|
+
});
|
|
57
|
+
if (!res.ok) {
|
|
58
|
+
throw res;
|
|
59
|
+
}
|
|
60
|
+
return handleRedirectResponse(res);
|
|
61
|
+
});
|
|
62
|
+
};
|
|
63
|
+
export {
|
|
64
|
+
createRequest,
|
|
65
|
+
getRequestUrl
|
|
66
|
+
};
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import path from "path";
|
|
2
|
+
const generateClient = ({
|
|
3
|
+
mapFile,
|
|
4
|
+
loaderId
|
|
5
|
+
}) => {
|
|
6
|
+
delete require.cache[mapFile];
|
|
7
|
+
const loadersMap = require(mapFile);
|
|
8
|
+
let requestCode = ``;
|
|
9
|
+
let exportsCode = ``;
|
|
10
|
+
const requestCreatorPath = path.join(__dirname, "./create-request").replace("/node/cli/", "/treeshaking/cli/").replace(/\\/g, "/");
|
|
11
|
+
const importCode = `
|
|
12
|
+
import { createRequest } from '${requestCreatorPath}';
|
|
13
|
+
`;
|
|
14
|
+
if (!loaderId) {
|
|
15
|
+
requestCode = Object.keys(loadersMap).map((loaderId2) => {
|
|
16
|
+
const { routeId } = loadersMap[loaderId2];
|
|
17
|
+
return `
|
|
18
|
+
const ${loaderId2} = createRequest('${routeId}');
|
|
19
|
+
`;
|
|
20
|
+
}).join("");
|
|
21
|
+
exportsCode = `export {`;
|
|
22
|
+
for (const loader of Object.keys(loadersMap)) {
|
|
23
|
+
exportsCode += `${loader},`;
|
|
24
|
+
}
|
|
25
|
+
exportsCode += "}";
|
|
26
|
+
} else {
|
|
27
|
+
const loader = loadersMap[loaderId];
|
|
28
|
+
requestCode = `
|
|
29
|
+
const loader = createRequest('${loader.routeId}');
|
|
30
|
+
`;
|
|
31
|
+
exportsCode = `export default loader;`;
|
|
32
|
+
}
|
|
33
|
+
const generatedCode = `
|
|
34
|
+
${importCode}
|
|
35
|
+
${requestCode}
|
|
36
|
+
${exportsCode}
|
|
37
|
+
`;
|
|
38
|
+
return generatedCode;
|
|
39
|
+
};
|
|
40
|
+
export {
|
|
41
|
+
generateClient
|
|
42
|
+
};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
var __async = (__this, __arguments, generator) => {
|
|
2
|
+
return new Promise((resolve, reject) => {
|
|
3
|
+
var fulfilled = (value) => {
|
|
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
|
+
};
|
|
21
|
+
import { generateClient } from "./generate-client";
|
|
22
|
+
function loader(source) {
|
|
23
|
+
return __async(this, null, function* () {
|
|
24
|
+
var _a;
|
|
25
|
+
this.cacheable();
|
|
26
|
+
const target = (_a = this._compiler) == null ? void 0 : _a.options.target;
|
|
27
|
+
if (target === "node") {
|
|
28
|
+
return source;
|
|
29
|
+
}
|
|
30
|
+
const options = this.getOptions();
|
|
31
|
+
const code = generateClient({
|
|
32
|
+
mapFile: options.mapFile,
|
|
33
|
+
loaderId: options.loaderId
|
|
34
|
+
});
|
|
35
|
+
return code;
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
export {
|
|
39
|
+
loader as default
|
|
40
|
+
};
|