Skip to content

Commit d958b47

Browse files
committed
dexie-cloud + SW build scripts & bundling
1 parent 6510b96 commit d958b47

File tree

8 files changed

+132
-86
lines changed

8 files changed

+132
-86
lines changed

.prettierrc.yml

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
trailingComma: 'none'
2+
tabWidth: 2
3+
semi: true
4+
singleQuote: true

addons/dexie-cloud/copydts.sh

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/bash -e
2+
rm -rf dist/types
3+
mkdir dist/types
4+
cd tools/tmp/modern
5+
find . -name '*.d.ts' | cpio -pdm ../../../dist/types/

addons/dexie-cloud/package.json

+41-16
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22
"name": "dexie-cloud-addon",
33
"version": "1.0.0-alpha.7",
44
"description": "Dexie addon that syncs with to Dexie Cloud",
5-
"main": "dist/dexie-cloud.js",
6-
"module": "dist/dexie-cloud.mjs",
7-
"esnext": "esnext/dexie-cloud.js",
8-
"typings": "dist/dexie-cloud.d.ts",
5+
"main": "dist/umd/dexie-cloud-addon.min.js",
6+
"module": "dist/module-es5/dexie-cloud-addon.min.js",
7+
"exports": {
8+
".": "./dist/modern/dexie-cloud-addon.min.js",
9+
"./service-worker": "./dist/modern/service-worker.min.js"
10+
},
11+
"types": "dist/types/dexie-cloud-client.d.ts",
912
"engines": {
1013
"node": ">=14"
1114
},
@@ -16,9 +19,10 @@
1619
"scripts": {
1720
"test": "just-build test && npm run test-unit",
1821
"test-unit": "karma start test/unit/karma.conf.js --single-run",
19-
"build": "just-build",
22+
"build": "just-build && bash ./copydts.sh",
2023
"watch": "just-build --watch",
21-
"clean": "rm -rf tools/tmp dist esnext test/unit/bundle.*"
24+
"clean": "rm -rf tools/tmp dist test/unit/bundle.*",
25+
"copydts": "bash ./copydts.sh"
2226
},
2327
"just-build": {
2428
"default": [
@@ -27,22 +31,43 @@
2731
"release": [
2832
"# Build outputs",
2933
"just-build src",
34+
"just-build dexie-cloud service-worker",
3035
"# Minify the default ES5 UMD module",
31-
"cd dist",
32-
"uglifyjs dexie-cloud.js -m -c negate_iife=0 -o dexie-cloud.min.js --source-map",
33-
"cd ..",
34-
"terser --compress --mangle --source-map -o esnext/dexie-cloud.min.js -- esnext/dexie-cloud.js",
35-
"terser --compress --mangle --source-map -o dist/dexie-cloud.min.js -- dist/dexie-cloud.mjs"
36+
"cd dist/umd",
37+
"# Minify ES5 UMD bundle",
38+
"uglifyjs dexie-cloud-addon.js -m -c negate_iife=0 -o dexie-cloud-addon.min.js --source-map",
39+
"cd ../..",
40+
"#dts-bundle-generator --external-imports=dexie --project src/tsconfig.json -o dist/dexie-cloud-addon.d.ts tools/tmp/modern/dexie-cloud-client.d.ts",
41+
"# Minify modern bundle",
42+
"terser --comments false --compress --mangle --source-map -o dist/modern/dexie-cloud-addon.min.js -- dist/modern/dexie-cloud-addon.js",
43+
"# Minify module bundle",
44+
"terser --comments false --compress --mangle --source-map -o dist/module-es5/dexie-cloud-addon.min.js -- dist/module-es5/dexie-cloud-addon.js",
45+
"# Minify modern service-worker",
46+
"terser --comments false --compress --mangle --source-map -o dist/modern/service-worker.min.js -- dist/modern/service-worker.js",
47+
"# Minify umd service-worker",
48+
"terser --comments false --compress --mangle --source-map -o dist/umd/service-worker.min.js -- dist/umd/service-worker.js"
3649
],
3750
"src": [
38-
"tsc -p src -t esnext --outDir tools/tmp/esnext [--watch 'Compilation complete.']",
39-
"rollup -c tools/build-configs/rollup.esnext.config.js",
40-
"tsc --allowJs -m es2015 -t es5 --sourceMap --outDir tools/tmp/es5 esnext/dexie-cloud.js",
51+
"# Build the entire typescript source into modern JS",
52+
"tsc -p src -t esnext --outDir tools/tmp/modern [--watch 'Compilation complete.']"
53+
],
54+
"dexie-cloud": [
55+
"# Create a modern bundle in dist/modern",
56+
"rollup -c tools/build-configs/rollup.modern.config.js",
57+
"# Transpile the modern dexie-cloud bundle into ES5 JS",
58+
"tsc --allowJs -m es2015 -t es5 --sourceMap --outDir tools/tmp/es5 dist/modern/dexie-cloud-addon.js",
59+
"# Create ES5 bundles: One UMD and one MJS",
4160
"rollup -c tools/build-configs/rollup.es5.config.js",
4261
"# Copy dexie-cloud-client.d.ts to dist",
43-
"node -e \"fs.writeFileSync('dist/dexie-cloud.d.ts', fs.readFileSync('tools/tmp/esnext/dexie-cloud-client.d.ts'))\"",
62+
"#node -e \"fs.writeFileSync('dist/dexie-cloud-addon.d.ts', fs.readFileSync('tools/tmp/modern/dexie-cloud-client.d.ts'))\"",
63+
"# Replace {version} and {date} in output files",
64+
"node tools/replaceVersionAndDate.js dist/umd/dexie-cloud-addon.js dist/module-es5/dexie-cloud-addon.js dist/modern/dexie-cloud-addon.js"
65+
],
66+
"service-worker": [
67+
"# Create a modern bundle in dist/modern",
68+
"rollup -c tools/build-configs/rollup.sw.modern.config.js",
4469
"# Replace {version} and {date} in output files",
45-
"node tools/replaceVersionAndDate.js dist/dexie-cloud.d.ts dist/dexie-cloud.js dist/dexie-cloud.mjs esnext/dexie-cloud.js"
70+
"node tools/replaceVersionAndDate.js dist/umd/service-worker.js dist/modern/service-worker.js"
4671
],
4772
"test": [
4873
"just-build test-unit"

addons/dexie-cloud/src/service-worker.ts

+5-40
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ async function syncDB(dbName: string) {
1515
let db = managedDBs.get(dbName);
1616

1717
if (!db) {
18+
console.debug('Dexie Cloud SW: Creating new Dexie instance for', dbName);
1819
const dexie = new Dexie(dbName, { addons: [dexieCloud] });
1920
db = DexieCloudDB(dexie);
2021
dexie.on("versionchange", stopManagingDB);
@@ -29,7 +30,9 @@ async function syncDB(dbName: string) {
2930
}
3031

3132
try {
33+
console.debug('Dexie Cloud SW: Syncing');
3234
await syncIfPossible(db);
35+
console.debug('Dexie Cloud SW: Done Syncing');
3336
} catch (e) {
3437
console.error(`Dexie Cloud SW Error`, e);
3538
// Error occured. Stop managing this DB until we wake up again by a sync event,
@@ -39,55 +42,17 @@ async function syncDB(dbName: string) {
3942
}
4043

4144
self.addEventListener("sync", (event: SyncEvent) => {
45+
console.debug('SW "sync" Event', event.tag);
4246
const dbName = getDbNameFromTag(event.tag);
4347
if (dbName) {
4448
event.waitUntil(syncDB(dbName));
4549
}
4650
});
4751

4852
self.addEventListener("periodicsync", (event: SyncEvent) => {
53+
console.debug('SW "periodicsync" Event', event.tag);
4954
const dbName = getDbNameFromTag(event.tag);
5055
if (dbName) {
5156
event.waitUntil(syncDB(dbName));
5257
}
5358
});
54-
55-
/*
56-
57-
58-
const subscription = observable.subscribe(async dbs => {
59-
// Open connection (which will start syncWorker for each db)
60-
for (const dbName of dbs) {
61-
const db = new Dexie(dbName, {addons: [dexieCloud]});
62-
const stopManagingDB = async () => {
63-
db.on('versionchange').unsubscribe(onVersionChange);
64-
managedDBs.delete(dbName);
65-
await dexieCloudGlobalDB.swManagedDBs.delete(dbName);
66-
}
67-
const onVersionChange = (event: IDBVersionChangeEvent) => {
68-
if (event.newVersion) {
69-
// Upgrade - reopen our connection
70-
db.close();
71-
db.open().catch(stopManagingDB);
72-
} else {
73-
// Deleted - close our connection and delete name from DexieCloud global DB.
74-
stopManagingDB();
75-
}
76-
return false;
77-
};
78-
if (!managedDBs.has(dbName)) {
79-
managedDBs.set(dbName, DexieCloudDB(db));
80-
db.on('versionchange', onVersionChange);
81-
await db.open().catch(stopManagingDB);
82-
}
83-
}
84-
// Close connection for those DBs that should not be managed by SW anymore:
85-
for (const dbName of [].slice.call(managedDBs.keys())) { // not yet using downlevel iterators (TS)
86-
if (!dbs.includes(dbName)) {
87-
managedDBs.get(dbName)!.close();
88-
managedDBs.delete(dbName);
89-
}
90-
}
91-
});
92-
93-
*/

addons/dexie-cloud/tools/build-configs/banner.txt

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
/* ==========================================================================
2-
* dexie-cloud-client.js
2+
* dexie-cloud-addom.js
33
* ==========================================================================
44
*
5-
* Dexie addon that connects to dexie.cloud.
5+
* Dexie addon that syncs IndexedDB with Dexie Cloud.
66
*
7-
* By David Fahlander, david[email protected],
7+
* By David Fahlander, david@dexie.org
88
*
99
* ==========================================================================
1010
*
1111
* Version {version}, {date}
1212
*
13-
* http://dexie.org
13+
* https://dexie.org
1414
*
1515
* Apache License Version 2.0, January 2004, http://www.apache.org/licenses/
1616
*

addons/dexie-cloud/tools/build-configs/rollup.es5.config.js

+24-23
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,28 @@ import sourcemaps from 'rollup-plugin-sourcemaps';
33
import commonjs from 'rollup-plugin-commonjs';
44
import nodeResolve from 'rollup-plugin-node-resolve';
55

6-
const ERRORS_TO_IGNORE = [
7-
"THIS_IS_UNDEFINED",
8-
];
6+
const ERRORS_TO_IGNORE = ['THIS_IS_UNDEFINED'];
97

108
export default {
11-
input: 'tools/tmp/es5/dexie-cloud.js',
12-
output: [{
13-
file: 'dist/dexie-cloud.js',
14-
format: 'umd',
15-
globals: {
16-
dexie: "Dexie"
9+
input: 'tools/tmp/es5/dexie-cloud-addon.js',
10+
output: [
11+
{
12+
file: 'dist/umd/dexie-cloud-addon.js',
13+
format: 'umd',
14+
globals: {
15+
dexie: 'Dexie'
16+
},
17+
name: 'DexieCloud',
18+
sourcemap: true,
19+
exports: 'named'
1720
},
18-
name: 'DexieCloud',
19-
sourcemap: true,
20-
exports: 'named'
21-
},{
22-
file: 'dist/dexie-cloud.mjs',
23-
format: 'es',
24-
sourcemap: true
25-
}],
26-
external: ['dexie', 'dexie-observable', 'dexie-syncable'],
21+
{
22+
file: 'dist/module-es5/dexie-cloud-addon.js',
23+
format: 'es',
24+
sourcemap: true
25+
}
26+
],
27+
external: ['dexie'],
2728
plugins: [
2829
sourcemaps(),
2930
nodeResolve({
@@ -32,13 +33,13 @@ export default {
3233
}),
3334
commonjs()
3435
],
35-
onwarn ({loc, frame, code, message}) {
36+
onwarn({ loc, frame, code, message }) {
3637
if (ERRORS_TO_IGNORE.includes(code)) return;
37-
if ( loc ) {
38-
console.warn( `${loc.file} (${loc.line}:${loc.column}) ${message}` );
39-
if ( frame ) console.warn( frame );
38+
if (loc) {
39+
console.warn(`${loc.file} (${loc.line}:${loc.column}) ${message}`);
40+
if (frame) console.warn(frame);
4041
} else {
4142
console.warn(`${code} ${message}`);
42-
}
43+
}
4344
}
4445
};

addons/dexie-cloud/tools/build-configs/rollup.esnext.config.js renamed to addons/dexie-cloud/tools/build-configs/rollup.modern.config.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ const ERRORS_TO_IGNORE = [
1010
];
1111

1212
export default {
13-
input: 'tools/tmp/esnext/dexie-cloud-client.js',
13+
input: 'tools/tmp/modern/dexie-cloud-client.js',
1414
output: [{
15-
file: 'esnext/dexie-cloud.js',
15+
file: 'dist/modern/dexie-cloud-addon.js',
1616
format: 'es',
1717
banner: readFileSync(path.resolve(__dirname, 'banner.txt'), "utf-8"),
1818
sourcemap: true
1919
}],
20-
external: ['dexie', 'dexie-observable', 'dexie-syncable'],
20+
external: ['dexie'],
2121
plugins: [
2222
sourcemaps(),
2323
nodeResolve({
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// @ts-check
2+
import sourcemaps from 'rollup-plugin-sourcemaps';
3+
import commonjs from 'rollup-plugin-commonjs';
4+
import nodeResolve from 'rollup-plugin-node-resolve';
5+
import {readFileSync} from 'fs';
6+
import path from 'path';
7+
8+
const ERRORS_TO_IGNORE = [
9+
"THIS_IS_UNDEFINED",
10+
];
11+
12+
export default {
13+
input: 'tools/tmp/modern/service-worker.js',
14+
output: [{
15+
file: 'dist/modern/service-worker.js',
16+
format: 'es',
17+
sourcemap: true
18+
},{
19+
file: 'dist/umd/service-worker.js',
20+
format: 'umd',
21+
globals: {
22+
dexie: 'Dexie'
23+
},
24+
name: 'DexieCloudSW',
25+
sourcemap: true,
26+
exports: 'named'
27+
}],
28+
external: ['dexie'],
29+
plugins: [
30+
sourcemaps(),
31+
nodeResolve({
32+
browser: true,
33+
preferBuiltins: false
34+
}),
35+
commonjs()
36+
],
37+
onwarn ({loc, frame, code, message}) {
38+
if (ERRORS_TO_IGNORE.includes(code)) return;
39+
if ( loc ) {
40+
console.warn( `${loc.file} (${loc.line}:${loc.column}) ${message}` );
41+
if ( frame ) console.warn( frame );
42+
} else {
43+
console.warn(`${code} ${message}`);
44+
}
45+
}
46+
};

0 commit comments

Comments
 (0)