Skip to content

Commit 3955014

Browse files
Fix @ aliases on Windows (#14)
* Fix `@` aliases on Windows * Add docs for `customResolver` usage
1 parent 51b4d4b commit 3955014

File tree

3 files changed

+24
-8
lines changed

3 files changed

+24
-8
lines changed

packages/create-vue-lib/src/template/playground/config/packages/playground/vite.config.mts.ejs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
<%_ if (config.includeAtAliases) { _%>
2+
import { relative, sep as pathSeparator } from 'node:path'
3+
<%_ } _%>
14
import { fileURLToPath, URL } from 'node:url'
25

36
import { defineConfig, type UserConfig } from 'vite'
@@ -28,10 +31,14 @@ export default defineConfig(({ mode }): UserConfig => ({
2831
find: '@',
2932
replacement: '@',
3033
customResolver(source, importer, options) {
31-
const filePath = source.replace(
32-
/^@\//,
33-
importer?.startsWith(librarySrc) ? librarySrc : playgroundSrc
34-
)
34+
let target = playgroundSrc
35+
36+
// If the importer is inside librarySrc we resolve @ to that path
37+
if (importer && relative(importer, librarySrc).split(pathSeparator).every(p => p === '..')) {
38+
target = librarySrc
39+
}
40+
41+
const filePath = source.replace(/^@\//, target)
3542
3643
return this.resolve(filePath, importer, options)
3744
}

packages/create-vue-lib/src/template/vitepress/config/packages/docs/.vitepress/config.mts.ejs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
<%_ if (config.includeAtAliases) { _%>
2+
import { relative, sep as pathSeparator } from 'node:path'
3+
<%_ } _%>
14
import { fileURLToPath, URL } from 'node:url'
25

36
import { defineConfigWithTheme } from 'vitepress'
@@ -50,10 +53,14 @@ export default ({ mode }: { mode: string }) => defineConfigWithTheme({
5053
find: '@',
5154
replacement: '@',
5255
customResolver(source, importer, options) {
53-
const filePath = source.replace(
54-
/^@\//,
55-
importer?.startsWith(librarySrc) ? librarySrc : docsSrc
56-
)
56+
let target = docsSrc
57+
58+
// If the importer is inside librarySrc we resolve @ to that path
59+
if (importer && relative(importer, librarySrc).split(pathSeparator).every(p => p === '..')) {
60+
target = librarySrc
61+
}
62+
63+
const filePath = source.replace(/^@\//, target)
5764
5865
return this.resolve(filePath, importer, options)
5966
}

packages/docs/src/why.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ As we can't build all the output files we need in one go, we instead run Vite th
7676

7777
In `rollupOptions` we configure `external: ['vue']`. This tells rollup to keep any imports from the `vue` package as imports, rather than pulling all the code into the built library. For output formats that don't support `import` it will be rewritten accordingly, e.g. using `require()` for CommonJS. For global (IIFE) builds, there is the extra setting `globals: { vue: 'Vue' }`, which tells rollup to rewrite imports like `import { ref } from 'vue'` as `const { ref } = Vue`, or code that's equivalent.
7878

79+
For pages that use [`@` aliases for `src` paths](questions#configure-src-alias), a `customResolver` is needed in the playground and docs packages. These packages pull in the library source code directly, so they need to resolve an `@` within the library code differently from an `@` within their own code.
80+
7981
## `__DEV__` and `__TEST__`
8082

8183
The project supports 'global variables' for `__DEV__` and `__TEST__`. The `__TEST__` variable isn't included by default and requires the `--extended` flag to opt in.

0 commit comments

Comments
 (0)