Socket
Socket
Sign inDemoInstall

@babel/plugin-transform-modules-commonjs

Package Overview
Dependencies
Maintainers
6
Versions
101
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 7.0.0-beta.42 to 7.0.0-beta.43

216

lib/index.js
"use strict";
exports.__esModule = true;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _helperPluginUtils = require("@babel/helper-plugin-utils");
function _helperPluginUtils() {
const data = require("@babel/helper-plugin-utils");
var _helperModuleTransforms = require("@babel/helper-module-transforms");
_helperPluginUtils = function () {
return data;
};
var _helperSimpleAccess = _interopRequireDefault(require("@babel/helper-simple-access"));
return data;
}
var _core = require("@babel/core");
function _helperModuleTransforms() {
const data = require("@babel/helper-module-transforms");
var _templateObject = _taggedTemplateLiteralLoose(["\n (function(){\n throw new Error(\n \"The CommonJS '\" + \"", "\" + \"' variable is not available in ES6 modules.\" +\n \"Consider setting setting sourceType:script or sourceType:unambiguous in your \" +\n \"Babel config for this file.\");\n })()\n "]),
_templateObject2 = _taggedTemplateLiteralLoose(["\n function ", "() {\n const data = ", ";\n ", " = function(){ return data; };\n return data;\n }\n "]),
_templateObject3 = _taggedTemplateLiteralLoose(["\n var ", " = ", ";\n "]);
_helperModuleTransforms = function () {
return data;
};
return data;
}
function _helperSimpleAccess() {
const data = _interopRequireDefault(require("@babel/helper-simple-access"));
_helperSimpleAccess = function () {
return data;
};
return data;
}
function _core() {
const data = require("@babel/core");
_core = function () {
return data;
};
return data;
}
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _taggedTemplateLiteralLoose(strings, raw) { if (!raw) { raw = strings.slice(0); } strings.raw = raw; return strings; }
var _default = (0, _helperPluginUtils.declare)(function (api, options) {
var _default = (0, _helperPluginUtils().declare)((api, options) => {
api.assertVersion(7);
var loose = options.loose,
_options$strictNamesp = options.strictNamespace,
strictNamespace = _options$strictNamesp === void 0 ? false : _options$strictNamesp,
_options$mjsStrictNam = options.mjsStrictNamespace,
mjsStrictNamespace = _options$mjsStrictNam === void 0 ? true : _options$mjsStrictNam,
allowTopLevelThis = options.allowTopLevelThis,
strict = options.strict,
strictMode = options.strictMode,
noInterop = options.noInterop,
_options$lazy = options.lazy,
lazy = _options$lazy === void 0 ? false : _options$lazy,
_options$allowCommonJ = options.allowCommonJSExports,
allowCommonJSExports = _options$allowCommonJ === void 0 ? true : _options$allowCommonJ;
const {
loose,
strictNamespace = false,
mjsStrictNamespace = true,
allowTopLevelThis,
strict,
strictMode,
noInterop,
lazy = false,
allowCommonJSExports = true
} = options;
if (typeof lazy !== "boolean" && typeof lazy !== "function" && (!Array.isArray(lazy) || !lazy.every(function (item) {
return typeof item === "string";
}))) {
throw new Error(".lazy must be a boolean, array of strings, or a function");
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");
throw new Error(`.strictNamespace must be a boolean, or undefined`);
}
if (typeof mjsStrictNamespace !== "boolean") {
throw new Error(".mjsStrictNamespace must be a boolean, or undefined");
throw new Error(`.mjsStrictNamespace must be a boolean, or undefined`);
}
var getAssertion = function getAssertion(localName) {
return _core.template.expression.ast(_templateObject, localName);
};
const getAssertion = localName => _core().template.expression.ast`
(function(){
throw new Error(
"The CommonJS '" + "${localName}" + "' variable is not available in ES6 modules." +
"Consider setting setting sourceType:script or sourceType:unambiguous in your " +
"Babel config for this file.");
})()
`;
var moduleExportsVisitor = {
ReferencedIdentifier: function ReferencedIdentifier(path) {
var localName = path.node.name;
const moduleExportsVisitor = {
ReferencedIdentifier(path) {
const localName = path.node.name;
if (localName !== "module" && localName !== "exports") return;
var localBinding = path.scope.getBinding(localName);
var rootBinding = this.scope.getBinding(localName);
const localBinding = path.scope.getBinding(localName);
const rootBinding = this.scope.getBinding(localName);

@@ -75,29 +104,28 @@ if (rootBinding !== localBinding || path.parentPath.isObjectProperty({

},
AssignmentExpression: function AssignmentExpression(path) {
var _this = this;
var left = path.get("left");
AssignmentExpression(path) {
const left = path.get("left");
if (left.isIdentifier()) {
var localName = path.node.name;
const localName = path.node.name;
if (localName !== "module" && localName !== "exports") return;
var localBinding = path.scope.getBinding(localName);
var rootBinding = this.scope.getBinding(localName);
const localBinding = path.scope.getBinding(localName);
const rootBinding = this.scope.getBinding(localName);
if (rootBinding !== localBinding) return;
var right = path.get("right");
right.replaceWith(_core.types.sequenceExpression([right.node, getAssertion(localName)]));
const right = path.get("right");
right.replaceWith(_core().types.sequenceExpression([right.node, getAssertion(localName)]));
} else if (left.isPattern()) {
var ids = left.getOuterBindingIdentifiers();
var _localName = Object.keys(ids).filter(function (localName) {
const ids = left.getOuterBindingIdentifiers();
const localName = Object.keys(ids).filter(localName => {
if (localName !== "module" && localName !== "exports") return false;
return _this.scope.getBinding(localName) === path.scope.getBinding(localName);
return this.scope.getBinding(localName) === path.scope.getBinding(localName);
})[0];
if (_localName) {
var _right = path.get("right");
_right.replaceWith(_core.types.sequenceExpression([_right.node, getAssertion(_localName)]));
if (localName) {
const right = path.get("right");
right.replaceWith(_core().types.sequenceExpression([right.node, getAssertion(localName)]));
}
}
}
};

@@ -107,4 +135,4 @@ return {

Program: {
exit: function exit(path, state) {
if (!(0, _helperModuleTransforms.isModule)(path)) return;
exit(path, state) {
if (!(0, _helperModuleTransforms().isModule)(path)) return;
path.scope.rename("exports");

@@ -117,3 +145,3 @@ path.scope.rename("module");

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

@@ -124,59 +152,53 @@ scope: path.scope

var moduleName = this.getModuleName();
if (moduleName) moduleName = _core.types.stringLiteral(moduleName);
var _rewriteModuleStateme = (0, _helperModuleTransforms.rewriteModuleStatementsAndPrepareHeader)(path, {
let moduleName = this.getModuleName();
if (moduleName) moduleName = _core().types.stringLiteral(moduleName);
const {
meta,
headers
} = (0, _helperModuleTransforms().rewriteModuleStatementsAndPrepareHeader)(path, {
exportName: "exports",
loose: loose,
strict: strict,
strictMode: strictMode,
allowTopLevelThis: allowTopLevelThis,
noInterop: noInterop,
lazy: lazy,
loose,
strict,
strictMode,
allowTopLevelThis,
noInterop,
lazy,
esNamespaceOnly: typeof state.filename === "string" && /\.mjs$/.test(state.filename) ? mjsStrictNamespace : strictNamespace
}),
meta = _rewriteModuleStateme.meta,
headers = _rewriteModuleStateme.headers;
});
for (var _iterator = meta.source, _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
var _ref2;
for (const [source, metadata] of meta.source) {
const loadExpr = _core().types.callExpression(_core().types.identifier("require"), [_core().types.stringLiteral(source)]);
if (_isArray) {
if (_i >= _iterator.length) break;
_ref2 = _iterator[_i++];
} else {
_i = _iterator.next();
if (_i.done) break;
_ref2 = _i.value;
}
let header;
var _ref3 = _ref2;
var _source = _ref3[0];
var _metadata = _ref3[1];
var loadExpr = _core.types.callExpression(_core.types.identifier("require"), [_core.types.stringLiteral(_source)]);
var header = void 0;
if ((0, _helperModuleTransforms.isSideEffectImport)(_metadata)) {
if (_metadata.lazy) throw new Error("Assertion failure");
header = _core.types.expressionStatement(loadExpr);
if ((0, _helperModuleTransforms().isSideEffectImport)(metadata)) {
if (metadata.lazy) throw new Error("Assertion failure");
header = _core().types.expressionStatement(loadExpr);
} else {
var init = (0, _helperModuleTransforms.wrapInterop)(path, loadExpr, _metadata.interop) || loadExpr;
const init = (0, _helperModuleTransforms().wrapInterop)(path, loadExpr, metadata.interop) || loadExpr;
if (_metadata.lazy) {
header = _core.template.ast(_templateObject2, _metadata.name, init, _metadata.name);
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(_templateObject3, _metadata.name, init);
header = _core().template.ast`
var ${metadata.name} = ${init};
`;
}
}
header.loc = _metadata.loc;
header.loc = metadata.loc;
headers.push(header);
headers.push.apply(headers, (0, _helperModuleTransforms.buildNamespaceInitStatements)(meta, _metadata, loose));
headers.push(...(0, _helperModuleTransforms().buildNamespaceInitStatements)(meta, metadata, loose));
}
(0, _helperModuleTransforms.ensureStatementsHoisted)(headers);
(0, _helperModuleTransforms().ensureStatementsHoisted)(headers);
path.unshiftContainer("body", headers);
}
}

@@ -183,0 +205,0 @@ }

{
"name": "@babel/plugin-transform-modules-commonjs",
"version": "7.0.0-beta.42",
"version": "7.0.0-beta.43",
"description": "This plugin transforms ES2015 modules to CommonJS",

@@ -9,5 +9,5 @@ "repository": "https://github.com/babel/babel/tree/master/packages/babel-plugin-transform-modules-commonjs",

"dependencies": {
"@babel/helper-module-transforms": "7.0.0-beta.42",
"@babel/helper-plugin-utils": "7.0.0-beta.42",
"@babel/helper-simple-access": "7.0.0-beta.42"
"@babel/helper-module-transforms": "7.0.0-beta.43",
"@babel/helper-plugin-utils": "7.0.0-beta.43",
"@babel/helper-simple-access": "7.0.0-beta.43"
},

@@ -18,9 +18,9 @@ "keywords": [

"peerDependencies": {
"@babel/core": "7.0.0-beta.42"
"@babel/core": "7.0.0-beta.43"
},
"devDependencies": {
"@babel/core": "7.0.0-beta.42",
"@babel/helper-plugin-test-runner": "7.0.0-beta.42",
"@babel/plugin-syntax-object-rest-spread": "7.0.0-beta.42"
"@babel/core": "7.0.0-beta.43",
"@babel/helper-plugin-test-runner": "7.0.0-beta.43",
"@babel/plugin-syntax-object-rest-spread": "7.0.0-beta.43"
}
}
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