@ms-cloudpack/esm-stub-utilities 0.13.22 → 0.14.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/lib/generateESMStubFromCJS.d.ts +4 -1
- package/lib/generateESMStubFromCJS.d.ts.map +1 -1
- package/lib/generateESMStubFromCJS.js +7 -3
- package/lib/generateESMStubFromCJS.js.map +1 -1
- package/lib/generateESMStubFromExports.d.ts.map +1 -1
- package/lib/generateESMStubFromExports.js.map +1 -1
- package/lib/isValidIdentifierName.d.ts.map +1 -1
- package/lib/isValidIdentifierName.js.map +1 -1
- package/lib/types/WriteESMStubsOptions.d.ts +1 -1
- package/lib/types/WriteESMStubsOptions.d.ts.map +1 -1
- package/lib/types/WriteESMStubsOptions.js.map +1 -1
- package/lib/verifyStub/verifyStub.js.map +1 -1
- package/lib/writeESMStubs.d.ts.map +1 -1
- package/lib/writeESMStubs.js +3 -2
- package/lib/writeESMStubs.js.map +1 -1
- package/lib/writeESMStubsInWorker.d.ts.map +1 -1
- package/lib/writeESMStubsInWorker.js +2 -1
- package/lib/writeESMStubsInWorker.js.map +1 -1
- package/package.json +4 -4
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Given entry/stub paths
|
|
2
|
+
* Given entry/stub paths, generates the esm stub content.
|
|
3
3
|
* The stubPath is required to generate the proper import statement.
|
|
4
|
+
* namedExports is optional, if present, the given export names will be used,
|
|
5
|
+
* but if missing, we'll attempt to require the CJS and extract export info.
|
|
4
6
|
*/
|
|
5
7
|
export declare function generateESMStubFromCJS(options: {
|
|
6
8
|
filePath: string;
|
|
7
9
|
stubPath: string;
|
|
10
|
+
namedExports?: string[];
|
|
8
11
|
}): string;
|
|
9
12
|
//# sourceMappingURL=generateESMStubFromCJS.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"generateESMStubFromCJS.d.ts","sourceRoot":"","sources":["../src/generateESMStubFromCJS.ts"],"names":[],"mappings":"AAQA
|
|
1
|
+
{"version":3,"file":"generateESMStubFromCJS.d.ts","sourceRoot":"","sources":["../src/generateESMStubFromCJS.ts"],"names":[],"mappings":"AAQA;;;;;GAKG;AACH,wBAAgB,sBAAsB,CAAC,OAAO,EAAE;IAC9C,QAAQ,EAAE,MAAM,CAAC;IACjB,QAAQ,EAAE,MAAM,CAAC;IACjB,YAAY,CAAC,EAAE,MAAM,EAAE,CAAC;CACzB,GAAG,MAAM,CAyBT"}
|
|
@@ -5,12 +5,16 @@ import { getExportInfo } from './getExportInfo.js';
|
|
|
5
5
|
import { generateESMStubFromExports } from './generateESMStubFromExports.js';
|
|
6
6
|
const require = createRequire(import.meta.url);
|
|
7
7
|
/**
|
|
8
|
-
* Given entry/stub paths
|
|
8
|
+
* Given entry/stub paths, generates the esm stub content.
|
|
9
9
|
* The stubPath is required to generate the proper import statement.
|
|
10
|
+
* namedExports is optional, if present, the given export names will be used,
|
|
11
|
+
* but if missing, we'll attempt to require the CJS and extract export info.
|
|
10
12
|
*/
|
|
11
13
|
export function generateESMStubFromCJS(options) {
|
|
12
|
-
const { filePath, stubPath } = options;
|
|
13
|
-
const exportInfo =
|
|
14
|
+
const { filePath, stubPath, namedExports } = options;
|
|
15
|
+
const exportInfo = namedExports
|
|
16
|
+
? { type: 'object', keys: namedExports }
|
|
17
|
+
: getExportInfo(require(filePath));
|
|
14
18
|
const { type: exportType } = exportInfo;
|
|
15
19
|
let relativePath;
|
|
16
20
|
// We need to compute a relative path from the stub to the entry to construct the proper import in the stub.
|
|
@@ -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,EA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AG,IAAI,GAAG,KAAK,CAAC,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC,EAAE,QAAQ,CAAC,CAAC,CAAC;IAC/E,CAAC;IAED,MAAM,WAAW,GAAG,0BAA0B,CAAC;QAC7C,eAAe,EACb,UAAU,KAAK,MAAM,CAAC,CAAC,CAAC,WAAW,YAAY,IAAI,CAAC,CAAC,CAAC,6BAA6B,YAAY,IAAI;QACrG,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';\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 * namedExports is optional, if present, the given export names will be used,\n * but if missing, we'll attempt to require the CJS and extract export info.\n */\nexport function generateESMStubFromCJS(options: {\n filePath: string;\n stubPath: string;\n namedExports?: string[];\n}): string {\n const { filePath, stubPath, namedExports } = options;\n\n const exportInfo = namedExports\n ? ({ type: 'object', keys: namedExports } as const)\n : getExportInfo(require(filePath) as unknown);\n\n const { type: exportType } = exportInfo;\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 exportType === 'none' ? `import \"${relativePath}\";` : `import moduleExport from \"${relativePath}\";`,\n export: exportInfo,\n });\n\n return stubContent;\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"generateESMStubFromExports.d.ts","sourceRoot":"","sources":["../src/generateESMStubFromExports.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,2BAA2B,CAAC;AAEhE;;;GAGG;AACH,wBAAgB,0BAA0B,CAAC,OAAO,EAAE;IAAE,eAAe,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,cAAc,CAAA;CAAE,
|
|
1
|
+
{"version":3,"file":"generateESMStubFromExports.d.ts","sourceRoot":"","sources":["../src/generateESMStubFromExports.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,2BAA2B,CAAC;AAEhE;;;GAGG;AACH,wBAAgB,0BAA0B,CAAC,OAAO,EAAE;IAAE,eAAe,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,cAAc,CAAA;CAAE,GAAG,MAAM,CA8C/G"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"generateESMStubFromExports.js","sourceRoot":"","sources":["../src/generateESMStubFromExports.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,MAAM,4BAA4B,CAAC;AAGnE;;;GAGG;AACH,MAAM,UAAU,0BAA0B,CAAC,OAA4D;IACrG,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,eAAe,EAAE,GAAG,OAAO,CAAC;IACrD,MAAM,UAAU,GAAG,OAAO,CAAC,IAAI,CAAC;IAEhC,IAAI,UAAU,KAAK,MAAM,EAAE,CAAC;QAC1B,sCAAsC;QACtC,OAAO,eAAe,CAAC;IACzB,CAAC;IAED,gEAAgE;IAChE,gGAAgG;IAChG,IAAI,CAAC,eAAe,CAAC,QAAQ,CAAC,cAAc,CAAC,EAAE,CAAC;QAC9C,MAAM,IAAI,KAAK,CAAC,wCAAwC,CAAC,CAAC;IAC5D,CAAC;IAED,IAAI,UAAU,KAAK,OAAO,EAAE,CAAC;QAC3B,wDAAwD;QACxD,sFAAsF;QACtF,6FAA6F;QAC7F,iDAAiD;QACjD,OAAO,CAAC,eAAe,EAAE,8BAA8B,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IACtE,CAAC;IAED,mGAAmG;IACnG,MAAM,YAAY,GAAG,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,qBAAqB,CAAC,IAAI,CAAC,CAAC,CAAC;IAChF,MAAM,gBAAgB,GAAG,OAAO,CAAC,IAAI,CAAC,QAAQ,CAAC,SAAS,CAAC,CAAC;IAC1D,MAAM,IAAI,GAAG,CAAC,eAAe,CAAC,CAAC;IAE/B,IAAI,YAAY,CAAC,MAAM,EAAE,CAAC;QACxB,IAAI,CAAC,IAAI,CAAC,WAAW,YAAY,CAAC,IAAI,CAAC,IAAI,CAAC,oBAAoB,CAAC,CAAC;IACpE,CAAC;IAED,IAAI,gBAAgB,EAAE,CAAC;QACrB,IAAI,CAAC,IAAI,CACP,kFAAkF,EAClF,+BAA+B,CAChC,CAAC;IACJ,CAAC;SAAM,CAAC;QACN,IAAI,CAAC,IAAI,CAAC,8BAA8B,CAAC,CAAC;IAC5C,CAAC;IAED,IAAI,YAAY,CAAC,MAAM,EAAE,CAAC;QACxB,IAAI,CAAC,IAAI,CAAC,YAAY,YAAY,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IACrD,CAAC;IAED,OAAO,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AACzB,CAAC","sourcesContent":["import { isValidIdentifierName } from './isValidIdentifierName.js';\nimport type { StubExportInfo } from './types/StubExportInfo.js';\n\n/**\n * Given export info and the import statement, generates the esm stub content.\n * The import statement must have moduleExport as the imported value.\n */\nexport function generateESMStubFromExports(options: { importStatement: string; export: StubExportInfo }) {\n const { export: exports, importStatement } = options;\n const exportType = exports.type;\n\n if (exportType === 'none') {\n // No exports. Just import the module.\n return importStatement;\n }\n\n // importStatement must have moduleExport as the imported value.\n // This is not the best way to check this, but it's the best we can do without parsing the code.\n if (!importStatement.includes('moduleExport')) {\n throw new Error('importStatement must have moduleExport');\n }\n\n if (exportType === 'other') {\n // Some kind of value. Import then re-export as default.\n // (Note that even for values such as numbers which we could in theory rewrite inline,\n // we don't know how the value is calculated at runtime or what other side effects the module\n // might have, so we should preserve the import.)\n return [importStatement, `export default moduleExport;`].join('\\n');\n }\n\n // Object or function. Export both named properties (if any, and filtering keywords) and a default.\n const namedExports = exports.keys.filter((name) => isValidIdentifierName(name));\n const hasDefaultExport = exports.keys.includes('default');\n const stub = [importStatement];\n\n if (namedExports.length) {\n stub.push(`const { ${namedExports.join(', ')} } = moduleExport;`);\n }\n\n if (hasDefaultExport) {\n stub.push(\n `const defaultExport = (moduleExport?.default?.default) ?? moduleExport?.default;`,\n `export default defaultExport;`,\n );\n } else {\n stub.push(`export default moduleExport;`);\n }\n\n if (namedExports.length) {\n stub.push(`export { ${namedExports.join(', ')} }`);\n }\n\n return stub.join('\\n');\n}\n"]}
|
|
1
|
+
{"version":3,"file":"generateESMStubFromExports.js","sourceRoot":"","sources":["../src/generateESMStubFromExports.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,MAAM,4BAA4B,CAAC;AAGnE;;;GAGG;AACH,MAAM,UAAU,0BAA0B,CAAC,OAA4D;IACrG,MAAM,EAAE,MAAM,EAAE,OAAO,EAAE,eAAe,EAAE,GAAG,OAAO,CAAC;IACrD,MAAM,UAAU,GAAG,OAAO,CAAC,IAAI,CAAC;IAEhC,IAAI,UAAU,KAAK,MAAM,EAAE,CAAC;QAC1B,sCAAsC;QACtC,OAAO,eAAe,CAAC;IACzB,CAAC;IAED,gEAAgE;IAChE,gGAAgG;IAChG,IAAI,CAAC,eAAe,CAAC,QAAQ,CAAC,cAAc,CAAC,EAAE,CAAC;QAC9C,MAAM,IAAI,KAAK,CAAC,wCAAwC,CAAC,CAAC;IAC5D,CAAC;IAED,IAAI,UAAU,KAAK,OAAO,EAAE,CAAC;QAC3B,wDAAwD;QACxD,sFAAsF;QACtF,6FAA6F;QAC7F,iDAAiD;QACjD,OAAO,CAAC,eAAe,EAAE,8BAA8B,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IACtE,CAAC;IAED,mGAAmG;IACnG,MAAM,YAAY,GAAG,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,qBAAqB,CAAC,IAAI,CAAC,CAAC,CAAC;IAChF,MAAM,gBAAgB,GAAG,OAAO,CAAC,IAAI,CAAC,QAAQ,CAAC,SAAS,CAAC,CAAC;IAC1D,MAAM,IAAI,GAAG,CAAC,eAAe,CAAC,CAAC;IAE/B,IAAI,YAAY,CAAC,MAAM,EAAE,CAAC;QACxB,IAAI,CAAC,IAAI,CAAC,WAAW,YAAY,CAAC,IAAI,CAAC,IAAI,CAAC,oBAAoB,CAAC,CAAC;IACpE,CAAC;IAED,IAAI,gBAAgB,EAAE,CAAC;QACrB,IAAI,CAAC,IAAI,CACP,kFAAkF,EAClF,+BAA+B,CAChC,CAAC;IACJ,CAAC;SAAM,CAAC;QACN,IAAI,CAAC,IAAI,CAAC,8BAA8B,CAAC,CAAC;IAC5C,CAAC;IAED,IAAI,YAAY,CAAC,MAAM,EAAE,CAAC;QACxB,IAAI,CAAC,IAAI,CAAC,YAAY,YAAY,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;IACrD,CAAC;IAED,OAAO,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AACzB,CAAC","sourcesContent":["import { isValidIdentifierName } from './isValidIdentifierName.js';\nimport type { StubExportInfo } from './types/StubExportInfo.js';\n\n/**\n * Given export info and the import statement, generates the esm stub content.\n * The import statement must have moduleExport as the imported value.\n */\nexport function generateESMStubFromExports(options: { importStatement: string; export: StubExportInfo }): string {\n const { export: exports, importStatement } = options;\n const exportType = exports.type;\n\n if (exportType === 'none') {\n // No exports. Just import the module.\n return importStatement;\n }\n\n // importStatement must have moduleExport as the imported value.\n // This is not the best way to check this, but it's the best we can do without parsing the code.\n if (!importStatement.includes('moduleExport')) {\n throw new Error('importStatement must have moduleExport');\n }\n\n if (exportType === 'other') {\n // Some kind of value. Import then re-export as default.\n // (Note that even for values such as numbers which we could in theory rewrite inline,\n // we don't know how the value is calculated at runtime or what other side effects the module\n // might have, so we should preserve the import.)\n return [importStatement, `export default moduleExport;`].join('\\n');\n }\n\n // Object or function. Export both named properties (if any, and filtering keywords) and a default.\n const namedExports = exports.keys.filter((name) => isValidIdentifierName(name));\n const hasDefaultExport = exports.keys.includes('default');\n const stub = [importStatement];\n\n if (namedExports.length) {\n stub.push(`const { ${namedExports.join(', ')} } = moduleExport;`);\n }\n\n if (hasDefaultExport) {\n stub.push(\n `const defaultExport = (moduleExport?.default?.default) ?? moduleExport?.default;`,\n `export default defaultExport;`,\n );\n } else {\n stub.push(`export default moduleExport;`);\n }\n\n if (namedExports.length) {\n stub.push(`export { ${namedExports.join(', ')} }`);\n }\n\n return stub.join('\\n');\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"isValidIdentifierName.d.ts","sourceRoot":"","sources":["../src/isValidIdentifierName.ts"],"names":[],"mappings":"AAIA;;;GAGG;AACH,eAAO,MAAM,oBAAoB,UAiDhC,CAAC;AAEF;;;;;;GAMG;AACH,wBAAgB,qBAAqB,CAAC,IAAI,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"isValidIdentifierName.d.ts","sourceRoot":"","sources":["../src/isValidIdentifierName.ts"],"names":[],"mappings":"AAIA;;;GAGG;AACH,eAAO,MAAM,oBAAoB,UAiDhC,CAAC;AAEF;;;;;;GAMG;AACH,wBAAgB,qBAAqB,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAE3D"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"isValidIdentifierName.js","sourceRoot":"","sources":["../src/isValidIdentifierName.ts"],"names":[],"mappings":"AAAA,8EAA8E;AAC9E,yEAAyE;AACzE,MAAM,iBAAiB,GAAG,4BAA4B,CAAC;AAEvD;;;GAGG;AACH,MAAM,CAAC,MAAM,oBAAoB,GAAG;IAClC,WAAW;IACX,OAAO;IACP,OAAO;IACP,MAAM;IACN,OAAO;IACP,OAAO;IACP,OAAO;IACP,UAAU;IACV,UAAU;IACV,SAAS;IACT,QAAQ;IACR,IAAI;IACJ,MAAM;IACN,MAAM;IACN,MAAM;IACN,QAAQ;IACR,SAAS;IACT,OAAO;IACP,SAAS;IACT,KAAK;IACL,UAAU;IACV,IAAI;IACJ,YAAY;IACZ,QAAQ;IACR,IAAI;IACJ,YAAY;IACZ,WAAW;IACX,KAAK;IACL,KAAK;IACL,MAAM;IACN,SAAS;IACT,SAAS;IACT,WAAW;IACX,QAAQ;IACR,QAAQ;IACR,QAAQ;IACR,OAAO;IACP,QAAQ;IACR,MAAM;IACN,OAAO;IACP,MAAM;IACN,KAAK;IACL,QAAQ;IACR,KAAK;IACL,MAAM;IACN,OAAO;IACP,MAAM;IACN,OAAO;CACR,CAAC;AAEF;;;;;;GAMG;AACH,MAAM,UAAU,qBAAqB,CAAC,IAAY;IAChD,OAAO,CAAC,oBAAoB,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,iBAAiB,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AAC9E,CAAC","sourcesContent":["// A valid variable name must start with a letter, underscore, or dollar sign,\n// and can be followed by letters, numbers, underscores, or dollar signs.\nconst variableNameRegex = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/;\n\n/**\n * Lists all export names which create problems with esm output.\n * Mostly this includes the JavaScript keywords, minus \"default\".\n */\nexport const forbiddenExportNames = [\n 'arguments',\n 'await',\n 'break',\n 'case',\n 'catch',\n 'class',\n 'const',\n 'continue',\n 'debugger',\n 'default',\n 'delete',\n 'do',\n 'else',\n 'enum',\n 'eval',\n 'export',\n 'extends',\n 'false',\n 'finally',\n 'for',\n 'function',\n 'if',\n 'implements',\n 'import',\n 'in',\n 'instanceof',\n 'interface',\n 'let',\n 'new',\n 'null',\n 'package',\n 'private',\n 'protected',\n 'public',\n 'return',\n 'static',\n 'super',\n 'switch',\n 'this',\n 'throw',\n 'true',\n 'try',\n 'typeof',\n 'var',\n 'void',\n 'while',\n 'with',\n 'yield',\n];\n\n/**\n * Checks if a string is a valid identifier name according to ECMASCript specs,\n * and is not one of the forbidden identifier names.\n * https://262.ecma-international.org/13.0/#sec-names-and-keywords\n * @param name - Identifier to check\n * @returns True if the string is a valid identifier name, false otherwise.\n */\nexport function isValidIdentifierName(name: string) {\n return !forbiddenExportNames.includes(name) && variableNameRegex.test(name);\n}\n"]}
|
|
1
|
+
{"version":3,"file":"isValidIdentifierName.js","sourceRoot":"","sources":["../src/isValidIdentifierName.ts"],"names":[],"mappings":"AAAA,8EAA8E;AAC9E,yEAAyE;AACzE,MAAM,iBAAiB,GAAG,4BAA4B,CAAC;AAEvD;;;GAGG;AACH,MAAM,CAAC,MAAM,oBAAoB,GAAG;IAClC,WAAW;IACX,OAAO;IACP,OAAO;IACP,MAAM;IACN,OAAO;IACP,OAAO;IACP,OAAO;IACP,UAAU;IACV,UAAU;IACV,SAAS;IACT,QAAQ;IACR,IAAI;IACJ,MAAM;IACN,MAAM;IACN,MAAM;IACN,QAAQ;IACR,SAAS;IACT,OAAO;IACP,SAAS;IACT,KAAK;IACL,UAAU;IACV,IAAI;IACJ,YAAY;IACZ,QAAQ;IACR,IAAI;IACJ,YAAY;IACZ,WAAW;IACX,KAAK;IACL,KAAK;IACL,MAAM;IACN,SAAS;IACT,SAAS;IACT,WAAW;IACX,QAAQ;IACR,QAAQ;IACR,QAAQ;IACR,OAAO;IACP,QAAQ;IACR,MAAM;IACN,OAAO;IACP,MAAM;IACN,KAAK;IACL,QAAQ;IACR,KAAK;IACL,MAAM;IACN,OAAO;IACP,MAAM;IACN,OAAO;CACR,CAAC;AAEF;;;;;;GAMG;AACH,MAAM,UAAU,qBAAqB,CAAC,IAAY;IAChD,OAAO,CAAC,oBAAoB,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,iBAAiB,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AAC9E,CAAC","sourcesContent":["// A valid variable name must start with a letter, underscore, or dollar sign,\n// and can be followed by letters, numbers, underscores, or dollar signs.\nconst variableNameRegex = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/;\n\n/**\n * Lists all export names which create problems with esm output.\n * Mostly this includes the JavaScript keywords, minus \"default\".\n */\nexport const forbiddenExportNames = [\n 'arguments',\n 'await',\n 'break',\n 'case',\n 'catch',\n 'class',\n 'const',\n 'continue',\n 'debugger',\n 'default',\n 'delete',\n 'do',\n 'else',\n 'enum',\n 'eval',\n 'export',\n 'extends',\n 'false',\n 'finally',\n 'for',\n 'function',\n 'if',\n 'implements',\n 'import',\n 'in',\n 'instanceof',\n 'interface',\n 'let',\n 'new',\n 'null',\n 'package',\n 'private',\n 'protected',\n 'public',\n 'return',\n 'static',\n 'super',\n 'switch',\n 'this',\n 'throw',\n 'true',\n 'try',\n 'typeof',\n 'var',\n 'void',\n 'while',\n 'with',\n 'yield',\n];\n\n/**\n * Checks if a string is a valid identifier name according to ECMASCript specs,\n * and is not one of the forbidden identifier names.\n * https://262.ecma-international.org/13.0/#sec-names-and-keywords\n * @param name - Identifier to check\n * @returns True if the string is a valid identifier name, false otherwise.\n */\nexport function isValidIdentifierName(name: string): boolean {\n return !forbiddenExportNames.includes(name) && variableNameRegex.test(name);\n}\n"]}
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import type { BundleOptions } from '@ms-cloudpack/common-types';
|
|
2
|
-
export type WriteESMStubsOptions = Pick<BundleOptions, 'inputPath' | 'entries'>;
|
|
2
|
+
export type WriteESMStubsOptions = Pick<BundleOptions, 'inputPath' | 'entries' | 'unsafeCjsExportNames'>;
|
|
3
3
|
//# sourceMappingURL=WriteESMStubsOptions.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"WriteESMStubsOptions.d.ts","sourceRoot":"","sources":["../../src/types/WriteESMStubsOptions.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,4BAA4B,CAAC;AAEhE,MAAM,MAAM,oBAAoB,GAAG,IAAI,CAAC,aAAa,EAAE,WAAW,GAAG,SAAS,CAAC,CAAC"}
|
|
1
|
+
{"version":3,"file":"WriteESMStubsOptions.d.ts","sourceRoot":"","sources":["../../src/types/WriteESMStubsOptions.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,4BAA4B,CAAC;AAEhE,MAAM,MAAM,oBAAoB,GAAG,IAAI,CAAC,aAAa,EAAE,WAAW,GAAG,SAAS,GAAG,sBAAsB,CAAC,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"WriteESMStubsOptions.js","sourceRoot":"","sources":["../../src/types/WriteESMStubsOptions.ts"],"names":[],"mappings":"","sourcesContent":["import type { BundleOptions } from '@ms-cloudpack/common-types';\n\nexport type WriteESMStubsOptions = Pick<BundleOptions, 'inputPath' | 'entries'>;\n"]}
|
|
1
|
+
{"version":3,"file":"WriteESMStubsOptions.js","sourceRoot":"","sources":["../../src/types/WriteESMStubsOptions.ts"],"names":[],"mappings":"","sourcesContent":["import type { BundleOptions } from '@ms-cloudpack/common-types';\n\nexport type WriteESMStubsOptions = Pick<BundleOptions, 'inputPath' | 'entries' | 'unsafeCjsExportNames'>;\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"verifyStub.js","sourceRoot":"","sources":["../../src/verifyStub/verifyStub.ts"],"names":[],"mappings":"AAAA,EAAE;AACF,EAAE;AACF,8FAA8F;AAC9F,qDAAqD;AACrD,EAAE;AACF,gDAAgD;AAChD,qFAAqF;AACrF,oEAAoE;AACpE,0EAA0E;AAC1E,EAAE;AACF,EAAE;AAEF,OAAO,EAAE,MAAM,aAAa,CAAC;AAC7B,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,EAAE,aAAa,EAAE,aAAa,EAAE,MAAM,KAAK,C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B,SAAS,GAAG,GAAG,CAAC;IAClB,CAAC,CAAC,CAAC;IACH,MAAM,CAAC,EAAE,CAAC,MAAM,EAAE,CAAC,IAAI,EAAE,EAAE;QACzB,MAAM,IAAI,KAAK,CACb,SAAS;YACP,CAAC,CAAC,+CAA+C,SAAS,CAAC,KAAK,EAAE;YAClE,CAAC,CAAC,wCAAwC,IAAI,IAAI,CAAC,EAAE,CACxD,CAAC;IACJ,CAAC,CAAC,CAAC;IAEH,OAAO,MAAM,CAAC;AAChB,CAAC;AAED;;GAEG;AACH,KAAK,UAAU,aAAa;IAC1B,IAAI,MAAM,EAAE,CAAC;QACX,2FAA2F;QAC3F,MAAM,CAAC,kBAAkB,EAAE,CAAC;QAC5B,IAAI,CAAC;YACH,MAAM,MAAM,CAAC,SAAS,EAAE,CAAC;QAC3B,CAAC;QAAC,MAAM,CAAC;YACP,SAAS;QACX,CAAC;QACD,MAAM,GAAG,SAAS,CAAC;IACrB,CAAC;AACH,CAAC","sourcesContent":["//\n//\n// The utilities in this folder are used to verify that a generated stub file can be imported,\n// and that the results of importing are as expected.\n//\n// Why must the stub file be loaded in a worker?\n// - Ensure that the module is imported in a \"vanilla\" way, without interference from\n// Jest's transpilation, custom module resolution, or environment.\n// - Allow the module to run in a simulated browser environment if needed.\n//\n//\n\nimport fs from 'fs/promises';\nimport path from 'path';\nimport { fileURLToPath, pathToFileURL } from 'url';\nimport { Worker } from 'worker_threads';\nimport type { _VerifyStubOptions } from '../types/VerifyStubOptions.js';\nimport type { LoadStubWorkerRequest, LoadStubWorkerResponse } from './loadStubWorker.js';\n\nconst dirname = path.dirname(fileURLToPath(import.meta.url));\nconst loadStubWorkerPath = path.join(dirname, 'loadStubWorker.js');\n\nlet worker: Worker | undefined;\n\n/**\n * FOR TESTING ONLY:\n * Write a stub file (fixing paths for Windows) and try importing it in a worker,\n * to verify that it's valid and returns the expected exports.\n * @internal\n */\nexport async function _verifyStub(options: _VerifyStubOptions): Promise<{\n /**\n * The result of importing the stub in the worker (if successful), in IPC-friendly format.\n * Any non-IPC-friendly values (such as functions) will be pretty-printed as strings.\n */\n value?: unknown;\n /** Error if unsuccessful. */\n error?: unknown;\n /** Dispose the worker. Unless/until this is called, the same worker will be reused. */\n dispose: () => Promise<void>;\n}> {\n // Write the stub content to disk (fixing paths if needed)\n await writeStubFile(options);\n\n // Try to import the stub in a worker\n worker = ensureWorker();\n const stubImportRequest: LoadStubWorkerRequest = {\n stubPath: getStubImportPath(options.stubPath),\n testCode: options.testCode,\n };\n\n return new Promise((resolve) => {\n const onMessage = (response: LoadStubWorkerResponse) => {\n worker?.off('error', onError);\n resolve({ ...response, dispose: disposeWorker });\n };\n // unhandled exception (not expected)\n const onError = (err: Error) => {\n worker?.off('message', onMessage);\n resolve({ error: err, dispose: disposeWorker });\n };\n worker?.once('message', onMessage);\n worker?.once('error', onError);\n\n worker?.postMessage(stubImportRequest);\n });\n}\n\nasync function writeStubFile(options: _VerifyStubOptions) {\n // Fix the stub content on Windows...\n let stub = options.stub;\n if (process.platform === 'win32') {\n // Node only supports Windows absolute paths as file:// URLs.\n // Unfortunately, the bundlers don't like this syntax, so we don't put it in the stub by default.\n // Convert any absolute paths here so that the worker is able to natively import the stub.\n stub = stub.replace(/(?:from|import) \"([^\"]*?)\";/g, (match, importPath: string) => {\n if (path.isAbsolute(importPath)) {\n return match.replace(importPath, pathToFileURL(importPath).href);\n }\n return match;\n });\n }\n\n // Write the stub file\n await fs.writeFile(options.stubPath, stub);\n}\n\n/** Get a stub import path that works for the current platform. */\nfunction getStubImportPath(stubPath: string): string {\n if (process.platform === 'win32' && path.isAbsolute(stubPath)) {\n // Convert the path to a file:// URL so the import works on Windows\n return pathToFileURL(stubPath).href;\n }\n return stubPath;\n}\n\n/**\n * Initialize the worker used by `verifyStub`. For testing only.\n *\n * (The worker is reused between tests for now because it's expensive to set up and tear down;\n * this can be changed later for certain tests if needed.)\n */\nfunction ensureWorker(): Worker {\n if (worker) return worker;\n\n worker = new Worker(loadStubWorkerPath);\n\n // An 'error' event from a worker means an unhandled exception and it's about to exit.\n // Don't throw an error in this fallback handler in case the error occurred during a call\n // to `verifyStub` (which has its own error handler that can fail the calling test),\n // but save the error to be included in the 'exit' handler's error message.\n let lastError: Error | undefined;\n worker.on('error', (err) => {\n lastError = err;\n });\n worker.on('exit', (code) => {\n throw new Error(\n lastError\n ? `Worker exited due to an uncaught exception: ${lastError.stack}`\n : `Worker exited unexpectedly with code ${code || 0}`,\n );\n });\n\n return worker;\n}\n\n/**\n * Stop the worker used by `verifyStub`.\n */\nasync function disposeWorker() {\n if (worker) {\n // Remove all listeners so that the exit handler set up during initialization doesn't throw\n worker.removeAllListeners();\n try {\n await worker.terminate();\n } catch {\n // ignore\n }\n worker = undefined;\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"verifyStub.js","sourceRoot":"","sources":["../../src/verifyStub/verifyStub.ts"],"names":[],"mappings":"AAAA,EAAE;AACF,EAAE;AACF,8FAA8F;AAC9F,qDAAqD;AACrD,EAAE;AACF,gDAAgD;AAChD,qFAAqF;AACrF,oEAAoE;AACpE,0EAA0E;AAC1E,EAAE;AACF,EAAE;AAEF,OAAO,EAAE,MAAM,aAAa,CAAC;AAC7B,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,EAAE,aAAa,EAAE,aAAa,EAAE,MAAM,KAAK,CAAC;AACnD,OAAO,EAAE,MAAM,EAAE,MAAM,gBAAgB,CAAC;AAIxC,MAAM,OAAO,GAAG,IAAI,CAAC,OAAO,CAAC,aAAa,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC;AAC7D,MAAM,kBAAkB,GAAG,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE,mBAAmB,CAAC,CAAC;AAEnE,IAAI,MAA0B,CAAC;AAE/B;;;;;GAKG;AACH,MAAM,CAAC,KAAK,UAAU,WAAW,CAAC,OAA2B;IAW3D,0DAA0D;IAC1D,MAAM,aAAa,CAAC,OAAO,CAAC,CAAC;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;QAC3F,MAAM,CAAC,kBAAkB,EAAE,CAAC;QAC5B,IAAI,CAAC;YACH,MAAM,MAAM,CAAC,SAAS,EAAE,CAAC;QAC3B,CAAC;QAAC,MAAM,CAAC;YACP,SAAS;QACX,CAAC;QACD,MAAM,GAAG,SAAS,CAAC;IACrB,CAAC;AACH,CAAC","sourcesContent":["//\n//\n// The utilities in this folder are used to verify that a generated stub file can be imported,\n// and that the results of importing are as expected.\n//\n// Why must the stub file be loaded in a worker?\n// - Ensure that the module is imported in a \"vanilla\" way, without interference from\n// Jest's transpilation, custom module resolution, or environment.\n// - Allow the module to run in a simulated browser environment if needed.\n//\n//\n\nimport fs from 'fs/promises';\nimport path from 'path';\nimport { fileURLToPath, pathToFileURL } from 'url';\nimport { Worker } from 'worker_threads';\nimport type { _VerifyStubOptions } from '../types/VerifyStubOptions.js';\nimport type { LoadStubWorkerRequest, LoadStubWorkerResponse } from './loadStubWorker.js';\n\nconst dirname = path.dirname(fileURLToPath(import.meta.url));\nconst loadStubWorkerPath = path.join(dirname, 'loadStubWorker.js');\n\nlet worker: Worker | undefined;\n\n/**\n * FOR TESTING ONLY:\n * Write a stub file (fixing paths for Windows) and try importing it in a worker,\n * to verify that it's valid and returns the expected exports.\n * @internal\n */\nexport async function _verifyStub(options: _VerifyStubOptions): Promise<{\n /**\n * The result of importing the stub in the worker (if successful), in IPC-friendly format.\n * Any non-IPC-friendly values (such as functions) will be pretty-printed as strings.\n */\n value?: unknown;\n /** Error if unsuccessful. */\n error?: unknown;\n /** Dispose the worker. Unless/until this is called, the same worker will be reused. */\n dispose: () => Promise<void>;\n}> {\n // Write the stub content to disk (fixing paths if needed)\n await writeStubFile(options);\n\n // Try to import the stub in a worker\n worker = ensureWorker();\n const stubImportRequest: LoadStubWorkerRequest = {\n stubPath: getStubImportPath(options.stubPath),\n testCode: options.testCode,\n };\n\n return new Promise((resolve) => {\n const onMessage = (response: LoadStubWorkerResponse) => {\n worker?.off('error', onError);\n resolve({ ...response, dispose: disposeWorker });\n };\n // unhandled exception (not expected)\n const onError = (err: Error) => {\n worker?.off('message', onMessage);\n resolve({ error: err, dispose: disposeWorker });\n };\n worker?.once('message', onMessage);\n worker?.once('error', onError);\n\n worker?.postMessage(stubImportRequest);\n });\n}\n\nasync function writeStubFile(options: _VerifyStubOptions): Promise<void> {\n // Fix the stub content on Windows...\n let stub = options.stub;\n if (process.platform === 'win32') {\n // Node only supports Windows absolute paths as file:// URLs.\n // Unfortunately, the bundlers don't like this syntax, so we don't put it in the stub by default.\n // Convert any absolute paths here so that the worker is able to natively import the stub.\n stub = stub.replace(/(?:from|import) \"([^\"]*?)\";/g, (match, importPath: string) => {\n if (path.isAbsolute(importPath)) {\n return match.replace(importPath, pathToFileURL(importPath).href);\n }\n return match;\n });\n }\n\n // Write the stub file\n await fs.writeFile(options.stubPath, stub);\n}\n\n/** Get a stub import path that works for the current platform. */\nfunction getStubImportPath(stubPath: string): string {\n if (process.platform === 'win32' && path.isAbsolute(stubPath)) {\n // Convert the path to a file:// URL so the import works on Windows\n return pathToFileURL(stubPath).href;\n }\n return stubPath;\n}\n\n/**\n * Initialize the worker used by `verifyStub`. For testing only.\n *\n * (The worker is reused between tests for now because it's expensive to set up and tear down;\n * this can be changed later for certain tests if needed.)\n */\nfunction ensureWorker(): Worker {\n if (worker) return worker;\n\n worker = new Worker(loadStubWorkerPath);\n\n // An 'error' event from a worker means an unhandled exception and it's about to exit.\n // Don't throw an error in this fallback handler in case the error occurred during a call\n // to `verifyStub` (which has its own error handler that can fail the calling test),\n // but save the error to be included in the 'exit' handler's error message.\n let lastError: Error | undefined;\n worker.on('error', (err) => {\n lastError = err;\n });\n worker.on('exit', (code) => {\n throw new Error(\n lastError\n ? `Worker exited due to an uncaught exception: ${lastError.stack}`\n : `Worker exited unexpectedly with code ${code || 0}`,\n );\n });\n\n return worker;\n}\n\n/**\n * Stop the worker used by `verifyStub`.\n */\nasync function disposeWorker(): Promise<void> {\n if (worker) {\n // Remove all listeners so that the exit handler set up during initialization doesn't throw\n worker.removeAllListeners();\n try {\n await worker.terminate();\n } catch {\n // ignore\n }\n worker = undefined;\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,CA0C/F"}
|
package/lib/writeESMStubs.js
CHANGED
|
@@ -10,7 +10,7 @@ import { processError } from './processError.js';
|
|
|
10
10
|
* If any files doesn't need a stub, the returned entry will use the original path.
|
|
11
11
|
*/
|
|
12
12
|
export async function writeESMStubs(options) {
|
|
13
|
-
const { inputPath, entries } = options;
|
|
13
|
+
const { inputPath, entries, unsafeCjsExportNames } = options;
|
|
14
14
|
const result = {
|
|
15
15
|
newEntries: {},
|
|
16
16
|
errors: [],
|
|
@@ -27,11 +27,12 @@ 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 ? (unsafeCjsExportNames[entryKey] ?? []) : undefined;
|
|
30
31
|
if (path.extname(entryPath).toLowerCase() === '.json') {
|
|
31
32
|
stubContent = await generateESMStubFromJSON({ filePath });
|
|
32
33
|
}
|
|
33
34
|
else {
|
|
34
|
-
stubContent = generateESMStubFromCJS({ filePath, stubPath });
|
|
35
|
+
stubContent = generateESMStubFromCJS({ filePath, stubPath, namedExports });
|
|
35
36
|
}
|
|
36
37
|
// Attempt to write it to disk.
|
|
37
38
|
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,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,GAAG,OAAO,CAAC;
|
|
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,MAA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,CAAC;iBAAM,CAAC;gBACN,WAAW,GAAG,sBAAsB,CAAC,EAAE,QAAQ,EAAE,QAAQ,EAAE,YAAY,EAAE,CAAC,CAAC;YAC7E,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 = unsafeCjsExportNames ? (unsafeCjsExportNames[entryKey] ?? []) : undefined;\n\n if (path.extname(entryPath).toLowerCase() === '.json') {\n stubContent = await generateESMStubFromJSON({ filePath });\n } else {\n stubContent = 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"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"writeESMStubsInWorker.d.ts","sourceRoot":"","sources":["../src/writeESMStubsInWorker.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,iCAAiC,CAAC;AAC5E,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,gCAAgC,CAAC;AAM1E;;GAEG;AAGH,wBAAsB,qBAAqB,CACzC,OAAO,EAAE,oBAAoB,GAAG;IAAE,UAAU,CAAC,EAAE,MAAM,CAAA;CAAE,GACtD,OAAO,CAAC,mBAAmB,CAAC,
|
|
1
|
+
{"version":3,"file":"writeESMStubsInWorker.d.ts","sourceRoot":"","sources":["../src/writeESMStubsInWorker.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,iCAAiC,CAAC;AAC5E,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,gCAAgC,CAAC;AAM1E;;GAEG;AAGH,wBAAsB,qBAAqB,CACzC,OAAO,EAAE,oBAAoB,GAAG;IAAE,UAAU,CAAC,EAAE,MAAM,CAAA;CAAE,GACtD,OAAO,CAAC,mBAAmB,CAAC,CAwE9B;AAED,wBAAsB,qBAAqB,IAAI,OAAO,CAAC,IAAI,CAAC,CAG3D"}
|
|
@@ -9,7 +9,7 @@ let workerPool;
|
|
|
9
9
|
// This is tested in bundler-rollup/src/writeESMStubsInWorker.test.ts so we can
|
|
10
10
|
// use the compiled version of the workerEntry file.
|
|
11
11
|
export async function writeESMStubsInWorker(options) {
|
|
12
|
-
const { inputPath, entries } = options;
|
|
12
|
+
const { inputPath, entries, unsafeCjsExportNames } = options;
|
|
13
13
|
// Mapping from entry key to entry absolute path
|
|
14
14
|
const nonStubbedEntries = {};
|
|
15
15
|
// We don't want to call into the worker for packages where stubbing is clearly not needed,
|
|
@@ -50,6 +50,7 @@ export async function writeESMStubsInWorker(options) {
|
|
|
50
50
|
const stubOptions = {
|
|
51
51
|
inputPath: options.inputPath,
|
|
52
52
|
entries: entriesToMaybeStub,
|
|
53
|
+
unsafeCjsExportNames,
|
|
53
54
|
};
|
|
54
55
|
try {
|
|
55
56
|
const stubResult = await workerPool.execute({
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"writeESMStubsInWorker.js","sourceRoot":"","sources":["../src/writeESMStubsInWorker.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,2BAA2B,CAAC;AAC/D,OAAO,EAAE,UAAU,EAAE,MAAM,2BAA2B,CAAC;AAGvD,OAAO,EAAE,mBAAmB,EAAE,MAAM,0BAA0B,CAAC;AAC/D,OAAO,IAAI,MAAM,MAAM,CAAC;AAExB,IAAI,UAAkC,CAAC;AAEvC;;GAEG;AACH,+EAA+E;AAC/E,oDAAoD;AACpD,MAAM,CAAC,KAAK,UAAU,qBAAqB,CACzC,OAAuD;IAEvD,MAAM,EAAE,SAAS,EAAE,OAAO,EAAE,GAAG,OAAO,CAAC;
|
|
1
|
+
{"version":3,"file":"writeESMStubsInWorker.js","sourceRoot":"","sources":["../src/writeESMStubsInWorker.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,2BAA2B,CAAC;AAC/D,OAAO,EAAE,UAAU,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AAU,EAAE,OAAO,CAAC,UAAU,IAAI,CAAC;QACnC,2FAA2F;QAC3F,+FAA+F;QAC/F,6FAA6F;QAC7F,2FAA2F;QAC3F,4BAA4B;QAC5B,eAAe,EAAE,CAAC,GAAG,EAAE,GAAG,IAAI;QAC9B,4FAA4F;QAC5F,oFAAoF;QACpF,iGAAiG;QACjG,WAAW,EAAE,GAAG,GAAG,IAAI,GAAG,IAAI;QAC9B,gDAAgD;QAChD,aAAa,EAAE,kBAAkB,CAAC,IAAI,CAAC;KACxC,CAAC,CAAC;IAEH,MAAM,WAAW,GAAyB;QACxC,SAAS,EAAE,OAAO,CAAC,SAAS;QAC5B,OAAO,EAAE,kBAAkB;QAC3B,oBAAoB;KACrB,CAAC;IAEF,IAAI,CAAC;QACH,MAAM,UAAU,GAAG,MAAM,UAAU,CAAC,OAAO,CAAsB;YAC/D,MAAM,EAAE,eAAe;YACvB,IAAI,EAAE,CAAC,WAAW,CAAC;SACpB,CAAC,CAAC;QAEH,iCAAiC;QACjC,MAAM,CAAC,MAAM,CAAC,UAAU,CAAC,UAAU,EAAE,iBAAiB,CAAC,CAAC;QAExD,OAAO,UAAU,CAAC;IACpB,CAAC;IAAC,OAAO,GAAG,EAAE,CAAC;QACb,OAAO;YACL,UAAU,EAAE,EAAE;YACd,MAAM,EAAE;gBACN;oBACE,MAAM,EAAE,qBAAqB;oBAC7B,mEAAmE;oBACnE,IAAI,EAAG,GAAa,EAAE,KAAK,IAAK,GAAa,EAAE,OAAO,IAAI,MAAM,CAAC,GAAG,CAAC;iBACtE;aACF;SACF,CAAC;IACJ,CAAC;AACH,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,qBAAqB;IACzC,MAAM,UAAU,EAAE,OAAO,EAAE,CAAC;IAC5B,UAAU,GAAG,SAAS,CAAC;AACzB,CAAC","sourcesContent":["import { getAdjustedTimeout } from '@ms-cloudpack/environment';\nimport { WorkerPool } from '@ms-cloudpack/worker-pool';\nimport type { WriteESMStubsOptions } from './types/WriteESMStubsOptions.js';\nimport type { WriteESMStubsResult } from './types/WriteESMStubsResult.js';\nimport { supportedExtensions } from './supportedExtensions.js';\nimport path from 'path';\n\nlet workerPool: WorkerPool | undefined;\n\n/**\n * Write ESM stubs using a worker pool.\n */\n// This is tested in bundler-rollup/src/writeESMStubsInWorker.test.ts so we can\n// use the compiled version of the workerEntry file.\nexport async function writeESMStubsInWorker(\n options: WriteESMStubsOptions & { maxWorkers?: number },\n): Promise<WriteESMStubsResult> {\n const { inputPath, entries, unsafeCjsExportNames } = options;\n\n // Mapping from entry key to entry absolute path\n const nonStubbedEntries: WriteESMStubsResult['newEntries'] = {};\n\n // We don't want to call into the worker for packages where stubbing is clearly not needed,\n // such as packages with only TS entries. So filter the entries by whether they potentially\n // need to be stubbed.\n const entriesToMaybeStub: Record<string, string> = {};\n\n for (const [entryKey, entryPath] of Object.entries(entries)) {\n const ext = path.extname(entryPath).toLowerCase();\n if (supportedExtensions.includes(ext)) {\n entriesToMaybeStub[entryKey] = entryPath;\n } else {\n nonStubbedEntries[entryKey] = path.join(inputPath, entryPath);\n }\n }\n\n if (!Object.keys(entriesToMaybeStub).length) {\n // Return early if there's nothing to stub\n return { newEntries: nonStubbedEntries, errors: [] };\n }\n\n workerPool ??= new WorkerPool({\n name: 'ESM stub generation',\n parentPath: import.meta.url,\n entryPath: './worker/workerEntry',\n maxWorkers: options.maxWorkers || 5,\n // Stop inactive workers after 5 minutes. This is intended to prevent one possible cause of\n // OOM crashes if cloudpack start is left running for a long time (though it's much more likely\n // that the issue is caused by servers or file watchers). The timeout is conservative because\n // the fake browser environment has nontrivial startup time, so killing workers too quickly\n // could slow down rebuilds.\n inactiveTimeout: 5 * 60 * 1000,\n // Most stubs seem to stay well under this size, and Node appears to be decent about freeing\n // memory after writeESMStubs finishes, but this will catch anything giant or leaky.\n // (This is only checked after task completion, so a package exceeding it will still be stubbed.)\n maxHeapSize: 500 * 1024 * 1024,\n // console.warn if a stub takes longer than this\n warnAboveTime: getAdjustedTimeout(2000),\n });\n\n const stubOptions: WriteESMStubsOptions = {\n inputPath: options.inputPath,\n entries: entriesToMaybeStub,\n unsafeCjsExportNames,\n };\n\n try {\n const stubResult = await workerPool.execute<WriteESMStubsResult>({\n method: 'writeESMStubs',\n args: [stubOptions],\n });\n\n // Add in any non-stubbed entries\n Object.assign(stubResult.newEntries, nonStubbedEntries);\n\n return stubResult;\n } catch (err) {\n return {\n newEntries: {},\n errors: [\n {\n source: 'ESM stub generation',\n // note: err won't be an instance of Error because of serialization\n text: (err as Error)?.stack || (err as Error)?.message || String(err),\n },\n ],\n };\n }\n}\n\nexport async function disposeStubWorkerPool(): Promise<void> {\n await workerPool?.dispose();\n workerPool = undefined;\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.14.0",
|
|
4
4
|
"description": "Generates ESM stubs for CommonJS entry files.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -14,10 +14,10 @@
|
|
|
14
14
|
}
|
|
15
15
|
},
|
|
16
16
|
"dependencies": {
|
|
17
|
-
"@ms-cloudpack/common-types": "^0.
|
|
17
|
+
"@ms-cloudpack/common-types": "^0.24.0",
|
|
18
18
|
"@ms-cloudpack/environment": "^0.1.1",
|
|
19
|
-
"@ms-cloudpack/json-utilities": "^0.1.
|
|
20
|
-
"@ms-cloudpack/package-utilities": "^11.
|
|
19
|
+
"@ms-cloudpack/json-utilities": "^0.1.10",
|
|
20
|
+
"@ms-cloudpack/package-utilities": "^11.1.0",
|
|
21
21
|
"@ms-cloudpack/path-string-parsing": "^1.2.6",
|
|
22
22
|
"@ms-cloudpack/worker-pool": "^0.3.2",
|
|
23
23
|
"jsdom": "^24.0.0",
|