GithubHelp home page GithubHelp logo

vite-plugin-cdn-import's People

Contributors

allenice avatar luckyryan-web avatar vfasky avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

vite-plugin-cdn-import's Issues

这种是不是不支持啊

<script type="module"> import codemirror from https://cdn.jsdelivr.net/npm/[email protected]/+esm </script>

{
name: 'codemirror',
var: 'codemirror',
alias: ['codemirror', '@codemirror/view', '@codemirror/lang-javascript', '@codemirror/lang-json', '@codemirror/theme-one-dark'],
path: [
'https://cdn.jsdelivr.net/npm/[email protected]/+esm',
'https://cdn.jsdelivr.net/npm/@codemirror/[email protected]/+esm',
'https://cdn.jsdelivr.net/npm/@codemirror/[email protected]/+esm',
'https://cdn.jsdelivr.net/npm/@codemirror/[email protected]/+esm',
'https://cdn.jsdelivr.net/npm/@codemirror/[email protected]/+esm']
}
以上写法浏览器报错:Uncaught SyntaxError: Cannot use import statement outside a module (at +esm:7:1)

另外建议示例能不能把所有方法都列举一下 全是简单使用的方法,不需要示例的给示例了。。。。

react+ts项目打包问题

当我使用 rollup-plugin-external-globals 排除打包,并手动引入cdn链接时能够成功运行项目,但是使用vite-plugin-cdn-import打包项目时,浏览器会出现如下问题
image
vite.config.ts

import { defineConfig, type PluginOption } from "vite";
import react from "@vitejs/plugin-react";
import { resolve } from "path";
import { visualizer } from "rollup-plugin-visualizer";
// import externalGlobals from "rollup-plugin-external-globals";
import { Plugin as importToCDN } from "vite-plugin-cdn-import";

// https://vitejs.dev/config/
export default defineConfig({
  resolve: {
    alias: {
      "@": resolve(__dirname, "src"),
    },
  },
  plugins: [
    react(),
    visualizer() as PluginOption,
    importToCDN({
      modules: [
        {
          name: "react",
          var: "React",
          path: "https://cdnjs.cloudflare.com/ajax/libs/react/18.3.1/umd/react.production.min.js",
        },
        {
          name: "react-dom",
          var: "ReactDOM",
          path: "https://cdnjs.cloudflare.com/ajax/libs/react-dom/18.3.1/umd/react-dom.production.min.js",
        },
        {
          name: "dayjs",
          var: "dayjs",
          path: "https://cdnjs.cloudflare.com/ajax/libs/dayjs/1.11.11/dayjs.min.js",
        },
        {
          name: "antd",
          var: "antd",
          path: "https://cdnjs.cloudflare.com/ajax/libs/antd/5.17.4/antd.min.js",
        },
      ],
    }),
  ],
});

Support for module packages.

This plugin loads all scripts with <script src="${url}"></script>. However this doesn't work for all packages.

Consider the following package: "nayuki-qr-code-generator": "1.8.0". When 'imported' in this way you get the following error:
Uncaught SyntaxError: export declarations may only appear at top level of a module.

Could support for these types of packages be added?


Notes:

I've found I could get the desired result by manually adding the url source to the rollupOptions { rollupOptions: { external: ['https://unpkg.com/[email protected]/index.js'] } } and modifying the import to:

// This imports the script from the cdn. Included in the build by adding it to rollupOptions.external.
// @ts-expect-error 2300 2307
import qrcodegen from 'https://unpkg.com/[email protected]/index.js';
// This part is marked as a type so it won't be included in the build but you can still use the type definitions.
// @ts-expect-error 2300
import type qrcodegen from 'nayuki-qr-code-generator';

无法正确引入moment的cdn

您好。
由于Vite无法识别UMD包,因此我导入了moment的ES包(import moment from 'moment/dist/moment.js')。
但我在尝试使用autoComplete('moment')后发现,moment依然在我打包产物中。

这是当我使用autoComplete('moment')

截屏2022-07-18 13 44 31

这是未使用autoComplete('moment')

截屏2022-07-18 13 44 02

并没有任何区别

autoComplete("crypto-js") 出错

crypto-js 引入: import CryptoJS from "crypto-js"
用 vite 打包完后出现以下情况

image

image

原始
image

vite: ^4.2.1
vite-plugin-cdn-import: ^0.3.5

但是手动换成以下就没问题

{
  name: "crypto-js",
  var: "CryptoJS",
  path: "https://cdn.jsdelivr.net/npm/[email protected]/crypto-js.min.js"
},

纯css库的CDN引入支持问题

path字段貌似是必须的且不能为空,但是这对纯css库并不友好。

