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: cli should rewrite the aliases #847

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

Conversation

nmn
Copy link
Contributor

@nmn nmn commented Jan 11, 2025

What changed / motivation ?

The CLI will now rewrite imports based on the aliases that are passed into stylexConfig.

Resolves #846

Side note:

It might make sense to extract some of the utilities that were copied from the babel-plugin to a separate utils package for such use-cases.

@nmn nmn requested review from mellyeliu and Jta26 January 11, 2025 22:24
@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Jan 11, 2025
Copy link

github-actions bot commented Jan 11, 2025

workflow: benchmarks/size

Comparison of minified (terser) and compressed (brotli) size results, measured in bytes. Smaller is better.

@stylexjs/[email protected] size:compare
./size-compare.js /tmp/tmp.UWp3aolmaI /tmp/tmp.KilR8Celbh

Results Base Patch Ratio
stylex/lib/stylex.js
· compressed 729 729 1.00
· minified 2,541 2,541 1.00
stylex/lib/StyleXSheet.js
· compressed 1,266 1,266 1.00
· minified 3,776 3,776 1.00
rollup-example/.build/bundle.js
· compressed 567,110 567,110 1.00
· minified 10,232,512 10,232,512 1.00
rollup-example/.build/stylex.css
· compressed 100,626 100,626 1.00
· minified 757,176 757,176 1.00

Copy link
Contributor

@Jta26 Jta26 left a comment

Choose a reason for hiding this comment

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

Looks good, just a few questions

@@ -24,7 +24,7 @@ export type CliConfig = {
babelPluginsPost?: $ReadOnlyArray<any>,
modules_EXPERIMENTAL: $ReadOnlyArray<ModuleType>,
useCSSLayers?: boolean,
styleXConfig?: { +[string]: mixed },
styleXConfig?: StyleXOptions,
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: match naming

Comment on lines 189 to 232
const matchesFileSuffix = (allowedSuffix: string) => (filename: string) =>
filename.endsWith(`${allowedSuffix}.js`) ||
filename.endsWith(`${allowedSuffix}.ts`) ||
filename.endsWith(`${allowedSuffix}.tsx`) ||
filename.endsWith(`${allowedSuffix}.jsx`) ||
filename.endsWith(`${allowedSuffix}.mjs`) ||
filename.endsWith(`${allowedSuffix}.cjs`) ||
filename.endsWith(allowedSuffix);

const EXTENSIONS = ['.js', '.ts', '.tsx', '.jsx', '.mjs', '.cjs'];
const filePathResolver = (
relativeFilePath: string,
sourceFilePath: string,
aliases: $ReadOnly<{ [string]: $ReadOnlyArray<string> }>,
): ?string => {
// Try importing without adding any extension
// and then every supported extension
for (const ext of ['', ...EXTENSIONS]) {
const importPathStr = relativeFilePath + ext;

// Try to resolve relative paths as is
if (importPathStr.startsWith('.')) {
try {
return moduleResolve(importPathStr, url.pathToFileURL(sourceFilePath))
.pathname;
} catch {
continue;
}
}

// Otherwise, try to resolve the path with aliases
const allAliases = possibleAliasedPaths(importPathStr, aliases);
for (const possiblePath of allAliases) {
try {
return moduleResolve(possiblePath, url.pathToFileURL(sourceFilePath))
.pathname;
} catch {
continue;
}
}
}
// Failed to resolve the file path
return null;
};
Copy link
Contributor

Choose a reason for hiding this comment

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

Did you intend to use the newly exported functions from the babel plugin here, or is there a special edge case that makes them incompatible?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The Babel plugin is compiled into a single bundle which makes using the individual exports impossible. I will need to refactor some of these utility functions to a separate package.

For now I just copied the relevant bits.

const filePathResolver = (
relativeFilePath: string,
sourceFilePath: string,
aliases: $ReadOnly<{ [string]: $ReadOnlyArray<string> }>,
Copy link
Contributor

Choose a reason for hiding this comment

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

Are the aliases in here going to have to be something you specify in the stylex.json where you have to explicitly tell the CLI where the aliases resolve to?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes. This is already the case in the cli-example.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

However, there is a PR up right now which discovers aliases automatically. That PR will need to be updated to do that work in the CLI as well.

It might also make sense to move alias rewriting to the main StyleX plugin as it already has all the relevant logic. (This might be the right thing to do actually....)

};

const matchesFileSuffix = (allowedSuffix: string) => (filename: string) =>
filename.endsWith(`${allowedSuffix}.js`) ||
Copy link
Member

Choose a reason for hiding this comment

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

can we use a loop ie. EXTENSIONS.some(ext => filename.endsWith(${allowedSuffix}${ext}))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[CLI] Rewrite aliases when compiling a folder
4 participants