@ms-cloudpack/esm-stub-utilities 0.11.2 → 0.11.3
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/processError.d.ts
CHANGED
|
@@ -7,11 +7,12 @@ export type ErrorLike = {
|
|
|
7
7
|
requireStack?: string[];
|
|
8
8
|
};
|
|
9
9
|
/**
|
|
10
|
-
*
|
|
10
|
+
* Process a raw error into a form which may be more useful for debugging.
|
|
11
11
|
*/
|
|
12
12
|
export declare function processError(params: {
|
|
13
13
|
entryKey: string;
|
|
14
14
|
entryFullPath: string;
|
|
15
15
|
error: unknown;
|
|
16
|
+
inputPath: string;
|
|
16
17
|
}): BundleMessage;
|
|
17
18
|
//# sourceMappingURL=processError.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"processError.d.ts","sourceRoot":"","sources":["../src/processError.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,4BAA4B,CAAC;
|
|
1
|
+
{"version":3,"file":"processError.d.ts","sourceRoot":"","sources":["../src/processError.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,4BAA4B,CAAC;AAIhE,MAAM,MAAM,SAAS,GAAG;IAAE,IAAI,CAAC,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,MAAM,CAAC;IAAC,KAAK,CAAC,EAAE,MAAM,CAAC;IAAC,IAAI,CAAC,EAAE,MAAM,CAAC;IAAC,YAAY,CAAC,EAAE,MAAM,EAAE,CAAA;CAAE,CAAC;AAYnH;;GAEG;AACH,wBAAgB,YAAY,CAAC,MAAM,EAAE;IACnC,QAAQ,EAAE,MAAM,CAAC;IACjB,aAAa,EAAE,MAAM,CAAC;IACtB,KAAK,EAAE,OAAO,CAAC;IACf,SAAS,EAAE,MAAM,CAAC;CACnB,GAAG,aAAa,CAgHhB"}
|
package/lib/processError.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { slash } from '@ms-cloudpack/path-string-parsing';
|
|
1
|
+
import { normalizeRelativePath, slash } from '@ms-cloudpack/path-string-parsing';
|
|
2
|
+
import path from 'path';
|
|
2
3
|
/** Some known error codes that can occur when running a file in a sandbox. */
|
|
3
4
|
const errorCodes = {
|
|
4
5
|
requireESM: 'ERR_REQUIRE_ESM',
|
|
@@ -8,14 +9,14 @@ const esmPackageName = 'esm-stub-utilities';
|
|
|
8
9
|
const workerPackageName = 'worker-pool';
|
|
9
10
|
const workerEntryName = 'workerEntry.js';
|
|
10
11
|
/**
|
|
11
|
-
*
|
|
12
|
+
* Process a raw error into a form which may be more useful for debugging.
|
|
12
13
|
*/
|
|
13
14
|
export function processError(params) {
|
|
14
|
-
const { entryKey, entryFullPath, error: errorParam } = params;
|
|
15
|
+
const { entryKey, entryFullPath, error: errorParam, inputPath } = params;
|
|
15
16
|
const error = errorParam.message ? errorParam : { message: String(errorParam) };
|
|
16
17
|
const { message: originalMessage, name, code, stack: originalStack, requireStack } = error;
|
|
17
|
-
let lineNumber
|
|
18
|
-
let columnNumber
|
|
18
|
+
let lineNumber;
|
|
19
|
+
let columnNumber;
|
|
19
20
|
let message = originalMessage;
|
|
20
21
|
if (code === errorCodes.requireESM) {
|
|
21
22
|
// Remove the instruction to change to a dynamic import, since it's less likely to be applicable here.
|
|
@@ -105,7 +106,11 @@ export function processError(params) {
|
|
|
105
106
|
}
|
|
106
107
|
return {
|
|
107
108
|
text: `Error creating stub for entry "./${entryKey}":\n${message}`,
|
|
108
|
-
location: {
|
|
109
|
+
location: {
|
|
110
|
+
file: normalizeRelativePath(path.relative(inputPath, entryFullPath)),
|
|
111
|
+
line: lineNumber,
|
|
112
|
+
column: columnNumber,
|
|
113
|
+
},
|
|
109
114
|
source: 'ESM stub generation',
|
|
110
115
|
};
|
|
111
116
|
}
|
package/lib/processError.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"processError.js","sourceRoot":"","s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CAAC,IAAI,CAAC,CAAC,QAAQ,CAAC,KAAK,CAAC,aAAa,CAAC,CAAC,CAAC,EAAE,KAAK,CAAC,iBAAiB,CAAC,CAAC;IAClH,IAAI,aAAa,EAAE,CAAC;QAClB,UAAU,GAAG,MAAM,CAAC,aAAa,CAAC,CAAC,CAAC,CAAC,CAAC;QACtC,YAAY,GAAG,MAAM,CAAC,aAAa,CAAC,CAAC,CAAC,CAAC,CAAC;IAC1C,CAAC;IAED,uEAAuE;IACvE,kFAAkF;IAClF,6FAA6F;IAC7F,sBAAsB;IACtB,IAAI,KAAK,EAAE,MAAM,IAAI,CAAC,KAAK,CAAC,MAAM,GAAG,CAAC,IAAI,CAAC,aAAa,CAAC,IAAI,IAAI,KAAK,UAAU,CAAC,cAAc,EAAE,CAAC;QAChG,+BAA+B;QAC/B,OAAO,IAAI,gDAAgD,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC;IAChF,CAAC;IAED,OAAO;QACL,IAAI,EAAE,oCAAoC,QAAQ,OAAO,OAAO,EAAE;QAClE,QAAQ,EAAE,EAAE,IAAI,EAAE,KAAK,CAAC,aAAa,CAAC,EAAE,IAAI,EAAE,UAAU,EAAE,MAAM,EAAE,YAAY,EAAE;QAChF,MAAM,EAAE,qBAAqB;KAC9B,CAAC;AACJ,CAAC","sourcesContent":["import type { BundleMessage } from '@ms-cloudpack/common-types';\nimport { slash } from '@ms-cloudpack/path-string-parsing';\n\nexport type ErrorLike = { name?: string; message: string; stack?: string; code?: string; requireStack?: string[] };\n\n/** Some known error codes that can occur when running a file in a sandbox. */\nconst errorCodes = {\n requireESM: 'ERR_REQUIRE_ESM',\n moduleNotFound: 'MODULE_NOT_FOUND',\n};\n\nconst esmPackageName = 'esm-stub-utilities';\nconst workerPackageName = 'worker-pool';\nconst workerEntryName = 'workerEntry.js';\n\n/**\n * Get info from an error which may be useful for debugging.\n */\nexport function processError(params: { entryKey: string; entryFullPath: string; error: unknown }): BundleMessage {\n const { entryKey, entryFullPath, error: errorParam } = params;\n const error = (errorParam as Error).message ? (errorParam as ErrorLike) : { message: String(errorParam) };\n\n const { message: originalMessage, name, code, stack: originalStack, requireStack } = error;\n\n let lineNumber = 1;\n let columnNumber = 0;\n let message = originalMessage;\n\n if (code === errorCodes.requireESM) {\n // Remove the instruction to change to a dynamic import, since it's less likely to be applicable here.\n // Error [ERR_REQUIRE_ESM]: require() of ES Module [...]/esm.mjs not supported.\n // Instead change the require of [...]/esm.mjs to a dynamic import() which is available in all CommonJS modules.\n message = message.split('\\n')[0];\n } else if (code === errorCodes.moduleNotFound && requireStack) {\n // Module not found errors have a require stack included in the error message, as well as a\n // requireStack property. Remove the stack from the message and rebuild it with filtering.\n // Error: Cannot find module 'foo'\n // Require stack:\n // - [...]/index.js\n // - [...]/generateESMStubFromCJS.js\n message = message.split('\\n')[0];\n // remove the generateESMStubFromCJS require line (not interesting)\n const filteredStack = requireStack.filter(\n (line) => !line.includes(esmPackageName) && !line.includes(workerPackageName),\n );\n if (filteredStack.length > 1) {\n // Only include the stack if it goes beyond the original file\n message += `.\\nRequire stack:\\n${filteredStack.map((line) => ` - ${line}`).join('\\n')}`;\n }\n } else if (name === 'SyntaxError' && originalStack) {\n // SyntaxErrors will have a `stack` like this:\n // /path/to/file.js:1\n // oh no\n // ^^\n // SyntaxError: Unexpected identifier\n // at ....\n // Use the first part as the message, but remove the stack trace (will be added back later if needed).\n message = originalStack.split(/\\n\\s+at /)[0];\n\n // If the error is in the entry point file, get the line and possibly column number.\n // (There won't be a stack line for the file with the syntax error, so the normal logic to get\n // the line and column later won't work.)\n const lineNumberMatch = message.match(/^(.*?):(\\d+)\\n/);\n if (lineNumberMatch && slash(lineNumberMatch[1]).includes(slash(entryFullPath))) {\n lineNumber = Number(lineNumberMatch[2]);\n\n // Get the column if the stack includes ^^ indicating the error location\n // (not all of them will have this, e.g. \"unexpected end of input\" errors won't)\n const columnNumberMatch = message.match(/^\\s*\\^/m);\n if (columnNumberMatch) {\n columnNumber = columnNumberMatch[0].length - 1;\n }\n }\n\n // This happens if a file is inferred from context or extension to be CJS but is actually ESM.\n if (/Unexpected token '(import|export)'/.test(message)) {\n message = `File appears to be an ES module that was loaded with require:\\n${message}`;\n }\n } else if (name !== 'Error') {\n message = `${name}: ${message}`;\n }\n\n let stack: string[] | undefined;\n if (originalStack) {\n // Get stack frame lines from the original error stack, and remove parts that are less likely\n // to be useful (node internals, and jest internals for errors while running tests).\n stack = originalStack\n .split('\\n')\n .filter((line) => /^\\s+at /.test(line) && !/\\(node:|jest-runtime|jest-resolve/.test(line));\n\n // The user probably only cares about the error location in the original code,\n // so make it easier to see that by removing all the esm-stub-utilities frames.\n const workerEntryIndex = stack.findIndex((line) => line.includes(workerEntryName));\n if (workerEntryIndex !== -1) {\n // Special case: if the error came from the file calling process.exit(), the most recent stack\n // frame will be from workerEntry.js, and the real frames will be after that.\n stack = stack.slice(workerEntryIndex + 1);\n }\n // Find the first esm-stub-utilities line and remove that and everything after.\n const utilsIndex = stack.findIndex((line) => line.includes(esmPackageName));\n if (utilsIndex !== -1) {\n stack = stack.slice(0, utilsIndex);\n }\n }\n\n // See if we can find the line and column in the entry file from the stack trace\n const locationMatch = stack?.find((line) => slash(line).includes(slash(entryFullPath)))?.match(/:(\\d+):(\\d+)\\)$/);\n if (locationMatch) {\n lineNumber = Number(locationMatch[1]);\n columnNumber = Number(locationMatch[2]);\n }\n\n // Include the stack if present, provided it's not just the entry file,\n // and this isn't a module not found error which already includes a require stack.\n // (If the only interesting stack line is the entry file, this is redundant with the returned\n // `location` object.)\n if (stack?.length && (stack.length > 1 || !locationMatch) && code !== errorCodes.moduleNotFound) {\n // Add the stack to the message\n message += `\\n\\nPartial stack (node internals omitted):\\n${stack.join('\\n')}`;\n }\n\n return {\n text: `Error creating stub for entry \"./${entryKey}\":\\n${message}`,\n location: { file: slash(entryFullPath), line: lineNumber, column: columnNumber },\n source: 'ESM stub generation',\n };\n}\n"]}
|
|
1
|
+
{"version":3,"file":"processError.js","s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IAAI,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,QAAQ,CAAC,KAAK,CAAC,aAAa,CAAC,CAAC,CAAC,EAAE,KAAK,CAAC,iBAAiB,CAAC,CAAC;IAClH,IAAI,aAAa,EAAE,CAAC;QAClB,UAAU,GAAG,MAAM,CAAC,aAAa,CAAC,CAAC,CAAC,CAAC,CAAC;QACtC,YAAY,GAAG,MAAM,CAAC,aAAa,CAAC,CAAC,CAAC,CAAC,CAAC;IAC1C,CAAC;IAED,uEAAuE;IACvE,kFAAkF;IAClF,6FAA6F;IAC7F,sBAAsB;IACtB,IAAI,KAAK,EAAE,MAAM,IAAI,CAAC,KAAK,CAAC,MAAM,GAAG,CAAC,IAAI,CAAC,aAAa,CAAC,IAAI,IAAI,KAAK,UAAU,CAAC,cAAc,EAAE,CAAC;QAChG,+BAA+B;QAC/B,OAAO,IAAI,gDAAgD,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC;IAChF,CAAC;IAED,OAAO;QACL,IAAI,EAAE,oCAAoC,QAAQ,OAAO,OAAO,EAAE;QAClE,QAAQ,EAAE;YACR,IAAI,EAAE,qBAAqB,CAAC,IAAI,CAAC,QAAQ,CAAC,SAAS,EAAE,aAAa,CAAC,CAAC;YACpE,IAAI,EAAE,UAAU;YAChB,MAAM,EAAE,YAAY;SACrB;QACD,MAAM,EAAE,qBAAqB;KAC9B,CAAC;AACJ,CAAC","sourcesContent":["import type { BundleMessage } from '@ms-cloudpack/common-types';\nimport { normalizeRelativePath, slash } from '@ms-cloudpack/path-string-parsing';\nimport path from 'path';\n\nexport type ErrorLike = { name?: string; message: string; stack?: string; code?: string; requireStack?: string[] };\n\n/** Some known error codes that can occur when running a file in a sandbox. */\nconst errorCodes = {\n requireESM: 'ERR_REQUIRE_ESM',\n moduleNotFound: 'MODULE_NOT_FOUND',\n};\n\nconst esmPackageName = 'esm-stub-utilities';\nconst workerPackageName = 'worker-pool';\nconst workerEntryName = 'workerEntry.js';\n\n/**\n * Process a raw error into a form which may be more useful for debugging.\n */\nexport function processError(params: {\n entryKey: string;\n entryFullPath: string;\n error: unknown;\n inputPath: string;\n}): BundleMessage {\n const { entryKey, entryFullPath, error: errorParam, inputPath } = params;\n const error = (errorParam as Error).message ? (errorParam as ErrorLike) : { message: String(errorParam) };\n\n const { message: originalMessage, name, code, stack: originalStack, requireStack } = error;\n\n let lineNumber: number | undefined;\n let columnNumber: number | undefined;\n let message = originalMessage;\n\n if (code === errorCodes.requireESM) {\n // Remove the instruction to change to a dynamic import, since it's less likely to be applicable here.\n // Error [ERR_REQUIRE_ESM]: require() of ES Module [...]/esm.mjs not supported.\n // Instead change the require of [...]/esm.mjs to a dynamic import() which is available in all CommonJS modules.\n message = message.split('\\n')[0];\n } else if (code === errorCodes.moduleNotFound && requireStack) {\n // Module not found errors have a require stack included in the error message, as well as a\n // requireStack property. Remove the stack from the message and rebuild it with filtering.\n // Error: Cannot find module 'foo'\n // Require stack:\n // - [...]/index.js\n // - [...]/generateESMStubFromCJS.js\n message = message.split('\\n')[0];\n // remove the generateESMStubFromCJS require line (not interesting)\n const filteredStack = requireStack.filter(\n (line) => !line.includes(esmPackageName) && !line.includes(workerPackageName),\n );\n if (filteredStack.length > 1) {\n // Only include the stack if it goes beyond the original file\n message += `.\\nRequire stack:\\n${filteredStack.map((line) => ` - ${line}`).join('\\n')}`;\n }\n } else if (name === 'SyntaxError' && originalStack) {\n // SyntaxErrors will have a `stack` like this:\n // /path/to/file.js:1\n // oh no\n // ^^\n // SyntaxError: Unexpected identifier\n // at ....\n // Use the first part as the message, but remove the stack trace (will be added back later if needed).\n message = originalStack.split(/\\n\\s+at /)[0];\n\n // If the error is in the entry point file, get the line and possibly column number.\n // (There won't be a stack line for the file with the syntax error, so the normal logic to get\n // the line and column later won't work.)\n const lineNumberMatch = message.match(/^(.*?):(\\d+)\\n/);\n if (lineNumberMatch && slash(lineNumberMatch[1]).includes(slash(entryFullPath))) {\n lineNumber = Number(lineNumberMatch[2]);\n\n // Get the column if the stack includes ^^ indicating the error location\n // (not all of them will have this, e.g. \"unexpected end of input\" errors won't)\n const columnNumberMatch = message.match(/^\\s*\\^/m);\n if (columnNumberMatch) {\n columnNumber = columnNumberMatch[0].length - 1;\n }\n }\n\n // This happens if a file is inferred from context or extension to be CJS but is actually ESM.\n if (/Unexpected token '(import|export)'/.test(message)) {\n message = `File appears to be an ES module that was loaded with require:\\n${message}`;\n }\n } else if (name !== 'Error') {\n message = `${name}: ${message}`;\n }\n\n let stack: string[] | undefined;\n if (originalStack) {\n // Get stack frame lines from the original error stack, and remove parts that are less likely\n // to be useful (node internals, and jest internals for errors while running tests).\n stack = originalStack\n .split('\\n')\n .filter((line) => /^\\s+at /.test(line) && !/\\(node:|jest-runtime|jest-resolve/.test(line));\n\n // The user probably only cares about the error location in the original code,\n // so make it easier to see that by removing all the esm-stub-utilities frames.\n const workerEntryIndex = stack.findIndex((line) => line.includes(workerEntryName));\n if (workerEntryIndex !== -1) {\n // Special case: if the error came from the file calling process.exit(), the most recent stack\n // frame will be from workerEntry.js, and the real frames will be after that.\n stack = stack.slice(workerEntryIndex + 1);\n }\n // Find the first esm-stub-utilities line and remove that and everything after.\n const utilsIndex = stack.findIndex((line) => line.includes(esmPackageName));\n if (utilsIndex !== -1) {\n stack = stack.slice(0, utilsIndex);\n }\n }\n\n // See if we can find the line and column in the entry file from the stack trace\n const locationMatch = stack?.find((line) => slash(line).includes(slash(entryFullPath)))?.match(/:(\\d+):(\\d+)\\)$/);\n if (locationMatch) {\n lineNumber = Number(locationMatch[1]);\n columnNumber = Number(locationMatch[2]);\n }\n\n // Include the stack if present, provided it's not just the entry file,\n // and this isn't a module not found error which already includes a require stack.\n // (If the only interesting stack line is the entry file, this is redundant with the returned\n // `location` object.)\n if (stack?.length && (stack.length > 1 || !locationMatch) && code !== errorCodes.moduleNotFound) {\n // Add the stack to the message\n message += `\\n\\nPartial stack (node internals omitted):\\n${stack.join('\\n')}`;\n }\n\n return {\n text: `Error creating stub for entry \"./${entryKey}\":\\n${message}`,\n location: {\n file: normalizeRelativePath(path.relative(inputPath, entryFullPath)),\n line: lineNumber,\n column: columnNumber,\n },\n source: 'ESM stub generation',\n };\n}\n"]}
|
package/lib/writeESMStubs.js
CHANGED
|
@@ -40,7 +40,7 @@ export async function writeESMStubs(options) {
|
|
|
40
40
|
catch (e) {
|
|
41
41
|
const entryFullPath = path.join(inputPath, entryPath);
|
|
42
42
|
result.newEntries[safeEntryKey] = entryFullPath;
|
|
43
|
-
result.errors.push(processError({ entryKey: safeEntryKey, entryFullPath, error: e }));
|
|
43
|
+
result.errors.push(processError({ entryKey: safeEntryKey, entryFullPath, inputPath, error: e }));
|
|
44
44
|
}
|
|
45
45
|
}
|
|
46
46
|
return result;
|
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,MAAM,aAAa,CAAC;AACxC,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,EAAE,sBAAsB,EAAE,MAAM,6BAA6B,CAAC;AACrE,OAAO,EAAE,uBAAuB,EAAE,MAAM,8BAA8B,CAAC;AACvE,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAC/C,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AAIjD;;;GAGG;AACH,MAAM,CAAC,KAAK,UAAU,aAAa,CAAC,OAA6B;IAC/D,MAAM,EAAE,SAAS,EAAE,OAAO,EAAE,QAAQ,EAAE,GAAG,OAAO,CAAC;IACjD,MAAM,MAAM,GAAwB;QAClC,UAAU,EAAE,EAAE;QACd,MAAM,EAAE,EAAE;KACX,CAAC;IAEF,KAAK,MAAM,CAAC,QAAQ,EAAE,SAAS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,UAAU,CAAC,YAAY,CAAC,GAAG,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE,SAAS,CAAC,CAAC;gBAClE,SAAS;YACX,CAAC;YAED,OAAO,CAAC,KAAK,CAAC,2BAA2B,QAAQ,OAAO,QAAQ,EAAE,CAAC,CAAC;YAEpE,IAAI,WAAW,GAAW,EAAE,CAAC;YAC7B,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,sBAAsB,CAAC,EAAE,QAAQ,EAAE,QAAQ,EAAE,CAAC,CAAC;YAC/D,CAAC;YAED,+BAA+B;YAC/B,MAAM,SAAS,CAAC,QAAQ,EAAE,WAAW,EAAE,OAAO,CAAC,CAAC;YAEhD,MAAM,CAAC,UAAU,CAAC,YAAY,CAAC,GAAG,QAAQ,CAAC;QAC7C,CAAC;QAAC,OAAO,CAAC,EAAE,CAAC;YACX,MAAM,aAAa,GAAG,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE,SAAS,CAAC,CAAC;YACtD,MAAM,CAAC,UAAU,CAAC,YAAY,CAAC,GAAG,aAAa,CAAC;YAChD,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,YAAY,CAAC,EAAE,QAAQ,EAAE,YAAY,EAAE,aAAa,EAAE,SAAS,EAAE,KAAK,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC;QACnG,CAAC;IACH,CAAC;IAED,OAAO,MAAM,CAAC;AAChB,CAAC;AAED,SAAS,wBAAwB,CAAC,YAAoB;IACpD,OAAO,YAAY,CAAC,OAAO,CAAC,OAAO,EAAE,EAAE,CAAC,CAAC;AAC3C,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, forceCJS } = 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 safeEntryKey = stripLeadingRelativePath(entryKey);\n\n try {\n const stubPath = await getStubPath({ inputPath, entryPath, forceCJS });\n if (!stubPath) {\n // Stub not needed\n result.newEntries[safeEntryKey] = path.join(inputPath, entryPath);\n continue;\n }\n\n console.debug(`Generating ESM stub for ${filePath} at ${stubPath}`);\n\n let stubContent: string = '';\n if (path.extname(entryPath).toLowerCase() === '.json') {\n stubContent = await generateESMStubFromJSON({ filePath });\n } else {\n stubContent = generateESMStubFromCJS({ filePath, stubPath });\n }\n\n // Attempt to write it to disk.\n await writeFile(stubPath, stubContent, 'utf-8');\n\n result.newEntries[safeEntryKey] = stubPath;\n } catch (e) {\n const entryFullPath = path.join(inputPath, entryPath);\n result.newEntries[safeEntryKey] = entryFullPath;\n result.errors.push(processError({ entryKey: safeEntryKey, entryFullPath, inputPath, error: e }));\n }\n }\n\n return result;\n}\n\nfunction stripLeadingRelativePath(relativePath: string) {\n return relativePath.replace(/^\\.\\//, '');\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ms-cloudpack/esm-stub-utilities",
|
|
3
|
-
"version": "0.11.
|
|
3
|
+
"version": "0.11.3",
|
|
4
4
|
"description": "Generates ESM stubs for CommonJS entry files.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -19,9 +19,9 @@
|
|
|
19
19
|
}
|
|
20
20
|
},
|
|
21
21
|
"dependencies": {
|
|
22
|
-
"@ms-cloudpack/common-types": "^0.2.
|
|
22
|
+
"@ms-cloudpack/common-types": "^0.2.2",
|
|
23
23
|
"@ms-cloudpack/json-utilities": "^0.1.4",
|
|
24
|
-
"@ms-cloudpack/package-utilities": "^7.1.
|
|
24
|
+
"@ms-cloudpack/package-utilities": "^7.1.3",
|
|
25
25
|
"@ms-cloudpack/path-string-parsing": "^1.2.1",
|
|
26
26
|
"@ms-cloudpack/worker-pool": "^0.1.2",
|
|
27
27
|
"jsdom": "^22.0.0",
|