Skip to content

Commit

Permalink
Feat: Using @dual-bundle/import-meta-resolve replace esm-resolve (#…
Browse files Browse the repository at this point in the history
…824)

* chore: using dual import-meta-resolve replace esm-resolve
* chore: update build deps
  • Loading branch information
nonzzz authored Dec 27, 2024
1 parent 347ebef commit edc9b2a
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 111 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
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

0 comments on commit edc9b2a

Please sign in to comment.