unbuild icon indicating copy to clipboard operation
unbuild copied to clipboard

passive watcher esm module

Open mdbetancourt opened this issue 2 years ago • 8 comments

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

mdbetancourt avatar Dec 23 '21 18:12 mdbetancourt

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)

pi0 avatar Dec 23 '21 18:12 pi0

Also facing the same 😞

jd-solanki avatar May 07 '22 08:05 jd-solanki

facing this issue

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

Used antfu's starter

wobsoriano avatar May 08 '22 16:05 wobsoriano

same

reslear avatar May 10 '22 00:05 reslear

same

Mr-xzq avatar Jul 25 '22 11:07 Mr-xzq

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.

aphex avatar Jul 27 '22 22:07 aphex

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

productdevbook avatar Aug 17 '22 04:08 productdevbook

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?

lucafanselau avatar Oct 05 '22 14:10 lucafanselau

also facing the same issue

xuemanchi avatar Dec 27 '22 06:12 xuemanchi

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.

jrolfs avatar Feb 08 '23 23:02 jrolfs

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.

JohnCampionJr avatar Apr 09 '23 02:04 JohnCampionJr

Let's track active watcher via https://github.com/unjs/unbuild/issues/281

pi0 avatar Jul 18 '23 16:07 pi0