MCPcopy Index your code
hub / github.com/rollup/plugins / transformCommonjs

Function transformCommonjs

packages/commonjs/src/transform-commonjs.js:46–597  ·  view source on GitHub ↗
(
  parse,
  code,
  id,
  isEsModule,
  ignoreGlobal,
  ignoreRequire,
  ignoreDynamicRequires,
  getIgnoreTryCatchRequireStatementMode,
  sourceMap,
  isDynamicRequireModulesEnabled,
  dynamicRequireModules,
  commonDir,
  astCache,
  defaultIsModuleExports,
  needsRequireWrapper,
  resolveRequireSourcesAndUpdateMeta,
  isRequired,
  checkDynamicRequire,
  commonjsMeta
)

Source from the content-addressed store, hash-verified

44// - usesRequireWrapper
45// - isWrapped
46export default async function transformCommonjs(
47 parse,
48 code,
49 id,
50 isEsModule,
51 ignoreGlobal,
52 ignoreRequire,
53 ignoreDynamicRequires,
54 getIgnoreTryCatchRequireStatementMode,
55 sourceMap,
56 isDynamicRequireModulesEnabled,
57 dynamicRequireModules,
58 commonDir,
59 astCache,
60 defaultIsModuleExports,
61 needsRequireWrapper,
62 resolveRequireSourcesAndUpdateMeta,
63 isRequired,
64 checkDynamicRequire,
65 commonjsMeta
66) {
67 const ast = astCache || tryParse(parse, code, id);
68 const magicString = new MagicString(code);
69 const uses = {
70 module: false,
71 exports: false,
72 global: false,
73 require: false
74 };
75 const virtualDynamicRequirePath =
76 isDynamicRequireModulesEnabled && getVirtualPathForDynamicRequirePath(dirname(id), commonDir);
77 let scope = attachScopes(ast, 'scope');
78 let lexicalDepth = 0;
79 let programDepth = 0;
80 let classBodyDepth = 0;
81 let currentTryBlockEnd = null;
82 let shouldWrap = false;
83
84 const globals = new Set();
85 // A conditionalNode is a node for which execution is not guaranteed. If such a node is a require
86 // or contains nested requires, those should be handled as function calls unless there is an
87 // unconditional require elsewhere.
88 let currentConditionalNodeEnd = null;
89 const conditionalNodes = new Set();
90 const { addRequireExpression, rewriteRequireExpressionsAndGetImportBlock } = getRequireHandlers();
91
92 // See which names are assigned to. This is necessary to prevent
93 // illegally replacing `var foo = require('foo')` with `import foo from 'foo'`,
94 // where `foo` is later reassigned. (This happens in the wild. CommonJS, sigh)
95 const reassignedNames = new Set();
96 const topLevelDeclarations = [];
97 const skippedNodes = new Set();
98 const moduleAccessScopes = new Set([scope]);
99 const exportsAccessScopes = new Set([scope]);
100 const moduleExportsAssignments = [];
101 let firstTopLevelModuleExportsAssignment = null;
102 const exportsAssignmentsByName = new Map();
103 const topLevelAssignments = new Set();

Callers 1

transformAndCheckExportsFunction · 0.85

Calls 10

tryParseFunction · 0.90
attachScopesFunction · 0.90
getRequireHandlersFunction · 0.90
getNameFunction · 0.90
deconflictFunction · 0.90
capitalizeFunction · 0.90
wrapCodeFunction · 0.90

Tested by

no test coverage detected