Socket
Socket
Sign inDemoInstall

@chialab/cjs-to-esm

Package Overview
Dependencies
3
Maintainers
2
Versions
78
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.18.0-alpha.0 to 0.18.0-alpha.1

45

lib/index.js
import { TokenType, parse, walk, getBlock, getStatement, parseCommonjs, parseEsm, createEmptySourcemapComment } from '@chialab/estransform';
import { detectUmdGlobalVariable } from './umd.js';
export { detectUmdGlobalVariable };
export const REQUIRE_REGEX = /([^.\w$]|^)require\s*\((['"])(.*?)\2\)/g;
export const UMD_REGEXES = [
/\btypeof\s+(?:module\.)?exports\s*===?\s*['|"]object['|"]/,
/\btypeof\s+(module\.exports|module|exports)\s*===?\s*['|"]object['|"]/,
/['|"]object['|"]\s*===?\s*typeof\s+(module\.exports|module|exports)/,
/\btypeof\s+define\s*===?\s*['|"]function['|"]/,
/['|"]function['|"]\s*===?\s*typeof\s+define/,
];
export const UMD_GLOBALS = ['globalThis', 'global', 'self', 'window'];
export const UMD_GLOBALS_REGEXES = UMD_GLOBALS.map((varName) => new RegExp(`\\btypeof\\s+(${varName})\\s*!==?\\s*['|"]undefined['|"]`));
export const ESM_KEYWORDS = /((?:^\s*|;\s*)(\bimport\s*(\{.*?\}\s*from|\s[\w$]+\s+from|\*\s*as\s+[^\s]+\s+from)?\s*['"])|((?:^\s*|;\s*)export(\s+(default|const|var|let|function|class)[^\w$]|\s*\{)))/m;

@@ -185,6 +184,6 @@ export const EXPORTS_KEYWORDS = /\b(module\.exports\b|exports\b)/;

const { helpers, processor } = await parse(code, source);
const globalVariable = UMD_REGEXES.every((regex) => regex.test(code)) && detectUmdGlobalVariable(processor);
const isUmd = UMD_REGEXES.some((regex) => regex.test(code));
let insertHelper = false;
if (!globalVariable) {
if (!isUmd) {
/**

@@ -257,3 +256,3 @@ * @type {*[]}

if (globalVariable) {
if (isUmd) {
let endDefinition = code.indexOf('\'use strict\';');

@@ -268,9 +267,29 @@ if (endDefinition === -1) {

helpers.prepend(`var __umdGlobal = ${GLOBAL_HELPER};
var __umdKeys = Object.keys(__umdGlobal);
(function(window, global, globalThis, self, module, exports) {
var __umdExports = [];
var __umdRoot = new Proxy(__umdGlobal, {
get: function(target, name) {
var value = Reflect.get(target, name);
if (__umdExports.indexOf(name) !== -1) {
return value;
}
if (typeof value === 'function' && !value.prototype) {
return value.bind(__umdGlobal);
}
return value;
},
set: function(target, name, value) {
__umdExports.push(name);
return Reflect.set(target, name, value);
},
});
var __umdFunction = function ProxyFunction(code) {
return __umdGlobal.Function(code).bind(__umdRoot);
};
__umdFunction.prototype = Function.prototype;
(function(window, global, globalThis, self, module, exports, Function) {
`);
helpers.append(`
}).call(__umdGlobal, __umdGlobal, __umdGlobal, __umdGlobal, __umdGlobal, undefined, undefined);
}).call(__umdRoot, __umdRoot, __umdRoot, __umdRoot, __umdRoot, undefined, undefined, __umdFunction);
export default __umdGlobal['${globalVariable}'];`);
export default (__umdExports.length !== 1 && __umdRoot[__umdExports[0]] !== __umdRoot[__umdExports[1]] ? __umdRoot : __umdRoot[__umdExports[0]]);`);

@@ -312,3 +331,3 @@ // replace the usage of `this` as global object because is not supported in esm

if (isEsModule) {
if (!globalVariable && (hasDefault || named.length === 0)) {
if (!isUmd && (hasDefault || named.length === 0)) {
helpers.append('\nexport default (module.exports != null && typeof module.exports === \'object\' && \'default\' in module.exports ? module.exports.default : module.exports);');

@@ -324,3 +343,3 @@ }

} else if (EXPORTS_KEYWORDS.test(code)) {
helpers.prepend(`var global = globalThis;
helpers.prepend(`var global = ${GLOBAL_HELPER};
var exports = {};

@@ -327,0 +346,0 @@ var module = {

{
"name": "@chialab/cjs-to-esm",
"type": "module",
"version": "0.18.0-alpha.0",
"version": "0.18.0-alpha.1",
"description": "A commonjs to esm converter.",

@@ -30,7 +30,8 @@ "main": "lib/index.js",

"dependencies": {
"@chialab/estransform": "^0.18.0-alpha.0"
"@chialab/estransform": "^0.18.0-alpha.1"
},
"devDependencies": {
"typescript": "^4.3.0"
"jsdom": "^21.0.0",
"typescript": "^5.0.0"
}
}

@@ -14,6 +14,9 @@ <p align="center">

```sh
$ npm i @chialab/cjs-to-esm -D
$ yarn add @chialab/cjs-to-esm -D
npm i @chialab/cjs-to-esm -D
```
```sh
yarn add @chialab/cjs-to-esm -D
```
## Usage

@@ -20,0 +23,0 @@

@@ -33,11 +33,3 @@ /**

code: string;
map: {
version: number;
sources: string[];
names: string[];
sourceRoot?: string | undefined;
sourcesContent?: string[] | undefined;
mappings: string;
file?: string | undefined;
} | import("magic-string").SourceMap | null;
map: any;
} | undefined>;

@@ -55,17 +47,7 @@ /**

code: string;
map: {
version: number;
sources: string[];
names: string[];
sourceRoot?: string | undefined;
sourcesContent?: string[] | undefined;
mappings: string;
file?: string | undefined;
} | import("magic-string").SourceMap | null;
map: any;
} | undefined>;
export { detectUmdGlobalVariable };
export const REQUIRE_REGEX: RegExp;
export const UMD_REGEXES: RegExp[];
export const UMD_GLOBALS: string[];
export const UMD_GLOBALS_REGEXES: RegExp[];
export const ESM_KEYWORDS: RegExp;

@@ -78,3 +60,3 @@ export const EXPORTS_KEYWORDS: RegExp;

export const GLOBAL_HELPER: "((typeof window !== 'undefined' && window) ||\n(typeof self !== 'undefined' && self) ||\n(typeof global !== 'undefined' && global) ||\n(typeof globalThis !== 'undefined' && globalThis) ||\n{})";
export const REQUIRE_HELPER: string;
export const REQUIRE_HELPER: "function __cjs_default__(requiredModule) {\n var Object = ((typeof window !== 'undefined' && window) ||\n(typeof self !== 'undefined' && self) ||\n(typeof global !== 'undefined' && global) ||\n(typeof globalThis !== 'undefined' && globalThis) ||\n{}).Object;\n var isEsModule = false;\n var specifiers = Object.create(null);\n var hasNamedExports = false;\n var hasDefaultExport = false;\n\n Object.defineProperty(specifiers, '__esModule', {\n value: true,\n enumerable: false,\n configurable: true,\n });\n\n if (requiredModule) {\n var names = Object.getOwnPropertyNames(requiredModule);;\n names.forEach(function(k) {\n if (k === 'default') {\n hasDefaultExport = true;\n } else if (!hasNamedExports && k != '__esModule') {\n try {\n hasNamedExports = requiredModule[k] != null;\n } catch (err) {\n //\n }\n }\n Object.defineProperty(specifiers, k, {\n get: function () {\n return requiredModule[k];\n },\n enumerable: true,\n configurable: false,\n });\n });\n if (Object.getOwnPropertySymbols) {\n var symbols = Object.getOwnPropertySymbols(requiredModule);\n symbols.forEach(function(k) {\n Object.defineProperty(specifiers, k, {\n get: function () {\n return requiredModule[k];\n },\n enumerable: false,\n configurable: false,\n });\n });\n }\n\n Object.preventExtensions(specifiers);\n Object.seal(specifiers);\n if (Object.freeze) {\n Object.freeze(specifiers);\n }\n }\n\n if (hasNamedExports) {\n return specifiers;\n }\n\n if (hasDefaultExport) {\n if (Object.isExtensible(specifiers.default) && !('default' in specifiers.default)) {\n Object.defineProperty(specifiers.default, 'default', {\n value: specifiers.default,\n configurable: false,\n enumerable: false,\n })\n }\n return specifiers.default;\n }\n\n return specifiers;\n}\n";
export type IgnoreCallback = (specifier: string) => boolean | Promise<boolean>;

@@ -89,2 +71,1 @@ export type TransformOptions = {

};
import { detectUmdGlobalVariable } from "./umd.js";
SocketSocket SOC 2 Logo

Product

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc