privatenumber / pkgroll

📦 Zero-config package bundler for Node.js + TypeScript
MIT License
1.01k stars 23 forks source link

Support `preserveSymlinks` from `tsconfig.json` #69

Open jrson83 opened 1 week ago

jrson83 commented 1 week ago

Problem

This might be more a feature request then a bug report.

This issue is related to rollup-plugin-dts https://github.com/Swatinem/rollup-plugin-dts/issues/143 which has not been solved.

As described in the issue, the plugin is not able to resolve Vue3 types from PNPM symlinks (default configuration).

As commented https://github.com/Swatinem/rollup-plugin-dts/issues/143#issuecomment-1036050903, the issue has not been fixed/solved, because it can be overcome setting preserveSymlinks: false, you have 3 options.

Workaround using tsconfig.json:

"compilerOptions": {
  "preserveSymlinks": false
}

Workaround using rollup.config plugins:

import dts from 'rollup-plugin-dts'
// rollup.config.ts
plugins: [
  dts({
   compilerOptions: {
      preserveSymlinks: false
    }
  })
]

Another workaround is using shamefully-hoist in .npmrc.

ignore-workspace-root-check=true
shamefully-hoist=true
node-linker=hoisted

Workarounds:

Build-Error:

packages/vue build$ pkgroll --clean-dist --target=es2020 --minify
packages/vue build: src/index.ts(2,10): error TS2305: Module '"vue"' has no exported member 'defineComponent'.
packages/vue build: src/index.ts(2,27): error TS2305: Module '"vue"' has no exported member 'h'.
packages/vue build: src/index.ts(8,9): error TS7006: Parameter 'props' implicitly has an 'any' type.
packages/vue build: Error [RollupError]: [plugin dts] src/index.ts: Failed to compile. Check the logs above.
packages/vue build:     at getRollupError (/home/dev/typescript-dev/begin/pkgroll-issue/node_modules/.pnpm/rollup@4.18.0/node_modules/rollup/dist/shared/parseAst.js:282:41)
packages/vue build:     at Object.error (/home/dev/typescript-dev/begin/pkgroll-issue/node_modules/.pnpm/rollup@4.18.0/node_modules/rollup/dist/shared/parseAst.js:278:42)
packages/vue build:     at Object.error (/home/dev/typescript-dev/begin/pkgroll-issue/node_modules/.pnpm/rollup@4.18.0/node_modules/rollup/dist/shared/rollup.js:804:32)
packages/vue build:     at Object.error (/home/dev/typescript-dev/begin/pkgroll-issue/node_modules/.pnpm/rollup@4.18.0/node_modules/rollup/dist/shared/rollup.js:19790:42)
packages/vue build:     at generateDtsFromTs (/home/dev/typescript-dev/begin/pkgroll-issue/node_modules/.pnpm/pkgroll@2.1.1_typescript@5.5.2/node_modules/pkgroll/dist/rollup-plugin-dts-BzOer96X.js:30:1744)
packages/vue build:     at Object.transform (/home/dev/typescript-dev/begin/pkgroll-issue/node_modules/.pnpm/pkgroll@2.1.1_typescript@5.5.2/node_modules/pkgroll/dist/rollup-plugin-dts-BzOer96X.js:30:1852)
packages/vue build:     at /home/dev/typescript-dev/begin/pkgroll-issue/node_modules/.pnpm/rollup@4.18.0/node_modules/rollup/dist/shared/rollup.js:989:40 {
packages/vue build:   id: '/home/dev/typescript-dev/begin/pkgroll-issue/packages/vue/src/index.ts',
packages/vue build:   hook: 'transform',
packages/vue build:   code: 'PLUGIN_ERROR',
packages/vue build:   plugin: 'dts',
packages/vue build:   watchFiles: [
packages/vue build:     '/home/dev/typescript-dev/begin/pkgroll-issue/packages/vue/src/index.ts'
packages/vue build:   ],
packages/vue build:   [Symbol(augmented)]: true
packages/vue build: }
packages/vue build: Failed
/home/dev/typescript-dev/begin/pkgroll-issue/packages/vue:
 ERR_PNPM_RECURSIVE_RUN_FIRST_FAIL  @pkgroll-issue/vue@0.0.1 build: `pkgroll --clean-dist --target=es2020 --minify`
Exit status 1
 ELIFECYCLE  Command failed with exit code 1.

Expected behavior

I expect pkgroll to pick-up compilerOptions.preserveSymlinks from tsconfig.json, to resolve the issue, but it doesn't.

Possible solutions

For testing I edited node_modules/pkgroll/dist/cli.js line 37 compilerOptions:{composite:!1,preserveSymlinks:!1}. This solves the issue.

pkgroll@2.1.1.patch

So a solution would be to check for and add preserveSymlinks here:

https://github.com/privatenumber/pkgroll/blob/46c38750a44992f9977471819f4bfa0ef2039639/src/utils/get-rollup-configs.ts#L66C6-L66C21

Minimal reproduction URL

https://github.com/jrson83/pkgroll-issue

Version

v2.1.1

Node.js version

v21.7.2

Package manager

pnpm

Operating system

Linux

Bugs are expected to be fixed by those affected by it

Compensating engineering work financially will speed up resolution

privatenumber commented 1 week ago

Are there any side effects to enabling preserveSymlinks all the time? If not, we can add it like we did with composite.

jrson83 commented 1 week ago

I did some more research and found more issues in several repositories on this apparently very complex topic.

This PR is very helpful, in addition this very precise explanation of the problem and the solution:

Quote:

false is actually the default of resolve, Rollup itself, TS itself, which all reflect how Node itself treats symlinks (i.e. require.resolve).


If not, we can add it like we did with composite.

This would be a solution that unbuild has also implemented with this PR:


Another thing we could try is following this PR (and also several rollup/esbuild related plugins):

The solution here is linked in the source code:

https://github.com/microsoft/TypeScript/blob/c219989232a9db9fb6a5057dd34e21acc628a3b1/src/compiler/sys.ts#L1488-L1490

We could try here:

https://github.com/privatenumber/pkgroll/blob/46c38750a44992f9977471819f4bfa0ef2039639/src/utils/get-rollup-configs.ts#L188-L189

@privatenumber maybe we could create a small test which fails the current issue, in order to try if implementing the condition solves the problem. This sounds like a cleaner solution to me.

If it is to complex we could just go with default value false.