简体   繁体   English

Vite+Vue - 通过 npm 链接引用的 Package 无法解析 vite.resolve.alias

[英]Vite+Vue - Package referenced via npm link can't resolve vite.resolve.alias

I'm developing a package to help other developers here in our company.我正在开发 package 来帮助我们公司的其他开发人员。

I know that I can create aliases to make the code more cleaner and help the readability of it.我知道我可以创建别名以使代码更简洁并提高其可读性。

So:所以:

PACKAGE PROJECT PACKAGE 项目

vite.config.js vite.config.js

import { defineConfig } from 'vite'
import path from 'path'

export default defineConfig({
    
  resolve:{
        preserveSymlinks: false,
        alias:{
            '@' : path.resolve(__dirname, 'src'),

  },
// ....
})

Assume that I've the following folder structure假设我有以下文件夹结构

  • src源代码
    • helpers帮手
      • commonHelper.js commonHelper.js
    • index.js index.js

src/index.js src/index.js

import {merge} from '@/helpers/commonHelper.js'
export {merge}

PROJECT THAT REFERENCES PACKAGE PROJECT参考PACKAGE项目的项目

vite.config.js vite.config.js

It alsos have a vite.config.js with an @ alias pointing to it self src它还有一个 vite.config.js 和一个 @ 别名指向它 self src

import { defineConfig } from 'vite'
import path from 'path'

export default defineConfig({
    
  resolve:{
        preserveSymlinks: false,
        alias:{
            '@' : path.resolve(__dirname, 'src'),

  },
// ....
})

anyfile.js任何文件.js

Lets assume that my package project is called foo and it's referenced using npm link for testing now, after finishing the package it will be referenced using npm install Lets assume that my package project is called foo and it's referenced using npm link for testing now, after finishing the package it will be referenced using npm install

import {merge} from 'foo'

merge({},{});

When i try to run my project i got the following error当我尝试运行我的项目时,出现以下错误

  [vite] Internal server error: Failed to resolve import "@/helpers/commonHelper.js" from 
  "..\..\npmPackages\foo\src\index.js". Does the file exist?       
  Plugin: vite:import-analysis
  File: E:/_Development/foo/src/index.js
  3  |  import {merge} from '@/helpers/commonHelper.js'
     |                       ^
  4  |  export {merge}
      at formatError (file:///E:/_Development/project/node_modules/vite/dist/node/chunks/dep-557f29e6.js:40853:46)
      at TransformContext.error (file:///E:/_Development/project/node_modules/vite/dist/node/chunks/dep-557f29e6.js:40849:19)
      at normalizeUrl (file:///E:/_Development/project/node_modules/vite/dist/node/chunks/dep-557f29e6.js:37586:33)
      at async TransformContext.transform (file:///E:/_Development/project/node_modules/vite/dist/node/chunks/dep-557f29e6.js:37719:47)
      at async Object.transform (file:///E:/_Development/project/node_modules/vite/dist/node/chunks/dep-557f29e6.js:41102:30)
      at async loadAndTransform (file:///E:/_Development/project/node_modules/vite/dist/node/chunks/dep-557f29e6.js:37364:29)

As far i noticed the problem is happening because @ is pointing to the project that references the foo package到目前为止,我注意到问题正在发生,因为@指向引用foo package 的项目

What can i do?我能做些什么?

I assume that you're using Vite to build the lib.我假设您正在使用 Vite 来构建库。

In that case using "npm link" or the publishing process, you should point in your package.json of the package using the field "module" to the generated ESM library file instead of the actual source code.在这种情况下,使用“npm 链接”或发布过程,您应该使用字段“模块源代码”而不是生成的实际 ESM 库文件指向 package 的 package 的 package.json。

Then you can use the package in your other project same as if it was installed from some package registry.然后,您可以在其他项目中使用 package,就像它是从某个 package 注册表安装的一样。

Please do not use '/src', use '@/' instead请不要使用 '/src',而是使用 '@/'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM