@ms-cloudpack/esm-stub-utilities 0.6.8 → 0.6.9

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.
@@ -20,7 +20,7 @@ export async function createESMStub(entryPath, stubPath) {
20
20
  `const data = ${JSON.stringify(jsonData, null, 2)};`,
21
21
  'export default data;',
22
22
  ...Object.keys(jsonData)
23
- .filter((name) => !forbiddenExportNames.includes(name))
23
+ .filter((name) => !forbiddenExportNames.includes(name) && !name.includes('-'))
24
24
  .map((prop) => `export const ${prop} = data.${prop};`),
25
25
  ].join('\n');
26
26
  }
@@ -1 +1 @@
1
- {"version":3,"file":"createESMStub.js","sourceRoot":"","sources":["../src/createESMStub.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,EAAE,QAAQ,EAAE,MAAM,8BAA8B,CAAC;AACxD,OAAO,EAAE,KAAK,EAAE,MAAM,mCAAmC,CAAC;AAC1D,OAAO,EAAE,oBAAoB,EAAE,MAAM,2BAA2B,CAAC;AACjE,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AAEjD;;;;;;GAMG;AACH,MAAM,CAAC,KAAK,UAAU,aAAa,CAAC,SAAiB,EAAE,QAAgB;IACrE,IAAI,IAAI,CAAC,OAAO,CAAC,SAAS,CAAC,KAAK,OAAO,EAAE;QACvC,uDAAuD;QACvD,0GAA0G;QAC1G,+DAA+D;QAC/D,MAAM,QAAQ,GAAG,CAAC,MAAM,QAAQ,CAAC,SAAS,CAAC,CAA4B,CAAC;QACxE,OAAO;YACL,gBAAgB,IAAI,CAAC,SAAS,CAAC,QAAQ,EAAE,IAAI,EAAE,CAAC,CAAC,GAAG;YACpD,sBAAsB;YACtB,GAAG,MAAM,CAAC,IAAI,CAAC,QAAQ,CAAC;iBACrB,MAAM,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,oBAAoB,CAAC,QAAQ,CAAC,IAAI,CAAC,C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,CAAC;gBACF,MAAM,CAAC,IAAI,CAAC,+BAA+B,CAAC,CAAC;aAC9C;iBAAM;gBACL,MAAM,CAAC,IAAI,CAAC,+BAA+B,CAAC,CAAC;aAC9C;YAED,IAAI,YAAY,CAAC,MAAM,EAAE;gBACvB,MAAM,CAAC,IAAI,CAAC,YAAY,YAAY,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;aACtD;SACF;aAAM;YACL,sBAAsB;YACtB,wFAAwF;YACxF,IAAI,gBAAgB,EAAE;gBACpB,MAAM,CAAC,IAAI,CAAC,4BAA4B,YAAY,IAAI,CAAC,CAAC;aAC3D;iBAAM;gBACL,MAAM,CAAC,IAAI,CAAC,WAAW,YAAY,IAAI,CAAC,CAAC;aAC1C;SACF;KACF;SAAM,IAAI,OAAO,aAAa,KAAK,SAAS,IAAI,OAAO,aAAa,KAAK,QAAQ,EAAE;QAClF,qBAAqB;QACrB,MAAM,CAAC,IAAI,CAAC,kBAAkB,aAAa,GAAG,CAAC,CAAC;KACjD;SAAM,IAAI,OAAO,aAAa,KAAK,QAAQ,EAAE;QAC5C,qBAAqB;QACrB,MAAM,CAAC,IAAI,CAAC,kBAAkB,IAAI,CAAC,SAAS,CAAC,aAAa,CAAC,GAAG,CAAC,CAAC;KACjE;SAAM,IAAI,KAAK,CAAC,OAAO,CAAC,aAAa,CAAC,EAAE;QACvC,MAAM,CAAC,IAAI,CAAC,8BAA8B,YAAY,IAAI,CAAC,CAAC;QAC5D,MAAM,CAAC,IAAI,CAAC,+BAA+B,CAAC,CAAC;KAC9C;SAAM;QACL,iFAAiF;QACjF,MAAM,CAAC,IAAI,CAAC,WAAW,YAAY,IAAI,CAAC,CAAC;KAC1C;IAED,OAAO,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AAC3B,CAAC","sourcesContent":["import path from 'path';\nimport { readJson } from '@ms-cloudpack/json-utilities';\nimport { slash } from '@ms-cloudpack/path-string-parsing';\nimport { forbiddenExportNames } from './forbiddenExportNames.js';\nimport { runInSandbox } from './runInSandbox.js';\n\n/**\n * Generates an ESM stub for CommonJS modules.\n * @param entryPath - The full path to the CommonJS entry file.\n * @param stubPath - The path where the stub file will live. This is important for determining\n * relative import paths.\n * @param packageExport - The exported package.\n */\nexport async function createESMStub(entryPath: string, stubPath: string): Promise<string> {\n if (path.extname(entryPath) === '.json') {\n // It's not necessary to run JSON files in the sandbox.\n // Also, `import`-ing named keys from JSON files doesn't work, so we just inline the entire file contents.\n // (More info: https://github.com/microsoft/cloudpack/pull/880)\n const jsonData = (await readJson(entryPath)) as Record<string, unknown>;\n return [\n `const data = ${JSON.stringify(jsonData, null, 2)};`,\n 'export default data;',\n ...Object.keys(jsonData)\n .filter((name) => !forbiddenExportNames.includes(name))\n .map((prop) => `export const ${prop} = data.${prop};`),\n ].join('\\n');\n }\n\n const result = [];\n const relativePath = './' + slash(path.relative(path.dirname(stubPath), entryPath));\n\n // Run the entry point file in a sandbox to determine the exports\n const packageExport = (await runInSandbox(entryPath)) as Record<string, unknown>;\n\n const isExportObject = typeof packageExport === 'object';\n const isExportFunction = typeof packageExport === 'function';\n\n if (isExportFunction || (isExportObject && !Array.isArray(packageExport))) {\n // Make sure to filter keywords.\n const namedExports = Object.keys(packageExport).filter((name) => !forbiddenExportNames.includes(name));\n const hasDefaultExport = 'default' in packageExport;\n\n if (namedExports.length || hasDefaultExport) {\n result.push(`import ${isExportFunction ? '' : '* as '}packageExport from \"${relativePath}\";`);\n\n if (namedExports.length) {\n result.push(`const { ${namedExports.join(', ')} } = packageExport;`);\n }\n\n if (hasDefaultExport) {\n result.push(\n `const defaultExport = (packageExport && packageExport.default?.default) ? packageExport?.default.default : packageExport?.default;`,\n );\n result.push(`export default defaultExport;`);\n } else {\n result.push(`export default packageExport;`);\n }\n\n if (namedExports.length) {\n result.push(`export { ${namedExports.join(', ')} }`);\n }\n } else {\n // No named exports...\n // Object with no members? Must be a polyfill. Just import it and don't export anything.\n if (isExportFunction) {\n result.push(`export { default } from \"${relativePath}\";`);\n } else {\n result.push(`import \"${relativePath}\";`);\n }\n }\n } else if (typeof packageExport === 'boolean' || typeof packageExport === 'number') {\n // Export as default.\n result.push(`export default ${packageExport};`);\n } else if (typeof packageExport === 'string') {\n // Export a constant.\n result.push(`export default ${JSON.stringify(packageExport)};`);\n } else if (Array.isArray(packageExport)) {\n result.push(`import packageExport from \"${relativePath}\";`);\n result.push('export default packageExport;');\n } else {\n // Unable to import the module in node, or it exported undefined for some reason.\n result.push(`import \"${relativePath}\";`);\n }\n\n return result.join('\\n');\n}\n"]}
1
+ {"version":3,"file":"createESMStub.js","sourceRoot":"","sources":["../src/createESMStub.ts"],"names":[],"mappings":"AAAA,OAAO,IAAI,MAAM,MAAM,CAAC;AACxB,OAAO,EAAE,QAAQ,EAAE,MAAM,8BAA8B,CAAC;AACxD,OAAO,EAAE,KAAK,EAAE,MAAM,mCAAmC,CAAC;AAC1D,OAAO,EAAE,oBAAoB,EAAE,MAAM,2BAA2B,CAAC;AACjE,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AAEjD;;;;;;GAMG;AACH,MAAM,CAAC,KAAK,UAAU,aAAa,CAAC,SAAiB,EAAE,QAAgB;IACrE,IAAI,IAAI,CAAC,OAAO,CAAC,SAAS,CAAC,KAAK,OAAO,EAAE;QACvC,uDAAuD;QACvD,0GAA0G;QAC1G,+DAA+D;QAC/D,MAAM,QAAQ,GAAG,CAAC,MAAM,QAAQ,CAAC,SAAS,CAAC,CAA4B,CAAC;QACxE,OAAO;YACL,gBAAgB,IAAI,CAAC,SAAS,CAAC,QAAQ,EAAE,IAAI,EAAE,CAAC,CAAC,GAAG;YACpD,sBAAsB;YACtB,GAAG,MAAM,CAAC,IAAI,CAAC,QAAQ,CAAC;iBACrB,MAAM,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,oBAAoB,CAAC,QAAQ,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,GAAG,C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I,CACrI,CAAC;gBACF,MAAM,CAAC,IAAI,CAAC,+BAA+B,CAAC,CAAC;aAC9C;iBAAM;gBACL,MAAM,CAAC,IAAI,CAAC,+BAA+B,CAAC,CAAC;aAC9C;YAED,IAAI,YAAY,CAAC,MAAM,EAAE;gBACvB,MAAM,CAAC,IAAI,CAAC,YAAY,YAAY,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;aACtD;SACF;aAAM;YACL,sBAAsB;YACtB,wFAAwF;YACxF,IAAI,gBAAgB,EAAE;gBACpB,MAAM,CAAC,IAAI,CAAC,4BAA4B,YAAY,IAAI,CAAC,CAAC;aAC3D;iBAAM;gBACL,MAAM,CAAC,IAAI,CAAC,WAAW,YAAY,IAAI,CAAC,CAAC;aAC1C;SACF;KACF;SAAM,IAAI,OAAO,aAAa,KAAK,SAAS,IAAI,OAAO,aAAa,KAAK,QAAQ,EAAE;QAClF,qBAAqB;QACrB,MAAM,CAAC,IAAI,CAAC,kBAAkB,aAAa,GAAG,CAAC,CAAC;KACjD;SAAM,IAAI,OAAO,aAAa,KAAK,QAAQ,EAAE;QAC5C,qBAAqB;QACrB,MAAM,CAAC,IAAI,CAAC,kBAAkB,IAAI,CAAC,SAAS,CAAC,aAAa,CAAC,GAAG,CAAC,CAAC;KACjE;SAAM,IAAI,KAAK,CAAC,OAAO,CAAC,aAAa,CAAC,EAAE;QACvC,MAAM,CAAC,IAAI,CAAC,8BAA8B,YAAY,IAAI,CAAC,CAAC;QAC5D,MAAM,CAAC,IAAI,CAAC,+BAA+B,CAAC,CAAC;KAC9C;SAAM;QACL,iFAAiF;QACjF,MAAM,CAAC,IAAI,CAAC,WAAW,YAAY,IAAI,CAAC,CAAC;KAC1C;IAED,OAAO,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AAC3B,CAAC","sourcesContent":["import path from 'path';\nimport { readJson } from '@ms-cloudpack/json-utilities';\nimport { slash } from '@ms-cloudpack/path-string-parsing';\nimport { forbiddenExportNames } from './forbiddenExportNames.js';\nimport { runInSandbox } from './runInSandbox.js';\n\n/**\n * Generates an ESM stub for CommonJS modules.\n * @param entryPath - The full path to the CommonJS entry file.\n * @param stubPath - The path where the stub file will live. This is important for determining\n * relative import paths.\n * @param packageExport - The exported package.\n */\nexport async function createESMStub(entryPath: string, stubPath: string): Promise<string> {\n if (path.extname(entryPath) === '.json') {\n // It's not necessary to run JSON files in the sandbox.\n // Also, `import`-ing named keys from JSON files doesn't work, so we just inline the entire file contents.\n // (More info: https://github.com/microsoft/cloudpack/pull/880)\n const jsonData = (await readJson(entryPath)) as Record<string, unknown>;\n return [\n `const data = ${JSON.stringify(jsonData, null, 2)};`,\n 'export default data;',\n ...Object.keys(jsonData)\n .filter((name) => !forbiddenExportNames.includes(name) && !name.includes('-'))\n .map((prop) => `export const ${prop} = data.${prop};`),\n ].join('\\n');\n }\n\n const result = [];\n const relativePath = './' + slash(path.relative(path.dirname(stubPath), entryPath));\n\n // Run the entry point file in a sandbox to determine the exports\n const packageExport = (await runInSandbox(entryPath)) as Record<string, unknown>;\n\n const isExportObject = typeof packageExport === 'object';\n const isExportFunction = typeof packageExport === 'function';\n\n if (isExportFunction || (isExportObject && !Array.isArray(packageExport))) {\n // Make sure to filter keywords.\n const namedExports = Object.keys(packageExport).filter((name) => !forbiddenExportNames.includes(name));\n const hasDefaultExport = 'default' in packageExport;\n\n if (namedExports.length || hasDefaultExport) {\n result.push(`import ${isExportFunction ? '' : '* as '}packageExport from \"${relativePath}\";`);\n\n if (namedExports.length) {\n result.push(`const { ${namedExports.join(', ')} } = packageExport;`);\n }\n\n if (hasDefaultExport) {\n result.push(\n `const defaultExport = (packageExport && packageExport.default?.default) ? packageExport?.default.default : packageExport?.default;`,\n );\n result.push(`export default defaultExport;`);\n } else {\n result.push(`export default packageExport;`);\n }\n\n if (namedExports.length) {\n result.push(`export { ${namedExports.join(', ')} }`);\n }\n } else {\n // No named exports...\n // Object with no members? Must be a polyfill. Just import it and don't export anything.\n if (isExportFunction) {\n result.push(`export { default } from \"${relativePath}\";`);\n } else {\n result.push(`import \"${relativePath}\";`);\n }\n }\n } else if (typeof packageExport === 'boolean' || typeof packageExport === 'number') {\n // Export as default.\n result.push(`export default ${packageExport};`);\n } else if (typeof packageExport === 'string') {\n // Export a constant.\n result.push(`export default ${JSON.stringify(packageExport)};`);\n } else if (Array.isArray(packageExport)) {\n result.push(`import packageExport from \"${relativePath}\";`);\n result.push('export default packageExport;');\n } else {\n // Unable to import the module in node, or it exported undefined for some reason.\n result.push(`import \"${relativePath}\";`);\n }\n\n return result.join('\\n');\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ms-cloudpack/esm-stub-utilities",
3
- "version": "0.6.8",
3
+ "version": "0.6.9",
4
4
  "description": "Generates ESM stubs for CommonJS entry files.",
5
5
  "license": "MIT",
6
6
  "type": "module",