Skip to content

Support for typescript #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# node-loader-extensionless

A node loader that auto-appends .js extensions to imports
A node loader that auto-appends .js or .ts extensions to imports, based on the parentURL.

When paired with [@node-loader/babel](https://github.com/node-loader/node-loader-babel) loader allows for loading typescript via NodeJS loaders

## Installation

Expand Down
3 changes: 3 additions & 0 deletions babel.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": ["@babel/preset-typescript"]
}
47 changes: 42 additions & 5 deletions lib/node-loader-extensionless.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,50 @@
import { extname, basename } from "node:path";

const typescriptFileExts = [
".ts",
".tsx",
".mts",
".cts",
".d.ts",
".d.mts",
".d.cts",
".js",
];

export async function resolve(specifier, context, nextResolve) {
const pathParts = specifier.split("/");
const fileName = pathParts[pathParts.length - 1];
const fileName = basename(specifier);
const isRelativePath =
specifier.startsWith("./") ?? specifier.startsWith("../");

// Only add .ts extension to path-like specifiers, not bare specifiers
let result;

// Only add extensions to path-like specifiers, not bare specifiers
if (isRelativePath && !fileName.includes(".")) {
return nextResolve(specifier + ".js", context);
const parentFileExt = context.parentURL
? extname(context.parentURL) || ".js"
: ".js";

const isTypescript = typescriptFileExts.includes(parentFileExt);
// Mimic the typescript file extension substitution algorithm
// Ideally we'd take into account the moduleResolution option, but for now we skip that
// https://www.typescriptlang.org/docs/handbook/modules/reference.html#file-extension-substitution
const fileExtensionsToTry = isTypescript ? typescriptFileExts : [".js"];

for (const ext of fileExtensionsToTry) {
try {
result = await nextResolve(specifier + ext, context);
break;
} catch {}
}
} else {
result = nextResolve(specifier, context);
}

if (!result) {
throw Error(
`Could not resolve specifier '${specifier}' from parent URL ${context.parentURL}`,
);
}

return nextResolve(specifier, context);
return result;
}
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"lib"
],
"scripts": {
"test": "cross-env node --loader ./lib/node-loader-extensionless.js ./test/run-tests.js",
"test": "cross-env node --import=./test/register.js ./test/run-tests.js",
"lint": "eslint .",
"check-format": "prettier --check .",
"prepare": "husky install"
Expand Down Expand Up @@ -41,7 +41,10 @@
"access": "public"
},
"devDependencies": {
"@babel/core": "^7.23.7",
"@babel/preset-typescript": "^7.23.3",
"@baseplate-sdk/utils": "^3.2.1",
"@node-loader/babel": "^2.1.0",
"cross-env": "^7.0.3",
"eslint": "^8.56.0",
"eslint-config-node-important-stuff": "^2.0.0",
Expand All @@ -54,6 +57,6 @@
},
"lint-staged": {
"*.js": "eslint --cache --fix",
"*.{js,css,md,yml,eslintrc}": "prettier --write"
"*.{js,css,md,yml,eslintrc,ts,tsx,json}": "prettier --write"
}
}
Loading