Skip to content

Commit

Permalink
2.1.1 - patch file:// for esm
Browse files Browse the repository at this point in the history
  • Loading branch information
nktnet committed Oct 21, 2023
1 parent 96ff7a3 commit 2542f91
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 9 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"type": "git",
"url": "https://github.com/nktnet1/import-sync"
},
"version": "2.1.0",
"version": "2.1.1",
"files": [
"dist"
],
Expand Down
12 changes: 9 additions & 3 deletions src/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,20 @@ import { VALID_FILE_EXTENSIONS } from './config';
*
* @returns {string} absolute path or an empty string if no caller
*/
export const getCallerFilePath = (): string => {
export const getCallerDirname = (): string => {
const orig = Error.prepareStackTrace;
Error.prepareStackTrace = (_, stack) => stack;
const err = new Error();
Error.captureStackTrace(err, getCallerFilePath);
Error.captureStackTrace(err, getCallerDirname);
const stack = err.stack as any;
Error.prepareStackTrace = orig;
return stack[1].getFileName();
const callerFilePath = stack[1].getFileName();
/* istanbul ignore next */
return path.dirname(
callerFilePath.startsWith('file://')
? callerFilePath.substring(7)
: callerFilePath
);
};

/**
Expand Down
5 changes: 2 additions & 3 deletions src/import.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import esm from 'esm';
import path from 'path';
import fs from 'fs';
import { ESMOptions, Options } from './types';
import { findModuleFile, getCallerFilePath } from './files';
import { findModuleFile, getCallerDirname } from './files';

/**
* Creates an esm-compatible require function that can import ES Modules
Expand Down Expand Up @@ -44,7 +43,7 @@ const createEs6Require = (esmOptions: ESMOptions) => {
*/
const importSync = (id: string, options: Options = {}) => {
const defaultOptions: Required<Options> = {
basePath: path.dirname(getCallerFilePath()),
basePath: getCallerDirname(),
esmOptions: {},
};
const opts = { ...defaultOptions, ...options };
Expand Down

0 comments on commit 2542f91

Please sign in to comment.