plugins: [
    vue(),
    importToCDN({
      modules: [
        {
          name: 'remixicon',
          // path: '',
          css: 'https://cdn.jsdelivr.net/npm/[email protected]/fonts/remixicon.css'
        }
      ]
    })
  ],

或许可以将他设置为可选字段?

关于0.3.5在开发环境时也引入cdn的问题

我又来了,一起把这个插件变得更加完善把!
我在公司开发时。项目引用了很多包的css cdn,但是我启动的时候发现在开发环境下,css的cdn反而阻塞了渲染的速度,也就是说这个渲染速度没有本地加载css快。所以我觉得在开发环境下也引入cdn不太合适。
我想要达到的功能是: 我在main.js中引入css,然后开发环境下是用的是我本地引入的css。到了生产打包的时候,这个插件能够自动移除引入的css,并且在index.html中引入cdn的css。
当然这个插件目前已经达到了后者,现在只需要区分在生产打包时移除引入的css包就行了。

如何解决css样式也被打包进去的问题?

之前提了一个issue,js被打包进去的问题解决了,后来发现了另一个问题,就是打包后css也被打包进去了。
0.3.2可以只配置css的cdn链接,不在项目引入css样式,开发时有样式的效果。
但是升到0.3.4后,不仅要配置css的cdn链接,还要在项目中引入css样式,否则开发环境下看不到样式。

这样的话,岂不是我每次重新打包,都要去main.js把css的代码注释掉?然后开发的时候,又把代码开启?

配置如下:
image
image
image
image

虽然没有引入css,但是打包有样式效果。不过如果想要在开发环境下开发,就去main.js配置css样式,这样又会把css给一起打包进来。
image
image

和unplugin-vue-components使用时,执行build会刷新components.d.ts导致组件没有编译

``使用版本:"vite-plugin-cdn-import": "^1.0.1",
"unplugin-vue-components": "^0.24.1",
"vite": "^4.5.3",
"vue": "^3.2.47",

dev没有问题,build才有问题。

<script setup lang="ts">
// 以为使用了 unplugin-vue-components 会自动导入 el-card,所以这里没有 import
</script>

<template>
  <el-card shadow="hover" header="(近十天)">
    ...
  </el-card>
</template>

<style scoped lang="scss"></style>

左边图是编译后自动少了Element相关组件

image

unplugin-vue-components配置
Components({
      resolvers: [
        // 自动导入 Element Plus 组件
        ElementPlusResolver(),
        // 自动注册图标组件
        IconsResolver(
          // https://github.com/antfu/unplugin-icons#auto-importing
          {
            // {prefix}-{collection}-{icon}
            // 默认前缀为 i,在 Element 图标前面加 IEp 加图标名 即可自动导入
            // prefix: 'icon',  // false 表示不用前缀
            // 自动添加 @iconify-json/ep 包,ep 代表 ElementPlus
            enabledCollections: ['ep'],
            // 自动导入自定义的图标集合,默认前缀下,为 IMine 加图标文件名
            customCollections: ['mine']
          }
        )
      ],
      dts: path.resolve(pathSrc, 'typings', 'components.d.ts'),
      dirs: [
        // 指定组件所在位置,默认为 src/components
        // 可以使用 glob 语法
        '*/**/components'
      ]
    })

vite-plugin-cdn-import 配置
import { Plugin as importToCDN } from 'vite-plugin-cdn-import'

export const cdnConfig = importToCDN({
  prodUrl: 'https://unpkg.com/{name}@{version}/{path}',
  modules: [
    {
      name: 'vue',
      var: 'Vue',
      path: 'dist/vue.global.prod.js'
    },
    {
      name: 'vue-router',
      var: 'VueRouter',
      path: 'dist/vue-router.global.js'
    },
    {
      name: 'vue-demi',
      var: 'VueDemi',
      path: 'lib/index.iife.js'
    },
    {
      name: 'pinia',
      var: 'Pinia',
      path: 'dist/pinia.iife.js'
    },
    {
      name: 'element-plus',
      var: 'ElementPlus',
      path: 'dist/index.full.min.js',
      css: 'dist/index.css'
    },
    {
      name: 'axios',
      var: 'axios',
      path: 'dist/axios.min.js'
    },
    {
      name: 'dayjs',
      var: 'dayjs',
      path: 'dayjs.min.js'
    },
    {
      name: 'echarts',
      var: 'echarts',
      path: 'dist/echarts.min.js'
    }
  ]
})

打包非production时无法生效

vite build --mode production能生效
vite build --mode staging不能生效

您这边看看是不是应该要换个判断方式,或者在config配置插件时手动让开发者选择
或者是否有其他方式能满足方式二,因为打包也有测试环境或生产环境之分

请问这种格式的怎么cdn啊

import { basicSetup } from 'codemirror'
import { EditorView } from "@codemirror/view"
import { javascript } from '@codemirror/lang-javascript'
import { json } from '@codemirror/lang-json'
import { oneDark } from '@codemirror/theme-one-dark'

