@modern-js/plugin-bff 3.8.1 → 3.8.2
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.js +4 -2
- package/dist/cjs/utils/clientGenerator.js +40 -40
- package/dist/esm/cli.mjs +4 -2
- package/dist/esm/utils/clientGenerator.mjs +38 -38
- package/dist/esm-node/cli.mjs +4 -2
- package/dist/esm-node/utils/clientGenerator.mjs +38 -38
- package/dist/types/utils/clientGenerator.d.ts +16 -2
- package/package.json +13 -13
package/dist/cjs/cli.js
CHANGED
|
@@ -83,7 +83,7 @@ const bffPlugin = ()=>({
|
|
|
83
83
|
}
|
|
84
84
|
};
|
|
85
85
|
const generator = async ()=>{
|
|
86
|
-
const { appDirectory, apiDirectory, lambdaDirectory, port } = api.getAppContext();
|
|
86
|
+
const { appDirectory, apiDirectory, lambdaDirectory, port, moduleType } = api.getAppContext();
|
|
87
87
|
const modernConfig = api.getNormalizedConfig();
|
|
88
88
|
const relativeDistPath = modernConfig?.output?.distPath?.root || 'dist';
|
|
89
89
|
const { bff } = modernConfig || {};
|
|
@@ -119,7 +119,9 @@ const bffPlugin = ()=>({
|
|
|
119
119
|
requestCreator: bff?.requestCreator,
|
|
120
120
|
httpMethodDecider,
|
|
121
121
|
relativeDistPath,
|
|
122
|
-
relativeApiPath
|
|
122
|
+
relativeApiPath,
|
|
123
|
+
moduleType,
|
|
124
|
+
apiFiles: apiRouter.getApiFiles()
|
|
123
125
|
});
|
|
124
126
|
await runtimeGenerator_js_default()({
|
|
125
127
|
runtime,
|
|
@@ -37,7 +37,7 @@ var __webpack_require__ = {};
|
|
|
37
37
|
var __webpack_exports__ = {};
|
|
38
38
|
__webpack_require__.r(__webpack_exports__);
|
|
39
39
|
__webpack_require__.d(__webpack_exports__, {
|
|
40
|
-
|
|
40
|
+
buildClientTypeFacade: ()=>buildClientTypeFacade,
|
|
41
41
|
default: ()=>utils_clientGenerator,
|
|
42
42
|
readDirectoryFiles: ()=>readDirectoryFiles
|
|
43
43
|
});
|
|
@@ -53,40 +53,41 @@ const EXPORT_PREFIX = `./${API_DIR}/`;
|
|
|
53
53
|
const TYPE_PREFIX = `${API_DIR}/`;
|
|
54
54
|
const toPosixPath = (p)=>p.replace(/\\/g, '/');
|
|
55
55
|
const posixJoin = (...args)=>toPosixPath(external_path_default().join(...args));
|
|
56
|
-
|
|
56
|
+
const DEFAULT_EXPORT_RE = /(^|[\s;])export\s+default\b/;
|
|
57
|
+
function buildClientTypeFacade(clientTypesFile, originTypesFile, hasDefaultExport, esm = false) {
|
|
58
|
+
const originNoExt = originTypesFile.replace(/\.d\.ts$/, '');
|
|
59
|
+
let specifier = toPosixPath(external_path_default().relative(external_path_default().dirname(clientTypesFile), originNoExt));
|
|
60
|
+
if (!specifier.startsWith('.')) specifier = `./${specifier}`;
|
|
61
|
+
if (esm) specifier = `${specifier}.js`;
|
|
62
|
+
const lines = [];
|
|
63
|
+
if (hasDefaultExport) lines.push(`export { default } from '${specifier}';`);
|
|
64
|
+
lines.push(`export * from '${specifier}';`);
|
|
65
|
+
return `${lines.join('\n')}\n`;
|
|
66
|
+
}
|
|
67
|
+
async function readDirectoryFiles(appDirectory, directory, relativeDistPath, apiFiles) {
|
|
57
68
|
const filesList = [];
|
|
58
|
-
|
|
59
|
-
const
|
|
60
|
-
|
|
69
|
+
for (const resourcePath of apiFiles){
|
|
70
|
+
const source = await utils_namespaceObject.fs.readFile(resourcePath, 'utf8');
|
|
71
|
+
const currentPath = external_path_default().dirname(resourcePath);
|
|
72
|
+
const relativePath = external_path_default().relative(directory, resourcePath);
|
|
73
|
+
const parsedPath = external_path_default().parse(relativePath);
|
|
74
|
+
const targetDir = posixJoin(`./${relativeDistPath}/${CLIENT_DIR}`, parsedPath.dir, `${parsedPath.name}.js`);
|
|
75
|
+
const name = parsedPath.name;
|
|
76
|
+
const absTargetDir = external_path_default().resolve(targetDir);
|
|
77
|
+
const relativePathFromAppDirectory = external_path_default().relative(appDirectory, currentPath);
|
|
78
|
+
const typesFilePath = posixJoin(`./${relativeDistPath}`, relativePathFromAppDirectory, `${name}.d.ts`);
|
|
79
|
+
const relativeTargetDistDir = `./${typesFilePath}`;
|
|
80
|
+
const exportKey = toPosixPath(external_path_default().join(parsedPath.dir, name));
|
|
81
|
+
filesList.push({
|
|
82
|
+
resourcePath,
|
|
83
|
+
source,
|
|
84
|
+
targetDir,
|
|
85
|
+
name,
|
|
86
|
+
absTargetDir,
|
|
87
|
+
relativeTargetDistDir,
|
|
88
|
+
exportKey
|
|
61
89
|
});
|
|
62
|
-
for (const entry of entries){
|
|
63
|
-
if ('_app.ts' === entry.name) continue;
|
|
64
|
-
const resourcePath = external_path_default().join(currentPath, entry.name);
|
|
65
|
-
if (entry.isDirectory()) await readFiles(resourcePath);
|
|
66
|
-
else {
|
|
67
|
-
const source = await utils_namespaceObject.fs.readFile(resourcePath, 'utf8');
|
|
68
|
-
const relativePath = external_path_default().relative(directory, resourcePath);
|
|
69
|
-
const parsedPath = external_path_default().parse(relativePath);
|
|
70
|
-
const targetDir = posixJoin(`./${relativeDistPath}/${CLIENT_DIR}`, parsedPath.dir, `${parsedPath.name}.js`);
|
|
71
|
-
const name = parsedPath.name;
|
|
72
|
-
const absTargetDir = external_path_default().resolve(targetDir);
|
|
73
|
-
const relativePathFromAppDirectory = external_path_default().relative(appDirectory, currentPath);
|
|
74
|
-
const typesFilePath = posixJoin(`./${relativeDistPath}`, relativePathFromAppDirectory, `${name}.d.ts`);
|
|
75
|
-
const relativeTargetDistDir = `./${typesFilePath}`;
|
|
76
|
-
const exportKey = toPosixPath(external_path_default().join(parsedPath.dir, name));
|
|
77
|
-
filesList.push({
|
|
78
|
-
resourcePath,
|
|
79
|
-
source,
|
|
80
|
-
targetDir,
|
|
81
|
-
name,
|
|
82
|
-
absTargetDir,
|
|
83
|
-
relativeTargetDistDir,
|
|
84
|
-
exportKey
|
|
85
|
-
});
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
90
|
}
|
|
89
|
-
await readFiles(directory);
|
|
90
91
|
return filesList;
|
|
91
92
|
}
|
|
92
93
|
function mergePackageJson(packageJson, files, typesVersion, exports1) {
|
|
@@ -121,7 +122,8 @@ async function setPackage(files, appDirectory, relativeDistPath) {
|
|
|
121
122
|
const addFiles = [
|
|
122
123
|
posixJoin(relativeDistPath, CLIENT_DIR, '**', '*'),
|
|
123
124
|
posixJoin(relativeDistPath, RUNTIME_DIR, '**', '*'),
|
|
124
|
-
posixJoin(relativeDistPath, PLUGIN_DIR, '**', '*')
|
|
125
|
+
posixJoin(relativeDistPath, PLUGIN_DIR, '**', '*'),
|
|
126
|
+
posixJoin(relativeDistPath, '**', '*.d.ts')
|
|
125
127
|
];
|
|
126
128
|
const typesVersions = {
|
|
127
129
|
'*': files.reduce((acc, file)=>{
|
|
@@ -182,11 +184,8 @@ async function setPackage(files, appDirectory, relativeDistPath) {
|
|
|
182
184
|
utils_namespaceObject.logger.error(`package.json update failed: ${error}`);
|
|
183
185
|
}
|
|
184
186
|
}
|
|
185
|
-
async function copyFiles(from, to) {
|
|
186
|
-
if (await utils_namespaceObject.fs.pathExists(from)) await utils_namespaceObject.fs.copy(toPosixPath(from), toPosixPath(to));
|
|
187
|
-
}
|
|
188
187
|
async function clientGenerator(draftOptions) {
|
|
189
|
-
const sourceList = await readDirectoryFiles(draftOptions.appDir, draftOptions.lambdaDir, draftOptions.relativeDistPath);
|
|
188
|
+
const sourceList = await readDirectoryFiles(draftOptions.appDir, draftOptions.lambdaDir, draftOptions.relativeDistPath, draftOptions.apiFiles);
|
|
190
189
|
const getClitentCode = async (resourcePath, source)=>{
|
|
191
190
|
const warning = `The file ${resourcePath} is not allowed to be imported in src directory, only API definition files are allowed.`;
|
|
192
191
|
if (!draftOptions.existLambda) return void utils_namespaceObject.logger.warn(warning);
|
|
@@ -212,7 +211,8 @@ async function clientGenerator(draftOptions) {
|
|
|
212
211
|
const code = await getClitentCode(source.resourcePath, source.source);
|
|
213
212
|
if (code?.value) {
|
|
214
213
|
await writeTargetFile(source.absTargetDir, code.value);
|
|
215
|
-
|
|
214
|
+
const clientTypesFile = source.targetDir.replace(/\.js$/, '.d.ts');
|
|
215
|
+
await writeTargetFile(external_path_default().resolve(clientTypesFile), buildClientTypeFacade(clientTypesFile, source.relativeTargetDistDir, DEFAULT_EXPORT_RE.test(code.value), 'module' === draftOptions.moduleType));
|
|
216
216
|
}
|
|
217
217
|
}
|
|
218
218
|
utils_namespaceObject.logger.info("Client bundle generate succeed");
|
|
@@ -222,11 +222,11 @@ async function clientGenerator(draftOptions) {
|
|
|
222
222
|
await setPackage(sourceList, draftOptions.appDir, draftOptions.relativeDistPath);
|
|
223
223
|
}
|
|
224
224
|
const utils_clientGenerator = clientGenerator;
|
|
225
|
-
exports.
|
|
225
|
+
exports.buildClientTypeFacade = __webpack_exports__.buildClientTypeFacade;
|
|
226
226
|
exports["default"] = __webpack_exports__["default"];
|
|
227
227
|
exports.readDirectoryFiles = __webpack_exports__.readDirectoryFiles;
|
|
228
228
|
for(var __rspack_i in __webpack_exports__)if (-1 === [
|
|
229
|
-
"
|
|
229
|
+
"buildClientTypeFacade",
|
|
230
230
|
"default",
|
|
231
231
|
"readDirectoryFiles"
|
|
232
232
|
].indexOf(__rspack_i)) exports[__rspack_i] = __webpack_exports__[__rspack_i];
|
package/dist/esm/cli.mjs
CHANGED
|
@@ -37,7 +37,7 @@ const bffPlugin = ()=>({
|
|
|
37
37
|
}
|
|
38
38
|
};
|
|
39
39
|
const generator = async ()=>{
|
|
40
|
-
const { appDirectory, apiDirectory, lambdaDirectory, port } = api.getAppContext();
|
|
40
|
+
const { appDirectory, apiDirectory, lambdaDirectory, port, moduleType } = api.getAppContext();
|
|
41
41
|
const modernConfig = api.getNormalizedConfig();
|
|
42
42
|
const relativeDistPath = modernConfig?.output?.distPath?.root || 'dist';
|
|
43
43
|
const { bff } = modernConfig || {};
|
|
@@ -73,7 +73,9 @@ const bffPlugin = ()=>({
|
|
|
73
73
|
requestCreator: bff?.requestCreator,
|
|
74
74
|
httpMethodDecider,
|
|
75
75
|
relativeDistPath,
|
|
76
|
-
relativeApiPath
|
|
76
|
+
relativeApiPath,
|
|
77
|
+
moduleType,
|
|
78
|
+
apiFiles: apiRouter.getApiFiles()
|
|
77
79
|
});
|
|
78
80
|
await runtimeGenerator({
|
|
79
81
|
runtime,
|
|
@@ -9,40 +9,41 @@ const EXPORT_PREFIX = `./${API_DIR}/`;
|
|
|
9
9
|
const TYPE_PREFIX = `${API_DIR}/`;
|
|
10
10
|
const toPosixPath = (p)=>p.replace(/\\/g, '/');
|
|
11
11
|
const posixJoin = (...args)=>toPosixPath(path.join(...args));
|
|
12
|
-
|
|
12
|
+
const DEFAULT_EXPORT_RE = /(^|[\s;])export\s+default\b/;
|
|
13
|
+
function buildClientTypeFacade(clientTypesFile, originTypesFile, hasDefaultExport, esm = false) {
|
|
14
|
+
const originNoExt = originTypesFile.replace(/\.d\.ts$/, '');
|
|
15
|
+
let specifier = toPosixPath(path.relative(path.dirname(clientTypesFile), originNoExt));
|
|
16
|
+
if (!specifier.startsWith('.')) specifier = `./${specifier}`;
|
|
17
|
+
if (esm) specifier = `${specifier}.js`;
|
|
18
|
+
const lines = [];
|
|
19
|
+
if (hasDefaultExport) lines.push(`export { default } from '${specifier}';`);
|
|
20
|
+
lines.push(`export * from '${specifier}';`);
|
|
21
|
+
return `${lines.join('\n')}\n`;
|
|
22
|
+
}
|
|
23
|
+
async function readDirectoryFiles(appDirectory, directory, relativeDistPath, apiFiles) {
|
|
13
24
|
const filesList = [];
|
|
14
|
-
|
|
15
|
-
const
|
|
16
|
-
|
|
25
|
+
for (const resourcePath of apiFiles){
|
|
26
|
+
const source = await fs.readFile(resourcePath, 'utf8');
|
|
27
|
+
const currentPath = path.dirname(resourcePath);
|
|
28
|
+
const relativePath = path.relative(directory, resourcePath);
|
|
29
|
+
const parsedPath = path.parse(relativePath);
|
|
30
|
+
const targetDir = posixJoin(`./${relativeDistPath}/${CLIENT_DIR}`, parsedPath.dir, `${parsedPath.name}.js`);
|
|
31
|
+
const name = parsedPath.name;
|
|
32
|
+
const absTargetDir = path.resolve(targetDir);
|
|
33
|
+
const relativePathFromAppDirectory = path.relative(appDirectory, currentPath);
|
|
34
|
+
const typesFilePath = posixJoin(`./${relativeDistPath}`, relativePathFromAppDirectory, `${name}.d.ts`);
|
|
35
|
+
const relativeTargetDistDir = `./${typesFilePath}`;
|
|
36
|
+
const exportKey = toPosixPath(path.join(parsedPath.dir, name));
|
|
37
|
+
filesList.push({
|
|
38
|
+
resourcePath,
|
|
39
|
+
source,
|
|
40
|
+
targetDir,
|
|
41
|
+
name,
|
|
42
|
+
absTargetDir,
|
|
43
|
+
relativeTargetDistDir,
|
|
44
|
+
exportKey
|
|
17
45
|
});
|
|
18
|
-
for (const entry of entries){
|
|
19
|
-
if ('_app.ts' === entry.name) continue;
|
|
20
|
-
const resourcePath = path.join(currentPath, entry.name);
|
|
21
|
-
if (entry.isDirectory()) await readFiles(resourcePath);
|
|
22
|
-
else {
|
|
23
|
-
const source = await fs.readFile(resourcePath, 'utf8');
|
|
24
|
-
const relativePath = path.relative(directory, resourcePath);
|
|
25
|
-
const parsedPath = path.parse(relativePath);
|
|
26
|
-
const targetDir = posixJoin(`./${relativeDistPath}/${CLIENT_DIR}`, parsedPath.dir, `${parsedPath.name}.js`);
|
|
27
|
-
const name = parsedPath.name;
|
|
28
|
-
const absTargetDir = path.resolve(targetDir);
|
|
29
|
-
const relativePathFromAppDirectory = path.relative(appDirectory, currentPath);
|
|
30
|
-
const typesFilePath = posixJoin(`./${relativeDistPath}`, relativePathFromAppDirectory, `${name}.d.ts`);
|
|
31
|
-
const relativeTargetDistDir = `./${typesFilePath}`;
|
|
32
|
-
const exportKey = toPosixPath(path.join(parsedPath.dir, name));
|
|
33
|
-
filesList.push({
|
|
34
|
-
resourcePath,
|
|
35
|
-
source,
|
|
36
|
-
targetDir,
|
|
37
|
-
name,
|
|
38
|
-
absTargetDir,
|
|
39
|
-
relativeTargetDistDir,
|
|
40
|
-
exportKey
|
|
41
|
-
});
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
46
|
}
|
|
45
|
-
await readFiles(directory);
|
|
46
47
|
return filesList;
|
|
47
48
|
}
|
|
48
49
|
function mergePackageJson(packageJson, files, typesVersion, exports) {
|
|
@@ -77,7 +78,8 @@ async function setPackage(files, appDirectory, relativeDistPath) {
|
|
|
77
78
|
const addFiles = [
|
|
78
79
|
posixJoin(relativeDistPath, CLIENT_DIR, '**', '*'),
|
|
79
80
|
posixJoin(relativeDistPath, RUNTIME_DIR, '**', '*'),
|
|
80
|
-
posixJoin(relativeDistPath, PLUGIN_DIR, '**', '*')
|
|
81
|
+
posixJoin(relativeDistPath, PLUGIN_DIR, '**', '*'),
|
|
82
|
+
posixJoin(relativeDistPath, '**', '*.d.ts')
|
|
81
83
|
];
|
|
82
84
|
const typesVersions = {
|
|
83
85
|
'*': files.reduce((acc, file)=>{
|
|
@@ -138,11 +140,8 @@ async function setPackage(files, appDirectory, relativeDistPath) {
|
|
|
138
140
|
logger.error(`package.json update failed: ${error}`);
|
|
139
141
|
}
|
|
140
142
|
}
|
|
141
|
-
async function copyFiles(from, to) {
|
|
142
|
-
if (await fs.pathExists(from)) await fs.copy(toPosixPath(from), toPosixPath(to));
|
|
143
|
-
}
|
|
144
143
|
async function clientGenerator(draftOptions) {
|
|
145
|
-
const sourceList = await readDirectoryFiles(draftOptions.appDir, draftOptions.lambdaDir, draftOptions.relativeDistPath);
|
|
144
|
+
const sourceList = await readDirectoryFiles(draftOptions.appDir, draftOptions.lambdaDir, draftOptions.relativeDistPath, draftOptions.apiFiles);
|
|
146
145
|
const getClitentCode = async (resourcePath, source)=>{
|
|
147
146
|
const warning = `The file ${resourcePath} is not allowed to be imported in src directory, only API definition files are allowed.`;
|
|
148
147
|
if (!draftOptions.existLambda) return void logger.warn(warning);
|
|
@@ -168,7 +167,8 @@ async function clientGenerator(draftOptions) {
|
|
|
168
167
|
const code = await getClitentCode(source.resourcePath, source.source);
|
|
169
168
|
if (code?.value) {
|
|
170
169
|
await writeTargetFile(source.absTargetDir, code.value);
|
|
171
|
-
|
|
170
|
+
const clientTypesFile = source.targetDir.replace(/\.js$/, '.d.ts');
|
|
171
|
+
await writeTargetFile(path.resolve(clientTypesFile), buildClientTypeFacade(clientTypesFile, source.relativeTargetDistDir, DEFAULT_EXPORT_RE.test(code.value), 'module' === draftOptions.moduleType));
|
|
172
172
|
}
|
|
173
173
|
}
|
|
174
174
|
logger.info("Client bundle generate succeed");
|
|
@@ -179,4 +179,4 @@ async function clientGenerator(draftOptions) {
|
|
|
179
179
|
}
|
|
180
180
|
const utils_clientGenerator = clientGenerator;
|
|
181
181
|
export default utils_clientGenerator;
|
|
182
|
-
export {
|
|
182
|
+
export { buildClientTypeFacade, readDirectoryFiles };
|
package/dist/esm-node/cli.mjs
CHANGED
|
@@ -41,7 +41,7 @@ const bffPlugin = ()=>({
|
|
|
41
41
|
}
|
|
42
42
|
};
|
|
43
43
|
const generator = async ()=>{
|
|
44
|
-
const { appDirectory, apiDirectory, lambdaDirectory, port } = api.getAppContext();
|
|
44
|
+
const { appDirectory, apiDirectory, lambdaDirectory, port, moduleType } = api.getAppContext();
|
|
45
45
|
const modernConfig = api.getNormalizedConfig();
|
|
46
46
|
const relativeDistPath = modernConfig?.output?.distPath?.root || 'dist';
|
|
47
47
|
const { bff } = modernConfig || {};
|
|
@@ -77,7 +77,9 @@ const bffPlugin = ()=>({
|
|
|
77
77
|
requestCreator: bff?.requestCreator,
|
|
78
78
|
httpMethodDecider,
|
|
79
79
|
relativeDistPath,
|
|
80
|
-
relativeApiPath
|
|
80
|
+
relativeApiPath,
|
|
81
|
+
moduleType,
|
|
82
|
+
apiFiles: apiRouter.getApiFiles()
|
|
81
83
|
});
|
|
82
84
|
await runtimeGenerator({
|
|
83
85
|
runtime,
|
|
@@ -10,40 +10,41 @@ const EXPORT_PREFIX = `./${API_DIR}/`;
|
|
|
10
10
|
const TYPE_PREFIX = `${API_DIR}/`;
|
|
11
11
|
const toPosixPath = (p)=>p.replace(/\\/g, '/');
|
|
12
12
|
const posixJoin = (...args)=>toPosixPath(path.join(...args));
|
|
13
|
-
|
|
13
|
+
const DEFAULT_EXPORT_RE = /(^|[\s;])export\s+default\b/;
|
|
14
|
+
function buildClientTypeFacade(clientTypesFile, originTypesFile, hasDefaultExport, esm = false) {
|
|
15
|
+
const originNoExt = originTypesFile.replace(/\.d\.ts$/, '');
|
|
16
|
+
let specifier = toPosixPath(path.relative(path.dirname(clientTypesFile), originNoExt));
|
|
17
|
+
if (!specifier.startsWith('.')) specifier = `./${specifier}`;
|
|
18
|
+
if (esm) specifier = `${specifier}.js`;
|
|
19
|
+
const lines = [];
|
|
20
|
+
if (hasDefaultExport) lines.push(`export { default } from '${specifier}';`);
|
|
21
|
+
lines.push(`export * from '${specifier}';`);
|
|
22
|
+
return `${lines.join('\n')}\n`;
|
|
23
|
+
}
|
|
24
|
+
async function readDirectoryFiles(appDirectory, directory, relativeDistPath, apiFiles) {
|
|
14
25
|
const filesList = [];
|
|
15
|
-
|
|
16
|
-
const
|
|
17
|
-
|
|
26
|
+
for (const resourcePath of apiFiles){
|
|
27
|
+
const source = await fs.readFile(resourcePath, 'utf8');
|
|
28
|
+
const currentPath = path.dirname(resourcePath);
|
|
29
|
+
const relativePath = path.relative(directory, resourcePath);
|
|
30
|
+
const parsedPath = path.parse(relativePath);
|
|
31
|
+
const targetDir = posixJoin(`./${relativeDistPath}/${CLIENT_DIR}`, parsedPath.dir, `${parsedPath.name}.js`);
|
|
32
|
+
const name = parsedPath.name;
|
|
33
|
+
const absTargetDir = path.resolve(targetDir);
|
|
34
|
+
const relativePathFromAppDirectory = path.relative(appDirectory, currentPath);
|
|
35
|
+
const typesFilePath = posixJoin(`./${relativeDistPath}`, relativePathFromAppDirectory, `${name}.d.ts`);
|
|
36
|
+
const relativeTargetDistDir = `./${typesFilePath}`;
|
|
37
|
+
const exportKey = toPosixPath(path.join(parsedPath.dir, name));
|
|
38
|
+
filesList.push({
|
|
39
|
+
resourcePath,
|
|
40
|
+
source,
|
|
41
|
+
targetDir,
|
|
42
|
+
name,
|
|
43
|
+
absTargetDir,
|
|
44
|
+
relativeTargetDistDir,
|
|
45
|
+
exportKey
|
|
18
46
|
});
|
|
19
|
-
for (const entry of entries){
|
|
20
|
-
if ('_app.ts' === entry.name) continue;
|
|
21
|
-
const resourcePath = path.join(currentPath, entry.name);
|
|
22
|
-
if (entry.isDirectory()) await readFiles(resourcePath);
|
|
23
|
-
else {
|
|
24
|
-
const source = await fs.readFile(resourcePath, 'utf8');
|
|
25
|
-
const relativePath = path.relative(directory, resourcePath);
|
|
26
|
-
const parsedPath = path.parse(relativePath);
|
|
27
|
-
const targetDir = posixJoin(`./${relativeDistPath}/${CLIENT_DIR}`, parsedPath.dir, `${parsedPath.name}.js`);
|
|
28
|
-
const name = parsedPath.name;
|
|
29
|
-
const absTargetDir = path.resolve(targetDir);
|
|
30
|
-
const relativePathFromAppDirectory = path.relative(appDirectory, currentPath);
|
|
31
|
-
const typesFilePath = posixJoin(`./${relativeDistPath}`, relativePathFromAppDirectory, `${name}.d.ts`);
|
|
32
|
-
const relativeTargetDistDir = `./${typesFilePath}`;
|
|
33
|
-
const exportKey = toPosixPath(path.join(parsedPath.dir, name));
|
|
34
|
-
filesList.push({
|
|
35
|
-
resourcePath,
|
|
36
|
-
source,
|
|
37
|
-
targetDir,
|
|
38
|
-
name,
|
|
39
|
-
absTargetDir,
|
|
40
|
-
relativeTargetDistDir,
|
|
41
|
-
exportKey
|
|
42
|
-
});
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
47
|
}
|
|
46
|
-
await readFiles(directory);
|
|
47
48
|
return filesList;
|
|
48
49
|
}
|
|
49
50
|
function mergePackageJson(packageJson, files, typesVersion, exports) {
|
|
@@ -78,7 +79,8 @@ async function setPackage(files, appDirectory, relativeDistPath) {
|
|
|
78
79
|
const addFiles = [
|
|
79
80
|
posixJoin(relativeDistPath, CLIENT_DIR, '**', '*'),
|
|
80
81
|
posixJoin(relativeDistPath, RUNTIME_DIR, '**', '*'),
|
|
81
|
-
posixJoin(relativeDistPath, PLUGIN_DIR, '**', '*')
|
|
82
|
+
posixJoin(relativeDistPath, PLUGIN_DIR, '**', '*'),
|
|
83
|
+
posixJoin(relativeDistPath, '**', '*.d.ts')
|
|
82
84
|
];
|
|
83
85
|
const typesVersions = {
|
|
84
86
|
'*': files.reduce((acc, file)=>{
|
|
@@ -139,11 +141,8 @@ async function setPackage(files, appDirectory, relativeDistPath) {
|
|
|
139
141
|
logger.error(`package.json update failed: ${error}`);
|
|
140
142
|
}
|
|
141
143
|
}
|
|
142
|
-
async function copyFiles(from, to) {
|
|
143
|
-
if (await fs.pathExists(from)) await fs.copy(toPosixPath(from), toPosixPath(to));
|
|
144
|
-
}
|
|
145
144
|
async function clientGenerator(draftOptions) {
|
|
146
|
-
const sourceList = await readDirectoryFiles(draftOptions.appDir, draftOptions.lambdaDir, draftOptions.relativeDistPath);
|
|
145
|
+
const sourceList = await readDirectoryFiles(draftOptions.appDir, draftOptions.lambdaDir, draftOptions.relativeDistPath, draftOptions.apiFiles);
|
|
147
146
|
const getClitentCode = async (resourcePath, source)=>{
|
|
148
147
|
const warning = `The file ${resourcePath} is not allowed to be imported in src directory, only API definition files are allowed.`;
|
|
149
148
|
if (!draftOptions.existLambda) return void logger.warn(warning);
|
|
@@ -169,7 +168,8 @@ async function clientGenerator(draftOptions) {
|
|
|
169
168
|
const code = await getClitentCode(source.resourcePath, source.source);
|
|
170
169
|
if (code?.value) {
|
|
171
170
|
await writeTargetFile(source.absTargetDir, code.value);
|
|
172
|
-
|
|
171
|
+
const clientTypesFile = source.targetDir.replace(/\.js$/, '.d.ts');
|
|
172
|
+
await writeTargetFile(path.resolve(clientTypesFile), buildClientTypeFacade(clientTypesFile, source.relativeTargetDistDir, DEFAULT_EXPORT_RE.test(code.value), 'module' === draftOptions.moduleType));
|
|
173
173
|
}
|
|
174
174
|
}
|
|
175
175
|
logger.info("Client bundle generate succeed");
|
|
@@ -180,4 +180,4 @@ async function clientGenerator(draftOptions) {
|
|
|
180
180
|
}
|
|
181
181
|
const utils_clientGenerator = clientGenerator;
|
|
182
182
|
export default utils_clientGenerator;
|
|
183
|
-
export {
|
|
183
|
+
export { buildClientTypeFacade, readDirectoryFiles };
|
|
@@ -10,6 +10,20 @@ export type APILoaderOptions = {
|
|
|
10
10
|
httpMethodDecider?: HttpMethodDecider;
|
|
11
11
|
relativeDistPath: string;
|
|
12
12
|
relativeApiPath: string;
|
|
13
|
+
/**
|
|
14
|
+
* `'module'` for native ESM output (`package.json#type: "module"`); the
|
|
15
|
+
* generated facade then re-exports with an explicit `.js` extension so
|
|
16
|
+
* `node16`/`nodenext` consumers resolve it.
|
|
17
|
+
*/
|
|
18
|
+
moduleType?: 'module' | 'commonjs';
|
|
19
|
+
/**
|
|
20
|
+
* Absolute paths of the valid API files, resolved by ApiRouter with the same
|
|
21
|
+
* `API_FILE_RULES` the runtime router uses. Passing them in keeps the client
|
|
22
|
+
* generator and the router in agreement about what an API module is, so stray
|
|
23
|
+
* artifacts next to the sources (compiled `.d.ts`/`.js`, tests, private files)
|
|
24
|
+
* never reach `generateClient`.
|
|
25
|
+
*/
|
|
26
|
+
apiFiles: string[];
|
|
13
27
|
};
|
|
14
28
|
interface FileDetails {
|
|
15
29
|
resourcePath: string;
|
|
@@ -20,7 +34,7 @@ interface FileDetails {
|
|
|
20
34
|
relativeTargetDistDir: string;
|
|
21
35
|
exportKey: string;
|
|
22
36
|
}
|
|
23
|
-
export declare function
|
|
24
|
-
export declare function
|
|
37
|
+
export declare function buildClientTypeFacade(clientTypesFile: string, originTypesFile: string, hasDefaultExport: boolean, esm?: boolean): string;
|
|
38
|
+
export declare function readDirectoryFiles(appDirectory: string, directory: string, relativeDistPath: string, apiFiles: string[]): Promise<FileDetails[]>;
|
|
25
39
|
declare function clientGenerator(draftOptions: APILoaderOptions): Promise<void>;
|
|
26
40
|
export default clientGenerator;
|
package/package.json
CHANGED
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
"modern",
|
|
16
16
|
"modern.js"
|
|
17
17
|
],
|
|
18
|
-
"version": "3.8.
|
|
18
|
+
"version": "3.8.2",
|
|
19
19
|
"types": "./dist/types/cli.d.ts",
|
|
20
20
|
"main": "./dist/cjs/cli.js",
|
|
21
21
|
"exports": {
|
|
@@ -79,12 +79,12 @@
|
|
|
79
79
|
"@swc/helpers": "^0.5.17",
|
|
80
80
|
"qs": "^6.15.3",
|
|
81
81
|
"type-is": "^1.6.18",
|
|
82
|
-
"@modern-js/bff-core": "3.8.
|
|
83
|
-
"@modern-js/
|
|
84
|
-
"@modern-js/
|
|
85
|
-
"@modern-js/
|
|
86
|
-
"@modern-js/server-
|
|
87
|
-
"@modern-js/utils": "3.8.
|
|
82
|
+
"@modern-js/bff-core": "3.8.2",
|
|
83
|
+
"@modern-js/builder": "3.8.2",
|
|
84
|
+
"@modern-js/create-request": "3.8.2",
|
|
85
|
+
"@modern-js/server-utils": "3.8.2",
|
|
86
|
+
"@modern-js/server-core": "3.8.2",
|
|
87
|
+
"@modern-js/utils": "3.8.2"
|
|
88
88
|
},
|
|
89
89
|
"devDependencies": {
|
|
90
90
|
"@rsbuild/core": "2.1.0",
|
|
@@ -95,13 +95,13 @@
|
|
|
95
95
|
"memfs": "^3.5.3",
|
|
96
96
|
"typescript": "^5",
|
|
97
97
|
"zod": "^3.25.76",
|
|
98
|
-
"@modern-js/app-tools": "3.8.
|
|
99
|
-
"@modern-js/bff-runtime": "3.8.
|
|
100
|
-
"@modern-js/plugin": "3.8.1",
|
|
101
|
-
"@modern-js/runtime": "3.8.1",
|
|
98
|
+
"@modern-js/app-tools": "3.8.2",
|
|
99
|
+
"@modern-js/bff-runtime": "3.8.2",
|
|
102
100
|
"@modern-js/rslib": "2.68.10",
|
|
103
|
-
"@modern-js/
|
|
104
|
-
"@
|
|
101
|
+
"@modern-js/plugin": "3.8.2",
|
|
102
|
+
"@modern-js/types": "3.8.2",
|
|
103
|
+
"@scripts/rstest-config": "2.66.0",
|
|
104
|
+
"@modern-js/runtime": "3.8.2"
|
|
105
105
|
},
|
|
106
106
|
"sideEffects": false,
|
|
107
107
|
"publishConfig": {
|