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

passive watcher esm module #35

Closed
mdbetancourt opened this issue Dec 23, 2021 · 12 comments
Closed

passive watcher esm module #35

mdbetancourt opened this issue Dec 23, 2021 · 12 comments

Comments

@mdbetancourt
Copy link

unbuild uses

await writeFile(output + '.mjs', `${shebang}import jiti from 'jiti';\nexport default jiti(null, { interopDefault: true })('${entry.input}');`)

but this results in

import jiti from 'jiti';
export default jiti(null, { interopDefault: true })('../src/index.ts');

which only do default export for esm modules (not named ones) i tried to figure it out how solve this but seems is not possible what you thing

@pi0
Copy link
Member

pi0 commented Dec 23, 2021

Hi @mdbetancourt. Thanks for feedback. Actually jiti preserves default + named exports but we cannot just imported them as named in stub mode. I guess we can improve this by emulating named exports using static analyze with mlly. (Generating named exports in stub)

@jd-solanki
Copy link

Also facing the same 😞

@wobsoriano
Copy link

facing this issue

Are they installed?
Failed to resolve import "file:///Users/username/Documents/tmp/test/node_modules/.pnpm/jiti@1.13.0/node_modules/jiti/lib/index.js" from "../dist/index.mjs". Does the file exist?

Used antfu's starter

@reslear
Copy link

reslear commented May 10, 2022

same

1 similar comment
@Mr-xzq
Copy link

Mr-xzq commented Jul 25, 2022

same

@aphex
Copy link

aphex commented Jul 27, 2022

I think what I am seeing is this same issue, originally though it was a jiti problem, but moving details here. I am not sure stubs can currently be used with ESM as trying to use named exports doesn't work for stubs but using the default export doesn't work when built.

  1. clone https://github.com/nuxt/framework
  2. run npx yarn install
  3. run npm yarn stub
  4. create test.mjs at the root with contents
import { findPath } from "@nuxt/kit";

console.log(findPath);
  1. run node index.mjs

you will see the error SyntaxError: The requested module '@nuxt/kit' does not provide an export named 'findPath'

Then run npm run build and node index.mjs again you will see findPath resolves fine.

@productdevbook
Copy link

some :/ "dev": "unbuild --stub"

@lucafanselau
Copy link

I am also experiencing the issue, "Failed to resolve import" when using the stubbed project from a vite project, that depends on the stubbed project through a "link:..." dependency. It seems like vite is not able to resolve the file protocol import of jiti. Is there any new information on this, I am also using the ts-starter from antfu.

Btw. the same issue also appears in larger projects like https://github.com/harlan-zw/unlighthouse for me, so maybe there is just a configuration problem on our end?

@xuemanchi
Copy link

also facing the same issue

@jrolfs
Copy link

jrolfs commented Feb 8, 2023

I'll just chime in here to point out that @pi0 has since implemented the following:

I guess we can improve this by emulating named exports using static analyze with mlly. (Generating named exports in stub)

So, the jiti ESM stub does provide named exports now. For any of you experiencing related problems, it's worth first making sure you're on the latest version of unbuild first.


Now, with that said, unfortunately, I think we will continue to run into issues with the stubs with different bundlers, etc. because we can't necessarily rely on the stub executing in the context of Node.js. You can see similar issues described here.

I love the stub approach over a watch process (especially in monorepos) and have been relying on something similar from Preconstruct for a while now. Unfortunately, I'm just not sure it's possible to reproduce that functionality for ESM bundles when they are consumed by Next.js or Nuxt, etc.

Preconstruct's CJS stub works with Next.js because it doesn't import any Node-specific packages (fs, module, etc.). jiti would have to be re-worked significantly to add an option to produce stubs that don't rely on those modules. Even then, I think Preconsctruct's CJS stub only works on account of the require hook (from pirates, which jiti also uses) — but I don't think we can ever get an ESM stub working that way with the bundlers that power Next.js and Nuxt (because require interop in ESM requires createRequire from the module Node.js module). Maaaybe if the stub leveraged an ESM loader instead of the require hook it could work in some contexts, but I'm not sure.

@JohnCampionJr
Copy link

I have to agree; this is an issue. We need the standard watch option here. I'm trying to replace tsc with unbuild for the vue support it adds, but without a watch feature, its making dev rather challenging.

@pi0
Copy link
Member

pi0 commented Jul 18, 2023

Let's track active watcher via #281

@pi0 pi0 closed this as completed Jul 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests