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 : Handle absolute paths for custom inject source #222

Open
wants to merge 10 commits into
base: feat/abs-paths
Choose a base branch
from
25 changes: 22 additions & 3 deletions packages/babel-plugin/src/utils/state-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,28 @@ export default class StateManager {
}

get runtimeInjection(): ?$ReadOnly<{ from: string, as?: string }> {
return typeof this.options.runtimeInjection === 'string'
? { from: this.options.runtimeInjection }
: this.options.runtimeInjection || null;
const options = this.options || {};
const runtimeInjection = options.runtimeInjection;
const rootDir = this.options.unstable_moduleResolution.rootDir || '';
const filename = this.filename || '';

if (typeof runtimeInjection === 'string' && rootDir && filename) {
if (
runtimeInjection.startsWith('./') ||
runtimeInjection.startsWith('../')
) {
const absolutePath = path.join(rootDir, runtimeInjection);
Copy link
Contributor

@nonzzz nonzzz Dec 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think should add a normalize function to remove platform differences.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you mean path.normalize(path.join()) ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return { from: absolutePath };
}

const relativePath = path.relative(
path.dirname(filename),
runtimeInjection,
);
return { from: relativePath };
}

return runtimeInjection || null;
}

get isDev(): boolean {
Expand Down