Skip to content

Commit b992200

Browse files
committed
WIP convert Svelte files to Svelte 5
1 parent 4a65845 commit b992200

11 files changed

+81
-70
lines changed

.gitignore

+4-2
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,7 @@
2020
/docs/.quarto/
2121
/docs/.sverto/
2222

23-
# sverto extension in docs site (we get it from project root on render)
24-
/docs/_extensions/sverto
23+
# sverto extension + files in docs site (we get it from project root on render)
24+
/docs/_extensions/sverto
25+
/docs/package.json
26+
/docs/package-lock.json

Circles.svelte

+5-7
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,14 @@
66
77
// here we declare `data` as a prop that this component can expect. it has
88
// a default value in case we don't provide anything
9-
// https://svelte.dev/tutorial/declaring-props
10-
export let data = [5, 15, 10, 12, 14];
11-
12-
// prefix a statement with $: to make it reactive (so it runs every time
13-
// data changes)
14-
// https://svelte.dev/tutorial/reactive-statements
15-
$: console.log("Dataset prop:", data);
9+
10+
let { data = [5, 15, 10, 12, 14] } = $props();
1611
1712
</script>
1813

14+
<!-- use @debug to log changes in an object to the developer console -->
15+
{@debug data}
16+
1917
<!-- we use svelte's in/out transitions for entering and exiting dom elements,
2018
and vanilla css transitions for retained elements that change. the
2119
#each block means we create an svg <circle> for each element of data -->

_extensions/sverto/rollup.config.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import svelte from "rollup-plugin-svelte"
22
import commonjs from "@rollup/plugin-commonjs"
3-
import resolve from "@rollup/plugin-node-resolve"
3+
import { nodeResolve } from "@rollup/plugin-node-resolve"
44
import terser from "@rollup/plugin-terser"
55

