nonzzz / vite-plugin-cdn

replace module with CDN. work with vite.
MIT License
75 stars 6 forks source link

echarts 配置无效,怀疑是没有扫描到? #48

Open AkenClub opened 2 months ago

AkenClub commented 2 months ago

cdn 配置,resolve设置和没设置效果一样,还没进去resolve,貌似前一步还有一个scanner?扫描可用的包?之后才循环可用的包一一进入resolve。 我对插件方面不太熟悉,可能是我配置问题,麻烦大佬了,谢谢。

import { cdn } from 'vite-plugin-cdn2'
import { defineResolve } from 'vite-plugin-cdn2/resolve'

export const cdnConfig = cdn({
  modules: [
    {
      name: 'dayjs',
      global: 'dayjs',
      relativeModule: 'dayjs.min.js'
    },
    {
      name: 'echarts',
      global: 'echarts',
      // aliases: ['charts', 'code', 'components', 'types/dist/shared', 'features', 'renderers'],
      relativeModule: 'dist/echarts.min.js'
    }
  ],
  resolve: defineResolve({
    name: 'resolve:custom',
    setup({ extra }) {
      const baseURL = 'https://unpkg.com/browse/'
      const { version, name, relativeModule } = extra
      const url = new URL(`${name}@${version}/${relativeModule}`, baseURL)
      return {
        url: url.href,
        injectTo: 'head-prepend',
        attrs: {}
      }
    }
  })
})

下面是日志,只输出了dayjs,echarts没有任何log,也建议输出一下为何不生效的原因,方便排查,谢谢

yarn.cmd run build-only
Active code page: 65001
yarn run v1.22.22
$ rimraf dist && set DEBUG=vite-plugin-cdn2 & vite build
  vite-plugin-cdn2 start scanning +0ms
  vite-plugin-cdn2 scanning done Map(1) {
  'dayjs' => {
    name: 'dayjs',
    version: '1.11.12',
    relativeModule: 'dayjs.min.js',
    aliases: [],
    global: 'dayjs',
    bindings: Set(1) { 'default' }
  }
} +472ms
vite v4.5.3 building for production...
🚀 开始构建
✓ 2202 modules transformed.
rendering chunks (2)...  vite-plugin-cdn2 start transformIndexHtml +21s
  vite-plugin-cdn2 transformIndexHtml Done [
  {
    tag: 'script',
    injectTo: 'head-prepend',
    attrs: { src: 'https://unpkg.com/browse/dayjs@1.11.12/dayjs.min.js' }
  }
] +0ms
dist/index.html                   1.65 kB │ gzip:   0.88 kB
dist/assets/index-189109ff.css    1.54 kB │ gzip:   0.61 kB
dist/assets/index-803f298e.css  129.13 kB │ gzip:  18.47 kB
dist/assets/index-9f0f4ed5.js    96.92 kB │ gzip:  38.57 kB
dist/assets/index-8a686ba7.js   871.10 kB │ gzip: 299.55 kB
AkenClub commented 2 months ago

看到另一个 issues 也有类似问题,的确是echarts/dist/package.json有影响,尝试把这层的package.json改名就可以扫描出来配出cdn了,但是打包出来的内容preview时会引出echarts的一些奇怪问题 image