[Svelte] firebase로 svelte 호스팅하기

Computer 관심/Svelte|2020. 12. 19. 00:44
반응형

netlify를 사용해서 호스팅을 할까 firebase로 호스팅 할까 생각하다.

더 익숙한 firebase로 호스팅을 해보기로 하였다.

 

 

1. firebase tools 설치 하기

npm install -g firebase-tools

 

 

2. 호스팅 관련 정보 초기화 (init) 하기

firebase init hosting

이렇게 클릭하면 질문들이 나온다.

(1) Are you ready to proceed? (진행 준비가 됬나요?) 엔터

 

(2)

use an existing project 이미 프로젝트가 있는경우

Create a new project 프로젝트를 새로 만들경우 선택

 

(3) What do you want to use as your public directory?(퍼블릭디렉토리로 사용할 것은?) 

퍼블릭폴더로 사용할 폴더명을 적음

 

(4) configure as a single page app? (싱글페이지로 세팅을 할건가요?)

Y 싱글페이지로 만들었는데 N을 선택하면 동적 라우팅??? (블로그처럼 /subject1 /subject2이렇게 만드는것) 불가

 

(5) Set up automatic builds and deploys with GitHub? (깃으로 자동빌드디플로이를 세팅할것이냐)

N 깃으로 배포할 것이면 Y

 

(6) (주의!!) file public/index.html already exists. Overwrite? (이미 public 폴더에 index.html파일이 있는데 덮어쓸꺼냐)

N 덮어쓰면 안됨

 

 

3. 디플로이 하기

firebase deploy --only hosting

 

 

데모

svelte-deploy.web.app

 

 

댓글()

자바스크립트 슬라이더 스벨트로 바꾸기(Javascript to Svelte slider)

Computer 관심/Svelte|2020. 12. 14. 23:31
반응형

스벨트로 슬라이더 컴포넌트를 만들어보면 재미있을 것 같았다. 직접 만들면 디자인이 좋지 않을 것 같아서

자바스크립트로 만들어진 슬라이더를 인터넷에서 찾아 스벨트 컴포넌트로 바꾸어 보았다.

 

www.w3schools.com/howto/howto_js_slideshow.asp

 

html

Original 

<!-- Slideshow container -->
<div class="slideshow-container">

  <!-- Full-width images with number and caption text -->
  <div class="mySlides fade">
    <div class="numbertext">1 / 3</div>
    <img src="img1.jpg" style="width:100%">
    <div class="text">Caption Text</div>
  </div>

  <div class="mySlides fade">
    <div class="numbertext">2 / 3</div>
    <img src="img2.jpg" style="width:100%">
    <div class="text">Caption Two</div>
  </div>

  <div class="mySlides fade">
    <div class="numbertext">3 / 3</div>
    <img src="img3.jpg" style="width:100%">
    <div class="text">Caption Three</div>
  </div>

  <!-- Next and previous buttons -->
  <a class="prev" onclick="plusSlides(-1)">&#10094;</a>
  <a class="next" onclick="plusSlides(1)">&#10095;</a>
</div>
<br>

<!-- The dots/circles -->
<div style="text-align:center">
  <span class="dot" onclick="currentSlide(1)"></span>
  <span class="dot" onclick="currentSlide(2)"></span>
  <span class="dot" onclick="currentSlide(3)"></span>
</div>

 

Svelte 

