How to use @alias in a local vuepress-next plugin? #1366
-
I have a local vuepress-next plugin that uses Project directory structure: docs/
.vuepress/ # for vuepress
config.ts
plugins/
my-plugin.ts # local vuepress plugin
src/
node/ # for node part
node-lib.ts
shared/ # for code shared with both node and vue's client parts
shared-lib.ts
tsconfig.json Aliases tsconfig.ts {
"compilerOptions": {
"baseUrl": ".",
"target": "ES6",
"module": "ESNext",
"moduleResolution": "node",
"lib": ["ESNext", "DOM", "DOM.Iterable"],
"paths": {
"@/*": ["./docs/.vuepress/*"],
"@node/*": ["./src/node/*"],
"@shared/*": ["./src/shared/*"]
}
... config.ts import myPlugin from './plugins/my-plugin';
const config: any = {
plugins: [
myPlugin({}),
],
alias: {
'@': path.resolve(__dirname),
'@node': path.resolve(__dirname, '../../src/node'),
'@shared': path.resolve(__dirname, '../../src/shared'),
}
... in import nodeLib from "@node/node-lib";
import sharedLib from "@shared/shared-lib"; The problem is that It seems one workaround is to delay the import to @alias, using a dynamic import. But it failed, I guess vite doesn't support @alias in dynamic import.
Another workaround is to use a relative import in
Your help is appreciated. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Vite/Webpack ONLY parse alias for bunded files, which are files used in browser. Your plugin entry is handled by node directly, so the alias never works. If you want to use alias, use a bundler to bundle your files and use that output as your plugin entry. |
Beta Was this translation helpful? Give feedback.
Vite/Webpack ONLY parse alias for bunded files, which are files used in browser.
Your plugin entry is handled by node directly, so the alias never works.
If you want to use alias, use a bundler to bundle your files and use that output as your plugin entry.