unplugin / unplugin-icons

🤹 Access thousands of icons as components on-demand universally.
https://www.npmjs.com/package/unplugin-icons
MIT License
4.1k stars 145 forks source link

autoInstall not work (自动安装图标集失败) #248

Open ghost opened 2 years ago

ghost commented 2 years ago

Describe the bug

设置autoInstall: true并在项目中使用element-plus的某个图标, unplugin-icons会尝试以命令yarn add -D --prefer-offline @iconify-json/ep安装@iconify-json/ep, 但是, 这会导致报错, 安装失败. vite.config.ts配置如下:

import { fileURLToPath, URL } from 'node:url'

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'

import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'

import Icons from 'unplugin-icons/vite'
import IconsResolver from 'unplugin-icons/resolver'
import { FileSystemIconLoader } from 'unplugin-icons/loaders'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    vueJsx(),
    AutoImport({
      dts: './auto-imports.d.ts',
      eslintrc: {
        enabled: true,
        filepath: './.eslintrc-auto-import.json',
      },
      resolvers: [
        ElementPlusResolver(),
      ],
    }),
    Components({
      dts: './components.d.ts',
      resolvers: [
        ElementPlusResolver(),
        IconsResolver({
          prefix: 'icon',
          enabledCollections: ['ep'],
          customCollections: ['custom'],
        }),
      ],
      types: [],
    }),
    Icons({
      autoInstall: true,
      customCollections: {
        custom: FileSystemIconLoader('./src/assets/icons/custom'),
      },
    }),
  ],
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url)),
    },
  },
  server: {
    host: true,
    open: true,
  },
})

报错信息如下:

Installing @iconify-json/ep...
[1/4] Resolving packages...
error Couldn't find a package.json file in "C:\\Users\\admin\\AppData\\Local\\Yarn\\Cache\\v6\\npm-@types-node-18.11.2-c59b7641832531264fda3f1ba610362dc9a7dfc8-integrity\\node_modules\\@types\\node"
info Visit https://yarnpkg.com/en/docs/cli/add for documentation about this command.
[@iconify-loader] Failed to install @iconify-json/ep
Error: Command failed with exit code 1: yarn add -D --prefer-offline @iconify-json/ep
    at makeError (D:\workspace\demo\node_modules\execa\lib\error.js:60:11)
    at handlePromise (D:\workspace\demo\node_modules\execa\index.js:118:26)
    at processTicksAndRejections (internal/process/task_queues.js:95:5)
    at async task (D:\workspace\demo\node_modules\@iconify\utils\lib\loader\fs.cjs:26:7)
    at async loadNodeIcon (D:\workspace\demo\node_modules\@iconify\utils\lib\loader\node-loader.cjs:33:19)
    at async generateComponent (D:\workspace\demo\node_modules\unplugin-icons\dist\chunk-ZY22PK6O.js:302:15)
    at async Context.load (D:\workspace\demo\node_modules\unplugin-icons\dist\chunk-ZY22PK6O.js:359:20)
    at async Object.load (file:///D:/workspace/demo/node_modules/vite/dist/node/chunks/dep-51c4f80a.js:40197:32)
    at async loadAndTransform (file:///D:/workspace/demo/node_modules/vite/dist/node/chunks/dep-51c4f80a.js:36549:24) {
  shortMessage: 'Command failed with exit code 1: yarn add -D --prefer-offline @iconify-json/ep',
  command: 'yarn add -D --prefer-offline @iconify-json/ep',
  escapedCommand: 'yarn add -D --prefer-offline "@iconify-json/ep"',
  exitCode: 1,
  signal: undefined,
  signalDescription: undefined,
  stdout: undefined,
  stderr: undefined,
  failed: true,
  timedOut: false,
  isCanceled: false,
  killed: false
}
Icon `ep/avatar` not found
16:10:03 [vite] Internal server error: Icon `ep/avatar` not found
      at generateComponent (D:\workspace\demo\node_modules\unplugin-icons\dist\chunk-ZY22PK6O.js:304:11)
      at processTicksAndRejections (internal/process/task_queues.js:95:5)
      at async Context.load (D:\workspace\demo\node_modules\unplugin-icons\dist\chunk-ZY22PK6O.js:359:20)
      at async Object.load (file:///D:/workspace/demo/node_modules/vite/dist/node/chunks/dep-51c4f80a.js:40197:32)
      at async loadAndTransform (file:///D:/workspace/demo/node_modules/vite/dist/node/chunks/dep-51c4f80a.js:36549:24)

当手动执行yarn add -D @iconify-json/ep(去掉--prefer-offline)时, 安装成功, 图标使用正常

Reproduction

上边贴的是完整的vite.config.ts配置内容, 应该很容易复现的

System Info

System:
    OS: Windows 10 10.0.19043
    CPU: (12) x64 Intel(R) Core(TM) i5-10400F CPU @ 2.90GHz
    Memory: 4.65 GB / 15.83 GB
  Binaries:
    Node: 14.20.0 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.22.19 - C:\Program Files (x86)\Yarn\bin\yarn.CMD
    npm: 6.14.17 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Edge: Spartan (44.19041.1266.0), Chromium (103.0.1264.49)
    Internet Explorer: 11.0.19041.1566

Used Package Manager

yarn

Validations

1109955705 commented 1 year ago

pnpm 全局包下载导致的问题,pnpm setup设置的环境变量也有有问题,需要把pnpm的环境变量放到系统变量中, 参考: https://github.com/pnpm/pnpm/issues/4658#issuecomment-1120392823

xiangshu233 commented 1 year ago

执行 yarn cache clean 应该就可以了,我刚才遇到这个问题,清除缓存可以了