add static file เช่น css, js, image ใน Eleventy (11ty)

#11ty20 ต.ค. 2567

add static file เช่น css, js, image ใน Eleventy (11ty)

ใน Eleventy (11ty) นั้น เวลาเราต้องการเพิ่ม static file พวก css, js หรือรูปภาพต่าง ๆ เราสามารถทำได้ดังนี้

ตัวอย่างเรามี ไฟล์ bundle.css เราสามารถ เพิ่มได้ดังนี้

export default function(eleventyConfig) {
  eleventyConfig.addPassthroughCopy("bundle.css");
};

ตอนเราใช้ ใช้ก็ใช้แบบนี้

<link rel="stylesheet" href="/bundle.css">

ถ้าเป็นรูปภาพ เราก็ใช้แบบนี้

export default function(eleventyConfig) {
  eleventyConfig.addPassthroughCopy("favicon.ico");
};

ตอนใช้เราก็ใช้ประมาณนี้

<link type="image/x-icon" href="/favicon.ico" rel="shortcut icon"/>

เรายังสามารถ public ทั้ง folder ได้ ดังนี้

export default function(eleventyConfig) {
  eleventyConfig.addPassthroughCopy("public");
}