Socket
Socket
Sign inDemoInstall

@babel/plugin-transform-modules-commonjs

Package Overview
Dependencies
58
Maintainers
4
Versions
93
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 8.0.0-alpha.2 to 8.0.0-alpha.3

103

lib/index.js
import { declare } from '@babel/helper-plugin-utils';
import { buildDynamicImport, isModule, getModuleName, rewriteModuleStatementsAndPrepareHeader, isSideEffectImport, wrapInterop, buildNamespaceInitStatements, ensureStatementsHoisted } from '@babel/helper-module-transforms';
import { buildDynamicImport, isSideEffectImport, isModule, getModuleName, rewriteModuleStatementsAndPrepareHeader, wrapInterop, buildNamespaceInitStatements, ensureStatementsHoisted } from '@babel/helper-module-transforms';
import simplifyAccess from '@babel/helper-simple-access';

@@ -13,2 +13,65 @@ import { template, types } from '@babel/core';

const lazyImportsHook = lazy => ({
name: `${"@babel/plugin-transform-modules-commonjs"}/lazy`,
version: "8.0.0-alpha.3",
getWrapperPayload(source, metadata) {
if (isSideEffectImport(metadata) || metadata.reexportAll) {
return null;
}
if (lazy === true) {
return /\./.test(source) ? null : "lazy/function";
}
if (Array.isArray(lazy)) {
return lazy.indexOf(source) === -1 ? null : "lazy/function";
}
if (typeof lazy === "function") {
return lazy(source) ? "lazy/function" : null;
}
},
buildRequireWrapper(name, init, payload, referenced) {
if (payload === "lazy/function") {
if (!referenced) return false;
return template.statement.ast`
function ${name}() {
const data = ${init};
${name} = function(){ return data; };
return data;
}
`;
}
},
wrapReference(ref, payload) {
if (payload === "lazy/function") return types.callExpression(ref, []);
}
});
const commonJSHooksKey = "@babel/plugin-transform-modules-commonjs/customWrapperPlugin";
function defineCommonJSHook(file, hook) {
let hooks = file.get(commonJSHooksKey);
if (!hooks) file.set(commonJSHooksKey, hooks = []);
hooks.push(hook);
}
function findMap(arr, cb) {
if (arr) {
for (const el of arr) {
const res = cb(el);
if (res != null) return res;
}
}
}
function makeInvokers(file) {
const hooks = file.get(commonJSHooksKey);
return {
getWrapperPayload(...args) {
return findMap(hooks, hook => hook.getWrapperPayload?.(...args));
},
wrapReference(...args) {
return findMap(hooks, hook => hook.wrapReference?.(...args));
},
buildRequireWrapper(...args) {
return findMap(hooks, hook => hook.buildRequireWrapper?.(...args));
}
};
}
var index = declare((api, options) => {

@@ -102,7 +165,8 @@ api.assertVersion(7);

this.file.set("@babel/plugin-transform-modules-*", "commonjs");
if (lazy) defineCommonJSHook(this.file, lazyImportsHook(lazy));
},
visitor: {
CallExpression(path) {
["CallExpression" + (api.types.importExpression ? "|ImportExpression" : "")](path) {
if (!this.file.has("@babel/plugin-proposal-dynamic-import")) return;
if (!types.isImport(path.node.callee)) return;
if (path.isCallExpression() && !types.isImport(path.node.callee)) return;
let {

@@ -134,2 +198,3 @@ scope

if (moduleName) moduleName = types.stringLiteral(moduleName);
const hooks = makeInvokers(this.file);
const {

@@ -147,3 +212,4 @@ meta,

importInterop,
lazy,
wrapReference: hooks.wrapReference,
getWrapperPayload: hooks.getWrapperPayload,
esNamespaceOnly: typeof state.filename === "string" && /\.mjs$/.test(state.filename) ? mjsStrictNamespace : strictNamespace,

@@ -157,26 +223,19 @@ noIncompleteNsImportDetection,

if (isSideEffectImport(metadata)) {
if (metadata.lazy) throw new Error("Assertion failure");
if (lazy && metadata.wrap === "function") {
throw new Error("Assertion failure");
}
header = types.expressionStatement(loadExpr);
} else {
if (metadata.lazy && !metadata.referenced) {
continue;
}
const init = wrapInterop(path, loadExpr, metadata.interop) || loadExpr;
if (metadata.lazy) {
header = template.statement.ast`
function ${metadata.name}() {
const data = ${init};
${metadata.name} = function(){ return data; };
return data;
}
`;
} else {
header = template.statement.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 ??= template.statement.ast`
var ${metadata.name} = ${init};
`;
}
header.loc = metadata.loc;
headers.push(header);
headers.push(...buildNamespaceInitStatements(meta, metadata, constantReexports));
headers.push(...buildNamespaceInitStatements(meta, metadata, constantReexports, hooks.wrapReference));
}

@@ -197,3 +256,3 @@ ensureStatementsHoisted(headers);

export { index as default };
export { index as default, defineCommonJSHook };
//# sourceMappingURL=index.js.map
{
"name": "@babel/plugin-transform-modules-commonjs",
"version": "8.0.0-alpha.2",
"version": "8.0.0-alpha.3",
"description": "This plugin transforms ES2015 modules to CommonJS",

@@ -16,5 +16,5 @@ "repository": {

"dependencies": {
"@babel/helper-module-transforms": "^8.0.0-alpha.2",
"@babel/helper-plugin-utils": "^8.0.0-alpha.2",
"@babel/helper-simple-access": "^8.0.0-alpha.2"
"@babel/helper-module-transforms": "^8.0.0-alpha.3",
"@babel/helper-plugin-utils": "^8.0.0-alpha.3",
"@babel/helper-simple-access": "^8.0.0-alpha.3"
},

@@ -25,8 +25,8 @@ "keywords": [

"peerDependencies": {
"@babel/core": "^8.0.0-alpha.2"
"@babel/core": "^8.0.0-alpha.3"
},
"devDependencies": {
"@babel/core": "^8.0.0-alpha.2",
"@babel/helper-plugin-test-runner": "^8.0.0-alpha.2",
"@babel/plugin-external-helpers": "^8.0.0-alpha.2"
"@babel/core": "^8.0.0-alpha.3",
"@babel/helper-plugin-test-runner": "^8.0.0-alpha.3",
"@babel/plugin-external-helpers": "^8.0.0-alpha.3"
},

@@ -33,0 +33,0 @@ "homepage": "https://babel.dev/docs/en/next/babel-plugin-transform-modules-commonjs",

Sorry, the diff of this file is not supported yet

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