MCPcopy Create free account
hub / github.com/developit/microbundle / getMain

Function getMain

src/index.js:277–321  ·  view source on GitHub ↗
({ options, entry, format })

Source from the content-addressed store, hash-verified

275}
276
277function getMain({ options, entry, format }) {
278 const { pkg } = options;
279 const pkgMain = options['pkg-main'];
280 const pkgTypeModule = pkg.type === 'module';
281
282 if (!pkgMain) {
283 return options.output;
284 }
285
286 let mainNoExtension = options.output;
287 if (options.multipleEntries) {
288 let name = entry.match(new RegExp(/([\\/])index/.source + EXTENSION.source))
289 ? mainNoExtension
290 : entry;
291 mainNoExtension = resolve(dirname(mainNoExtension), basename(name));
292 }
293 mainNoExtension = mainNoExtension.replace(EXTENSION, '');
294
295 const mainsByFormat = {};
296
297 mainsByFormat.es = replaceName(
298 pkg.module && !pkg.module.match(/src\//)
299 ? pkg.module
300 : pkg['jsnext:main'] || (pkgTypeModule ? 'x.esm.js' : 'x.esm.mjs'),
301 mainNoExtension,
302 );
303
304 mainsByFormat.modern = replaceName(
305 (pkg.exports && walk(pkg.exports, pkgTypeModule)) ||
306 (pkg.syntax && pkg.syntax.esmodules) ||
307 pkg.esmodule ||
308 (pkgTypeModule ? 'x.modern.js' : 'x.modern.mjs'),
309 mainNoExtension,
310 );
311 mainsByFormat.cjs = replaceName(
312 pkg['cjs:main'] || (pkgTypeModule ? 'x.cjs' : 'x.js'),
313 mainNoExtension,
314 );
315 mainsByFormat.umd = replaceName(
316 pkg['umd:main'] || pkg.unpkg || 'x.umd.js',
317 mainNoExtension,
318 );
319
320 return mainsByFormat[format] || mainsByFormat.cjs;
321}
322
323// shebang cache map because the transform only gets run once
324const shebang = {};

Callers 1

createConfigFunction · 0.85

Calls 2

replaceNameFunction · 0.85
walkFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…