<file_sep>import Preloader from "../src/preloader.js";

const preloader = new Preloader(
  "http://pic1.win4000.com/wallpaper/f/5876e4b550212.jpg"
);
<file_sep>export async function myLoader(images, callback) {
  let loaded = 0;
  const total = images.length;
  for (const [index, image] of images.entries()) {
    loaded = index + 1;
    try {
      await loadImage(image);
    } catch (e) {
      console.log(e);
    }
    callback((100 * loaded) / total);
  }
}

const loadImage = (src) =>
  new Promise((resolve, reject) => {
    const img = new Image();
    img.src = src;
    if (img.complete) {
      resolve();
    } else {
      img.onload = () => {
        resolve();
      };
      img.onerror = (e) => {
        reject(e);
      };
    }
  });
<file_sep>import Preloader from "./preloader.js";

const preloader = new Preloader("123");

const img = new Image();
<file_sep>export default class Preloader {
  constructor(resource, option = { mode: "no-cors", method: "GET" }) {
    if (typeof resource === "string") {
      console.log("string is good");
      this.initSingle(resource, option);
    } else if (resource instanceof Array) {
      console.log("array is good");
    } else {
      console.log("bad type");
    }

    // this.test = new Test();
  }

  initSingle(r, o) {
    const option = Object.assign(o, { responseType: "blob" });
    fetch(r, option)
      .then((response) => {
        // if (response.ok) {
        //   return response.blob();
        // }
        // throw new Error("Network response was not ok.");
        console.log(response);
      })
      .then((blob) => {
        console.log("done", blob);
      })
      .catch((e) => {
        console.log(e);
      });
  }
}
<file_sep>const http = require("http");
const cp = require("child_process");
const fs = require("fs");
const mime = require("mime");
const url = require("url");
const path = require("path");

const server = http.createServer();
const STATIC_FOLDER = "lib";

server.listen(8080, () => {
  console.log("server start at port 8080");
});

server.on("request", (req, res) => {
  const rootName = path.join(__dirname, "../");
  if (req.url === "/") {
    res.writeHead(200, { "Content-Type": "text/html" });
    fs.readFile("./test/index.html", "utf-8", (err, data) => {
      if (err) {
        throw err;
      }
      res.end(data);
    });
  } else if (req.url === "/favicon.ico") {
    const pathname = url.parse(req.url).pathname;
    const favicon = path.join(rootName, pathname);
    const ext = path.extname(pathname);
    const contentType = mime.getType(ext) || "text/plain";
    let raw = fs.createReadStream(favicon);
    res.writeHead(200, "ok", {
      "Content-Type": contentType,
    });
    raw.pipe(res);
  } else {
    const pathname = url.parse(req.url).pathname;
    const ext = path.extname(pathname);
    const contentType = mime.getType(ext) || "text/plain";
    const realPath = path.join(rootName, STATIC_FOLDER, pathname);
    console.log(pathname, __dirname, realPath);

    let raw = fs.createReadStream(realPath);
    res.writeHead(200, "ok", {
      "Content-Type": contentType,
    });
    raw.pipe(res);
  }
});

cp.exec("start http://127.0.0.1:8080/");
