Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
privatenumber committed Nov 6, 2023
1 parent 3d6b56d commit fc46f9f
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 21 deletions.
1 change: 0 additions & 1 deletion src/cjs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import type { TransformOptions } from 'esbuild';
import { installSourceMapSupport } from '../source-map';
import { transformSync, transformDynamicImport } from '../utils/transform';
import { resolveTsPath } from '../utils/resolve-ts-path';
import { time } from '../utils/debug';
import { isESM } from '../utils/esm-pattern';

const isRelativePathPattern = /^\.{1,2}\//;
Expand Down
1 change: 0 additions & 1 deletion src/esm/loaders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import {
type MaybePromise,
type NodeError,
} from './utils.js';
import { time } from '../utils/debug';

const isDirectoryPattern = /\/(?:$|\?)/;

Expand Down
30 changes: 19 additions & 11 deletions src/utils/debug.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,30 @@
export const time = (name: string, fn: Function) => {
return function (this: unknown, ...args: unknown[]) {
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,
// });
// console.log({
// name,
// args,
// elapsed,
// });
}
};

const result = Reflect.apply(fn, this, args);
if (result && 'then' in result) {
result.then(
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
() => {},
Expand All @@ -25,4 +34,3 @@ export const time = (name: string, fn: Function) => {
}
return result;
};
};
2 changes: 1 addition & 1 deletion src/utils/esm-pattern.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ export const isESM = (code: string) => {
return imports.length > 0 || exports.length > 0;
}
return false;
};
};
16 changes: 9 additions & 7 deletions src/utils/transform/transform-dynamic-import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,21 @@ import type { RawSourceMap } from '../../source-map';
import { parseEsm } from '../es-module-lexer';

const handlerName = '___tsxInteropDynamicImport';
const handleEsModuleFunction = 'function ' + handlerName + (function (imported: Record<string, unknown>) {
const handleEsModuleFunction = `function ${handlerName}${(function (imported: Record<string, unknown>) {
const d = 'default';
const exports = Object.keys(imported);
if (
exports.length === 1
&& exports[0] === 'default'
&& imported.default
&& imported.default.__esModule
){
return imported.default;
&& exports[0] === d
&& imported[d]
&& typeof imported[d] === 'object'
&& '__esModule' in imported[d]
) {
return imported[d];
}
return imported;
}).toString().slice('function'.length);
}).toString().slice('function'.length)}`;

const handleDynamicImport = `.then(${handlerName})`;

Expand Down

0 comments on commit fc46f9f

Please sign in to comment.