simonhaenisch / rollup-plugin-typescript-paths

Rollup Plugin to automatically resolve path aliases set in the compilerOptions section of tsconfig.json.
https://npmjs.com/rollup-plugin-typescript-paths
MIT License
43 stars 12 forks source link

didn't load non ts file #14

Open ko22009 opened 1 year ago

ko22009 commented 1 year ago

I use alias for assets, it doesn't work.

simonhaenisch commented 1 year ago

You'll need to be more specific than that, this is barely any more descriptive than just "doesn't work".

ko22009 commented 1 year ago

Try use alias with fonts or images.

simonhaenisch commented 1 year ago

k not sure this is a use case that can be supported, not even sure how it's supposed to work without a path alias. are you using some custom loader with webpack or sth?

ko22009 commented 1 year ago

I use @rollup/plugin-url, it's great plugin.

elado commented 1 year ago

After upgrading Rollup from v2 to v3 and commonjs plugin, I'm having an issue with loading .js files from a .ts file. The resolveId return value is /Users/<user>/<project>/Users/<user>/<project>/src/js-file.js with 2 full paths.

{
  errno: -2,
  code: 'PLUGIN_ERROR',
  syscall: 'open',
  path: '/Users/<user>/<project>/Users/<user>/<project>/src/js-file.js',
  pluginCode: 'ENOENT',
  plugin: 'commonjs--resolver',
  hook: 'resolveId',
  watchFiles: [
    '/Users/<user>/<project>/src/file.ts',
    '/Users/<user>/<project>/Users/<user>/<project>/src/js-file.js'
  ]
}

A few things I observed:

This was the exact behavior with rollup v2 (the double path) but now it's actually failing, specifically with .js files.

ko22009 commented 1 year ago

@elado, allowJS: true in your tsconfif file?

elado commented 1 year ago

Yes. with allowJS

simonhaenisch commented 1 year ago

Ok cool I wasn't aware that Rollup has released a v3 yet (not actively working with Rollup anymore at the moment).

https://rollupjs.org/migration/#changes-to-the-plugin-api looks like there were some plugin API changes so until I have a closer look I'm not sure it's currently compatible with v3.

simonhaenisch commented 1 year ago

Sorry @elado just read your message properly... can you maybe try a patch in your project by opening node_modules/rollup-plugin-typescript-paths/dist/index.js, and in line 19 at the end change

outDir = _g.outDir

to

outDir = _g.compilerOptions.outDir

(at least I think that's the fix you were suggesting?)

:pray:

elado commented 1 year ago

This doesn't change much - path.join('dist', '/abs/path') just spits dist/abs/path. So I'm not really sure how the malformed returned value is resolved by rollup.

simonhaenisch commented 1 year ago

What I wonder is why you'd get an absolute path in the first place... usually import paths are always relative?

mdorda commented 1 year ago

Any update? It is also impossible to use rollup-plugin-svelte together with this plugin. Build fails with error:

Error: Could not load /Users/<user>/<project>/Users/<user>/<project>/node_modules/svelte/index.mjs (imported by test.svelte): ENOENT: no such file or directory, open '/Users/<user>/<project>/Users/<user>/<project>/node_modules/svelte/index.mjs'
simonhaenisch commented 1 year ago

Just from having a quick look at this again, it seems like using path.join here

https://github.com/simonhaenisch/rollup-plugin-typescript-paths/blob/a65deddb4edb445e47e95e534034a51ef812caeb/index.ts#L49-L52

is ~incorrect~ not always correct? it should instead use path.relative or sth to find the relative path from outDir to the resolved file?

If anyone could try that I'm happy to fix it. I just don't have any test case to reproduce, a PR just creating a repro test case would also greatly help with it.

mdorda commented 1 year ago

Thanks for quick response. path.relative did the job and works like a charm for my setting:

{
        preserveExtensions: true,
        nonRelative: true,
}
simonhaenisch commented 1 year ago

Ok cool, thanks for testing that! Just to confirm, you just switched out join with relative and that did the trick?

mdorda commented 1 year ago

Exactly.

const targetFileName = path.relative( 
    outDir, 
    preserveExtensions ? resolvedFileName : resolvedFileName.replace(/\.tsx?$/i, '.js'), 
 );