🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
Book a DemoInstallSign in
Socket

@babel/plugin-transform-modules-commonjs

Package Overview
Dependencies
Maintainers
4
Versions
110
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@babel/plugin-transform-modules-commonjs - npm Package Compare versions

Comparing version

to
7.21.5

lib/dynamic-import.js

95

lib/index.js

@@ -7,22 +7,13 @@ "use strict";

exports.default = void 0;
var _helperPluginUtils = require("@babel/helper-plugin-utils");
var _helperModuleTransforms = require("@babel/helper-module-transforms");
var _helperSimpleAccess = _interopRequireDefault(require("@babel/helper-simple-access"));
var _helperSimpleAccess = require("@babel/helper-simple-access");
var _core = require("@babel/core");
var _utils = require("babel-plugin-dynamic-import-node/utils");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var _dynamicImport = require("./dynamic-import");
var _default = (0, _helperPluginUtils.declare)((api, options) => {
var _api$assumption, _api$assumption2, _api$assumption3;
api.assertVersion(7);
const transformImportCall = (0, _utils.createDynamicImportTransform)(api);
const {
loose,
strictNamespace = false,
mjsStrictNamespace = true,
mjsStrictNamespace = strictNamespace,
allowTopLevelThis,

@@ -32,18 +23,19 @@ strict,

noInterop,
importInterop,
lazy = false,
allowCommonJSExports = true
allowCommonJSExports = true,
loose = false
} = options;
const constantReexports = (_api$assumption = api.assumption("constantReexports")) != null ? _api$assumption : loose;
const enumerableModuleMeta = (_api$assumption2 = api.assumption("enumerableModuleMeta")) != null ? _api$assumption2 : loose;
const noIncompleteNsImportDetection = (_api$assumption3 = api.assumption("noIncompleteNsImportDetection")) != null ? _api$assumption3 : false;
if (typeof lazy !== "boolean" && typeof lazy !== "function" && (!Array.isArray(lazy) || !lazy.every(item => typeof item === "string"))) {
throw new Error(`.lazy must be a boolean, array of strings, or a function`);
}
if (typeof strictNamespace !== "boolean") {
throw new Error(`.strictNamespace must be a boolean, or undefined`);
}
if (typeof mjsStrictNamespace !== "boolean") {
throw new Error(`.mjsStrictNamespace must be a boolean, or undefined`);
}
const getAssertion = localName => _core.template.expression.ast`

@@ -57,3 +49,2 @@ (function(){

`;
const moduleExportsVisitor = {

@@ -65,3 +56,2 @@ ReferencedIdentifier(path) {

const rootBinding = this.scope.getBinding(localName);
if (rootBinding !== localBinding || path.parentPath.isObjectProperty({

@@ -76,11 +66,18 @@ value: path.node

}
path.replaceWith(getAssertion(localName));
},
UpdateExpression(path) {
const arg = path.get("argument");
if (!arg.isIdentifier()) return;
const localName = arg.node.name;
if (localName !== "module" && localName !== "exports") return;
const localBinding = path.scope.getBinding(localName);
const rootBinding = this.scope.getBinding(localName);
if (rootBinding !== localBinding) return;
path.replaceWith(_core.types.assignmentExpression(path.node.operator[0] + "=", arg.node, getAssertion(localName)));
},
AssignmentExpression(path) {
const left = path.get("left");
if (left.isIdentifier()) {
const localName = path.node.name;
const localName = left.node.name;
if (localName !== "module" && localName !== "exports") return;

@@ -98,3 +95,2 @@ const localBinding = path.scope.getBinding(localName);

})[0];
if (localName) {

@@ -106,26 +102,20 @@ const right = path.get("right");

}
};
return {
name: "transform-modules-commonjs",
pre() {
this.file.set("@babel/plugin-transform-modules-*", "commonjs");
},
visitor: {
CallExpression(path) {
if (!this.file.has("@babel/plugin-proposal-dynamic-import")) return;
if (!path.get("callee").isImport()) return;
if (!_core.types.isImport(path.node.callee)) return;
let {
scope
} = path;
do {
scope.rename("require");
} while (scope = scope.parent);
transformImportCall(this, path.get("callee"));
(0, _dynamicImport.transformDynamicImport)(path, noInterop, this.file);
},
Program: {

@@ -139,5 +129,6 @@ exit(path, state) {

path.scope.rename("__dirname");
if (!allowCommonJSExports) {
(0, _helperSimpleAccess.default)(path, new Set(["module", "exports"]));
{
(0, _helperSimpleAccess.default)(path, new Set(["module", "exports"]), false);
}
path.traverse(moduleExportsVisitor, {

@@ -147,3 +138,2 @@ scope: path.scope

}
let moduleName = (0, _helperModuleTransforms.getModuleName)(this.file.opts, options);

@@ -156,3 +146,4 @@ if (moduleName) moduleName = _core.types.stringLiteral(moduleName);

exportName: "exports",
loose,
constantReexports,
enumerableModuleMeta,
strict,

@@ -162,11 +153,11 @@ strictMode,

noInterop,
importInterop,
lazy,
esNamespaceOnly: typeof state.filename === "string" && /\.mjs$/.test(state.filename) ? mjsStrictNamespace : strictNamespace
esNamespaceOnly: typeof state.filename === "string" && /\.mjs$/.test(state.filename) ? mjsStrictNamespace : strictNamespace,
noIncompleteNsImportDetection,
filename: this.file.opts.filename
});
for (const [source, metadata] of meta.source) {
const loadExpr = _core.types.callExpression(_core.types.identifier("require"), [_core.types.stringLiteral(source)]);
let header;
if ((0, _helperModuleTransforms.isSideEffectImport)(metadata)) {

@@ -176,6 +167,8 @@ if (metadata.lazy) throw new Error("Assertion failure");

} else {
if (metadata.lazy && !metadata.referenced) {
continue;
}
const init = (0, _helperModuleTransforms.wrapInterop)(path, loadExpr, metadata.interop) || loadExpr;
if (metadata.lazy) {
header = _core.template.ast`
header = _core.template.statement.ast`
function ${metadata.name}() {

@@ -188,3 +181,3 @@ const data = ${init};

} else {
header = _core.template.ast`
header = _core.template.statement.ast`
var ${metadata.name} = ${init};

@@ -194,12 +187,15 @@ `;

}
header.loc = metadata.loc;
headers.push(header);
headers.push(...(0, _helperModuleTransforms.buildNamespaceInitStatements)(meta, metadata, loose));
headers.push(...(0, _helperModuleTransforms.buildNamespaceInitStatements)(meta, metadata, constantReexports));
}
(0, _helperModuleTransforms.ensureStatementsHoisted)(headers);
path.unshiftContainer("body", headers);
path.get("body").forEach(path => {
if (headers.indexOf(path.node) === -1) return;
if (path.isVariableDeclaration()) {
path.scope.registerDeclaration(path);
}
});
}
}

@@ -209,3 +205,4 @@ }

});
exports.default = _default;
exports.default = _default;
//# sourceMappingURL=index.js.map
{
"name": "@babel/plugin-transform-modules-commonjs",
"version": "7.12.13",
"version": "7.21.5",
"description": "This plugin transforms ES2015 modules to CommonJS",

@@ -14,8 +14,7 @@ "repository": {

},
"main": "lib/index.js",
"main": "./lib/index.js",
"dependencies": {
"@babel/helper-module-transforms": "^7.12.13",
"@babel/helper-plugin-utils": "^7.12.13",
"@babel/helper-simple-access": "^7.12.13",
"babel-plugin-dynamic-import-node": "^2.3.3"
"@babel/helper-module-transforms": "^7.21.5",
"@babel/helper-plugin-utils": "^7.21.5",
"@babel/helper-simple-access": "^7.21.5"
},

@@ -29,7 +28,14 @@ "keywords": [

"devDependencies": {
"@babel/core": "7.12.13",
"@babel/helper-plugin-test-runner": "7.12.13",
"@babel/plugin-syntax-object-rest-spread": "^7.8.0"
"@babel/core": "^7.21.5",
"@babel/helper-plugin-test-runner": "^7.18.6",
"@babel/plugin-external-helpers": "^7.18.6",
"@babel/plugin-syntax-class-static-block": "^7.14.5",
"@babel/plugin-syntax-object-rest-spread": "^7.8.3"
},
"homepage": "https://babel.dev/docs/en/next/babel-plugin-transform-modules-commonjs"
"homepage": "https://babel.dev/docs/en/next/babel-plugin-transform-modules-commonjs",
"engines": {
"node": ">=6.9.0"
},
"author": "The Babel Team (https://babel.dev/team)",
"type": "commonjs"
}