Skip to content

Commit

Permalink
force basePath to only work with ./ or ../
Browse files Browse the repository at this point in the history
  • Loading branch information
nktnet committed Oct 20, 2023
1 parent 6b45cc5 commit 0a89e21
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,21 @@ const createEs6Require = (esmOptions: ESMOptions) => {
*/
const importSync = (id: string, options: Options = {}) => {
const defaultOptions: Required<Options> = {
basePath: require.main?.path || '',
basePath: require.main?.path ?? '',
esmOptions: {},
};
const opts = { ...defaultOptions, ...options };
const filePath = path.join(opts.basePath, id);
let modulePath = id;
if (/^\.\.?\//.test(id)) {
modulePath = path.join(opts.basePath, id);
}
const es6Require = createEs6Require(opts.esmOptions);
try {
return es6Require(filePath);
return es6Require(modulePath);
} catch (error: any) {
throw new Error(`
Failed to import from:
${filePath}.
${modulePath}.
Options:
${JSON.stringify(options)}
Require error message:
Expand Down

0 comments on commit 0a89e21

Please sign in to comment.