nonzzz / vite-plugin-cdn

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

vite-plugin-cdn2: xxx try resolve file failed. #41

Closed M69W closed 8 months ago

M69W commented 10 months ago

demo https://github.com/yulimchen/vue3-h5-template

package.json

"dependencies": {
    "axios": "^1.6.2",
    "normalize.css": "^8.0.1",
    "nprogress": "^0.2.0",
    "pinia": "^2.1.7",
    "vant": "^4.7.3",
    "vconsole": "^3.15.1",
    "vue": "^3.3.4",
    "vue-router": "^4.2.5"
  },
"devDependencies": {
    "vite-plugin-cdn2": "^1.1.0",
  }

cdn.js

export function enableCDN(isEnabled) {
  console.log("enableCDN", isEnabled);
  if (isEnabled === "true" || isEnabled === true) {
    console.log("enableCDN", isEnabled);
    return cdn({
      modules: [
        {
          name: "axios",
          aliasName: "axios.min.js"
        },
        "normalize.css",
        // {
        //   name: "normalize.css",
        //   spare: ["https://unpkg.com/normalize.css@8.0.1/normalize.css"]
        // },
        "nprogress",
        {
          name: "vue",
          aliasName: "vue.global.js"
        },
        "vue-demi",
        {
          name: "vue-router",
          aliasName: "vue-router.global.js"
        },
        {
          name: "pinia",
          aliasName: "pinia.iife.js"
        },
        {
          name: "vant",
          aliasName: "vant.min.js"
        },
        // {
        //   name: "vconsole",
        //   aliasName: "vconsole.min.js"
        // }
        "vconsole"
      ],
      resolve: {
        name: "resolve:custom",
        setup({ extra }) {
          const { name, aliasName, relativeModule, version } = extra;
          let url = "";
          if (name && aliasName) {
            const baseURL = "https://cdn-anc.com";
            url = new URL(`${aliasName}`, baseURL);
          } else {
            const baseURL = "https://unpkg.com/";
            url = new URL(`${name}/${version}/${relativeModule}`, baseURL);
          }
          console.log("url", url);
          return {
            url: url.href,
            injectTo: "head-prepend",
            attrs: {}
          };
        }
      }
    });
  }
}
 ERROR  vite-plugin-cdn2: normalize.css try resolve file failed.                                10:59:12
 ERROR  vite-plugin-cdn2: nprogress try resolve file failed.                                    10:59:12
 ERROR  vite-plugin-cdn2: vconsole try resolve file failed. 

image

nonzzz commented 10 months ago

First vite-plugin-cdn2 is not a perfect solution but it can convert most of scenes. The follow error message means you should check the right relativeModule for them.(perhaps they don't have unpkg or jsdelivr filed to scanner or it's not a javascript file to scan.) The network error seems like the error resolve caused :)

M69W commented 10 months ago

First vite-plugin-cdn2 is not a perfect solution but it can convert most of scenes. The follow error message means you should check the right relativeModule for them.(perhaps they don't have unpkg or jsdelivr filed to scanner or it's not a javascript file to scan.) The network error seems like the error resolve caused :)

Thanks a lot. The plugin would be export the name by modules when custom setting ? some dependencies by installed and existed.

return cdn({
      modules: [
        {
          name: "axios",
          aliasName: "axios.min.js"
        },
        "normalize.css",
        "nprogress",
        "vconsole"
      ],
      resolve: {
        name: "resolve:custom",
        setup({ extra }) {
          const { name, relativeModule, version } = extra;
          let url = "";
          if (name) {// here the name by modules setting be custom setting
            const baseURL = "https://cdn-anc.com";
            url = new URL(`${name}`, baseURL);
          }
          console.log("url", url);
          return {
            url: url.href,
            injectTo: "head-prepend",
            attrs: {}
          };
        }
      }
    });

https://github.com/nonzzz/vite-plugin-cdn/blob/d392ab443cce7005ca25e70d609d727a6d7c1649/src/scanner.ts#L115-L116

nonzzz commented 10 months ago

@M69W Yeah, everything should lived in your local environment