1 deprecated subdependencies found: [email protected]

I have an issue, that 1 deprecated subdependencies found: [email protected] in my project.

It turns out that this subdependencies is used by vite-plugin-cdn-import
devDependencies:
vite-plugin-cdn-import 1.0.1
└─┬ vite-plugin-externals 0.6.2
└─┬ magic-string 0.25.9
└── sourcemap-codec 1.4.8

What I can do about it ? Can you update the deprecated subdependencies?

Failed to resolve component: HelloWorld

image
三种引入方式都试了,只有中间那种没有报错。但是有警告且页面的 el-button 也没有显示
image
这是配置
image
这是警告
有没有大佬知道怎么解决

要如何使用element-plus?

我还是不太清楚要如何使用这个插件。

我在vite.config.js中配置了element-plus的cdn
image

但是没有安装element-plus的包,也没有全局配置。
image

无法显示样式
image
element-plus的cdn没有引入进来
image
image

我是在开发环境下使用的,npm run dev
难道是我使用的姿势不对?

path 为什么不能指定完整的url?

  cdn({
      modules: [
        {
          name: 'react',
          var: 'React',
          path: `https://xxxx.com/react.production.min.js`,
        },
     
      ],
    }),

我就不能选择自己的js地址吗? 非要我按照你的格式要求使用吗? cdn厂商也必须按照你的格式要求提供包吗?
此外, 能否配置一些preloader defer async 之类的标签?

请求支持monorepo

monorepo下vite+react 构建报错

image

monorepo会将公共依赖提至根目录

看了一下源码,希望能支持下monorepo

image

感谢!

使用后打包报错RollupError: node_modules/react/cjs/react-jsx-runtime.production.min.js (12:7): "default" is not exported by "react?commonjs-external", imported by "node_modules/react/cjs/react-jsx-runtime.production.min.js".

Build failed in 9.67s
error during build:
RollupError: node_modules/react/cjs/react-jsx-runtime.production.min.js (12:7): "default" is not exported by "react?commonjs-external", imported by "node_modules/react/cjs/react-jsx-runtime.production.min.js".
file: G:/WorkSpace/WebStormProjects/react-learn-2024/day-06/rt-jk/node_modules/react/cjs/react-jsx-runtime.production.min.js:12:7
10: import * as commonjsHelpers from "commonjsHelpers.js";
11: import { __exports as reactJsxRuntime_production_min } from "\u0000G:/WorkSpace/WebStormProjects/react-learn-2024/day...
12: import require$$0 from "\u0000react?commonjs-external";
^
13:
14: 'use strict';var f=require$$0,k=Symbol.for("react.element"),l=Symbol.for("react.fragment"),m=Object.prototype.hasOwnP...
at getRollupError (file:///G:/WorkSpace/WebStormProjects/react-learn-2024/day-06/rt-jk/node_modules/rollup/dist/es/shared/parseAst.js:394:41)
at error (file:///G:/WorkSpace/WebStormProjects/react-learn-2024/day-06/rt-jk/node_modules/rollup/dist/es/shared/parseAst.js:390:42)
at Module.error (file:///G:/WorkSpace/WebStormProjects/react-learn-2024/day-06/rt-jk/node_modules/rollup/dist/es/shared/node-entry.js:13896:16)
at Module.traceVariable (file:///G:/WorkSpace/WebStormProjects/react-learn-2024/day-06/rt-jk/node_modules/rollup/dist/es/shared/node-entry.js:14344:29)
at ModuleScope.findVariable (file:///G:/WorkSpace/WebStormProjects/react-learn-2024/day-06/rt-jk/node_modules/rollup/dist/es/shared/node-entry.js:12025:39)
at Identifier.bind (file:///G:/WorkSpace/WebStormProjects/react-learn-2024/day-06/rt-jk/node_modules/rollup/dist/es/shared/node-entry.js:7382:40)
at VariableDeclarator.bind (file:///G:/WorkSpace/WebStormProjects/react-learn-2024/day-06/rt-jk/node_modules/rollup/dist/es/shared/node-entry.js:4776:23)
at VariableDeclaration.bind (file:///G:/WorkSpace/WebStormProjects/react-learn-2024/day-06/rt-jk/node_modules/rollup/dist/es/shared/node-entry.js:4772:28)
at Program.bind (file:///G:/WorkSpace/WebStormProjects/react-learn-2024/day-06/rt-jk/node_modules/rollup/dist/es/shared/node-entry.js:4772:28)
at Module.bindReferences (file:///G:/WorkSpace/WebStormProjects/react-learn-2024/day-06/rt-jk/node_modules/rollup/dist/es/shared/node-entry.js:13877:18)
`

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.