66
const path = require('node:path')
@@ -51,7 +51,7 @@ export default cmd => {
5151
dev: !production,
5252
}
5353
}),
54-
resolve({
54+
nodeResolve({
5555
browser: true,
5656
dedupe: ["svelte"]
5757
}),

_extensions/sverto/sverto-prerender.lua

+2-1
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,8 @@ cmd =
134134
"npm run build " ..
135135
rollup_config .. " -- " ..
136136
'--configQuartoOutPath="' .. os.getenv("QUARTO_PROJECT_OUTPUT_DIR") .. '" ' ..
137-
'--configSvelteInPaths="' .. svelte_path_string .. '"'
137+
'--configSvelteInPaths="' .. svelte_path_string .. '" ' ..
138+
'--bundleConfigAsCjs'
138139

139140
local svelteResult = os.execute(cmd)
140141

_extensions/sverto/sverto.lua

+2-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@ function inject_svelte_and_compile(m)
9999
local svelteCommand =
100100
"npm run build rollup.config.js -- " ..
101101
'--configQuartoOutPath="./" ' ..
102-
'--configSvelteInPaths="' .. sveltePaths .. '"'
102+
'--configSvelteInPaths="' .. sveltePaths .. '" ' ..
103+
'--bundleConfigAsCjs'
103104
local svelteResult = os.execute(svelteCommand)
104105
quarto.log.warning("Svelte compiler finished with code " .. svelteResult)
105106

docs/_quarto.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
project:
22
type: sverto
3-
pre-render:
4-
- './copy-extension.sh'
3+
# pre-render:
4+
# - './copy-extension.sh'
55

66
execute:
77
freeze: auto

docs/copy-extension.sh

+5-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
if [ "$QUARTO_PROJECT_RENDER_ALL" = 1 ]; then
77
mkdir -p _extensions/
88
cp -Rf ../_extensions/sverto _extensions/
9-
cp ../package.json package.json
109
echo "> Sverto extension retrieved"
10+
11+
if [ ! -f ../package.json ]; then
12+
echo "> Sverto package.json not found; also copying"
13+
cp ../package.json package.json
14+
fi
1115
fi

docs/examples/barchart/BarChart.svelte

+12-11
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,33 @@
11
<script>
2+
23
/* let's borrow svelte's fly transitions for the circles that need to be
34
created or destroyed
45
https://svelte.dev/tutorial/transition */
56
import { fly } from "svelte/transition";
67
7-
/* these are our props and their default values (starting with `export let`).
8-
in particular, note that data is an array of numbers
9-
https://svelte.dev/tutorial/declaring-props */
10-
export let data = [50, 45, 15, 25, 30];
11-
export let height = 100;
12-
export let width = 400;
13-
export let barWidth = 25;
14-
export let barColor = "black"
8+
let {
9+
data = [50, 45, 15, 25, 30],
10+
height = 100,
11+
width = 400,
12+
barWidth = 25,
13+
barColor = "black"
14+
} = $props();
1515
1616
/* unlike ojs, svelte code isn't "naturally" reactive (except down below in
1717
the rendered html or svg), but you can prefix a statement with `$:` to
1818
make it reactive (so it runs every time `data` changes).
1919
here we're going to normalise our data to 100, and we'll use the normalised
2020
data in our barchart instead
2121
https://svelte.dev/tutorial/reactive-statements */
22-
$: normalisedData = data.map(d => ({
22+
let normalisedData = $derived(data.map(d => ({
2323
y: d,
2424
h: d / Math.max(...data) * height
25-
}));
26-
$: console.log(normalisedData);
25+
})));
2726
2827
</script>
2928

29+
{@debug normalisedData}
30+
3031
<!-- these css styles just apply to our component -->
3132
<style>
3233
text {

docs/examples/time-series/TimeSeriesChart.svelte

+38-34
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,66 @@
11
<script>
2+
23
import { blur } from "svelte/transition"
34
import { extent } from "d3-array"
45
import { scaleLinear, scaleSequential } from "d3-scale"
5-
import { interpolateYlGnBu, interpolateYlOrRd, select, axisLeft, axisBottom, format, tickFormat, formatLocale } from "d3"
6-
7-
// should be an array of objects with:
8-
// year
9-
// value
10-
export let data = []
11-
export let valueSuffix = "°C"
12-
export let colourScheme = "cool" // or warm
13-
$: console.log(colourScheme)
14-
$: colourRamp = (colourScheme == "cool") ?
6+
import { interpolateYlGnBu, interpolateYlOrRd, select, axisLeft, axisBottom, format, formatLocale } from "d3"
7+
8+
// `data` should be an array of objects with:
9+
// - year
10+
// - value
11+
let { data = [], valueSuffix = "°C", colourScheme = "cool" } = $props();
12+
13+
let colourRamp = $derived((colourScheme == "cool") ?
1514
interpolateYlGnBu :
16-
interpolateYlOrRd
15+
interpolateYlOrRd)
1716
1817
// dimensions bound to size of container
19-
let height = 500
20-
let width = 300
18+
let height = $state(500)
19+
let width = $state(300)
2120
2221
// add padding to chart
23-
$: padX = [60, width - 10]
24-
$: padY = [height - 30, 10]
22+
let padX = $derived([60, width - 10])
23+
let padY = $derived([height - 30, 10])
2524
26-
$: xDomain = extent(data.map(d => d.year))
27-
$: yDomain = extent(data.map(d => d.value))
25+
let xDomain = $derived(extent(data.map(d => d.year)))
26+
let yDomain = $derived(extent(data.map(d => d.value)))
2827
2928
// scales (flip the colours if they're cool)
30-
$: xScale = scaleLinear()
29+
let xScale = $derived(scaleLinear()
3130
.domain(xDomain)
32-
.range(padX)
33-
$: yScale = scaleLinear()
31+
.range(padX))
32+
let yScale = $derived(scaleLinear()
3433
.domain(yDomain)
35-
.range(padY)
36-
$: colourScale = scaleSequential()
34+
.range(padY))
35+
let colourScale = $derived(scaleSequential()
3736
.domain(colourScheme == "cool" ? yDomain.reverse() : yDomain)
38-
.interpolator(colourRamp)
37+
.interpolator(colourRamp))
3938
4039
// temperature formatter (for x-axis)
4140
const tempFormat = formatLocale({
4241
currency: ["", valueSuffix]
4342
});
4443
4544
// axes
46-
let xAxisGroup
47-
let yAxisGroup
48-
$: select(xAxisGroup)
49-
.transition()
50-
.duration(500)
51-
.call(axisBottom(xScale).tickFormat(format(".0f")))
52-
$: select(yAxisGroup)
53-
.transition()
54-
.duration(500)
55-
.call(axisLeft(yScale).tickFormat(tempFormat.format("$.1f")))
45+
let xAxisGroup = $state()
46+
let yAxisGroup = $state()
47+
$effect(() => {
48+
select(xAxisGroup)
49+
.transition()
50+
.duration(500)
51+
.call(axisBottom(xScale).tickFormat(format(".0f")))
52+
});
53+
$effect(() => {
54+
select(yAxisGroup)
55+
.transition()
56+
.duration(500)
57+
.call(axisLeft(yScale).tickFormat(tempFormat.format("$.1f")))
58+
});
5659
5760
</script>
5861

62+
{@debug colourScheme}
63+
5964
<style>
6065
6166
svg circle {
@@ -70,7 +75,6 @@
7075
font-size: 14px;
7176
}
7277
73-
7478
</style>
7579

7680
<main bind:clientHeight={height} bind:clientWidth={width}>

docs/index.qmd

+3-3
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ This will add the extension itself (which includes some project scripts) to the
3636

3737
When you use the Sverto template in a project, it creates some files for you:
3838

39-
* [`example.qmd`](./example.qmd): an example Quarto doc that uses a Svelte component. If you're adding Sverto to an existing document, you can delete this.
40-
* [`Circles.svelte`](./Circles.svelte): an example Svelte visualisation. If you have a Svelte component that you want to use instead, you can delete this.
41-
* [`package.json`](./package.json): this is used to keep track of the dependencies of your Svelte components. **You should add this to version control.**
39+
* `example.qmd`: an example Quarto doc that uses a Svelte component. If you're adding Sverto to an existing document, you can delete this.
40+
* `Circles.svelte`: an example Svelte visualisation. If you have a Svelte component that you want to use instead, you can delete this.
41+
* `package.json`: this is used to keep track of the dependencies of your Svelte components. **You should add this to version control.**
4242
* `package-lock.json` is created once you run `npm install`. You should add this to version control.
4343
* `node_modules/`: This folder is created once you rum `npm install`. Don't add it to version control.
4444

package.json

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
{
22
"name": "svelte-app",
3-
"version": "1.0.0",
3+
"version": "2.0.0",
44
"type": "module",
55
"private": true,
66
"scripts": {
77
"build": "rollup -c",
88
"dev": "rollup -w -c"
99
},
1010
"devDependencies": {
11-
"@rollup/plugin-commonjs": "^17.0.0",
12-
"@rollup/plugin-node-resolve": "^11.0.0",
11+
"@rollup/plugin-commonjs": "^28.0.0",
1312
"@rollup/plugin-terser": "^0.4.4",
14-
"rollup": "^2.3.4",
15-
"rollup-plugin-svelte": "^7.0.0",
16-
"svelte": "^3.0.0"
13+
"@rollup/plugin-node-resolve": "^16.0.0",
14+
"rollup": "^4.22.0",
15+
"rollup-plugin-svelte": "^7.2.2",
16+
"svelte": "^5.0.0"
1717
}
1818
}

0 commit comments

Comments
 (0)