-
-
Notifications
You must be signed in to change notification settings - Fork 167
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: check if js needs compilation (#365)
- Loading branch information
1 parent
b32b135
commit 4853443
Showing
5 changed files
with
127 additions
and
35 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
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,36 @@ | ||
export const time = <Argument>( | ||
name: string, | ||
_function: (...args: Argument[]) => unknown, | ||
) => function ( | ||
this: unknown, | ||
...args: Argument[] | ||
) { | ||
const timeStart = Date.now(); | ||
const logTimeElapsed = () => { | ||
const elapsed = Date.now() - timeStart; | ||
|
||
if (elapsed > 10) { | ||
// console.log({ | ||
// name, | ||
// args, | ||
// elapsed, | ||
// }); | ||
} | ||
}; | ||
|
||
const result = Reflect.apply(_function, this, args); | ||
if ( | ||
result | ||
&& typeof result === 'object' | ||
&& 'then' in result | ||
) { | ||
(result as Promise<unknown>).then( | ||
logTimeElapsed, | ||
// Ignore error in this chain | ||
() => {}, | ||
); | ||
} else { | ||
logTimeElapsed(); | ||
} | ||
return result; | ||
}; |
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,24 @@ | ||
import { parseEsm } from './es-module-lexer'; | ||
/* | ||
TODO: Add tests | ||
Catches: | ||
import a from 'b' | ||
import 'b'; | ||
import('b'); | ||
export{a}; | ||
export default a; | ||
Doesn't catch: | ||
EXPORT{a} | ||
exports.a = 1 | ||
module.exports = 1 | ||
*/ | ||
const esmPattern = /\b(?:import|export)\b/; | ||
|
||
export const isESM = (code: string) => { | ||
if (esmPattern.test(code)) { | ||
const [imports, exports] = parseEsm(code); | ||
return imports.length > 0 || exports.length > 0; | ||
} | ||
return false; | ||
}; |
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