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

debug/playground #1735

Open
wants to merge 2 commits into
base: v5-candidate
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@

/plugin-examples
/packages/create-lwc-plugin
/debug/**
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ npm-debug.log*
package-lock.json

/debug/**/*
!/debug/playground
!/debug/README.md
!/debug/debug-standalone.html.example
!/debug/debug-esm.html.example
Expand Down
3 changes: 3 additions & 0 deletions debug/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ This folder is designed for creating and testing debug files that won't be
committed to the repository. It's a safe space for contributors to experiment
and test without affecting the main codebase.

If you looking for development environment akin to blitzstack or codesandbox, please
see [PLAYGROUND/README.md](./PLAYGROUND).

## Contents

This folder contains example files to help you get started:
Expand Down
15 changes: 15 additions & 0 deletions debug/playground/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/* eslint-env node */

module.exports = {
env: {
browser: true,
node: false,
},
rules: {
'no-console': 0,
},
globals: {
LightweightCharts: false,
chart: false,
},
};
7 changes: 7 additions & 0 deletions debug/playground/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
node_modules/
dist/
typings/
index.html
wwwroot/*
package.json
package-lock.json
28 changes: 28 additions & 0 deletions debug/playground/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# About

The playground is designed for running your snippet continuously against a local build of the `lightweight-charts` package. It's a space for contributors to validate local changes or experiment interactively with charts.

The snippet is served using the `wwwroot` folder. Every modification triggers live reload to reflect your changes instantly at [http://localhost:5173](http://localhost:5173).

## Usage

To start the playground, run:

```bash
npm run dev
```

To start the playground from a clean state, run:

```bash
npm run dev:clean
```

## Details

| Name | Description |
| ---- | ----------- |
| Extra packages | If you need extra dependencies in your playground, feel free to install them. These will not be committed. |
| Entrypoint location | The entry point for the playground is `wwwroot/index.html`, which loads `wwwroot/src/main.tsx`. |
| Sources root | The source files for running snippets in the playground are located in the `wwwroot/src/` folder. The `wwwroot/src/main.tsx` script is loaded automatically. |
| Ignored paths | `package.json` and `wwwroot/` |
15 changes: 15 additions & 0 deletions debug/playground/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "lwc-playground",
"private": true,
"version": "0.0.0",
"type": "module",
"dependencies": {
"eslint": "^9.15.0",
"typescript": "^5.6.3",
"vite": "^5.4.11"
},
"scripts": {
"dev": "/bin/sh ./playground.sh",
"dev:clean": "/usr/bin/env CLEAN=1 /bin/sh ./playground.sh"
}
}
49 changes: 49 additions & 0 deletions debug/playground/playground.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/bin/sh

set -eu

: "${WWW:=wwwroot}"
: "${CLEAN:=}"
: "${HOST:=localhost}"
: "${PORT:=5173}"

npm() {
printf "[playground] npm %s\n" "$*" >&2
command npm "$@"
}

cd() {
printf "\n[playground] cd %s\n" "$*" >&2
builtin cd "$@"
}

lwc="$(git rev-parse --show-toplevel)"
playground="$(dirname $(readlink -f $0))"

cd "$lwc"
if [ ! -e "$(npm prefix -g)/lib/node_modules/lightweight-charts" ]; then
npm link >/dev/null 2>&1
fi

cd "$playground"
if [ "$CLEAN" = "1" ]; then
rm -fr ./node_modules ./package-lock.json
fi

if [ ! -d node_modules ]; then
npm install --no-audit
npm link lightweight-charts >/dev/null 2>&1
fi

cd "$WWW"

if [ ! -e index.html ]; then
cp index.html.example index.html
cp src/main.ts.example src/main.ts
fi

npm exec vite -- "." \
--clearScreen=false \
--logLevel=info \
--host $HOST \
--port $PORT
26 changes: 26 additions & 0 deletions debug/playground/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"compilerOptions": {
"target": "ESNext",
"useDefineForClassFields": true,
"module": "ESNext",
"lib": ["ESNext", "DOM"],
"moduleResolution": "Node",
"strict": true,
"resolveJsonModule": true,
"isolatedModules": true,
"esModuleInterop": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitReturns": true,
"skipLibCheck": true,
"declaration": true,
"declarationDir": "typings",
"emitDeclarationOnly": true
},
"include": [
"./**/*.ts",
"./**/*.tsx",
"./**/*.js",
"./**/*.jsx",
]
}
15 changes: 15 additions & 0 deletions debug/playground/wwwroot/index.html.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!doctype html>
<html lang="en">

<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>lwc-playground</title>
</head>

<body>
<div id="container"></div>
<script type="module" src="/src/main.ts"></script>
</body>

</html>
35 changes: 35 additions & 0 deletions debug/playground/wwwroot/src/main.ts.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { createChart, LineSeries } from 'lightweight-charts';

// Chart
const chart = createChart(document.querySelector<HTMLDivElement>('#container')!, {
width: 1024,
height: 768,
});

// Series
const series = chart.addSeries(LineSeries);

// Data
const data = genData(new Date(2024, 0, 1).getTime(), 100, 2, 10);
series.setData(data.map(item => ({ ...item })))
chart.timeScale().fitContent()

// Update
setInterval(() => {
const last = data.at(-1);
const [item] = genData(new Date(last.time).getTime(), last.value, 1.5, 1)
series.update({ ...item });
data.push(item);
}, 1500)

function genData(startTs: number, startPrice: number, volatility: number, total: number = 100) {
let currentPrice = startPrice;
const data: any = [];
for (let i = 1; i <= total; i++) {
data.push({
time: (new Date(startTs + i * 86400000)).toISOString().split('T')[0],
value: (currentPrice = Math.max(0, currentPrice + (Math.random() - 0.5) * volatility)),
});
}
return data;
};
Loading