rollup-plugin-typescript-paths icon indicating copy to clipboard operation
rollup-plugin-typescript-paths copied to clipboard

didn't load non ts file

Open ko22009 opened this issue 2 years ago • 16 comments

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

ko22009 avatar Feb 21 '23 15:02 ko22009

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

simonhaenisch avatar Feb 22 '23 13:02 simonhaenisch

Try use alias with fonts or images.

ko22009 avatar Feb 22 '23 17:02 ko22009

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?

simonhaenisch avatar Feb 22 '23 18:02 simonhaenisch

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

ko22009 avatar Feb 22 '23 20:02 ko22009

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:

  • outDir is read from the config instead of from compilerOptions https://github.com/simonhaenisch/rollup-plugin-typescript-paths/blob/master/index.ts#L12 - so it always defaults to '.'
  • path.join('.', '/abs/path') becomes 'abs/path'.

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

elado avatar Feb 25 '23 18:02 elado

@elado, allowJS: true in your tsconfif file?

ko22009 avatar Feb 25 '23 18:02 ko22009

Yes. with allowJS

elado avatar Feb 25 '23 18:02 elado

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 avatar Feb 25 '23 18:02 simonhaenisch

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:

simonhaenisch avatar Feb 25 '23 18:02 simonhaenisch

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.

elado avatar Mar 01 '23 21:03 elado

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

simonhaenisch avatar Mar 02 '23 15:03 simonhaenisch

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'

mdorda avatar Jun 16 '23 09:06 mdorda

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.

simonhaenisch avatar Jun 19 '23 09:06 simonhaenisch

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

{
        preserveExtensions: true,
        nonRelative: true,
}

mdorda avatar Jun 19 '23 11:06 mdorda

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

simonhaenisch avatar Jun 19 '23 13:06 simonhaenisch

Exactly.

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

mdorda avatar Jun 20 '23 06:06 mdorda