-
Notifications
You must be signed in to change notification settings - Fork 318
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
base: main
Are you sure you want to change the base?
Conversation
workflow: benchmarks/sizeComparison of minified (terser) and compressed (brotli) size results, measured in bytes. Smaller is better.
|
There was a problem hiding this 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, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: match naming
packages/cli/src/plugins.js
Outdated
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; | ||
}; |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
packages/cli/src/plugins.js
Outdated
const filePathResolver = ( | ||
relativeFilePath: string, | ||
sourceFilePath: string, | ||
aliases: $ReadOnly<{ [string]: $ReadOnlyArray<string> }>, |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
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....)
packages/cli/src/plugins.js
Outdated
}; | ||
|
||
const matchesFileSuffix = (allowedSuffix: string) => (filename: string) => | ||
filename.endsWith(`${allowedSuffix}.js`) || |
There was a problem hiding this comment.
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}))
7e3b37c
to
3ac5d73
Compare
What changed / motivation ?
The CLI will now rewrite imports based on the
aliases
that are passed intostylexConfig
.Resolves #846
Side note:
It might make sense to extract some of the utilities that were copied from the
babel-plugin
to a separateutils
package for such use-cases.