@ms-cloudpack/esm-stub-utilities 0.14.37 → 0.15.1
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/lib/generateESMStubFromCJS.d.ts +7 -4
- package/lib/generateESMStubFromCJS.d.ts.map +1 -1
- package/lib/generateESMStubFromCJS.js +23 -8
- package/lib/generateESMStubFromCJS.js.map +1 -1
- package/lib/getTypesExportInfo.d.ts +7 -0
- package/lib/getTypesExportInfo.d.ts.map +1 -0
- package/lib/getTypesExportInfo.js +75 -0
- package/lib/getTypesExportInfo.js.map +1 -0
- package/lib/verifyStub/verifyStubWorker.js +1 -1
- package/lib/verifyStub/verifyStubWorker.js.map +1 -1
- package/lib/writeESMStubs.d.ts.map +1 -1
- package/lib/writeESMStubs.js +6 -2
- package/lib/writeESMStubs.js.map +1 -1
- package/package.json +6 -5
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Given entry/stub paths, generates the esm stub content.
|
|
3
3
|
* The stubPath is required to generate the proper import statement.
|
|
4
|
-
*
|
|
5
|
-
*
|
|
4
|
+
*
|
|
5
|
+
* Export information is determined in this priority order:
|
|
6
|
+
* 1. Use `namedExports` if provided
|
|
7
|
+
* 2. Extract export info directly from the CJS module
|
|
8
|
+
* 3. Extract export info from the .d.ts file if it exists
|
|
6
9
|
*/
|
|
7
10
|
export declare function generateESMStubFromCJS(options: {
|
|
8
11
|
filePath: string;
|
|
9
12
|
stubPath: string;
|
|
10
|
-
namedExports?: string[];
|
|
11
|
-
}): string
|
|
13
|
+
namedExports?: string[] | 'types';
|
|
14
|
+
}): Promise<string>;
|
|
12
15
|
//# sourceMappingURL=generateESMStubFromCJS.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"generateESMStubFromCJS.d.ts","sourceRoot":"","sources":["../src/generateESMStubFromCJS.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"generateESMStubFromCJS.d.ts","sourceRoot":"","sources":["../src/generateESMStubFromCJS.ts"],"names":[],"mappings":"AASA;;;;;;;;GAQG;AACH,wBAAsB,sBAAsB,CAAC,OAAO,EAAE;IACpD,QAAQ,EAAE,MAAM,CAAC;IACjB,QAAQ,EAAE,MAAM,CAAC;IACjB,YAAY,CAAC,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC;CACnC,GAAG,OAAO,CAAC,MAAM,CAAC,CAmClB"}
|
|
@@ -3,19 +3,34 @@ import { createRequire } from 'module';
|
|
|
3
3
|
import path from 'path';
|
|
4
4
|
import { getExportInfo } from './getExportInfo.js';
|
|
5
5
|
import { generateESMStubFromExports } from './generateESMStubFromExports.js';
|
|
6
|
+
import { getTypesExportInfo } from './getTypesExportInfo.js';
|
|
6
7
|
const require = createRequire(import.meta.url);
|
|
7
8
|
/**
|
|
8
9
|
* Given entry/stub paths, generates the esm stub content.
|
|
9
10
|
* The stubPath is required to generate the proper import statement.
|
|
10
|
-
*
|
|
11
|
-
*
|
|
11
|
+
*
|
|
12
|
+
* Export information is determined in this priority order:
|
|
13
|
+
* 1. Use `namedExports` if provided
|
|
14
|
+
* 2. Extract export info directly from the CJS module
|
|
15
|
+
* 3. Extract export info from the .d.ts file if it exists
|
|
12
16
|
*/
|
|
13
|
-
export function generateESMStubFromCJS(options) {
|
|
17
|
+
export async function generateESMStubFromCJS(options) {
|
|
14
18
|
const { filePath, stubPath, namedExports } = options;
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
+
// Determine export information using priority order
|
|
20
|
+
let exportInfo;
|
|
21
|
+
// 1. Use types if specified
|
|
22
|
+
if (namedExports === 'types') {
|
|
23
|
+
exportInfo = await getTypesExportInfo(filePath);
|
|
24
|
+
}
|
|
25
|
+
else if (namedExports) {
|
|
26
|
+
// 2. Use provided named exports
|
|
27
|
+
exportInfo = { type: 'object', keys: namedExports };
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
// 3. Try to get export info directly from the CJS module
|
|
31
|
+
exportInfo = getExportInfo(require(filePath));
|
|
32
|
+
}
|
|
33
|
+
// Calculate the relative path for import statement
|
|
19
34
|
let relativePath;
|
|
20
35
|
// We need to compute a relative path from the stub to the entry to construct the proper import in the stub.
|
|
21
36
|
if (process.platform === 'win32' && path.parse(filePath).root !== path.parse(stubPath).root) {
|
|
@@ -26,7 +41,7 @@ export function generateESMStubFromCJS(options) {
|
|
|
26
41
|
relativePath = './' + slash(path.relative(path.dirname(stubPath), filePath));
|
|
27
42
|
}
|
|
28
43
|
const stubContent = generateESMStubFromExports({
|
|
29
|
-
importStatement:
|
|
44
|
+
importStatement: exportInfo.type === 'none' ? `import "${relativePath}";` : `import moduleExport from "${relativePath}";`,
|
|
30
45
|
export: exportInfo,
|
|
31
46
|
});
|
|
32
47
|
return stubContent;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"generateESMStubFromCJS.js","sourceRoot":"","sources":["../src/generateESMStubFromCJS.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,mCAAmC,CAAC;AAC1D,OAAO,EAAE,aAAa,EAAE,MAAM,QAAQ,CAAC;AACvC,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACnD,OAAO,EAAE,0BAA0B,EAAE,MAAM,iCAAiC,CAAC;
|
|
1
|
+
{"version":3,"file":"generateESMStubFromCJS.js","sourceRoot":"","sources":["../src/generateESMStubFromCJS.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,mCAAmC,CAAC;AAC1D,OAAO,EAAE,aAAa,EAAE,MAAM,QAAQ,CAAC;AACvC,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACnD,OAAO,EAAE,0BAA0B,EAAE,MAAM,iCAAiC,CAAC;AAC7E,OAAO,EAAE,kBAAkB,EAA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,CAAC,6BAA6B,YAAY,IAAI;QAC1G,MAAM,EAAE,UAAU;KACnB,CAAC,CAAC;IAEH,OAAO,WAAW,CAAC;AACrB,CAAC","sourcesContent":["import { slash } from '@ms-cloudpack/path-string-parsing';\nimport { createRequire } from 'module';\nimport path from 'path';\nimport { getExportInfo } from './getExportInfo.js';\nimport { generateESMStubFromExports } from './generateESMStubFromExports.js';\nimport { getTypesExportInfo } from './getTypesExportInfo.js';\n\nconst require = createRequire(import.meta.url);\n\n/**\n * Given entry/stub paths, generates the esm stub content.\n * The stubPath is required to generate the proper import statement.\n *\n * Export information is determined in this priority order:\n * 1. Use `namedExports` if provided\n * 2. Extract export info directly from the CJS module\n * 3. Extract export info from the .d.ts file if it exists\n */\nexport async function generateESMStubFromCJS(options: {\n filePath: string;\n stubPath: string;\n namedExports?: string[] | 'types';\n}): Promise<string> {\n const { filePath, stubPath, namedExports } = options;\n\n // Determine export information using priority order\n let exportInfo;\n\n // 1. Use types if specified\n if (namedExports === 'types') {\n exportInfo = await getTypesExportInfo(filePath);\n } else if (namedExports) {\n // 2. Use provided named exports\n exportInfo = { type: 'object', keys: namedExports } as const;\n } else {\n // 3. Try to get export info directly from the CJS module\n exportInfo = getExportInfo(require(filePath) as unknown);\n }\n\n // Calculate the relative path for import statement\n let relativePath: string;\n\n // We need to compute a relative path from the stub to the entry to construct the proper import in the stub.\n if (process.platform === 'win32' && path.parse(filePath).root !== path.parse(stubPath).root) {\n // Different drive letters. These can't be relative to each other.\n relativePath = filePath;\n } else {\n relativePath = './' + slash(path.relative(path.dirname(stubPath), filePath));\n }\n\n const stubContent = generateESMStubFromExports({\n importStatement:\n exportInfo.type === 'none' ? `import \"${relativePath}\";` : `import moduleExport from \"${relativePath}\";`,\n export: exportInfo,\n });\n\n return stubContent;\n}\n"]}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { StubExportInfo } from './types/StubExportInfo.js';
|
|
2
|
+
/**
|
|
3
|
+
* Extract export names from a TypeScript declaration file.
|
|
4
|
+
* Handles named exports, default exports, and re-exports from other files within the same package.
|
|
5
|
+
*/
|
|
6
|
+
export declare function getTypesExportInfo(filePath: string): Promise<StubExportInfo>;
|
|
7
|
+
//# sourceMappingURL=getTypesExportInfo.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getTypesExportInfo.d.ts","sourceRoot":"","sources":["../src/getTypesExportInfo.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,2BAA2B,CAAC;AAIhE;;;GAGG;AACH,wBAAsB,kBAAkB,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,cAAc,CAAC,CAOlF"}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
/**
|
|
4
|
+
* Extract export names from a TypeScript declaration file.
|
|
5
|
+
* Handles named exports, default exports, and re-exports from other files within the same package.
|
|
6
|
+
*/
|
|
7
|
+
export async function getTypesExportInfo(filePath) {
|
|
8
|
+
const parsedPath = path.parse(filePath);
|
|
9
|
+
const typesPath = path.resolve(parsedPath.dir, parsedPath.name + '.d.ts');
|
|
10
|
+
const exportNames = await collectExports(typesPath);
|
|
11
|
+
return { type: 'object', keys: Array.from(exportNames) };
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Recursively collect exports from a TypeScript declaration file and its dependencies.
|
|
15
|
+
*/
|
|
16
|
+
async function collectExports(typesPath, exportNames = new Set(), visitedFiles = new Set()) {
|
|
17
|
+
if (!fs.existsSync(typesPath)) {
|
|
18
|
+
throw new Error(`TypeScript declaration file not found at: ${typesPath}`);
|
|
19
|
+
}
|
|
20
|
+
// Skip if we've already processed this file
|
|
21
|
+
if (visitedFiles.has(typesPath)) {
|
|
22
|
+
return exportNames;
|
|
23
|
+
}
|
|
24
|
+
visitedFiles.add(typesPath);
|
|
25
|
+
const code = fs.readFileSync(typesPath, 'utf-8');
|
|
26
|
+
const baseDir = path.dirname(typesPath);
|
|
27
|
+
// Use oxc-parser for accurate TypeScript parsing (dynamic import)
|
|
28
|
+
const { parseSync } = await import('oxc-parser');
|
|
29
|
+
const result = parseSync(typesPath, code, {
|
|
30
|
+
sourceType: 'module',
|
|
31
|
+
lang: 'ts',
|
|
32
|
+
astType: 'ts',
|
|
33
|
+
});
|
|
34
|
+
if (result.module.staticExports.length === 0) {
|
|
35
|
+
throw new Error(`No static exports found in ${typesPath}`);
|
|
36
|
+
}
|
|
37
|
+
// Process each export statement
|
|
38
|
+
for (const staticExport of result.module.staticExports) {
|
|
39
|
+
// Skip if the export uses " type " or " interface " keywords, as they are not actual exports.
|
|
40
|
+
const exportString = code.slice(staticExport.start, staticExport.end);
|
|
41
|
+
// export type, export interface, export declare type, export declare interface
|
|
42
|
+
if (/^\s*export\s+(type|interface|declare\s+(type|interface))\b/.test(exportString)) {
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
for (const entry of staticExport.entries) {
|
|
46
|
+
// Don't skip type exports "entry.isType" as they can still be regular exports in TypeScript.
|
|
47
|
+
// This might lead to some type exports being included, but they will be undefined at runtime.
|
|
48
|
+
// Extract the export name based on its kind
|
|
49
|
+
if (entry.exportName.kind === 'Name' && entry.exportName.name) {
|
|
50
|
+
exportNames.add(entry.exportName.name);
|
|
51
|
+
}
|
|
52
|
+
else if (entry.exportName.kind === 'Default') {
|
|
53
|
+
exportNames.add('default');
|
|
54
|
+
}
|
|
55
|
+
else if (entry.exportName.kind === 'None' && entry.moduleRequest) {
|
|
56
|
+
const importPath = entry.moduleRequest.value;
|
|
57
|
+
// Handle relative imports only; skip package imports
|
|
58
|
+
if (!importPath.startsWith('.')) {
|
|
59
|
+
// We can't handle non-relative imports here, as they may refer to external packages
|
|
60
|
+
throw new Error(`Non-relative import found in ${typesPath}: ${importPath}`);
|
|
61
|
+
}
|
|
62
|
+
// Resolve the import path relative to the current file
|
|
63
|
+
let importFilePath = path.resolve(baseDir, importPath);
|
|
64
|
+
// Add .d.ts extension if needed
|
|
65
|
+
if (!importFilePath.endsWith('.d.ts')) {
|
|
66
|
+
importFilePath += '.d.ts';
|
|
67
|
+
}
|
|
68
|
+
// Recursively process the imported file
|
|
69
|
+
await collectExports(importFilePath, exportNames, visitedFiles);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return exportNames;
|
|
74
|
+
}
|
|
75
|
+
//# sourceMappingURL=getTypesExportInfo.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getTypesExportInfo.js","sourceRoot":"","sources":["../src/getTypesExportInfo.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,IAAI,CAAC;AACpB,OAAO,IAAI,MAAM,MAAM,CAAC;AAExB;;;GAGG;AACH,MAAM,CAAC,KAAK,UAAU,kBAAkB,CAAC,QAAgB;IACvD,MAAM,UAAU,GAAG,IAAI,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC;IACxC,MAAM,SAAS,GAAG,IAAI,CAAC,OAAO,CAAC,UAAU,CAAC,GAAG,EAAE,UAAU,CAAC,IAAI,GAAG,OAAO,CAAC,CAAC;IAE1E,MAAM,WAAW,GAAG,MAAM,cAAc,CAAC,SAAS,CAAC,CAAC;IAEpD,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,IAAI,EAAE,KAAK,CAAC,IAAI,CAAC,WAAW,CAAC,EAAE,CAAC;AAC3D,CAAC;AAED;;GAEG;AACH,KAAK,UAAU,cAAc,CAC3B,SAAiB,EACjB,cAAc,IAAI,GAAG,EAAU,EAC/B,eAAe,IAAI,GAAG,EAAU;IAEhC,IAAI,CAAC,EAAE,CAAC,UAAU,CAAC,SAAS,CAAC,EAAE,CAAC;QAC9B,MAAM,IAAI,KAAK,CAAC,6CAA6C,SAAS,EAAE,CAAC,CAAC;IAC5E,CAAC;IAED,4CAA4C;IAC5C,IAAI,YAAY,CAAC,GAAG,CAAC,SAAS,CAAC,EAA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M,IAAI,KAAK,CAAC,UAAU,CAAC,IAAI,KAAK,SAAS,EAAE,CAAC;gBAC/C,WAAW,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;YAC7B,CAAC;iBAAM,IAAI,KAAK,CAAC,UAAU,CAAC,IAAI,KAAK,MAAM,IAAI,KAAK,CAAC,aAAa,EAAE,CAAC;gBACnE,MAAM,UAAU,GAAG,KAAK,CAAC,aAAa,CAAC,KAAK,CAAC;gBAE7C,qDAAqD;gBACrD,IAAI,CAAC,UAAU,CAAC,UAAU,CAAC,GAAG,CAAC,EAAE,CAAC;oBAChC,oFAAoF;oBACpF,MAAM,IAAI,KAAK,CAAC,gCAAgC,SAAS,KAAK,UAAU,EAAE,CAAC,CAAC;gBAC9E,CAAC;gBAED,uDAAuD;gBACvD,IAAI,cAAc,GAAG,IAAI,CAAC,OAAO,CAAC,OAAO,EAAE,UAAU,CAAC,CAAC;gBAEvD,gCAAgC;gBAChC,IAAI,CAAC,cAAc,CAAC,QAAQ,CAAC,OAAO,CAAC,EAAE,CAAC;oBACtC,cAAc,IAAI,OAAO,CAAC;gBAC5B,CAAC;gBAED,wCAAwC;gBACxC,MAAM,cAAc,CAAC,cAAc,EAAE,WAAW,EAAE,YAAY,CAAC,CAAC;YAClE,CAAC;QACH,CAAC;IACH,CAAC;IAED,OAAO,WAAW,CAAC;AACrB,CAAC","sourcesContent":["import type { StubExportInfo } from './types/StubExportInfo.js';\nimport fs from 'fs';\nimport path from 'path';\n\n/**\n * Extract export names from a TypeScript declaration file.\n * Handles named exports, default exports, and re-exports from other files within the same package.\n */\nexport async function getTypesExportInfo(filePath: string): Promise<StubExportInfo> {\n const parsedPath = path.parse(filePath);\n const typesPath = path.resolve(parsedPath.dir, parsedPath.name + '.d.ts');\n\n const exportNames = await collectExports(typesPath);\n\n return { type: 'object', keys: Array.from(exportNames) };\n}\n\n/**\n * Recursively collect exports from a TypeScript declaration file and its dependencies.\n */\nasync function collectExports(\n typesPath: string,\n exportNames = new Set<string>(),\n visitedFiles = new Set<string>(),\n): Promise<Set<string>> {\n if (!fs.existsSync(typesPath)) {\n throw new Error(`TypeScript declaration file not found at: ${typesPath}`);\n }\n\n // Skip if we've already processed this file\n if (visitedFiles.has(typesPath)) {\n return exportNames;\n }\n\n visitedFiles.add(typesPath);\n\n const code = fs.readFileSync(typesPath, 'utf-8');\n const baseDir = path.dirname(typesPath);\n\n // Use oxc-parser for accurate TypeScript parsing (dynamic import)\n const { parseSync } = await import('oxc-parser');\n const result = parseSync(typesPath, code, {\n sourceType: 'module',\n lang: 'ts',\n astType: 'ts',\n });\n\n if (result.module.staticExports.length === 0) {\n throw new Error(`No static exports found in ${typesPath}`);\n }\n\n // Process each export statement\n for (const staticExport of result.module.staticExports) {\n // Skip if the export uses \" type \" or \" interface \" keywords, as they are not actual exports.\n const exportString = code.slice(staticExport.start, staticExport.end);\n // export type, export interface, export declare type, export declare interface\n if (/^\\s*export\\s+(type|interface|declare\\s+(type|interface))\\b/.test(exportString)) {\n continue;\n }\n for (const entry of staticExport.entries) {\n // Don't skip type exports \"entry.isType\" as they can still be regular exports in TypeScript.\n // This might lead to some type exports being included, but they will be undefined at runtime.\n\n // Extract the export name based on its kind\n if (entry.exportName.kind === 'Name' && entry.exportName.name) {\n exportNames.add(entry.exportName.name);\n } else if (entry.exportName.kind === 'Default') {\n exportNames.add('default');\n } else if (entry.exportName.kind === 'None' && entry.moduleRequest) {\n const importPath = entry.moduleRequest.value;\n\n // Handle relative imports only; skip package imports\n if (!importPath.startsWith('.')) {\n // We can't handle non-relative imports here, as they may refer to external packages\n throw new Error(`Non-relative import found in ${typesPath}: ${importPath}`);\n }\n\n // Resolve the import path relative to the current file\n let importFilePath = path.resolve(baseDir, importPath);\n\n // Add .d.ts extension if needed\n if (!importFilePath.endsWith('.d.ts')) {\n importFilePath += '.d.ts';\n }\n\n // Recursively process the imported file\n await collectExports(importFilePath, exportNames, visitedFiles);\n }\n }\n }\n\n return exportNames;\n}\n"]}
|
|
@@ -41,7 +41,7 @@ async function tryImportStub(request) {
|
|
|
41
41
|
moduleExports = await import(stubPath);
|
|
42
42
|
step = 'Running test code';
|
|
43
43
|
if (testCode) {
|
|
44
|
-
// eslint-disable-next-line
|
|
44
|
+
// eslint-disable-next-line
|
|
45
45
|
new Function('moduleExports', testCode)(moduleExports);
|
|
46
46
|
}
|
|
47
47
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"verifyStubWorker.js","sourceRoot":"","sources":["../../src/verifyStub/verifyStubWorker.js"],"names":[],"mappings":"AAAA,EAAE;AACF,EAAE;AACF,oFAAoF;AACpF,qDAAqD;AACrD,EAAE;AACF,uDAAuD;AACvD,EAAE;AACF,EAAE;AAEF,+DAA+D;AAC/D,wDAAwD;AACxD,0DAA0D;AAE1D,0EAA0E;AAC1E;;GAEG;AACH,OAAO,EAAE,MAAM,IAAI,CAAC;AACpB,OAAO,EAAE,gBAAgB,EAAE,MAAM,2BAA2B,CAAC;AAC7D,OAAO,EAAE,sBAAsB,EAAE,MAAM,qCAAqC,CAAC;AAC7E,OAAO,EAAE,cAAc,EAAE,mBAAmB,EAAE,MAAM,sBAAsB,CAAC;AAE3E,iDAAiD;AACjD,MAAM,sBAAsB,EAAE,CAAC;AAC/B,MAAM,uBAAuB,GAAG,mBAAmB,EAAE,CAAC;AAEtD,gBAAgB,CAAC;IACf,OAAO,EAAE;QACP,aAAa;KACd;IACD,SAAS,EAAE,GAAG,EAAE;QACd,cAAc,CAAC,uBAAuB,CAAC,CAAC;IAC1C,CAAC;CACF,CAAC,CAAC;AAEH;;;GAGG;AACH,KAAK,UAAU,aAAa,CAAC,OAAO;IAClC,MAAM,EAAE,QAAQ,EAAE,QAAQ,EAAE,GAAG,OAAO,CAAC;IACvC,IAAI,aAAa,CAAC;IAClB,IAAI,IAAI,GAAG,QAAQ,CAAC;IACpB,IAAI,CAAC;QACH,mEAAmE;QACnE,aAAa,GAAG,MAAM,MAAM,CAAC,QAAQ,CAAC,CAAC;QACvC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,EAAE,CAAC;IAC1B,CAAC;IAED,IAAI,CAAC;QACH,sDAAsD;QACtD,+EAA+E;QAC/E,EAAE,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC;QACpB,OAAO,KAAK,CAAC;IACf,CAAC;IAAC,MAAM,CAAC;QACP,gDAAgD;QAChD,OAAO,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC;IAC/B,CAAC;AACH,CAAC","sourcesContent":["//\n//\n// This worker is used in tests to verify that generated stub files can be imported,\n// and that the results of importing are as expected.\n//\n// See verifyStub.ts for more about why this is needed.\n//\n//\n\n/* eslint-disable @typescript-eslint/no-unsafe-member-access */\n/* eslint-disable @typescript-eslint/no-unsafe-return */\n/* eslint-disable @typescript-eslint/no-unsafe-argument */\n\n/** @import { _VerifyStubOptions } from '../types/VerifyStubOptions.js' */\n/**\n * @typedef {Pick<_VerifyStubOptions, 'stubPath' | 'testCode'>} TryImportStubRequest Request to `verifyStubWorker.js` `tryImportStub`\n */\nimport v8 from 'v8';\nimport { initializeWorker } from '@ms-cloudpack/worker-pool';\nimport { initBrowserEnvironment } from '../worker/initBrowserEnvironment.js';\nimport { cleanUpGlobals, getGlobalProperties } from '../worker/globals.js';\n\n// These modules may expect a browser environment\nawait initBrowserEnvironment();\nconst initialGlobalProperties = getGlobalProperties();\n\ninitializeWorker({\n methods: {\n tryImportStub,\n },\n afterEach: () => {\n cleanUpGlobals(initialGlobalProperties);\n },\n});\n\n/**\n * @param {TryImportStubRequest} request\n * @returns {Promise<unknown>}\n */\nasync function tryImportStub(request) {\n const { stubPath, testCode } = request;\n let moduleExports;\n let step = 'Import';\n try {\n // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment\n moduleExports = await import(stubPath);\n step = 'Running test code';\n if (testCode) {\n // eslint-disable-next-line\n new Function('moduleExports', testCode)(moduleExports);\n }\n } catch (err) {\n const error = err instanceof Error ? err : new Error(String(err));\n error.message = `${step} failed: ${error.message}`;\n throw error;\n }\n\n // This might throw on a serialization error. Let it propagate to be handled as a rejection.\n return serialize(moduleExports);\n}\n\n/**\n * Convert a value into an IPC-serializable form, so it can be passed back to the test and verified.\n * @param {*} value\n * @returns {*}\n */\nfunction serialize(value, depth = 1, encountered = new WeakSet()) {\n if (depth > 3) {\n return '...';\n }\n\n if (value && typeof value === 'object') {\n // track encountered objects to avoid entering circular references\n if (encountered.has(value)) {\n return '[Circular]';\n }\n encountered.add(value);\n\n // Convert non-serializable types\n if (value instanceof Set) {\n return `Set ${JSON.stringify([...value])}`;\n }\n if (value instanceof Map) {\n return `Map ${JSON.stringify(Object.fromEntries(value))}`;\n }\n if (value instanceof Error) {\n return `[${value.name}: ${value.message}]`;\n }\n if (value instanceof RegExp) {\n return value.toString();\n }\n if (Array.isArray(value)) {\n return value.map((v) => serialize(v, depth + 1, encountered));\n }\n if (Object.keys(value).length || value.constructor?.name === 'Object') {\n return Object.fromEntries(Object.entries(value).map(([k, v]) => [k, serialize(v, depth + 1, encountered)]));\n }\n if (value.constructor) {\n return `[${value.constructor.name}]`;\n }\n if (value[Symbol.toStringTag] == 'Module') {\n return '[empty module]';\n }\n }\n\n if (typeof value === 'function') {\n const properties = JSON.stringify({ ...value });\n return `[Function ${value.name || '<anonymous>'}${properties === '{}' ? '' : ` ${properties}`}]`;\n }\n\n if (typeof value === 'symbol') {\n return value.toString();\n }\n\n try {\n // try to serialize the same way postMessage does, per\n // https://nodejs.org/api/worker_threads.html#portpostmessagevalue-transferlist\n v8.serialize(value);\n return value;\n } catch {\n // not serializable, so format the value instead\n return JSON.stringify(value);\n }\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"writeESMStubs.d.ts","sourceRoot":"","sources":["../src/writeESMStubs.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,iCAAiC,CAAC;AAC5E,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,gCAAgC,CAAC;AAE1E;;;GAGG;AACH,wBAAsB,aAAa,CAAC,OAAO,EAAE,oBAAoB,GAAG,OAAO,CAAC,mBAAmB,CAAC,
|
|
1
|
+
{"version":3,"file":"writeESMStubs.d.ts","sourceRoot":"","sources":["../src/writeESMStubs.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,iCAAiC,CAAC;AAC5E,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,gCAAgC,CAAC;AAE1E;;;GAGG;AACH,wBAAsB,aAAa,CAAC,OAAO,EAAE,oBAAoB,GAAG,OAAO,CAAC,mBAAmB,CAAC,CA+C/F"}
|
package/lib/writeESMStubs.js
CHANGED
|
@@ -27,12 +27,16 @@ export async function writeESMStubs(options) {
|
|
|
27
27
|
}
|
|
28
28
|
console.debug(`Generating ESM stub for ${filePath} at ${stubPath}`);
|
|
29
29
|
let stubContent = '';
|
|
30
|
-
const namedExports = unsafeCjsExportNames
|
|
30
|
+
const namedExports = unsafeCjsExportNames === 'types'
|
|
31
|
+
? 'types'
|
|
32
|
+
: unsafeCjsExportNames
|
|
33
|
+
? (unsafeCjsExportNames[entryKey] ?? [])
|
|
34
|
+
: undefined;
|
|
31
35
|
if (path.extname(entryPath).toLowerCase() === '.json') {
|
|
32
36
|
stubContent = await generateESMStubFromJSON({ filePath });
|
|
33
37
|
}
|
|
34
38
|
else {
|
|
35
|
-
stubContent = generateESMStubFromCJS({ filePath, stubPath, namedExports });
|
|
39
|
+
stubContent = await generateESMStubFromCJS({ filePath, stubPath, namedExports });
|
|
36
40
|
}
|
|
37
41
|
// Attempt to write it to disk.
|
|
38
42
|
await writeFile(stubPath, stubContent, 'utf-8');
|
package/lib/writeESMStubs.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"writeESMStubs.js","sourceRoot":"","sources":["../src/writeESMStubs.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,mCAAmC,CAAC;AAC1D,OAAO,EAAE,SAAS,EAAE,MAA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AAK,OAAO;gBAC9B,CAAC,CAAC,OAAO;gBACT,CAAC,CAAC,oBAAoB;oBACpB,CAAC,CAAC,CAAC,oBAAoB,CAAC,QAAQ,CAAC,IAAI,EAAE,CAAC;oBACxC,CAAC,CAAC,SAAS,CAAC;YAElB,IAAI,IAAI,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC,WAAW,EAAE,KAAK,OAAO,EAAE,CAAC;gBACtD,WAAW,GAAG,MAAM,uBAAuB,CAAC,EAAE,QAAQ,EAAE,CAAC,CAAC;YAC5D,CAAC;iBAAM,CAAC;gBACN,WAAW,GAAG,MAAM,sBAAsB,CAAC,EAAE,QAAQ,EAAE,QAAQ,EAAE,YAAY,EAAE,CAAC,CAAC;YACnF,CAAC;YAED,+BAA+B;YAC/B,MAAM,SAAS,CAAC,QAAQ,EAAE,WAAW,EAAE,OAAO,CAAC,CAAC;YAEhD,MAAM,CAAC,UAAU,CAAC,QAAQ,CAAC,GAAG,QAAQ,CAAC;QACzC,CAAC;QAAC,OAAO,CAAC,EAAE,CAAC;YACX,MAAM,CAAC,UAAU,CAAC,QAAQ,CAAC,GAAG,aAAa,CAAC;YAC5C,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,YAAY,CAAC,EAAE,QAAQ,EAAE,aAAa,EAAE,SAAS,EAAE,KAAK,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC;QACrF,CAAC;IACH,CAAC;IAED,OAAO,MAAM,CAAC;AAChB,CAAC","sourcesContent":["import { slash } from '@ms-cloudpack/path-string-parsing';\nimport { writeFile } from 'fs/promises';\nimport path from 'path';\nimport { generateESMStubFromCJS } from './generateESMStubFromCJS.js';\nimport { generateESMStubFromJSON } from './generateESMStubFromJSON.js';\nimport { getStubPath } from './getStubPath.js';\nimport { processError } from './processError.js';\nimport type { WriteESMStubsOptions } from './types/WriteESMStubsOptions.js';\nimport type { WriteESMStubsResult } from './types/WriteESMStubsResult.js';\n\n/**\n * Generates a set of ESM stubs for given entries and writes it to disk.\n * If any files doesn't need a stub, the returned entry will use the original path.\n */\nexport async function writeESMStubs(options: WriteESMStubsOptions): Promise<WriteESMStubsResult> {\n const { inputPath, entries, unsafeCjsExportNames } = options;\n const result: WriteESMStubsResult = {\n newEntries: {},\n errors: [],\n };\n\n for (const [entryKey, entryPath] of Object.entries(entries)) {\n const filePath = slash(path.join(inputPath, entryPath));\n const entryFullPath = path.join(inputPath, entryPath);\n\n try {\n const stubPath = await getStubPath({ inputPath, entryPath });\n if (!stubPath) {\n // Stub not needed\n result.newEntries[entryKey] = entryFullPath;\n continue;\n }\n\n console.debug(`Generating ESM stub for ${filePath} at ${stubPath}`);\n\n let stubContent: string = '';\n\n const namedExports =\n unsafeCjsExportNames === 'types'\n ? 'types'\n : unsafeCjsExportNames\n ? (unsafeCjsExportNames[entryKey] ?? [])\n : undefined;\n\n if (path.extname(entryPath).toLowerCase() === '.json') {\n stubContent = await generateESMStubFromJSON({ filePath });\n } else {\n stubContent = await generateESMStubFromCJS({ filePath, stubPath, namedExports });\n }\n\n // Attempt to write it to disk.\n await writeFile(stubPath, stubContent, 'utf-8');\n\n result.newEntries[entryKey] = stubPath;\n } catch (e) {\n result.newEntries[entryKey] = entryFullPath;\n result.errors.push(processError({ entryKey, entryFullPath, inputPath, error: e }));\n }\n }\n\n return result;\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ms-cloudpack/esm-stub-utilities",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.15.1",
|
|
4
4
|
"description": "Generates ESM stubs for CommonJS entry files.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -14,13 +14,14 @@
|
|
|
14
14
|
}
|
|
15
15
|
},
|
|
16
16
|
"dependencies": {
|
|
17
|
-
"@happy-dom/global-registrator": "^
|
|
18
|
-
"@ms-cloudpack/common-types": "^0.
|
|
17
|
+
"@happy-dom/global-registrator": "^18.0.0",
|
|
18
|
+
"@ms-cloudpack/common-types": "^0.25.1",
|
|
19
19
|
"@ms-cloudpack/environment": "^0.1.1",
|
|
20
20
|
"@ms-cloudpack/json-utilities": "^0.1.10",
|
|
21
|
-
"@ms-cloudpack/package-utilities": "^12.3.
|
|
21
|
+
"@ms-cloudpack/package-utilities": "^12.3.14",
|
|
22
22
|
"@ms-cloudpack/path-string-parsing": "^1.2.7",
|
|
23
|
-
"@ms-cloudpack/worker-pool": "^0.4.
|
|
23
|
+
"@ms-cloudpack/worker-pool": "^0.4.1",
|
|
24
|
+
"oxc-parser": "^0.73.0",
|
|
24
25
|
"regenerator-runtime": "^0.14.1"
|
|
25
26
|
},
|
|
26
27
|
"devDependencies": {
|