Skip to content

Commit

Permalink
More work on creating a separate sqlite3.js build which is hopefully …
Browse files Browse the repository at this point in the history
…friendly to JS bundlers.
  • Loading branch information
sgbeal committed Jan 27, 2023
1 parent 3435f7a commit 19fd1c4
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 12 deletions.
4 changes: 3 additions & 1 deletion ext/wasm/GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,9 @@ $(sqlite3-bundler-friendly.mjs):
# (and harmless, just a waste of build time).
$(sqlite3.wasm): $(sqlite3.js)
$(sqlite3.mjs): $(sqlite3.js)
$(sqlite3-bundler-friendly.mjs): $(sqlite3.js)
$(sqlite3-bundler-friendly.mjs): $(sqlite3.mjs)
# maintenance reminder: the deps on ^^^ must all be such that they are
# never built in parallel.
CLEAN_FILES += $(sqlite3.js) $(sqlite3.mjs) $(sqlite3-bundler-friendly.mjs) \
$(sqlite3.wasm)
all: $(sqlite3.js) $(sqlite3.mjs) $(sqlite3-bundler-friendly.mjs)
Expand Down
15 changes: 12 additions & 3 deletions ext/wasm/api/extern-post-js.c-pp.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,23 @@ const toExportForES6 =
moduleScript: self?.document?.currentScript,
isWorker: ('undefined' !== typeof WorkerGlobalScope),
location: self.location,
urlParams: new URL(self.location.href).searchParams
urlParams:
//#if target=es6-bundler-friendly
undefined
//#else
new URL(self.location.href).searchParams
//#endif
});
initModuleState.debugModule =
(new URL(self.location.href).searchParams).has('sqlite3.debugModule')
//#if target=es6-bundler-friendly
()=>{}
//#else
(new URL(self.location.href).searchParams).has('sqlite3.debugModule')
? (...args)=>console.warn('sqlite3.debugModule:',...args)
: ()=>{};
//#endif

if(initModuleState.urlParams.has('sqlite3.dir')){
if(initModuleState.urlParams && initModuleState.urlParams.has('sqlite3.dir')){
initModuleState.sqlite3Dir = initModuleState.urlParams.get('sqlite3.dir') +'/';
}else if(initModuleState.moduleScript){
const li = initModuleState.moduleScript.src.split('/');
Expand Down
5 changes: 4 additions & 1 deletion ext/wasm/api/pre-js.c-pp.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
*/

// See notes in extern-post-js.js
const sqlite3InitModuleState = self.sqlite3InitModuleState || Object.create(null);
const sqlite3InitModuleState = self.sqlite3InitModuleState
|| Object.assign(Object.create(null),{
debugModule: ()=>{}
});
delete self.sqlite3InitModuleState;
sqlite3InitModuleState.debugModule('self.location =',self.location);

Expand Down
21 changes: 14 additions & 7 deletions ext/wasm/api/sqlite3-vfs-opfs.c-pp.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,20 @@ const installOpfsVfs = function callee(options){
if(!options || 'object'!==typeof options){
options = Object.create(null);
}
const urlParams = new URL(self.location.href).searchParams;
if(undefined===options.verbose){
options.verbose = urlParams.has('opfs-verbose')
? (+urlParams.get('opfs-verbose') || 2) : 1;
}
if(undefined===options.sanityChecks){
options.sanityChecks = urlParams.has('opfs-sanity-check');
const urlParams =
//#if target=es6-bundler-friendly
undefined;
//#else
new URL(self.location.href).searchParams;
//#endif
if(urlParams){
if(undefined===options.verbose){
options.verbose = urlParams.has('opfs-verbose')
? (+urlParams.get('opfs-verbose') || 2) : 1;
}
if(undefined===options.sanityChecks){
options.sanityChecks = urlParams.has('opfs-sanity-check');
}
}
if(undefined===options.proxyUri){
options.proxyUri = callee.defaultProxyUri;
Expand Down
4 changes: 4 additions & 0 deletions ext/wasm/api/sqlite3-worker1-promiser.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,9 @@ self.sqlite3Worker1Promiser = function callee(config = callee.defaultConfig){
}/*sqlite3Worker1Promiser()*/;
self.sqlite3Worker1Promiser.defaultConfig = {
worker: function(){
//#if target=es6-bundler-friendly
return new Worker("sqlite3-worker1.js");
//#else
let theJs = "sqlite3-worker1.js";
if(this.currentScript){
const src = this.currentScript.src.split('/');
Expand All @@ -252,6 +255,7 @@ self.sqlite3Worker1Promiser.defaultConfig = {
}
}
return new Worker(theJs + self.location.search);
//#endif
}.bind({
currentScript: self?.document?.currentScript
}),
Expand Down
4 changes: 4 additions & 0 deletions ext/wasm/api/sqlite3-worker1.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,17 @@
*/
"use strict";
(()=>{
//#if target=es6-bundler-friendly
importScripts('sqlite3.js');
//#else
const urlParams = new URL(self.location.href).searchParams;
let theJs = 'sqlite3.js';
if(urlParams.has('sqlite3.dir')){
theJs = urlParams.get('sqlite3.dir') + '/' + theJs;
}
//console.warn("worker1 theJs =",theJs);
importScripts(theJs);
//#endif
sqlite3InitModule().then((sqlite3)=>{
sqlite3.initWorker1API();
});
Expand Down

0 comments on commit 19fd1c4

Please sign in to comment.