カテゴリー
【Svelte Framework入門】Svelteの開発環境をDocker Alpine内で簡単に構築する
※ 当ページには【広告/PR】を含む場合があります。
2022/01/17
2022/08/19
Hello World
Alpine DockerコンテナでSvelteの開発環境を整える
Dockerfile
docker-compose.yml
$ touch Dockerfile docker-compose.yml
$ tree
.
├── Dockerfile
└── docker-compose.yml
FROM node:17-alpine
RUN apk update && apk upgrade && \
apk add --no-cache bash openssh
CMD ["bash"]
17-alpine
version: '3'
services:
app:
image: svelte_dev:17-alpine
build: .
user: "node:node"
container_name: svelte_dev
environment:
NODE_ENV: "development"
volumes:
- ./:/usr/src/app
working_dir: "/usr/src/app"
ports:
- "4200:4200"
tty: true
tty: true
$ docker-compose build
$ docker-compose up -d && docker-compose exec app bash
#コンテナのインタラクティブモード内...
exit
CTRL + D
$ docker-compose down
Svelteプロジェクトを初期化する
Svelteの基本プロジェクトを構築する
svelte-app
$ mkdir svelte-app && cd svelte-app
npm init
package.json
{
"name": "svelte-app",
"version": "1.0.0",
"private": true,
"scripts": {
"build": "rollup -c",
"dev": "rollup -c -w",
"start": "sirv public --host 0.0.0.0 --port 4200"
},
"devDependencies": {
"@rollup/plugin-commonjs": "^17.0.0",
"@rollup/plugin-node-resolve": "^11.0.0",
"rollup": "^2.3.4",
"rollup-plugin-css-only": "^3.1.0",
"rollup-plugin-livereload": "^2.0.0",
"rollup-plugin-svelte": "^7.0.0",
"rollup-plugin-terser": "^7.0.0",
"svelte": "^3.0.0"
},
"dependencies": {
"sirv-cli": "^2.0.0"
}
}
yarn add <パッケージ名>
$ yarn install
rollup.config.js
import svelte from 'rollup-plugin-svelte';
import commonjs from '@rollup/plugin-commonjs';
import resolve from '@rollup/plugin-node-resolve';
import livereload from 'rollup-plugin-livereload';
import { terser } from 'rollup-plugin-terser';
import css from 'rollup-plugin-css-only';
const production = !process.env.ROLLUP_WATCH;
function serve() {
let server;
function toExit() {
if (server) server.kill(0);
}
return {
writeBundle() {
if (server) return;
server = require('child_process').spawn('npm', ['run', 'start', '--', '--dev'], {
stdio: ['ignore', 'inherit', 'inherit'],
shell: true
});
process.on('SIGTERM', toExit);
process.on('exit', toExit);
}
};
}
export default {
input: 'src/main.js',
output: {
sourcemap: !production,
format: 'iife',
name: 'app',
file: 'public/build/bundle.js'
},
plugins: [
svelte({
compilerOptions: {
dev: !production
}
}),
css({ output: 'bundle.css' }),
resolve({
browser: true,
dedupe: ['svelte']
}),
commonjs(),
!production && serve(),
!production && livereload('public'),
production && terser()
],
watch: {
clearScreen: false
}
};
src
main.js
App.svelte
$ mkdir src && cd src
$ touch main.js App.svelte
import App from './App.svelte';
const app = new App({
target: document.body,
props: { name: 'world' }
});
export default app;
App.svelte
<script>
export let name;
</script>
<main>
<h1>Hello {name}!</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-app
public
index.html
$ cd ../
$ mkdir public && cd public
$ touch index.html
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset='utf-8'>
<meta name='viewport' content='width=device-width,initial-scale=1'>
<title>Svelte app</title>
<link rel='icon' type='image/png' href='/favicon.png'>
<link rel='stylesheet' href='/build/bundle.css'>
<script defer src='/build/bundle.js'></script>
</head>
<body></body>
</html>
$ wget https://raw.githubusercontent.com/sveltejs/template/master/public/favicon.png
svelte-app
$ tree
.
├── node_modules
├── package.json
├── public
│ ├── favicon.png
│ └── index.html
├── rollup.config.js
├── src
│ ├── App.svelte
│ └── main.js
└── yarn.lock
Svelteアプリのビルド
$ yarn dev
yarn run v1.22.5
Your application is ready~! 🚀
- Local: http://0.0.0.0:4200
- Network: http://172.24.0.2:4200
$ yarn build
yarn run v1.22.5
rollup -c
src/main.js → public/build/bundle.js...
created public/build/bundle.js in 939ms
Done in 1.28s.
public/build
$ ls public/build/
bundle.css bundle.js
$ du -ab public/build/
225 public/build/bundle.css
3362 public/build/bundle.js
SvelteでSassを使う
●●.scss
<style>
$ yarn add sass svelte-preprocess -D
rollup.config.js
///...中略
//👇プリプロセスプラグインを追加
import sveltePreprocess from 'svelte-preprocess';
///...中略
export default {
///...中略
plugins: [
svelte({
compilerOptions: {
dev: !production
},
//👇プリプロセスプラグインを注入
preprocess: sveltePreprocess()
}),
//...以下略
};
...
<style lang="scss">
//...Sassのスタイルコード
</style>
@import
...
<style lang="scss">
@import "./hoge.scss";
@import "./styles/piyo.scss";
</style>
@import
VSCode内でSvelteのLinter対策
lang="scss"
node-sass
node-sass
$ yarn add autoprefixer postcss -D
svelte.config.js
const sveltePreprocess = require('svelte-preprocess');
const production = !process.env.ROLLUP_WATCH;
const preprocessOptions = {
sourcemap: !production,
defaults: {
script: "typescript",
style: "scss",
},
postcss: {
plugins: [require('autoprefixer')()]
}
};
module.exports = {
preprocess: sveltePreprocess(preprocessOptions),
preprocessOptions,
}
Typescriptのインストール
$ mkdir scripts && cd scripts
$ wget https://raw.githubusercontent.com/sveltejs/template/master/scripts/setupTypeScript.js
$ cd ../
$ node scripts/setupTypeScript.js
Converted to TypeScript.
You will need to re-run your dependency manager to get started.
$ yarn install
rollup.config.js
[!] Error: Identifier 'sveltePreprocess' has already been declared
rollup.config.js (10:7)
8: import css from 'rollup-plugin-css-only';
9:
10: import sveltePreprocess from 'svelte-preprocess';
^
rollup.config.js
まとめ
参考サイト
記事を書いた人
ナンデモ系エンジニア
主にAngularでフロントエンド開発することが多いです。 開発環境はLinuxメインで進めているので、シェルコマンドも多用しております。 コツコツとプログラミングするのが好きな人間です。
カテゴリー