Recently, I have seen Snowpack
so many times, so I think it's time to try it.
Actually, we can use create-snowpack-app
to create a base app, but I think trying to create a simple app without using that can be helpful to understand the basics of Snowpack
.
In this article, I tried Snowpack with reactjs and typescript.
What I made is here.
Tried snowpack with reactjs and typescript
snowpacktest
Tried snowpack with reactjs and typescript
$ cd snowpacktest
# install packages
$ yarn
# run devServer
$ yarn dev
# build
$ yarn build
Enter fullscreen mode
Exit fullscreen mode
You can use npm
instead of yarn
$ cd snowpacktest
# install packages
$ npm install
# run devServer
$ npm run dev
# build
$ npm run build
Enter fullscreen mode
Exit fullscreen mode
What is Snowpack?
Snowpack is a modern, lightweight build tool for faster web development. Traditional JavaScript build tools like webpack and Parcel need to rebuild & rebundle entire chunks of your application every time you save a single file. This rebundling step introduces lag between hitting save on your changes and seeing them reflected in the browser.
How Snowpack Works
https://www.snowpack.dev/concepts/how-snowpack-works
Create a project dir and init project
$ mkdir snowpacktest
$ yarn init
Enter fullscreen mode
Exit fullscreen mode
Install Snowpack and add npm script
$ yarn add --dev snowpack
Enter fullscreen mode
Exit fullscreen mode
"scripts" : {
"dev" : "snowpack dev" ,
"build" : "snowpack build"
} ,
Enter fullscreen mode
Exit fullscreen mode
Create snowpack.config.js
In this case, public
is set as the root dir.
module . exports = {
mount : {
public : { url : " / " , static : true },
src : " / " ,
},
};
Enter fullscreen mode
Exit fullscreen mode
Doc: snowpack.config.js
https://www.snowpack.dev/reference/configuration#config.mount
Create index.html
<!DOCTYPE html>
<html lang= "en" >
<head>
<meta charset= "utf-8" />
<meta name= "viewport" content= "width=device-width, initial-scale=1" />
<title> my first snowpack app</title>
</head>
<body>
<div id= "root" ></div>
<script type= "module" src= "/index.js" ></script>
</body>
</html>
Enter fullscreen mode
Exit fullscreen mode
Install react and typescript
$ yarn add react react-dom
$ yarn add -D typescript @types/react @types/react-dom
Enter fullscreen mode
Exit fullscreen mode
Just in case, I put my tsconfig.json
{
"compilerOptions" : {
"module" : "ESNext" ,
"target" : "ESNext" ,
"strict" : true ,
"moduleResolution" : "node" ,
"esModuleInterop" : true ,
"jsx" : "preserve" ,
"noEmit" : true ,
"skipLibCheck" : true ,
"typeRoots" : [
"node_modules/@types" ,
"types"
]
},
"include" : [
"src" ,
"types"
]
}
Enter fullscreen mode
Exit fullscreen mode
Create a simple component
src/index.tsx
import React from ' react ' ;
import { render } from ' react-dom ' ;
const App = () => {
return (
<>
< h1 > my first snowpack + react app < /h1 >
< h2 > hello ❄️ Snowpack ❄️ < /h2 >
< / >
);
};
render ( < App /> , document . getElementById ( ' root ' ));
Enter fullscreen mode
Exit fullscreen mode
Use devServer
$ yarn dev
snowpack
http://localhost:8080 • http://192.168.86.27:8080
Server started in 14ms.
Enter fullscreen mode
Exit fullscreen mode
If you know webpack, you may think that's it?
since generally, we need to put more lines in config.js
file for webpack. But, actually, that is it 😁
Let's try to display an image!
Create types/image.d.ts
To display image, we need to create a .d.ts
file. In this case, we put .png
, .jpg
, and .svg
.
declare module ' *.png ' ;
declare module ' *.jpg ' ;
declare module ' *.svg ' ;
Enter fullscreen mode
Exit fullscreen mode
Add lines to index.tsx
To display an image, we need to modify index.tsx a little bit.
First, install styled-components
since I like it lol
$ yarn add styled-components
$ yarn add -D @types/styled-components
Enter fullscreen mode
Exit fullscreen mode
If you are not familiar with styled-components, please see the link.
https://styled-components.com/
import React from ' react ' ;
import { render } from ' react-dom ' ;
import logo from ' ./source.png ' ;
import styled from ' styled-components ' ;
const Wrapper = styled . section `
padding: 4em;
background: #ededed;
` ;
const Title = styled . h1 `
font-size: 3em;
text-align: center;
color: #ea1ed4;
` ;
const ImageWrapper = styled . div `
display: flex;
align-items: center;
justify-content: center;
` ;
const Greeting = styled . h2 `
font-size: 1.5em;
text-align: center;
color: palevioletred;
` ;
const App = () => {
return (
< Wrapper >
< Title > my first snowpack + react app < /Title >
< ImageWrapper >
< img src = { logo } / >
< /ImageWrapper >
< Greeting > hello ❄️ Snowpack ❄️ < /Greeting >
< /Wrapper >
);
};
render ( < App /> , document . getElementById ( ' root ' ));
Enter fullscreen mode
Exit fullscreen mode
If you prefer to use css, you will need to create a .d.ts
for importing css
types/css.d.ts
declare module ' *.css ' {
const classNames : { [ className : string ]: string };
export default classNames ;
}
Enter fullscreen mode
Exit fullscreen mode
Conclusion
Snowpack is pretty cool since not it supports jsx and typescript source code by default. Also we don't need to install any plugin to use dev server which is great. Of course, we can extend the build with custom plugins.
https://www.snowpack.dev/plugins
I think I will start using Snowpack for my side-project!