You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

@babel/plugin-transform-modules-commonjs

Package Overview
Dependencies
Maintainers
4
Versions
111
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.23.0

lib/dynamic-import.js

126

lib/index.js

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

exports.default = void 0;
Object.defineProperty(exports, "defineCommonJSHook", {
enumerable: true,
get: function () {
return _hooks.defineCommonJSHook;
}
});
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.js");
var _lazy = require("./lazy.js");
var _hooks = require("./hooks.js");
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 +31,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 +57,2 @@ (function(){

`;
const moduleExportsVisitor = {

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

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

@@ -76,11 +74,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 +103,2 @@ const localBinding = path.scope.getBinding(localName);

})[0];
if (localName) {

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

}
};
return {
name: "transform-modules-commonjs",
pre() {
this.file.set("@babel/plugin-transform-modules-*", "commonjs");
if (lazy) (0, _hooks.defineCommonJSHook)(this.file, (0, _lazy.lazyImportsHook)(lazy));
},
visitor: {
CallExpression(path) {
["CallExpression" + (api.types.importExpression ? "|ImportExpression" : "")](path) {
if (!this.file.has("@babel/plugin-proposal-dynamic-import")) return;
if (!path.get("callee").isImport()) return;
if (path.isCallExpression() && !_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 +138,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,5 +147,5 @@ scope: path.scope

}
let moduleName = (0, _helperModuleTransforms.getModuleName)(this.file.opts, options);
if (moduleName) moduleName = _core.types.stringLiteral(moduleName);
const hooks = (0, _hooks.makeInvokers)(this.file);
const {

@@ -156,3 +156,4 @@ meta,

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

@@ -162,41 +163,41 @@ strictMode,

noInterop,
lazy,
esNamespaceOnly: typeof state.filename === "string" && /\.mjs$/.test(state.filename) ? mjsStrictNamespace : strictNamespace
importInterop,
wrapReference: hooks.wrapReference,
getWrapperPayload: hooks.getWrapperPayload,
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)) {
if (metadata.lazy) throw new Error("Assertion failure");
if (lazy && metadata.wrap === "function") {
throw new Error("Assertion failure");
}
header = _core.types.expressionStatement(loadExpr);
} else {
var _header;
const init = (0, _helperModuleTransforms.wrapInterop)(path, loadExpr, metadata.interop) || loadExpr;
if (metadata.lazy) {
header = _core.template.ast`
function ${metadata.name}() {
const data = ${init};
${metadata.name} = function(){ return data; };
return data;
}
`;
} else {
header = _core.template.ast`
var ${metadata.name} = ${init};
`;
if (metadata.wrap) {
const res = hooks.buildRequireWrapper(metadata.name, init, metadata.wrap, metadata.referenced);
if (res === false) continue;else header = res;
}
(_header = header) != null ? _header : header = _core.template.statement.ast`
var ${metadata.name} = ${init};
`;
}
header.loc = metadata.loc;
headers.push(header);
headers.push(...(0, _helperModuleTransforms.buildNamespaceInitStatements)(meta, metadata, loose));
headers.push(...(0, _helperModuleTransforms.buildNamespaceInitStatements)(meta, metadata, constantReexports, hooks.wrapReference));
}
(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);
}
});
}
}

@@ -206,3 +207,4 @@ }

});
exports.default = _default;
exports.default = _default;
//# sourceMappingURL=index.js.map
{
"name": "@babel/plugin-transform-modules-commonjs",
"version": "7.12.13",
"version": "7.23.0",
"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.23.0",
"@babel/helper-plugin-utils": "^7.22.5",
"@babel/helper-simple-access": "^7.22.5"
},

@@ -29,7 +28,12 @@ "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.23.0",
"@babel/helper-plugin-test-runner": "^7.22.5",
"@babel/plugin-external-helpers": "^7.22.5"
},
"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"
}

@@ -5,3 +5,3 @@ # @babel/plugin-transform-modules-commonjs

See our website [@babel/plugin-transform-modules-commonjs](https://babeljs.io/docs/en/babel-plugin-transform-modules-commonjs) for more information.
See our website [@babel/plugin-transform-modules-commonjs](https://babeljs.io/docs/babel-plugin-transform-modules-commonjs) for more information.

@@ -8,0 +8,0 @@ ## Install