Skip to content
Closed
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: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [14.x, 16.x, 18.x]
node-version: [16.x, 18.x, 20.x]
steps:
- uses: actions/checkout@v3
- name: Use Node.js ${{ matrix.node-version }}
Expand All @@ -31,7 +31,7 @@ jobs:
run: npm run test
- name: Import with CJS
if: ${{ always() }}
run: node smoke-tests/smoke-test-cjs.js
run: node smoke-tests/smoke-test-cjs.cjs
- name: Import with ESM
if: ${{ always() }}
run: node smoke-tests/smoke-test-esm.mjs
Expand Down
1 change: 0 additions & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ jobs:
env:
CI: true
GITHUB_TOKEN: ${{ secrets.ACTION_GITHUB_TOKEN }}
NPM_OTP_TOKEN: ${{ github.event.inputs.otp }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
runs-on: ubuntu-latest
steps:
Expand Down
8 changes: 6 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
.idea
.npmrc
.vscode

node_modules

coverage
dist
node_modules

.npmrc
54 changes: 0 additions & 54 deletions esbuild.config.mjs

This file was deleted.

3 changes: 3 additions & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ const config: Config.InitialOptions = {
"!<rootDir>/lib/__fixtures__/**/*",
],
moduleFileExtensions: ["ts", "js"],
moduleNameMapper: {
"^(\\.{1,2}/.*)\\.js$": "$1",
},
transform: {
"^.+\\.ts$": "@swc/jest",
},
Expand Down
16 changes: 8 additions & 8 deletions lib/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from "node:path";

import { cosmiconfig, cosmiconfigSync } from "cosmiconfig";
import { cosmiconfig } from "cosmiconfig";

import { TypeScriptLoader } from ".";

Expand Down Expand Up @@ -46,13 +46,13 @@ describe("TypeScriptLoader", () => {
});

describe("cosmiconfigSync", () => {
it("should load a valid TS file", () => {
const cfg = cosmiconfigSync("test", {
it("should load a valid TS file", async () => {
const cfg = cosmiconfig("test", {
loaders: {
".ts": TypeScriptLoader(),
},
});
const loadedCfg = cfg.load(
const loadedCfg = await cfg.load(
path.resolve(fixturesPath, "valid.fixture.ts")
);

Expand All @@ -61,16 +61,16 @@ describe("TypeScriptLoader", () => {
expect(loadedCfg!.config.test.cake).toStrictEqual("a lie");
});

it("should throw an error on loading an invalid TS file", () => {
const cfg = cosmiconfigSync("test", {
it("should throw an error on loading an invalid TS file", async () => {
const cfg = cosmiconfig("test", {
loaders: {
".ts": TypeScriptLoader(),
},
});

expect(() =>
await expect(() =>
cfg.load(path.resolve(fixturesPath, "invalid.fixture.ts"))
).toThrowError();
).rejects.toThrowError();
});
});
});
4 changes: 2 additions & 2 deletions lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export { TypeScriptLoader } from "./loader";
export type { TypeScriptCompileError } from "./typescript-compile-error";
export { TypeScriptLoader } from "./loader.js";
export type { TypeScriptCompileError } from "./typescript-compile-error.js";
14 changes: 8 additions & 6 deletions lib/loader.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ describe("TypeScriptLoader", () => {
loader(filePath, readFixtureContent(filePath));
});

it("should fail on parsing an invalid TS file", () => {
it("should fail on parsing an invalid TS file", async () => {
const filePath = path.resolve(fixturesPath, "invalid.fixture.ts");
expect(() => loader(filePath, readFixtureContent(filePath))).toThrowError();
await expect(() =>
loader(filePath, readFixtureContent(filePath))
).rejects.toThrowError();
});

it("should use the same instance of ts-node across multiple calls", () => {
Expand All @@ -38,10 +40,10 @@ describe("TypeScriptLoader", () => {
expect(tsNodeSpy).toHaveBeenCalledTimes(1);
});

it("should throw a TypeScriptCompileError on error", () => {
it("should throw a TypeScriptCompileError on error", async () => {
try {
const filePath = path.resolve(fixturesPath, "invalid.fixture.ts");
loader(filePath, readFixtureContent(filePath));
await loader(filePath, readFixtureContent(filePath));
fail(
"Error should be thrown upon failing to transpile an invalid TS file."
);
Expand Down Expand Up @@ -72,9 +74,9 @@ describe("TypeScriptLoader", () => {
stub.mockRestore();
});

it("rethrows an error if it is not an instance of Error", () => {
it("rethrows an error if it is not an instance of Error", async () => {
try {
loader("filePath", "readFixtureContent(filePath)");
await loader("filePath", "readFixtureContent(filePath)");
fail(
"Error should be thrown upon failing to transpile an invalid TS file."
);
Expand Down
11 changes: 4 additions & 7 deletions lib/loader.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
import type { Loader } from "cosmiconfig";
import { register, RegisterOptions } from "ts-node";

import { TypeScriptCompileError } from "./typescript-compile-error";
import { TypeScriptCompileError } from "./typescript-compile-error.js";

export function TypeScriptLoader(options?: RegisterOptions): Loader {
const tsNodeInstance = register({
...options,
compilerOptions: { module: "commonjs" },
});
return (path: string, content: string) => {
const tsNodeInstance = register(options);
return async (path: string, content: string) => {
try {
// cosmiconfig requires the transpiled configuration to be CJS
tsNodeInstance.compile(content, path);
const result = require(path);
const result = await import(path);

// `default` is used when exporting using export default, some modules
// may still use `module.exports` or if in TS `export = `
Expand Down
Loading