Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[dev-server-esbuild] Unable to resolve packages with correct extensions when imported in an .ts file using node's exports #1376

Closed
motss opened this issue Mar 31, 2021 · 18 comments · Fixed by #1942

Comments

@motss
Copy link

motss commented Mar 31, 2021

When files being imported in an .ts file, the resolver automatically assumes all the imports must be ended with .ts when some could be .js. It seems like the dev server does not support node's exports quite well.

Actual behavior

Packages that use exports in node resolves with .ts when it gets imported in a .ts file.

Expected behavior

Packages that use exports in node must be resolved with the correct extensions when it gets imported in a .ts file

Steps to reproduce

Clone this repo and run the project to reproduce the stated problem.

@LarsDenBakker
Copy link
Member

I looked into this, and it's an issue inside rollup node resolve. There is special handling for .ts files: https://github.com/rollup/plugins/blob/master/packages/node-resolve/src/index.js#L178. Together with the export map of this package, it creates this situation...

@motss
Copy link
Author

motss commented Apr 6, 2021

Not sure if it is a rollup plugin issue but I tried it with rollup, rollup-plugin-typescript2 and @rollup/plugin-node-resolve. Everything runs without issue. There is no resolution issue with node's export map.

@LarsDenBakker
Copy link
Member

Will need to do some more investigation on our side

@peschee
Copy link
Contributor

peschee commented May 3, 2021

@LarsDenBakker @motss I think I am running into the same or similar issue when using Lit 2.x directives. I have created a small test repo: https://github.com/peschee/wds-esbuild-lit-test

@peschee
Copy link
Contributor

peschee commented May 4, 2021

I think this issue blocks users from upgrading to Lit 2.x when using TypeScript and the recommended esbuild setup in wds.

@swissmanu
Copy link

swissmanu commented May 13, 2021

Same problem here, in conjunction with lit 2.x.

@abdonrd
Copy link
Contributor

abdonrd commented Jun 3, 2021

@LarsDenBakker @motss I think I am running into the same or similar issue when using Lit 2.x directives. I have created a small test repo: https://github.com/peschee/wds-esbuild-lit-test

I have the exactly same problem!

@abdonrd
Copy link
Contributor

abdonrd commented Jul 3, 2021

Will need to do some more investigation on our side

@LarsDenBakker Have you had time to investigate it? Thanks in advance!

@thescientist13
Copy link

thescientist13 commented Jul 4, 2021

Not sure if related, but I may be running into something similar? I'm using test runner, but as I understand it, all the file routing and handling gets handled by dev server, so I think the context applies?

Was using web test runner, writing some ES2015+ custom elements and specs with new lit, everything is good. 💯
https://github.com/AnalogStudiosRI/www.analogstudios.net/tree/chore/design-system-poc

Added a new TypeScript based component following the lit.dev playground, using tsc and middleware to transform .ts files on the fly for web test runner.
https://github.com/AnalogStudiosRI/www.analogstudios.net/tree/chore/design-system-poc-typescript

Now getting this error when running test runner

src/components/greeting/greeting.spec.js:

 🚧 Browser logs:
      TypeError: Failed to resolve module specifier "lit". Relative references must start with either "/", "./", or "../".

 ❌ Could not import your test module. Check the browser logs or open the browser in debug mode for more information. 

Chrome: |██████████████████████████████| 3/3 test files | 6 passed, 0 failed

Code coverage: 100 %

Finished running tests, watching for file changes... 

I noticed comparing the responses using the browser debugger that Dev Server (presumably) was correctly re-writing my bare imports in my other components, like footer.js
Screen Shot 2021-07-04 at 11 30 10 AM

But not for greeting.ts. Maybe that's because import is not at the top?
Screen Shot 2021-07-04 at 11 30 28 AM

Either way, for greeting.ts the bare imports stay, well bare. 🐻👀


Let me know if you need more info or if this should be its own issue. 🙏

@LarsDenBakker
Copy link
Member

LarsDenBakker commented Jul 4, 2021

@thescientist13 that's a different issues, you need to serve your files as JS. This might work:

        return {
          body: result.outputText,
          type: 'js'
        };

but it might be that your plugin runs after node resolve. So then you might need to add a mimeTypes option to your config instead:

{
  mimeTypes: {
    '**/*.ts': 'js',
},

@abdonrd The issue is in rollup node resolve, as I expected. We need to make this behavior of the rollup plugin configurable, so that we can turn it off.

@thescientist13
Copy link

Thanks @LarsDenBakker , that did it! 🙌

@abdonrd
Copy link
Contributor

abdonrd commented Jul 7, 2021

@tjenkinson is working on a fix for this (rollup/plugins#921)! Thanks!

@zenwork
Copy link

zenwork commented Jul 12, 2021

return {
          body: result.outputText,
          type: 'js'
        };

for anyone looking for a more complete quick workaround example:

if (context.path.includes('node_modules/lit') && context.path.endsWith('.ts')) {
      const path = `${__dirname}/../../..${context.request.url}`.replace(/\.ts$/,'.js')
      console.log(path)
      const body = fs.readFileSync(path)
      return { body, type: 'js' };
    }

@abdonrd
Copy link
Contributor

abdonrd commented Jul 25, 2021

rollup/plugins#921 has been solved and @rollup/plugin-node-resolve@13.0.4 published!

@abdonrd
Copy link
Contributor

abdonrd commented Jul 25, 2021

@LarsDenBakker could you merge this #1433 to see if fix it? Thanks!

@abdonrd
Copy link
Contributor

abdonrd commented Jul 25, 2021

Fixed in @web/dev-server@0.1.18! 👏


Edit: Reverted in new version: #1569. 😢

@WickyNilliams
Copy link
Contributor

WickyNilliams commented Aug 18, 2021

Hey. What was the reason for rolling #1433 back in #1569?

I am experiencing this issue where an import of a .js file ends up being replaced with an import for a non-existent .ts file when served by the dev-server.

Forcing my local install to use @rollup/plugin-node-resolve@13.0.4 (by hand editing my package-lock.json) fixes the problem. So it seems this all is good in latest releases.

Edit: I see the rollback was forced because of a failing test. What is the plan for this issue?

@bennypowers
Copy link
Member

bennypowers commented Aug 26, 2021

the failing test case is cjs dependencies outside the web root (e.g. in node_modules). Rollup recently made a breaking change to how it internally handles module resolution, which causes those imports to either not resolve at all or to resolve with rollup internal cjs urls (depending on which attempted solution is tried)
So far we don't have a clear solution, see #1633

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
9 participants