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

Can't navigate with the compiled version of histoire #79

Closed
hugoattal opened this issue May 3, 2022 · 10 comments
Closed

Can't navigate with the compiled version of histoire #79

hugoattal opened this issue May 3, 2022 · 10 comments
Assignees
Labels
bug Something isn't working help wanted Extra attention is needed

Comments

@hugoattal
Copy link
Collaborator

hugoattal commented May 3, 2022

I have no problem when developing histoire, but when I use it on my project, I get this error when I want to navigate:
image

This should be availabe was according to the doc 🤔 (+ it passed cypress tests) https://vitejs.dev/guide/env-and-mode.html
https://github.com/histoire-dev/histoire/blob/main/packages/histoire/src/client/app/router.ts#L3-L4


Here's what the compiled file looks like...
image

@hugoattal hugoattal added the bug Something isn't working label May 3, 2022
@hugoattal hugoattal self-assigned this May 3, 2022
@Akryum
Copy link
Member

Akryum commented May 3, 2022

Hmm we don't have this issue on our examples in production. 🤔

@hugoattal
Copy link
Collaborator Author

Yes, I don't understand why I'm having this... I'm investigating it.

@hugoattal
Copy link
Collaborator Author

hugoattal commented May 3, 2022

Seems this bug only show when I export a setupVue3 function in my setup file 🤔 (even if it's empty)

EDIT: Actually, it's not even totally that... Seems it depends on the content of my setup file, even if I don't export a setupVue3 function

EDIT2: It actually depends on that import:

import { defineSetupVue3 } from 'histoire/client'

Without it, there's no problem!

@hugoattal hugoattal added the help wanted Extra attention is needed label May 3, 2022
@hugoattal
Copy link
Collaborator Author

I spend the last two hours on this and I have no idea where does it come from 😕... Seems like Vite convert this:

import { createRouter, createWebHistory } from 'vue-router';
// @ts-expect-error env not defined
export const base = import.meta.env.BASE_URL;
export const router = createRouter({
    history: createWebHistory(base),

to this

// ../../node_modules/.pnpm/file+..+histoire+packages+histoire_nqcd5hyezk7xu2dwelgfmciih4/node_modules/histoire/dist/client/app/router.js
var import_meta = {};
var base = import_meta.env.BASE_URL;
var router = createRouter({
  history: createWebHistory(base),

inside the node_modules/.vite/deps/histoire_client.js file

Here's a minimal repro: https://github.com/hugoattal/histoire-bug

@Akryum Could you take a quick look if you have any idea?

@Akryum
Copy link
Member

Akryum commented May 3, 2022

I'll take a look 👀

@Akryum
Copy link
Member

Akryum commented May 6, 2022

I didn't reproduce on linux:
image

@Akryum
Copy link
Member

Akryum commented May 6, 2022

image

@hugoattal
Copy link
Collaborator Author

Why is it always on Windows that there are some weird stuff happening 😅...

Thanks for looking, I'll try to solve this. That also might be a Vite bug.

@Akryum
Copy link
Member

Akryum commented May 6, 2022

Could you try putting import.meta.env.BASE_URL in the actual code and build the vite app?

@hugoattal
Copy link
Collaborator Author

hugoattal commented May 6, 2022

@Akryum Just to explain the problem a little better:


Here's the content of router.ts
histoire/packages/histoire/src/client/app/router.ts

// @ts-expect-error env not defined
export const base = import.meta.env.BASE_URL

Here's the file built with pnpm run build on the histoire package
histoire/packages/histoire/dist/client/app/router.js

// @ts-expect-error env not defined
export const base = import.meta.env.BASE_URL;

What is served by vite when doing pnpm run story:dev on examples/vue3
http://localhost:3000/@fs/D:/Library/Projects/histoire/packages/histoire/dist/client/app/router.js

import.meta.env = {"BASE_URL":"/","MODE":"development","DEV":true,"PROD":false,"SSR":false};import { createRouter, createWebHistory } from '/node_modules/.vite/deps/vue-router.js?v=86493c02';
// @ts-expect-error env not defined
export const base = import.meta.env.BASE_URL;

What is served by vite when doing pnpm run story;dev on another project where I installed histoire is the same thing
http://localhost:3001/node_modules/.pnpm/[email protected]_xhpwbmdaa2vm3qs36nmkicclpa/node_modules/histoire/dist/client/app/router.js

import.meta.env = {"BASE_URL":"/","MODE":"development","DEV":true,"PROD":false,"SSR":false};import { createRouter, createWebHistory } from '/node_modules/.pnpm/[email protected][email protected]/node_modules/vue-router/dist/vue-router.esm-bundler.js?v=3ff50232';
// @ts-expect-error env not defined
export const base = import.meta.env.BASE_URL;

But when I click on a story, it triggers this on my console and reload the page

22:59:09 [vite] ✨ new dependencies optimized: histoire/client
22:59:09 [vite] ✨ optimized dependencies changed. reloading

And then it serves a big file called histoire_client.js, which contains this
http://localhost:3001/node_modules/.vite/deps/histoire_client.js?v=3ff50232

// node_modules/.pnpm/[email protected]_xhpwbmdaa2vm3qs36nmkicclpa/node_modules/histoire/dist/client/app/router.js
var import_meta = {};
var base = import_meta.env.BASE_URL;

And THIS is the thing that causes the bug


To sum up, I think the optimization step of Vite might generate a faulty file, but I have no idea how it works, so I may be wrong.

I suggest adding a question mark here to solve the bug

export const base = import.meta.env?.BASE_URL;

This generates this (as histoire_client.js) which works on my end:

var import_meta = {};
var _a;
var base = (_a = import_meta.env) == null ? void 0 : _a.BASE_URL;

Wdyt?

@Akryum Akryum closed this as completed in ea22d1b May 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants