Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update template to Webpack 5 for better experience #36

Merged
merged 8 commits into from
Jan 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 9 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,17 @@
"name": "svelte-app",
"version": "1.0.0",
"devDependencies": {
"cross-env": "^5.2.0",
"css-loader": "^2.1.1",
"mini-css-extract-plugin": "^0.6.0",
"serve": "^11.0.0",
"style-loader": "^0.23.1",
"svelte": "^3.0.0",
"svelte-loader": "2.13.3",
"webpack": "^4.30.0",
"webpack-cli": "^3.3.0",
"webpack-dev-server": "^3.3.1"
"cross-env": "^7.0.3",
"css-loader": "^5.0.1",
"mini-css-extract-plugin": "^1.3.4",
"svelte": "^3.31.2",
"svelte-loader": "^3.0.0",
"webpack": "^5.16.0",
"webpack-cli": "^4.4.0",
"webpack-dev-server": "^3.11.2"
},
"scripts": {
"build": "cross-env NODE_ENV=production webpack",
"dev": "webpack-dev-server --content-base public"
"dev": "webpack serve --content-base public"
}
}
18 changes: 9 additions & 9 deletions public/index.html
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<!doctype html>
<html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset='utf8'>
<meta name='viewport' content='width=device-width'>
<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='global.css'>
<link rel='stylesheet' href='bundle.css'>
<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>
<script src='bundle.js'></script>
</body>
</html>
</html>
25 changes: 22 additions & 3 deletions src/App.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,29 @@
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: purple;
color: #ff3e00;
text-transform: uppercase;
font-size: 4em;
font-weight: 100;
}
</style>

<h1>Hello {name}!</h1>
@media (min-width: 640px) {
main {
max-width: none;
}
}
</style>
14 changes: 8 additions & 6 deletions public/global.css → src/global.css
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ label {
input, button, select, textarea {
font-family: inherit;
font-size: inherit;
-webkit-padding: 0.4em 0;
padding: 0.4em;
margin: 0 0 0.5em 0;
box-sizing: border-box;
Expand All @@ -43,19 +44,20 @@ input:disabled {
color: #ccc;
}

input[type="range"] {
height: 0;
}

button {
color: #333;
background-color: #f4f4f4;
outline: none;
}

button:active {
button:disabled {
color: #999;
}

button:not(:disabled):active {
background-color: #ddd;
}

button:focus {
border-color: #666;
}
}
4 changes: 3 additions & 1 deletion src/main.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import './global.css';

import App from './App.svelte';

const app = new App({
Expand All @@ -9,4 +11,4 @@ const app = new App({

window.app = app;

export default app;
export default app;
31 changes: 20 additions & 11 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ const prod = mode === 'production';

module.exports = {
entry: {
bundle: ['./src/main.js']
'build/bundle': ['./src/main.js']
},
resolve: {
alias: {
svelte: path.resolve('node_modules', 'svelte')
svelte: path.dirname(require.resolve('svelte/package.json'))
},
extensions: ['.mjs', '.js', '.svelte'],
mainFields: ['svelte', 'browser', 'module', 'main']
},
output: {
path: __dirname + '/public',
path: path.join(__dirname, '/public'),
filename: '[name].js',
chunkFilename: '[name].[id].js'
},
Expand All @@ -27,21 +27,27 @@ module.exports = {
use: {
loader: 'svelte-loader',
options: {
emitCss: true,
hotReload: true
compilerOptions: {
dev: !prod
},
emitCss: prod,
hotReload: !prod
}
}
},
{
test: /\.css$/,
use: [
/**
* MiniCssExtractPlugin doesn't support HMR.
* For developing, use 'style-loader' instead.
* */
prod ? MiniCssExtractPlugin.loader : 'style-loader',
MiniCssExtractPlugin.loader,
'css-loader'
]
},
{
// required to prevent errors from Svelte on Webpack 5+
test: /node_modules\/svelte\/.*\.mjs$/,
resolve: {
fullySpecified: false
}
}
]
},
Expand All @@ -51,5 +57,8 @@ module.exports = {
filename: '[name].css'
})
],
devtool: prod ? false: 'source-map'
devtool: prod ? false : 'source-map',
devServer: {
hot: true
}
};