From 1b4299b09ef3e1736e808a0c4b07fce0cc40cbec Mon Sep 17 00:00:00 2001 From: Kristian Jones Date: Tue, 12 May 2020 17:37:09 -0500 Subject: [PATCH] Revert "fix(Resovler): Refactor resolver and module loader to prevent loading of URLs and already discovered javascript files (#28)" This reverts commit cda330e153a58976f0dab63820bb533487b5270b. --- .devcontainer.json | 5 +--- .vscode/settings.json | 2 +- Testing/Runner/Utils/runTests.ts | 4 +-- src/findFiles.ts | 4 ++- src/index.ts | 46 +++++++++++--------------------- 5 files changed, 22 insertions(+), 39 deletions(-) diff --git a/.devcontainer.json b/.devcontainer.json index 5d09890..157e090 100644 --- a/.devcontainer.json +++ b/.devcontainer.json @@ -10,13 +10,10 @@ "SHELL": "/bin/sh" }, - "extensions": ["esbenp.prettier-vscode", "dbaeumer.vscode-eslint"], - + "extensions": ["esbenp.prettier-vscode"], "remoteUser": "node", - "workspaceMount": "source=${localWorkspaceFolder},target=/workspace,type=bind,consistency=cached", "workspaceFolder": "/workspace", - "mounts": [ "source=ts-esnode-modules,target=/workspace/node_modules,type=volume" ], diff --git a/.vscode/settings.json b/.vscode/settings.json index 25fa621..3662b37 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,3 +1,3 @@ { "typescript.tsdk": "node_modules/typescript/lib" -} +} \ No newline at end of file diff --git a/Testing/Runner/Utils/runTests.ts b/Testing/Runner/Utils/runTests.ts index 8e77c49..74d9be0 100644 --- a/Testing/Runner/Utils/runTests.ts +++ b/Testing/Runner/Utils/runTests.ts @@ -9,9 +9,7 @@ export interface Result { } export function runTest(test: Test): Promise { - const worker = spawnWorker(test.path, { - helloWorld: ['test', 'test2'], - }); + const worker = spawnWorker(test.path, {}); return new Promise((resolve, reject) => { worker.on('exit', (exitCode) => { diff --git a/src/findFiles.ts b/src/findFiles.ts index 53a7916..46b31aa 100644 --- a/src/findFiles.ts +++ b/src/findFiles.ts @@ -3,6 +3,8 @@ import { promises as fs } from 'fs'; import { resolve as resolvePath } from 'path'; import { pathToFileURL, URL } from 'url'; +const JS_EXTS = ['.js', '.jsx']; + interface FileRule { fileName: string; extensions: string[]; @@ -22,7 +24,7 @@ async function findFile( if (directoryEntry.name.includes(fileName)) { if (directoryEntry.isDirectory()) return true; - for (let extension of extensions) { + for (let extension of [...extensions, ...JS_EXTS]) { if (directoryFileName === fileName + extension) { return true; } diff --git a/src/index.ts b/src/index.ts index 00065bf..5a63f08 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,26 +2,26 @@ import { createRequire } from 'module'; import { basename, dirname } from 'path'; import ts from 'typescript'; -import { fileURLToPath, pathToFileURL } from 'url'; +import { fileURLToPath, pathToFileURL, URL, format } from 'url'; import { findFiles } from './findFiles'; import { - ModuleFormat, ResolveContext, ResolveResponse, Source, TransformContext, TransformResponse, + ModuleFormat, } from './types'; import { getTSConfig } from './Utils'; const rootModulePath = `${process.cwd()}/`; const baseURL = pathToFileURL(rootModulePath).href; -const relativePathRegex = /^\.{1,2}[/]/; +const relativePathRegex = /^\.{0,2}[/]/; // TODO: Allow customization of extensions const extensions = ['.ts', '.tsx']; -const extensionsRegex = new RegExp(`\\${extensions.join('$|\\')}`); +const extensionsRegex = new RegExp(`\\${extensions.join('$|\\')}$`); // Custom resolver to allow `.ts` and `.tsx` extensions, along with finding files if no extension is provided. export async function resolve( @@ -31,11 +31,8 @@ export async function resolve( ): Promise { const { parentURL = baseURL } = context; - const resolvedUrl = new URL(specifier, parentURL); - const fileName = basename(resolvedUrl.pathname); - // If we can already see a `.ts` or `.tsx` extensions then we can create a File URL - if (extensionsRegex.test(fileName)) { + if (extensionsRegex.test(specifier)) { // Node.js normally errors on unknown file extensions, so return a URL for // specifiers ending in the TypeScript file extensions. return { @@ -46,14 +43,14 @@ export async function resolve( /** * If no extension is passed and is a relative import then let's try to find a `.ts` or `.tsx` file at the path */ - if (relativePathRegex.test(specifier)) { - const filePath = fileURLToPath(resolvedUrl); + if (relativePathRegex.test(specifier) && !specifier.startsWith('file:')) { + const fileURL = new URL(specifier, parentURL); + const filePath = fileURLToPath(fileURL); const file = await findFiles(dirname(filePath), { - fileName, + fileName: basename(filePath), extensions, }); - return { url: file.href, }; @@ -68,19 +65,13 @@ export async function resolve( * @param url fileURL given by Node.JS */ export async function dynamicInstantiate(url: string) { - const moduleUrl = new URL(url); - - const [nodeModulesBase, specifier] = moduleUrl.pathname.split( - 'node_modules/', - ); - - const nodeModuleUrl = new URL('node_modules', pathToFileURL(nodeModulesBase)); - // Create a Node.JS Require using the `node_modules` folder as the base URL. - const require = createRequire(nodeModuleUrl); + const require = createRequire( + `${url.split('/node_modules/')[0].replace('file://', '')}/node_modules/`, + ); // Import the module file path - let dynModule = require(specifier); + let dynModule = require(url.replace(/.*\/node_modules\//, '')); /** * This is needed to allow for default exports in CommonJS modules. @@ -114,11 +105,8 @@ export async function getFormat( let format = formatCache.get(url); if (format) return { format }; - const resolvedUrl = new URL(url); - const fileName = basename(resolvedUrl.pathname); - // If it's a TypeScript extension then force `module` mode. - if (extensionsRegex.test(fileName)) format = 'module'; + if (extensionsRegex.test(url)) format = 'module'; if (!format) { const defaultResolve = defaultGetFormat(url, context, defaultGetFormat) as { @@ -148,11 +136,8 @@ export async function transformSource( context: TransformContext, defaultTransformSource: Function, ): Promise { - const resolvedUrl = new URL(context.url); - const fileName = basename(resolvedUrl.pathname); - // Only transform TypeScript Modules - if (extensionsRegex.test(fileName)) { + if (extensionsRegex.test(context.url)) { const sourceFilePath = fileURLToPath(context.url); // Load the closest `tsconfig.json` to the source file @@ -164,6 +149,7 @@ export async function transformSource( reportDiagnostics: true, }); + // TODO: Actually "check" the TypeScript Code. return { source: transpiledModule.outputText, };