Skip to content

Commit

Permalink
feat: add aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
nonzzz committed Apr 23, 2024
1 parent 2fbcad2 commit ea70b9d
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
3 changes: 2 additions & 1 deletion packages/babel-plugin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ const defaultOptions: InternalPluginOptions = {
type: 'commonJS',
rootDir: process.cwd(),
themeFileExtension: '.stylex'
}
},
aliases: {}
}

function declare({ types: t }: typeof b): PluginObj {
Expand Down
1 change: 1 addition & 0 deletions packages/babel-plugin/src/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export type ModuleResolution = ModuleResolutionCommonJS | MOduleResolutionHaste
export interface StylexExtendBabelPluginOptions {
stylex?: boolean | StylexBindingMeta
enableInjectGlobalStyle?: boolean
aliases?: Record<string, string | string[]>
/**
* @default 'x'
* @see {@link https://stylexjs.com/docs/api/configuration/babel-plugin/#classnameprefix}
Expand Down
41 changes: 39 additions & 2 deletions packages/babel-plugin/src/state-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,35 @@ export function matchesFileSuffix(allowedSuffix: string) {

const EXTENSIONS = ['.js', '.ts', '.tsx', '.jsx', '.mjs', '.cjs']

function filePathResolver(relativePath: string, sourceFilePath: string) {
function possibleAliasedPaths(importPath: string, aliases: StylexExtendBabelPluginOptions['aliases']) {
const result = [importPath]
if (aliases == null || Object.keys(aliases).length === 0) {
return result
}
for (const [alias, _value] of Object.entries(aliases)) {
const value = Array.isArray(_value) ? _value : [_value]
if (alias.includes('*')) {
const [before, after] = alias.split('*')
if (importPath.startsWith(before) && importPath.endsWith(after)) {
const replacementString = importPath.slice(
before.length,
after.length > 0 ? -after.length : undefined
)
value.forEach((v) => {
result.push(v.split('*').join(replacementString))
})
}
} else if (alias === importPath) {
value.forEach((v) => {
result.push(v)
})
}
}

return result
}

function filePathResolver(relativePath: string, sourceFilePath: string, aliases: StylexExtendBabelPluginOptions['aliases']) {
for (const ext of ['', ...EXTENSIONS]) {
const importPathStr = relativePath + ext
if (importPathStr.startsWith('.')) {
Expand All @@ -38,7 +66,16 @@ function filePathResolver(relativePath: string, sourceFilePath: string) {

}
}
const allAliases = possibleAliasedPaths(importPathStr, aliases)
for (const possiblePath of allAliases) {
try {
return require.resolve(possiblePath, {
paths: [path.dirname(sourceFilePath)]
})
} catch {}
}
}
return null
}

export class Context {
Expand Down Expand Up @@ -81,7 +118,7 @@ export class Context {

importPathResolver(importPath: string) {
if (!this.filename) throw new Error('filename is not defined')
const importerPath = filePathResolver(importPath, this.filename)
const importerPath = filePathResolver(importPath, this.filename, this.options.aliases)
if (!importerPath) throw new Error(`[stylex-extend]: Cannot resolve module ${importPath}`)
switch (this.options.unstable_moduleResolution.type) {
case 'commonJS':
Expand Down

0 comments on commit ea70b9d

Please sign in to comment.