<!-- Slideshow container -->
<div class="slideshow-container">
  {#each slideData as slide}
  <!-- Full-width images with number and caption text -->
  <div class="fade {slide.show ? '':'mySlides'}">
    <div class="numbertext">{slide.id+1} / {slides.length}</div>
    <img src={slide.src} style="width:100%" alt="img_nature_wide">
    <div class="text">{slide.caption}</div>
  </div>
  {/each}

  <!-- Next and previous buttons -->
  <span class="prev" on:click={() => plusSlides(-1)}>&#10094;</span>
  <span class="next" on:click={() => plusSlides(+1)}>&#10095;</span>
</div>
<br>

<!-- The dots/circles -->
<div style="text-align:center">
  {#each slideData as slide}
  <span class="dot {slide.show==true?'active':''}" on:click={()=>currentSlide(slide)}></span>
  {/each}
</div>

 


Javascript

original

var slideIndex = 1;
showSlides(slideIndex);

// Next/previous controls
function plusSlides(n) {
  showSlides(slideIndex += n);
}

// Thumbnail image controls
function currentSlide(n) {
  showSlides(slideIndex = n);
}

function showSlides(n) {
  var i;
  var slides = document.getElementsByClassName("mySlides");
  var dots = document.getElementsByClassName("dot");
  if (n > slides.length) {slideIndex = 1}
  if (n < 1) {slideIndex = slides.length}
  for (i = 0; i < slides.length; i++) {
      slides[i].style.display = "none";
  }
  for (i = 0; i < dots.length; i++) {
      dots[i].className = dots[i].className.replace(" active", "");
  }
  slides[slideIndex-1].style.display = "block";
  dots[slideIndex-1].className += " active";
}

 

svelte

var slideIndex = 0;

let slideData =[{
  id:0,
  caption:'안녕하세요.',
  src:'https://www.w3schools.com/howto/img_nature_wide.jpg',
  show:false
  },{
  id:1,
  caption:'방갑습니다.',
  src:'https://www.w3schools.com/howto/img_snow_wide.jpg',
  show:false
  },{
  id:2,
  caption:'여기에요.',
  src:'https://www.w3schools.com/howto/img_lights_wide.jpg',
  show:false
  }
]

slideData[slideIndex].show = true

// Next/previous controls
function plusSlides(value) {
  let num = slideIndex + value
  slideData[slideIndex].show = false
  if (num == slides.length){
  slideIndex = 0
  } else if(num < 0){
  slideIndex = slides.length+value
  } else {
  slideIndex = num
  }
  slideData[slideIndex].show = true
}

// Thumbnail image controls
function currentSlide(obj) {
  slideData[slideIndex].show = false
  slideIndex =obj.id
  slideData[slideIndex].show = true
}

 


Style

style은 바뀐게 없음

* {box-sizing:border-box}

/* Slideshow container */
.slideshow-container {
  max-width: 1000px;
  position: relative;
  margin: auto;
}

/* Hide the images by default */
.mySlides {
  display: none;
}

/* Next & previous buttons */
.prev, .next {
  cursor: pointer;
  position: absolute;
  top: 50%;
  width: auto;
  margin-top: -22px;
  padding: 16px;
  color: white;
  font-weight: bold;
  font-size: 18px;
  transition: 0.6s ease;
  border-radius: 0 3px 3px 0;
  user-select: none;
}

/* Position the "next button" to the right */
.next {
  right: 0;
  border-radius: 3px 0 0 3px;
}

/* On hover, add a black background color with a little bit see-through */
.prev:hover, .next:hover {
  background-color: rgba(0,0,0,0.8);
}

/* Caption text */
.text {
  color: #f2f2f2;
  font-size: 15px;
  padding: 8px 12px;
  position: absolute;
  bottom: 8px;
  width: 100%;
  text-align: center;
}

/* Number text (1/3 etc) */
.numbertext {
  color: #f2f2f2;
  font-size: 12px;
  padding: 8px 12px;
  position: absolute;
  top: 0;
}

/* The dots/bullets/indicators */
.dot {
  cursor: pointer;
  height: 15px;
  width: 15px;
  margin: 0 2px;
  background-color: #bbb;
  border-radius: 50%;
  display: inline-block;
  transition: background-color 0.6s ease;
}

.active, .dot:hover {
  background-color: #717171;
}

/* Fading animation */
.fade {
  -webkit-animation-name: fade;
  -webkit-animation-duration: 1.5s;
  animation-name: fade;
  animation-duration: 1.5s;
}

@-webkit-keyframes fade {
  from {opacity: .4}
  to {opacity: 1}
}

@keyframes fade {
  from {opacity: .4}
  to {opacity: 1}
}

 

댓글()

Svelte 가 좋은 이유.

Computer 관심/Svelte|2020. 12. 8. 01:39
반응형

전문적인 개발자가 아닌 사람의 개인적인 생각이다.

 

 

자바스크립트가 한창 보급될 때 JQuery가 자바스크립트를 직접 입력하는 것 보다 쉽기 때문에 많이 사용되었다. 그러다 리액트가 유행하였고 기존 웹개발에 익숙한 개발자들은 좀 더 쉽게 쓸수 있는 vuejs를 선택 하였다.

 

간단하게 사용할 수 있었던게 점점 커지면서 점점 복잡해지게 되는 것 같다. vuejs도 방법들이 나누어지면서 복잡해졌다. 

 

 

Svelte는 정말 여러가지의 장점들을 잘 종합해서 나온 웹컴파일러라고 생각된다. 

컴파일러라고 불리는 이유는 뷰나 리액트가 가상돔에서 랜더링 하는 것과 다르게 Svelte는 자바스크립트로 컴파일한다.

그래서 프로그램의 크기와 속도가 체감 가능하게 빠르다. 

 

 

일단. Svelte공식 다큐먼트를 보면 느끼겠지만 쉽다. 군더더기가 없이 깔끔하다. 

굳이 영상을 보며 배울 필요 없이 홈페이지에 들어가서 30분 정도 예제를 읽어보고 따라 해보면 어렵지 않게 익힐 수 있다.

 

 

Svelte가 자체 라우터를 제공하지 않는데 여러가지 사용해본 결과 가볍게 개발할때는 page.js가 사용하기 쉽고 좋았다.

visionmedia.github.io/page.js/

 

Page.js by visionmedia

Page.js Micro client-side router inspired by the Express router (~1200 bytes) View the Project on GitHub visionmedia/page.js Tiny ~1200 byte Express-inspired client-side router. page('/', index) page('/user/:user', show) page('/user/:user/edit', edit) page

visionmedia.github.io

 

물론.

여러가지 모듈들은 아직 리액트와 뷰에 비해 부족한것 같지만 그런걸 상쇄할 정도의 가치는 있다고 생각된다.

 

 

 

 

 

간단한 라우팅 예)

 

 

App.svelte

<script>
  import router from "page"
  import Navbar from "./components/Navbar.svelte"
  import Pages from "./Pages.svelte"
</script>

<Navbar/>
<Pages/>

 

Pages.svelte

<script>
  import router from "page"
  import Home from "./pages/Home.svelte"
  import About from "./pages/About.svelte"

  let page
  router('/', () => page = Home)
  router('/about', () => page = About)
  router.start()
</script>

<svelte:component this={page} />

 

Navbar.svelte

<div>
  <a href="/">home</a>
  <a href="/about">about</a>
</div>

 

About.svelte

<main>
	<h1>About</h1>
	<p>Visit the <a href="https://svelte.dev/tutorial">Svelte tutorial</a> to learn how to build Svelte apps.</p>
</main>

 

home.svelte

<main>
	<h1>Home</h1>
	<p>Visit the <a href="https://svelte.dev/tutorial">Svelte tutorial</a> to learn how to build Svelte apps.</p>
</main>

<style>
	main {
		text-align: center;
		padding: 1em;
		max-width: 240px;
		margin: 0 auto;
	}

	h1 {
		color: #ff3e00;
		text-transform: uppercase;
		font-size: 4em;
		font-weight: 100;
	}

	@media (min-width: 640px) {
		main {
			max-width: none;
		}
	}
</style>

 

스벨트 공식홈페이지.

svelte.dev/

 

Svelte • Cybernetically enhanced web apps

Svelte is a radical new approach to building user interfaces. Whereas traditional frameworks like React and Vue do the bulk of their work in the browser, Svelte shifts that work into a compile step that happens when you build your app. Instead of using tec

svelte.dev

 

-----버튼 컴포넌트 만들기 예) ------

 

app.svelte

<script>
	import Button from './Button.svelte';
</script>

<Button btnName="button1" click={function (){alert('button1 clicked')}}/>
<Button btnName="button2" click={function (){alert('button2 clicked')}}/>

 

button.svelte

<script>
	export let click, btnName; 
</script>

<button on:click={click}>
	{btnName}
</button>

댓글()