Skip to content

Commit 81b1517

Browse files
committed
Merge branch 'vite-plugin' (#94)
2 parents 20194d9 + d2c837d commit 81b1517

File tree

6 files changed

+4503
-6301
lines changed

6 files changed

+4503
-6301
lines changed

README.md

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -125,36 +125,29 @@ pnpm install sqlocal
125125

126126
### Cross-Origin Isolation
127127

128-
In order to persist data to the origin private file system, this package relies on APIs that require cross-origin isolation, so the page you use this package on must be served with the following HTTP headers. Otherwise, the browser will block access to the origin private file system.
128+
In order to persist data to the origin private file system, this package relies on APIs that require [cross-origin isolation](https://developer.mozilla.org/en-US/docs/Web/API/Window/crossOriginIsolated), so the page you use this package on must be served with the following HTTP headers. Otherwise, the browser will block access to the origin private file system.
129129

130130
```http
131131
Cross-Origin-Embedder-Policy: require-corp
132132
Cross-Origin-Opener-Policy: same-origin
133133
```
134134

135-
If your development server uses Vite, you can do this by adding the following to your Vite configuration.
136-
137-
```javascript
138-
plugins: [
139-
{
140-
name: 'configure-response-headers',
141-
configureServer: (server) => {
142-
server.middlewares.use((_req, res, next) => {
143-
res.setHeader('Cross-Origin-Embedder-Policy', 'require-corp');
144-
res.setHeader('Cross-Origin-Opener-Policy', 'same-origin');
145-
next();
146-
});
147-
},
148-
},
149-
],
150-
```
135+
How this is configured will depend on what web server or hosting service your application uses. If your development server uses Vite, [see the configuration below](#vite-configuration).
151136

152137
### Vite Configuration
153138

154-
Vite currently has an issue that prevents it from loading web worker files correctly with the default configuration. If you use Vite, please add the below to your Vite configuration to fix this.
139+
Vite needs some additional configuration to handle web worker files correctly. If you or your framework uses Vite as your build tool, you can use SQLocal's Vite plugin to set this up.
140+
141+
The plugin will also enable [cross-origin isolation](#cross-origin-isolation) (required for origin private file system persistence) for the Vite development server by default. Just don't forget to also configure your _production_ web server to use the same HTTP headers.
142+
143+
Import the plugin from `sqlocal/vite` and add it to your [Vite configuration](https://vitejs.dev/config/).
155144

156145
```javascript
157-
optimizeDeps: {
158-
exclude: ['sqlocal'],
159-
},
146+
import { defineConfig } from 'vite';
147+
import sqlocal from 'sqlocal/vite';
148+
149+
export default defineConfig({
150+
plugins: [sqlocal()],
151+
});
160152
```
153+

docs/guide/setup.md

Lines changed: 75 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ pnpm install sqlocal
2424

2525
## Cross-Origin Isolation
2626

27-
In order to persist data to the origin private file system, this package relies on APIs that require cross-origin isolation, so the page you use this package on must be served with the following HTTP headers. Otherwise, the browser will block access to the origin private file system.
27+
In order to persist data to the origin private file system, this package relies on APIs that require [cross-origin isolation](https://developer.mozilla.org/en-US/docs/Web/API/Window/crossOriginIsolated), so the page you use this package on must be served with the following HTTP headers. Otherwise, the browser will block access to the origin private file system.
2828

2929
```http
3030
Cross-Origin-Embedder-Policy: require-corp
@@ -80,27 +80,84 @@ export const db = new SQLocal({
8080

8181
## Vite Configuration
8282

83-
Vite currently has an issue that prevents it from loading web worker files correctly with the default configuration. If you use Vite, please add the below to your [Vite configuration](https://vitejs.dev/config/) to fix this. Don't worry: it will have no impact on production performance.
83+
Vite needs some additional configuration to handle web worker files correctly. If you or your framework uses Vite as your build tool, you can use SQLocal's Vite plugin to set this up.
8484

85-
```javascript
86-
optimizeDeps: {
87-
exclude: ['sqlocal'],
88-
},
89-
```
85+
The plugin will also enable [cross-origin isolation](#cross-origin-isolation) (required for origin private file system persistence) for the Vite development server by default. Just don't forget to also configure your _production_ web server to use the same HTTP headers.
9086

91-
To enable cross-origin isolation (required for origin private file system persistence) for the Vite development server, you can add this to your Vite configuration. Just don't forget to also configure your _production_ web server to use the same HTTP headers.
87+
Import the plugin from `sqlocal/vite` and add it to your [Vite configuration](https://vitejs.dev/config/).
9288

9389
```javascript
94-
plugins: [
95-
{
96-
name: 'configure-response-headers',
97-
configureServer: (server) => {
98-
server.middlewares.use((_req, res, next) => {
99-
res.setHeader('Cross-Origin-Embedder-Policy', 'require-corp');
100-
res.setHeader('Cross-Origin-Opener-Policy', 'same-origin');
101-
next();
102-
});
90+
import { defineConfig } from 'vite';
91+
import sqlocal from 'sqlocal/vite';
92+
93+
export default defineConfig({
94+
plugins: [sqlocal()],
95+
});
96+
```
97+
98+
::: details Angular
99+
100+
Since Angular does not expose its Vite configuration, you will need to configure it differently than other Vite-based frameworks. You can reference the [SQLocal Shell codebase](https://github.com/DallasHoff/sqlocal-shell) as an example.
101+
102+
First, it's important to disable prebundling in the development server so that Vite compiles the web workers correctly. You can do this in your serve architect in `angular.json`. This is also where you can set the headers for cross-origin isolation in development.
103+
104+
```json{8-12}
105+
"architect": {
106+
"build": { ... },
107+
"serve": {
108+
"builder": "@angular/build:dev-server",
109+
"configurations": {
110+
"development": {
111+
"buildTarget": "my-app:build:development",
112+
"headers": {
113+
"Cross-Origin-Embedder-Policy": "require-corp",
114+
"Cross-Origin-Opener-Policy": "same-origin"
115+
},
116+
"prebundle": false
117+
}
103118
},
119+
"defaultConfiguration": "development"
120+
}
121+
}
122+
```
123+
124+
You will also need to serve `sqlite3.wasm` from the root of your site. You can configure this in your build architect in `angular.json`.
125+
126+
```json{10-13}
127+
"architect": {
128+
"build": {
129+
"builder": "@angular/build:application",
130+
"options": {
131+
"outputPath": "dist/my-app",
132+
"index": "src/index.html",
133+
"browser": "src/main.ts",
134+
"tsConfig": "tsconfig.app.json",
135+
"assets": [
136+
{
137+
"glob": "**/*",
138+
"input": "node_modules/@sqlite.org/sqlite-wasm/sqlite-wasm/jswasm"
139+
},
140+
{
141+
"glob": "**/*",
142+
"input": "public"
143+
}
144+
],
145+
"styles": ["src/styles.scss"],
146+
"scripts": [],
147+
"webWorkerTsConfig": "tsconfig.worker.json"
148+
}
104149
},
105-
],
150+
"serve": { ... }
151+
}
106152
```
153+
154+
Finally, you will need to create your own web worker file and pass it to the `SQLocal` constructor, rather than use the built-in worker. Run `ng create web-worker sqlocal` to make a web worker file, add [this code](https://github.com/DallasHoff/sqlocal-shell/blob/main/src/sqlocal.worker.ts), and pass the worker to SQLocal's `processor` option.
155+
156+
```typescript
157+
const db = new SQLocal({
158+
databasePath: 'database.sqlite3',
159+
processor: new Worker(new URL('../sqlocal.worker', import.meta.url)),
160+
});
161+
```
162+
163+
:::

0 commit comments

Comments
 (0)