Skip to content

Commit

Permalink
chore: using dual import-meta-resolve replace esm-resolve
Browse files Browse the repository at this point in the history
  • Loading branch information
nonzzz committed Dec 23, 2024
1 parent 347ebef commit 5a6b0cc
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 98 deletions.
96 changes: 28 additions & 68 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 packages/babel-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"@babel/core": "^7.25.8",
"@babel/traverse": "^7.25.7",
"@babel/types": "^7.25.8",
"esm-resolve": "^1.0.11"
"@dual-bundle/import-meta-resolve": "^4.1.0"
},
"jest": {
"verbose": true,
Expand Down
37 changes: 8 additions & 29 deletions packages/babel-plugin/src/utils/state-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ import type {
import { name } from '@stylexjs/stylex/package.json';
import path from 'path';
import fs from 'fs';
import url from 'url';
import type { Check } from './validate';
import * as z from './validate';
import { addDefault, addNamed } from '@babel/helper-module-imports';
import type { ImportOptions } from '@babel/helper-module-imports';
import * as pathUtils from '../babel-path-utils';
import { buildResolver } from 'esm-resolve';
import { moduleResolve } from '@dual-bundle/import-meta-resolve';

type ImportAdditionOptions = Omit<
Partial<ImportOptions>,
Expand Down Expand Up @@ -663,8 +664,6 @@ const filePathResolver = (
sourceFilePath: string,
aliases: StyleXStateOptions['aliases'],
): ?string => {
const esmResolve = buildResolver(sourceFilePath);

// Try importing without adding any extension
// and then every supported extension
for (const ext of ['', ...EXTENSIONS]) {
Expand All @@ -673,41 +672,21 @@ const filePathResolver = (
// Try to resolve relative paths as is
if (importPathStr.startsWith('.')) {
try {
return require.resolve(importPathStr, {
paths: [path.dirname(sourceFilePath)],
});
return moduleResolve(importPathStr, url.pathToFileURL(sourceFilePath))
.pathname;
} catch {
const resolved = esmResolve(importPathStr, {
allowImportingExtraExtensions: true,
});

if (resolved) {
if (resolved.startsWith('.')) {
return path.resolve(path.dirname(sourceFilePath), resolved);
}
return resolved;
}
continue;
}
}

// Otherwise, try to resolve the path with aliases
const allAliases = possibleAliasedPaths(importPathStr, aliases);
for (const possiblePath of allAliases) {
try {
return require.resolve(possiblePath, {
paths: [path.dirname(sourceFilePath)],
});
return moduleResolve(possiblePath, url.pathToFileURL(sourceFilePath))
.pathname;
} catch {
const resolved = esmResolve(importPathStr, {
allowImportingExtraExtensions: true,
});

if (resolved) {
if (resolved.startsWith('.')) {
return path.resolve(path.dirname(sourceFilePath), resolved);
}
return resolved;
}
continue;
}
}
}
Expand Down

0 comments on commit 5a6b0cc

Please sign in to comment.