-
Notifications
You must be signed in to change notification settings - Fork 317
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: Automatically Discover StyleX Aliases from Configuration Files #810
Open
p0nch000
wants to merge
19
commits into
facebook:main
Choose a base branch
from
p0nch000:feat/auto-aliases
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+382
−17
Open
Changes from 10 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
e739e09
Correctly parse functions like counter, url, etc, added a unit test
p0nch000 0566af3
Unit test for transform value features
p0nch000 554931b
ÂRan prettier/lint
p0nch000 93b3500
Merge branch 'facebook:main' into main
p0nch000 bd44f72
PR changes after review, fixed transform value function
p0nch000 6284257
lint and prettier
p0nch000 2608e81
Final fixes for flow test
p0nch000 39a8203
Merge branch 'facebook:main' into main
p0nch000 86b4489
First approach on aliases, unit test added
p0nch000 d098d00
First approach on aliases, unit test added
p0nch000 4cdd041
Considered deno.json, fix pr review comments
p0nch000 58e5271
Fixed test fail
p0nch000 65be7f8
Fixed flow and prettier tests failing
p0nch000 2ffdd78
Fixed test fail
p0nch000 eb2935f
flow type error fix
p0nch000 c1656f2
flow type error fix
p0nch000 f8e15f1
verification functions for casting configs
p0nch000 2875f7e
verification functions for casting configs
p0nch000 1c2100a
Correct install of json5 dependency
p0nch000 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
158 changes: 158 additions & 0 deletions
158
packages/babel-plugin/__tests__/stylex-transform-alias-config-test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* | ||
*/ | ||
|
||
|
||
'use strict'; | ||
|
||
import path from 'path'; | ||
import fs from 'fs'; | ||
import os from 'os'; | ||
import StateManager from '../src/utils/state-manager'; | ||
|
||
describe('StyleX Alias Configuration', () => { | ||
let tmpDir; | ||
let state; | ||
|
||
beforeEach(() => { | ||
// Create a temporary directory for our test files | ||
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'stylex-test-')); | ||
|
||
// Create a mock babel state | ||
state = { | ||
file: { | ||
metadata: {}, | ||
}, | ||
filename: path.join(tmpDir, 'src/components/Button.js'), | ||
}; | ||
}); | ||
|
||
afterEach(() => { | ||
// Clean up temporary directory | ||
fs.rmSync(tmpDir, { recursive: true, force: true }); | ||
}); | ||
|
||
const setupFiles = (files) => { | ||
for (const [filePath, content] of Object.entries(files)) { | ||
const fullPath = path.join(tmpDir, filePath); | ||
fs.mkdirSync(path.dirname(fullPath), { recursive: true }); | ||
fs.writeFileSync(fullPath, JSON.stringify(content, null, 2)); | ||
} | ||
}; | ||
|
||
test('discovers aliases from package.json', () => { | ||
setupFiles({ | ||
'package.json': { | ||
name: 'test-package', | ||
stylex: { | ||
aliases: { | ||
'@components': './src/components', | ||
'@utils/*': ['./src/utils/*'], | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
const manager = new StateManager(state); | ||
|
||
expect(manager.options.aliases).toEqual({ | ||
'@components': ['./src/components'], | ||
'@utils/*': ['./src/utils/*'], | ||
}); | ||
}); | ||
|
||
test('discovers aliases from tsconfig.json', () => { | ||
setupFiles({ | ||
'package.json': { name: 'test-package' }, | ||
'tsconfig.json': { | ||
compilerOptions: { | ||
baseUrl: '.', | ||
paths: { | ||
'@components/*': ['src/components/*'], | ||
'@utils/*': ['src/utils/*'], | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
const manager = new StateManager(state); | ||
|
||
expect(manager.options.aliases).toEqual({ | ||
'@components': ['src/components'], | ||
'@utils': ['src/utils'], | ||
}); | ||
}); | ||
|
||
test('merges aliases from both package.json and tsconfig.json', () => { | ||
setupFiles({ | ||
'package.json': { | ||
name: 'test-package', | ||
stylex: { | ||
aliases: { | ||
'@components': './src/components', | ||
}, | ||
}, | ||
}, | ||
'tsconfig.json': { | ||
compilerOptions: { | ||
baseUrl: '.', | ||
paths: { | ||
'@utils/*': ['src/utils/*'], | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
const manager = new StateManager(state); | ||
|
||
expect(manager.options.aliases).toEqual({ | ||
'@components': ['./src/components'], | ||
'@utils': ['src/utils'], | ||
}); | ||
}); | ||
|
||
test('manual configuration overrides discovered aliases', () => { | ||
setupFiles({ | ||
'package.json': { | ||
name: 'test-package', | ||
stylex: { | ||
aliases: { | ||
'@components': './src/components', | ||
}, | ||
}, | ||
}, | ||
}); | ||
|
||
state.opts = { | ||
aliases: { | ||
'@components': './custom/path', | ||
}, | ||
}; | ||
|
||
const manager = new StateManager(state); | ||
|
||
expect(manager.options.aliases).toEqual({ | ||
'@components': ['./custom/path'], | ||
}); | ||
}); | ||
|
||
test('handles missing configuration files gracefully', () => { | ||
const manager = new StateManager(state); | ||
expect(manager.options.aliases).toBeNull(); | ||
}); | ||
|
||
test('handles invalid JSON files gracefully', () => { | ||
setupFiles({ | ||
'package.json': '{invalid json', | ||
'tsconfig.json': '{also invalid', | ||
}); | ||
|
||
const manager = new StateManager(state); | ||
expect(manager.options.aliases).toBeNull(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Making this change fixed the problem for
tsconfig.json
filesSimilar fixes might be needed for the other config files as well.