-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#8 Add checksum caching per domain for transpiled ts files
You do not normally change _all_ files constantly, so the performance boost should be pretty viable
- Loading branch information
Showing
6 changed files
with
180 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
//import ParseTsModule from "./actions/ParseTsModule.js"; | ||
|
||
const main = () => { | ||
self.importScripts( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import {tryEvalLegacyJsModule} from "../sideEffectModules/sideEffectUtils.js"; | ||
|
||
let whenLib = null; | ||
|
||
/** @return {Promise<ts>} */ | ||
const get = () => { | ||
if (!whenLib) { | ||
if (window.md5) { | ||
whenLib = Promise.resolve(window.md5); | ||
} else { | ||
// kind of lame that typescript does not provide it's own CDN | ||
const url = 'https://cdnjs.cloudflare.com/ajax/libs/blueimp-md5/2.12.0/js/md5.js'; | ||
whenLib = fetch(url) | ||
.then(rs => rs.text()) | ||
.then(jsCode => { | ||
jsCode += '\n//# sourceURL=' + url; | ||
const module = tryEvalLegacyJsModule(jsCode, false); | ||
if (module) { | ||
return module.default; | ||
} else { | ||
return Promise.reject(new Error('Failed to load ' + url)); | ||
} | ||
}); | ||
} | ||
} | ||
return whenLib; | ||
}; | ||
|
||
export default { | ||
get: get, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import {tryEvalLegacyJsModule} from "../sideEffectModules/sideEffectUtils.js"; | ||
|
||
let whenLib = null; | ||
|
||
/** @return {Promise<ts>} */ | ||
const get = () => { | ||
if (!whenLib) { | ||
if (window.ts) { | ||
whenLib = Promise.resolve(window.ts); | ||
} else { | ||
// kind of lame that typescript does not provide it's own CDN | ||
const url = 'https://klesun-misc.github.io/TypeScript/lib/typescriptServices.js'; | ||
whenLib = fetch(url) | ||
.then(rs => rs.text()) | ||
.then(jsCode => { | ||
jsCode += '\nwindow.ts = ts;'; | ||
jsCode += '\n//# sourceURL=' + url; | ||
const module = tryEvalLegacyJsModule(jsCode, false); | ||
if (module) { | ||
return module.default; | ||
} else { | ||
return Promise.reject(new Error('Failed to load ' + url)); | ||
} | ||
}); | ||
} | ||
} | ||
return whenLib; | ||
}; | ||
|
||
export default { | ||
get: get, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
|
||
export const tryEvalLegacyJsModule = (jsCode, silent = true) => { | ||
try { | ||
// trying to support non es6 modules | ||
const globalsBefore = new Set(Object.keys(window)); | ||
const self = {}; | ||
const evalResult = eval.apply(self, [jsCode]); | ||
const newGlobals = Object.keys(window) | ||
.filter(k => !globalsBefore.has(k)); | ||
const result = {}; | ||
for (const name of newGlobals) { | ||
result[name] = window[name]; | ||
} | ||
if (new Set(newGlobals.map(g => window[g])).size === 1) { | ||
result['default'] = window[newGlobals[0]]; | ||
} | ||
const name = jsCode.slice(-100).replace(/[\s\S]*\//, ''); | ||
console.debug('side-effects js lib loaded ' + name, { | ||
newGlobals, evalResult, self, | ||
}); | ||
if (newGlobals.length === 0) { | ||
const msg = 'warning: imported lib ' + name + ' did not add any keys to window. ' + | ||
'If it is imported in both html and js, you can only use it with side-effects ' + | ||
'import like `import "someLib.js"; const someLib = window.someLib;`'; | ||
console.warn(msg); | ||
return {warning: msg, self}; | ||
} else { | ||
return result; | ||
} | ||
} catch (exc) { | ||
if (silent) { | ||
// Unexpected token 'import/export' - means it is a es6 module | ||
return null; | ||
} else { | ||
throw exc; | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters