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

Feat: Using @dual-bundle/import-meta-resolve replace esm-resolve #824

Merged
merged 2 commits into from
Dec 27, 2024
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
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
19 changes: 6 additions & 13 deletions packages/babel-plugin/rollup.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
*
*/

import alias from '@rollup/plugin-alias';
import { nodeResolve } from '@rollup/plugin-node-resolve';
import commonjs from '@rollup/plugin-commonjs';
import json from '@rollup/plugin-json';
Expand Down Expand Up @@ -42,19 +41,13 @@ const config = {
},
external: process.env['HASTE']
? external
: [...external, 'esm-resolve', '@stylexjs/shared', '@stylexjs/stylex'],
plugins: [
alias({
entries: [
{
find: 'esm-resolve',
replacement: path.resolve(
rootDir,
'node_modules/esm-resolve/bundle.js',
),
},
: [
...external,
'@dual-bundle/import-meta-resolve',
'@stylexjs/shared',
'@stylexjs/stylex',
],
}),
plugins: [
babel({ babelHelpers: 'bundled', extensions, include: ['./src/**/*'] }),
nodeResolve({
preferBuiltins: false,
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
Loading