New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

cjs-esm

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cjs-esm - npm Package Compare versions

Comparing version 0.4.2 to 0.4.3

9

dist/generate-export.d.ts
import { Analyzed } from './analyze';
import { AcornNode } from './types';
export interface ExportsRuntime {
exportDefault?: {
nodes: AcornNode[];
name: string;
statement: string;
};
exportMembers?: string;
polyfill: string;
exportDeclaration: string;
}
export declare function generateExport(analyzed: Analyzed): ExportsRuntime | null;

@@ -8,33 +8,27 @@ "use strict";

}
let exportDefault;
const moduleExports = analyzed.exports
.filter(exp => exp.token.left === 'module')
.map(exp => exp.node);
if (moduleExports.length) {
const name = '__CJS__export_default__';
exportDefault = {
nodes: moduleExports,
name,
statement: `export { ${name} as default }`
};
}
const memberDefault = analyzed.exports
// Find `module.exports` or `exports.default`
.find(exp => exp.token.left === 'module' || exp.token.right === 'default');
let members = analyzed.exports
.map(exp => exp.token.right)
.filter(member => member !== 'exports')
.filter(member => member !== 'default');
// Exclude `module.exports` and `exports.default`
.filter(exp => exp.token.left !== 'module' && exp.token.right !== 'default')
.map(exp => exp.token.right);
// Remove duplicate export
members = [...new Set(members)];
const membersDeclaration = members
.map(m => `const __CJS__export_${m}__ = (module.exports == null ? {} : module.exports).${m};`);
const exportMembers = `
${membersDeclaration.join('\n')}
const membersDeclaration = members.map(m => `const __CJS__export_${m}__ = (module.exports == null ? {} : module.exports).${m}`);
const membersExport = members.map(m => `__CJS__export_${m}__ as ${m}`);
if (memberDefault) {
membersDeclaration.unshift(`const __CJS__export_default__ = (module.exports == null ? {} : module.exports).default || module.exports`);
membersExport.unshift('__CJS__export_default__ as default');
}
return {
polyfill: 'const module = { exports: {} }; const exports = module.exports;',
exportDeclaration: `
${membersDeclaration.join(';\n')};
export {
${members.map(m => `__CJS__export_${m}__ as ${m}`).join(',\n ')}
${membersExport.join(',\n ')},
}
`.trim();
return {
exportDefault,
exportMembers,
`.trim(),
};
}
exports.generateExport = generateExport;

@@ -5,12 +5,15 @@ import { Analyzed, RequireStatement } from './analyze';

* At present, the `require` is divided into two cases
* 目前,将 require 分为两种情况
* 目前,将 require 分为三种情况
*
* ①:
* At top level scope and can be converted into an `import` (🎯-①)
* 在顶级作用域,并且可以转换成 import
*
* At non top level scope, the `require` will be promoted
* 不在顶级作用域,require 将会被提升
* ②:
* At top level scope, but cannot be converted directly, the `require` will be promoted
* 在顶级作用域,但不能直接转换,require 将会被提升
*
* At non top level scope and in the function scope, tt will be converted into `import()` (🚧-①: 🐞)
* 不在顶级作用域在函数作用域中,require 将会转换成 import()
* ③:
* In a block level scope or function scope, tt will be converted into `import()` (🚧-①: 🐞)
* 在块级作用域或函数作用域中,require 将会转换成 import()
*/

@@ -17,0 +20,0 @@ export interface ImportRecord {

@@ -10,3 +10,2 @@ "use strict";

function cjs2esm(code) {
var _a;
if (!(0, utils_1.isCommonjs)(code)) {

@@ -54,17 +53,13 @@ return { code };

if (exportRuntime) {
let moduleRuntime = 'const module = { exports: {} }; const exports = module.exports;';
if (exportRuntime.exportDefault) {
const { nodes, name } = exportRuntime.exportDefault;
moduleRuntime += ` let ${name} = undefined;`;
for (const node of nodes) {
ms.appendLeft(node.start, `${name} = `);
}
}
const polyfill = ['/* export-runtime-S */', moduleRuntime, '/* export-runtime-E */'].join(' ');
const polyfill = [
'/* export-runtime-S */',
exportRuntime.polyfill,
'/* export-runtime-E */',
].join(' ');
const _exports = [
'/* export-statement-S */',
(_a = exportRuntime.exportDefault) === null || _a === void 0 ? void 0 : _a.statement,
exportRuntime.exportMembers,
exportRuntime.exportDeclaration,
'/* export-statement-E */',
].filter(Boolean).join('\n');
].filter(Boolean)
.join('\n');
ms.prepend(polyfill).append(_exports);

@@ -71,0 +66,0 @@ }

{
"name": "cjs-esm",
"version": "0.4.2",
"version": "0.4.3",
"description": "Another CommonJs transform ESModule lib.",

@@ -5,0 +5,0 @@ "main": "dist/index.js",

@@ -30,3 +30,3 @@ # cjs-esm

function load(path) {
import/*🚧-🐞*/(path);
import/*🚧-🐞*/(path).then(m => m.default || m);
}

@@ -33,0 +33,0 @@ ```

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc