Skip to content

Commit

Permalink
refactor: use explicit import paths
Browse files Browse the repository at this point in the history
  • Loading branch information
privatenumber committed Nov 21, 2023
1 parent acc9eb0 commit 9233f67
Show file tree
Hide file tree
Showing 22 changed files with 52 additions and 50 deletions.
10 changes: 5 additions & 5 deletions src/cjs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import {
createFilesMatcher,
} from 'get-tsconfig';
import type { TransformOptions } from 'esbuild';
import { installSourceMapSupport } from '../source-map';
import { transformSync } from '../utils/transform';
import { transformDynamicImport } from '../utils/transform/transform-dynamic-import';
import { resolveTsPath } from '../utils/resolve-ts-path';
import { isESM } from '../utils/esm-pattern';
import { installSourceMapSupport } from '../source-map.js';
import { transformSync } from '../utils/transform/index.js';
import { transformDynamicImport } from '../utils/transform/transform-dynamic-import.js';
import { resolveTsPath } from '../utils/resolve-ts-path.js';
import { isESM } from '../utils/esm-pattern.js';

const isRelativePathPattern = /^\.{1,2}\//;
const isTsFilePatten = /\.[cm]?tsx?$/;
Expand Down
6 changes: 3 additions & 3 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import {
transformSync as esbuildTransformSync,
} from 'esbuild';
import { version } from '../package.json';
import { run } from './run';
import { watchCommand } from './watch';
import { run } from './run.js';
import { watchCommand } from './watch/index.js';
import {
removeArgvFlags,
ignoreAfterArgument,
} from './remove-argv-flags';
} from './remove-argv-flags.js';

const tsxFlags = {
noCache: {
Expand Down
4 changes: 2 additions & 2 deletions src/esm/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { isMainThread } from 'node:worker_threads';
import { supportsModuleRegister } from '../utils/node-features';
import { registerLoader } from './register';
import { supportsModuleRegister } from '../utils/node-features.js';
import { registerLoader } from './register.js';

// Loaded via --import flag
if (
Expand Down
10 changes: 5 additions & 5 deletions src/esm/loaders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import type {
ResolveFnOutput, ResolveHookContext, LoadHook, GlobalPreloadHook, InitializeHook,
} from 'module';
import type { TransformOptions } from 'esbuild';
import { transform } from '../utils/transform';
import { transformDynamicImport } from '../utils/transform/transform-dynamic-import';
import { resolveTsPath } from '../utils/resolve-ts-path';
import { installSourceMapSupport } from '../source-map';
import { importAttributes } from '../utils/node-features';
import { transform } from '../utils/transform/index.js';
import { transformDynamicImport } from '../utils/transform/transform-dynamic-import.js';
import { resolveTsPath } from '../utils/resolve-ts-path.js';
import { installSourceMapSupport } from '../source-map.js';
import { importAttributes } from '../utils/node-features.js';
import {
tsconfigPathsMatcher,
fileMatcher,
Expand Down
2 changes: 1 addition & 1 deletion src/esm/register.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import module from 'node:module';
import { MessageChannel } from 'node:worker_threads';
import { installSourceMapSupport } from '../source-map';
import { installSourceMapSupport } from '../source-map.js';

export const registerLoader = () => {
const { port1, port2 } = new MessageChannel();
Expand Down
4 changes: 2 additions & 2 deletions src/loader.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import './patch-repl';
import './patch-repl.js';

// Hook require() to transform to CJS
// eslint-disable-next-line import/no-unresolved
Expand All @@ -8,4 +8,4 @@ require('./cjs/index.cjs');
Hook import/import() to transform to ESM
Can be used in Node v12 to support dynamic `import()`
*/
export * from './esm';
export * from './esm/index.js';
2 changes: 1 addition & 1 deletion src/patch-repl.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import repl, { type REPLServer, type REPLEval } from 'repl';
import { transform } from './utils/transform';
import { transform } from './utils/transform/index.js';

function patchEval(nodeRepl: REPLServer) {
const { eval: defaultEval } = nodeRepl;
Expand Down
6 changes: 3 additions & 3 deletions src/remove-argv-flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ export const ignoreAfterArgument = (
};
};

export function removeArgvFlags(
export const removeArgvFlags = (
tsxFlags: Flags,
argv = process.argv.slice(2),
) {
) => {
typeFlag(
tsxFlags,
argv,
Expand All @@ -37,4 +37,4 @@ export function removeArgvFlags(
);

return argv;
}
};
2 changes: 1 addition & 1 deletion src/repl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import repl, { type REPLEval } from 'repl';
import { version } from '../package.json';
import { transform } from './utils/transform';
import { transform } from './utils/transform/index.js';

// Copied from
// https://github.com/nodejs/node/blob/v18.2.0/lib/internal/main/repl.js#L37
Expand Down
6 changes: 3 additions & 3 deletions src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import { pathToFileURL } from 'url';
import spawn from 'cross-spawn';
import { supportsModuleRegister } from './utils/node-features';

export function run(
export const run = (
argv: string[],
options?: {
noCache?: boolean;
tsconfigPath?: string;
ipc?: boolean;
},
) {
) => {
const environment = { ...process.env };
const stdio: StdioOptions = [
'inherit', // stdin
Expand Down Expand Up @@ -45,4 +45,4 @@ export function run(
env: environment,
},
);
}
};
2 changes: 1 addition & 1 deletion src/utils/esm-pattern.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { parseEsm } from './es-module-lexer';
import { parseEsm } from './es-module-lexer.js';

/*
Previously, this regex was used as a naive ESM catch,
Expand Down
6 changes: 4 additions & 2 deletions src/utils/read-json-file.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import fs from 'fs';

export function readJsonFile<JsonType>(filePath: string) {
export const readJsonFile = <JsonType>(
filePath: string,
) => {
try {
const jsonString = fs.readFileSync(filePath, 'utf8');
return JSON.parse(jsonString) as JsonType;
} catch {}
}
};
2 changes: 1 addition & 1 deletion src/utils/transform/apply-transformers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import remapping from '@ampproject/remapping';
import type { SourceMapInput } from '@ampproject/remapping';
import type { RawSourceMap } from '../../source-map';
import type { RawSourceMap } from '../../source-map.js';

type SourceMap = SourceMapInput | RawSourceMap;

Expand Down
4 changes: 2 additions & 2 deletions src/utils/transform/cache.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import fs from 'fs';
import path from 'path';
import os from 'os';
import { readJsonFile } from '../read-json-file';
import type { Transformed } from './apply-transformers';
import { readJsonFile } from '../read-json-file.js';
import type { Transformed } from './apply-transformers.js';

const getTime = () => Math.floor(Date.now() / 1e8);

Expand Down
10 changes: 5 additions & 5 deletions src/utils/transform/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ import {
transformSync as esbuildTransformSync,
version as esbuildVersion,
} from 'esbuild';
import { sha1 } from '../sha1';
import { version as transformDynamicImportVersion, transformDynamicImport } from './transform-dynamic-import';
import cache from './cache';
import { sha1 } from '../sha1.js';
import { version as transformDynamicImportVersion, transformDynamicImport } from './transform-dynamic-import.js';
import cache from './cache.js';
import {
applyTransformersSync,
applyTransformers,
type Transformed,
} from './apply-transformers';
} from './apply-transformers.js';
import {
cacheConfig,
patchOptions,
} from './get-esbuild-options';
} from './get-esbuild-options.js';

// Used by cjs-loader
export function transformSync(
Expand Down
4 changes: 2 additions & 2 deletions src/utils/transform/transform-dynamic-import.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import MagicString from 'magic-string';
import type { RawSourceMap } from '../../source-map';
import { parseEsm } from '../es-module-lexer';
import type { RawSourceMap } from '../../source-map.js';
import { parseEsm } from '../es-module-lexer.js';

export const version = '1';

Expand Down
6 changes: 3 additions & 3 deletions src/watch/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ import { constants as osConstants } from 'os';
import path from 'path';
import { command } from 'cleye';
import { watch } from 'chokidar';
import { run } from '../run';
import { run } from '../run.js';
import {
removeArgvFlags,
ignoreAfterArgument,
} from '../remove-argv-flags';
} from '../remove-argv-flags.js';
import {
clearScreen,
debounce,
log,
} from './utils';
} from './utils.js';

const killProcess = async (
childProcess: ChildProcess,
Expand Down
6 changes: 3 additions & 3 deletions tests/specs/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import path from 'path';
import { testSuite, expect } from 'manten';
import { createFixture } from 'fs-fixture';
import packageJson from '../../package.json';
import { tsx, tsxPath } from '../utils/tsx';
import { ptyShell, isWindows } from '../utils/pty-shell';
import { expectMatchInOrder } from '../utils/expect-match-in-order';
import { tsx, tsxPath } from '../utils/tsx.js';
import { ptyShell, isWindows } from '../utils/pty-shell/index';
import { expectMatchInOrder } from '../utils/expect-match-in-order.js';

export default testSuite(({ describe }) => {
describe('CLI', ({ describe, test }) => {
Expand Down
2 changes: 1 addition & 1 deletion tests/specs/repl.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { testSuite } from 'manten';
import { tsx } from '../utils/tsx';
import { processInteract } from '../utils/process-interact';
import { processInteract } from '../utils/process-interact.js';

export default testSuite(async ({ describe }) => {
describe('REPL', ({ test }) => {
Expand Down
2 changes: 1 addition & 1 deletion tests/specs/smoke.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { pathToFileURL } from 'url';
import { testSuite, expect } from 'manten';
import { createFixture } from 'fs-fixture';
import outdent from 'outdent';
import type { NodeApis } from '../utils/tsx';
import type { NodeApis } from '../utils/tsx.js';

const cjsContextCheck = 'typeof module !== \'undefined\'';
const tsCheck = '1 as number';
Expand Down
2 changes: 1 addition & 1 deletion tests/specs/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { testSuite, expect } from 'manten';
import { createFsRequire } from 'fs-require';
import { Volume } from 'memfs';
import outdent from 'outdent';
import { transform, transformSync } from '../../src/utils/transform';
import { transform, transformSync } from '../../src/utils/transform/index.js';

const base64Module = (code: string) => `data:text/javascript;base64,${Buffer.from(code).toString('base64')}`;

Expand Down
4 changes: 2 additions & 2 deletions tests/specs/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import path from 'path';
import { setTimeout } from 'timers/promises';
import { testSuite, expect } from 'manten';
import { createFixture } from 'fs-fixture';
import { tsx } from '../utils/tsx';
import { processInteract } from '../utils/process-interact';
import { tsx } from '../utils/tsx.js';
import { processInteract } from '../utils/process-interact.js';

export default testSuite(async ({ describe }) => {
describe('watch', async ({ test, describe, onFinish }) => {
Expand Down

0 comments on commit 9233f67

Please sign in to comment.