Skip to content
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

Fix windows ESM issue #48

Merged
merged 2 commits into from
Jan 6, 2025
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
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)));
delatrie marked this conversation as resolved.
Show resolved Hide resolved
};
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) => {
delatrie marked this conversation as resolved.
Show resolved Hide resolved
return pathToFileURL(path).href;
};
Loading