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

custom resolve alias not available in .paths.js file #3125

Open
4 tasks done
jcbhmr opened this issue Oct 23, 2023 · 5 comments
Open
4 tasks done

custom resolve alias not available in .paths.js file #3125

jcbhmr opened this issue Oct 23, 2023 · 5 comments
Labels
build Related to the build system enhancement New feature or request stale

Comments

@jcbhmr
Copy link
Contributor

jcbhmr commented Oct 23, 2023

Describe the bug

when using a [slug].md and [slug].paths.js file for dynamic routes the [slug].paths.js file doesnt seem to get access to the magic resolve.alias["alias-goes-here"] = "/the/actual/file.js" stuff.

Reproduction

https://stackblitz.com/edit/vite-lt2rg5?file=docs%2F.vitepress%2Fconfig.ts,docs%2F.vitepress%2Fhello.js,docs%2F[slug].paths.js

  vite: {
    clearScreen: false,
    resolve: {
      alias: {
        '~hello': fileURLToPath(new URL('./hello.js', import.meta.url)),
      },
    },
  },
import hello from '~hello';

export default {
  paths() {
    return [{ params: { slug: hello } }];
  },
};
failed to load config from /home/projects/vite-lt2rg5/docs/[slug].paths.js
Invalid paths file export in /home/projects/vite-lt2rg5/docs/[slug].paths.js. Expects default export of an object with a "paths" property.

if i remove the import hello from "~hello" line the error goes away

// import hello from '~hello';

export default {
  paths() {
    return [{ params: { slug: 'hello' } }];
  },
};

Expected behavior

i should be able to import hello from "~hello" and have it work, right?

System Info

System:
    OS: Linux 5.0 undefined
    CPU: (4) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 0 Bytes / 0 Bytes
    Shell: 1.0 - /bin/jsh
  Binaries:
    Node: 18.18.0 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 9.4.2 - /usr/local/bin/npm
    pnpm: 8.9.2 - /usr/local/bin/pnpm
  npmPackages:
    vitepress: latest => 1.0.0-rc.23

Additional context

image
image

Validations

@jcbhmr jcbhmr added the bug: pending triage Maybe a bug, waiting for confirmation label Oct 23, 2023
@jcbhmr
Copy link
Contributor Author

jcbhmr commented Oct 23, 2023

i think the issue comes from this:

mod = (await loadConfigFromFile({} as any, pathsFile)) as RouteModule

being a completely new Vite "load like vite.config.js" operation which doesn't inherit the existing Vite options (including resolve.alias)

is ssrLoadModule() a way to do the "load with ts and esm support" operation? idk https://vitejs.dev/guide/ssr.html#:~:text=%3D%20await%20vite.-,ssrLoadModule,-(%27/src/entry

@brc-dd
Copy link
Member

brc-dd commented Oct 24, 2023

server isn't there in build, so can't do server.ssrLoadModule. I'm not sure if there is any simple solution to this.

There was some discussion at #2289. But the solution, until vite itself lands something like vite-node or vavite into the core, is to not use aliases in data/path loader files.

@brc-dd brc-dd added enhancement New feature or request build Related to the build system and removed bug: pending triage Maybe a bug, waiting for confirmation labels Oct 24, 2023
@vincesp
Copy link

vincesp commented Oct 30, 2023

Hint: There is native support for this in Node itself. This works everywhere, including in data/path loader files:
https://nodejs.org/api/packages.html#subpath-imports

package.json:

  "imports": {
    "#theme/*": "./docs/.vitepress/theme/*"
  }

[slug].paths.js:

import hello from '#theme/hello.js'

@jcbhmr
Copy link
Contributor Author

jcbhmr commented Oct 30, 2023

this is a great trick! it doesn't work so well for my usecase where the import { thing } from "~hello" comes from a vite plugin that adds a resolve alias. 😢

@babakfp
Copy link

babakfp commented Dec 8, 2023

Hint: There is native support for this in Node itself. This works everywhere, including in data/path loader files: https://nodejs.org/api/packages.html#subpath-imports

package.json:

  "imports": {
    "#theme/*": "./docs/.vitepress/theme/*"
  }

[slug].paths.js:

import hello from '#theme/hello.js'

This solved my issue, THANKS ❤️
#3309
Sending this ^^^ here so people may have the same issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
build Related to the build system enhancement New feature or request stale
Projects
None yet
Development

No branches or pull requests

4 participants