unjs / unbuild

📦 An unified javascript build system
MIT License
2.19k stars 84 forks source link

Prefer `publishConfig` from `package.json` when defined #362

Open humphd opened 5 months ago

humphd commented 5 months ago

Describe the feature

I'm using unbuild in a pnpm monorepo. I love how unbuild works seamlessly with the definitions in my package.json to produce CommonJS, ESM, and Types automatically. However, I've run into an issue.

I'd like to use the src/ dir (i.e., TS) vs. dist/ (i.e., JS) when using the package within the monorepo via the workspace: protocol. Essentially, I want to be able to define a different config when not published, relying on pnpm's publishConfig to do the overrides when I publish to the npm registry:

    "publishConfig": {
        "type": "module",
        "exports": {
            ".": {
                "types": "./dist/index.d.ts",
                "require": "./dist/index.js",
                "import": "./dist/index.mjs"
            }
        },
        "main": "./dist/index.js",
        "module": "./dist/index.mjs",
        "types": "./dist/index.d.ts"
    },
    "main": "./src/index.ts",

Maybe there is a better way to do what I'm after, or maybe this is a feature that could be made to work with unbuild: if publishConfig is present in package.json, override things before using the bast config in package.json to define the build.

Additional information

humphd commented 5 months ago

Maybe this would mean teaching https://github.com/unjs/unbuild/blob/b9ebcf8b4d590dd95f051178ebab70bddb4cbeb8/src/auto.ts#L66 to look for publishConfig and the list of keys it can override?