unbuild
unbuild copied to clipboard
passive watcher esm module
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
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)
Also facing the same 😞
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
same
same
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.
- clone https://github.com/nuxt/framework
- run
npx yarn install
- run
npm yarn stub
- create
test.mjs
at the root with contents
import { findPath } from "@nuxt/kit";
console.log(findPath);
- 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.
some :/ "dev": "unbuild --stub"
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?
also facing the same issue
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.
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.
Let's track active watcher via https://github.com/unjs/unbuild/issues/281