Skip to content

Commit 65ef0e6

Browse files
[Demo] Vite Example (#78)
1 parent 3a1b8fd commit 65ef0e6

File tree

4 files changed

+205
-2
lines changed

4 files changed

+205
-2
lines changed

demos/example-vite/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
"@journeyapps/powersync-sdk-web": "workspace:*"
1414
},
1515
"devDependencies": {
16-
"vite": "^5.0.12"
16+
"vite": "^5.0.12",
17+
"vite-plugin-top-level-await": "^1.4.1",
18+
"vite-plugin-wasm": "^3.3.0"
1719
}
1820
}

demos/example-vite/src/index.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,22 @@ import {
66
Table
77
} from '@journeyapps/powersync-sdk-web';
88

9+
/**
10+
* A placeholder connector which doesn't do anything.
11+
* This is just used to verify that the sync workers can be loaded
12+
* when connecting.
13+
*/
14+
class DummyConnector {
15+
async fetchCredentials() {
16+
return {
17+
endpoint: '',
18+
token: ''
19+
};
20+
}
21+
22+
async uploadData(database) {}
23+
}
24+
925
export const AppSchema = new Schema([
1026
new Table({ name: 'customers', columns: [new Column({ name: 'name', type: ColumnType.TEXT })] })
1127
]);
@@ -29,6 +45,19 @@ const openDatabase = async () => {
2945

3046
const result = await PowerSync.getAll('SELECT * FROM customers');
3147
console.log('contents of customers: ', result);
48+
49+
console.log(
50+
`Attempting to connect in order to verify web workers are correctly loaded.
51+
This doesn't use any actual network credentials.
52+
Network errors will be shown: these can be ignored.`
53+
);
54+
55+
/**
56+
* Try and connect, this will setup shared sync workers
57+
* This will fail due to not having a valid endpoint,
58+
* but it will try - which is all that matters.
59+
*/
60+
await PowerSync.connect(new DummyConnector());
3261
};
3362

3463
document.addEventListener('DOMContentLoaded', (event) => {

demos/example-vite/vite.config.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import wasm from 'vite-plugin-wasm';
2+
import topLevelAwait from 'vite-plugin-top-level-await';
3+
14
import { defineConfig } from 'vite';
25

36
// https://vitejs.dev/config/
@@ -9,7 +12,15 @@ export default defineConfig({
912
input: 'src/index.html'
1013
}
1114
},
15+
optimizeDeps: {
16+
// Don't optimize these packages as they contain web workers and WASM files.
17+
// https://github.com/vitejs/vite/issues/11672#issuecomment-1415820673
18+
exclude: ['@journeyapps/wa-sqlite', '@journeyapps/powersync-sdk-web'],
19+
include: ['object-hash', 'uuid', 'event-iterator', 'js-logger', 'lodash', 'can-ndjson-stream']
20+
},
21+
plugins: [wasm(), topLevelAwait()],
1222
worker: {
13-
format: 'es'
23+
format: 'es',
24+
plugins: () => [wasm(), topLevelAwait()]
1425
}
1526
});

pnpm-lock.yaml

Lines changed: 161 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)