Skip to content

Bug: Shouldn't fall back to pkg name in the absence of output flag & pkg main #884

@rschristian

Description

@rschristian

Packages that don't use the output flag or have a "main" in their package.json have their preferred filename output ignored.

For example, say I have the following package:

{
  "name": "foo",
  "exports": {
    ".": "./dist/bar.js",
    ...
  }
}

Unfortunately, because of the replaceName() here, the desired output is replaced by the package name instead.

microbundle/src/index.js

Lines 297 to 303 in b1a6374

mainsByFormat.modern = replaceName(
(pkg.exports && walk(pkg.exports)) ||
(pkg.syntax && pkg.syntax.esmodules) ||
pkg.esmodule ||
'x.modern.js',
mainNoExtension,
);

microbundle/src/index.js

Lines 255 to 260 in b1a6374

function replaceName(filename, name) {
return resolve(
dirname(filename),
name + basename(filename).replace(/^[^.]+/, ''),
);
}

mainNoExtension traces back to here, which defines a couple of fallbacks.

microbundle/src/index.js

Lines 219 to 225 in b1a6374

async function getOutput({ cwd, output, pkgMain, pkgName }) {
let main = resolve(cwd, output || pkgMain || 'dist');
if (!main.match(/\.[a-z]+$/) || (await isDir(main))) {
main = resolve(main, `${removeScope(pkgName)}.js`);
}
return main;
}

I can't really say that I understand why this was done in the first place, so don't want to go "fixing" it as there might be some relying on this. To me, it seems like that in the absence of the output flag, these fields should be read and used exactly, no replacing.

mainNoExtension seems like it should be the last of the fallbacks, not something that overwrites valid entries & chosen names.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions