Skip to content

Commit

Permalink
Fix windows ESM issue (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
epszaw authored Jan 6, 2025
1 parent 1d41117 commit 0badb58
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 2 deletions.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
8 changes: 7 additions & 1 deletion packages/core/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { readHistory } from "./history.js";
import { readKnownIssues } from "./known.js";
import { FileSystemReportFiles } from "./plugin.js";
import { importWrapper } from "./utils/module.js";
import { normalizeImportPath } from "./utils/path.js";

export const getPluginId = (key: string) => {
return key.replace(/^@.*\//, "").replace(/[/\\]/g, "-");
Expand All @@ -19,21 +20,26 @@ const defaultConfig: Config = {};
export const findConfig = async (cwd: string, configPath?: string) => {
if (configPath) {
const resolved = resolve(cwd, configPath);

try {
const stats = await stat(resolved);

if (stats.isFile()) {
return resolved;
}
} catch (e) {
console.error(e);
}

throw new Error(`invalid config path ${resolved}: not a regular file`);
}

for (const configName of configNames) {
const resolved = resolve(cwd, configName);

try {
const stats = await stat(resolved);

if (stats.isFile()) {
return resolved;
}
Expand Down Expand Up @@ -61,7 +67,7 @@ export const readConfig = async (
};

export const loadConfig = async (configPath: string): Promise<Config> => {
return (await import(configPath)).default;
return (await import(normalizeImportPath(configPath))).default;
};

export const resolveConfig = async (config: Config, override: ConfigOverride = {}): Promise<FullConfig> => {
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export type * from "./api.js";
export * from "./utils/misc.js";
export * from "./utils/crypto.js";
export * from "./utils/path.js";
export * from "./history.js";
export * from "./known.js";
export { resolveConfig, readConfig } from "./config.js";
Expand Down
7 changes: 6 additions & 1 deletion packages/core/src/utils/module.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { createRequire } from "node:module";
import { normalizeImportPath } from "./path.js";

const require = createRequire(import.meta.url);

/**
* Dead simple wrapper around import function to make it possible to mock it in the tests
* @param path
*/
export const importWrapper = async (path: string) => {
return import(path);
return import(normalizeImportPath(require.resolve(path)));
};
9 changes: 9 additions & 0 deletions packages/core/src/utils/path.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { pathToFileURL } from "node:url";

/**
* The function appends `file:///` protocol to the absolute paths on Windows due to ES modules imports specifics
* @param path
*/
export const normalizeImportPath = (path: string) => {
return pathToFileURL(path).href;
};

0 comments on commit 0badb58

Please sign in to comment.