1 Star 1 Fork 0

masx200 / rollup-plugin-http-resolve

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
README.md 1.73 KB
一键复制 编辑 原始数据 按行查看 历史
masx200 提交于 2023-05-29 20:34 . 格式化

@masx200/rollup-plugin-http-resolve

npm install @masx200/rollup-plugin-http-resolve --save

缓存默认为内存缓存, 添加了文件系统缓存的功能,可以按需使用

import { fileCache, httpResolve } from "@masx200/rollup-plugin-http-resolve";

httpResolve({ cache: new fileCache() });

Example

// rollup.config.js
import { httpResolve } from "@masx200/rollup-plugin-http-resolve";
export default {
    input: "index.js",
    plugins: [
        httpResolve({
            cache,
        }),
    ],
};

Example: CDN Resolve

const vol = Volume.fromJSON({
    "/index.js": `
    import {h} from "preact";
    console.log(h);
    `,
});

const memfs = createFs(vol) as IPromisesAPI;
const rolled = await rollup({
    input: "/index.js",
    plugins: [
        httpResolve({
            fallback(id) {
                // Avoid local relative path
                if (!id.startsWith(".")) {
                    return `https://esm.sh/${id}`;
                }
            },
        }),
        memfsPlugin(memfs),
    ],
});
const out = await rolled.generate({ format: "es" });
const code = out.output[0].code;

With transform import

Rewrite https:// code by your self.

import ts from "typescript";
export default {
  plugins: [
    httpResolve(),
    {
      name: "transform-cdn",
      transform(code, id) {
        if (id?.startsWith("https://")) {
          const out = ts.transpileModule(code, {
            compilerOptions: {
              module: ts.ModuleKind.ESNext,
              target: ts.ScriptTarget.ES5,
            },
          });
          return {
            code: out.outputText,
            map: out.sourceMapText,
          };
        }
      },
    },
  ],
});

LICENSE

MIT

马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/masx200/rollup-plugin-http-resolve.git
git@gitee.com:masx200/rollup-plugin-http-resolve.git
masx200
rollup-plugin-http-resolve
rollup-plugin-http-resolve
main

搜索帮助

344bd9b3 5694891 D2dac590 5694891