milesj/packemon

package.main incorrectly set to cjs/index.js

kaleidawave opened this issue ยท 13 comments

Running packemon build sets the main field on package.json to ./cjs/index.cjs but ./cjs/index.cjs does not exist. I think it should be set to the value under exports["."].node value which in this case points to an actual file: ./cjs/node.cjs.

It also adds "src/**/*.{ts,tsx,json}" to the files field. Is there a reason for this? Is there a way to make it not touch package.json

@kaleidawave That's odd, what's your config? It sets main based on your chosen formats.

The files is intentional. It's so you don't have to manage npmignore yourself and only the files that should be packed will be in the tar file.

Yep, I have no idea, it doesn't mention ./cjs/index.cjs in the config:

"packemon": [
  {
    "inputs": {
      "index": "src/node.ts"
    },
    "format": "cjs",
    "platform": "node"
  },
  {
    "inputs": {
      "bin": "src/cli.ts"
    },
    "platform": "node"
  },
  {
    "inputs": {
      "web": "src/web.ts"
    },
    "platform": "browser",
    "support": "current"
  },
  {
    "inputs": {
      "import": "src/web.ts"
    },
    "platform": "node",
    "format": "mjs",
    "support": "current"
  }
]

Does packemon read any other inputs..?

On adding specifically the src to the files section: I am not sure it should be added as none of the files in src are used in the export or main fields.

The rest of packemon is excellent but at the moment I'm having to modify these two lines between builds.

@kaleidawave Wait, so this input:

{
    "inputs": {
      "index": "src/node.ts"
    },
    "format": "cjs",
    "platform": "node"
  },

Is writing to cjs/node.cjs and not cjs/index.cjs? That seems like a bug.

Also, src is included in files because they are required for source maps.

Yep, from that input the transpiled contents of (the entrypoint) src/node.ts end up in cjs/node.cjs. I think that is the desired output as forcing it to cjs/index.cjs could mess up direct imports pointing to cjs/node.cjs. I don't think there is any need for it to be specifically named index. Just as long as the main field points to the correct file.

@kaleidawave So the left-hand side of the inputs object should be the filename, hence index.

I'll give this exact configuration a test and see what's up.

@kaleidawave So I have quick test case here and given your config, it's generating the correct package.json to me. #43

Do you have a branch or something I can check out?

@kaleidawave Bumping this ^^^

https://replit.com/@kaleidawave/packemon-package-main-issue

With the following inputs with the entrypoint src/my-entrypoint.ts:

"packemon": [
  {
    "inputs": {
      "index": "src/my-entrypoint.ts"
    },
    "format": "cjs",
    "platform": "node"
  }
],

Packemon builds to cjs/my-entrypoint.cjs and dts/my-entrypoint.d.ts but sets the main and types fields in package.json to the non-existant cjs/index.cjs and dts/index.d.ts. I am not quite sure if this is a issue with the location it writes to or a issue with the data it writes to package.json

In the project I am currently using packemon in, I currently correct it in postbuild so I don't need this fix urgently.

@kaleidawave Is this still happening on the latest version? I think it may be related to not bundling, but will have to verify.

Hi, updated the repl to 0.16.3 and the "main" field now correctly points to the correct entry file so that is fixed, thanks! ๐Ÿ˜ƒ. However the "types" field which points to the type declarations is still pointing to dts/index.d.ts rather than dts/my-entrypoint.d.ts.

@kaleidawave Haha damn. Should be an easy fix now that I know what's happening.

Ok, now fixed!

Yep, just checked and types field is correct now! Thanks ๐Ÿ˜