-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add create api exploration * fix: type violation when using xcess properties * feat: add css map types to create api * chore: add to test case * chore: another test case * chore: fix types * chore: expose cs * chore: fix * feat: adds spike code * feat: add xcss func type * chore: rename api * fix: pseudo support for xcss prop * chore: separate test cases * chore: fix test * chore: move api behind a module * feat: add support for custom module origins * chore: add assertions to xcss prop usage * chore: add assertions for css() * chore: add tests for cssMap() * feat: add support for absolute/pkg paths * chore: rename to import sources * chore: rename to strict * chore: update jsdoc * chore: add jsdoc * chore: stub * chore: rename * chore: fix tests * chore: fix build * chore: changeset * chore: use root path * chore: remove example tags * chore: update error message to give as much context as possible
- Loading branch information
Showing
22 changed files
with
847 additions
and
31 deletions.
There are no files selected for viewing
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,49 @@ | ||
--- | ||
'@compiled/babel-plugin': patch | ||
'@compiled/react': patch | ||
--- | ||
|
||
Introduce new API `createStrictAPI` which returns a strict subset of Compiled APIs augmented by a type definition. | ||
This API does not change Compileds build time behavior — merely augmenting | ||
the returned API types which enforce: | ||
|
||
- all APIs use object types | ||
- property values declared in the type definition must be used (else fallback to defaults) | ||
- a strict subset of pseudo states/selectors | ||
- unknown properties to be a type violation | ||
|
||
To set up: | ||
|
||
1. Declare the API in a module (either local or in a package): | ||
|
||
```tsx | ||
import { createStrictAPI } from '@compiled/react'; | ||
|
||
// ./foo.ts | ||
const { css, cssMap, XCSSProp, cx } = createStrictAPI<{ | ||
color: 'var(--ds-text)'; | ||
'&:hover': { color: 'var(--ds-text-hover)' }; | ||
}>(); | ||
|
||
// Expose APIs you want to support. | ||
export { css, cssMap, XCSSProp, cx }; | ||
``` | ||
|
||
2. Configure Compiled to pick up this module: | ||
|
||
```diff | ||
// .compiledcssrc | ||
{ | ||
+ "importSources": ["./foo.ts"] | ||
} | ||
``` | ||
|
||
3. Use the module in your application code: | ||
|
||
```tsx | ||
import { css } from './foo'; | ||
|
||
const styles = css({ color: 'var(--ds-text)' }); | ||
|
||
<div css={styles} />; | ||
``` |
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
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,9 @@ | ||
{ | ||
"name": "@fixture/strict-api-test", | ||
"version": "0.1.0", | ||
"private": true, | ||
"main": "./src/index.ts", | ||
"dependencies": { | ||
"@compiled/react": "*" | ||
} | ||
} |
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,12 @@ | ||
import { createStrictAPI } from '@compiled/react'; | ||
|
||
const { css, XCSSProp, cssMap, cx } = createStrictAPI<{ | ||
'&:hover': { | ||
color: 'var(--ds-text-hover)'; | ||
background: 'var(--ds-surface-hover)' | 'var(--ds-surface-sunken-hover)'; | ||
}; | ||
color: 'var(--ds-text)'; | ||
background: 'var(--ds-surface)' | 'var(--ds-surface-sunken)'; | ||
}>(); | ||
|
||
export { css, XCSSProp, cssMap, cx }; |
88 changes: 88 additions & 0 deletions
88
packages/babel-plugin/src/__tests__/custom-import-source.test.ts
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,88 @@ | ||
import { transform } from '../test-utils'; | ||
|
||
describe('custom import source', () => { | ||
it('should pick up custom relative import source', () => { | ||
const actual = transform( | ||
` | ||
import { css } from '../bar/stub-api'; | ||
const styles = css({ color: 'red' }); | ||
<div css={styles} /> | ||
`, | ||
{ filename: './foo/index.js', importSources: ['./bar/stub-api'] } | ||
); | ||
|
||
expect(actual).toInclude('@compiled/react/runtime'); | ||
}); | ||
|
||
it('should pick up custom absolute import source', () => { | ||
const actual = transform( | ||
` | ||
import { css } from '/bar/stub-api'; | ||
const styles = css({ color: 'red' }); | ||
<div css={styles} /> | ||
`, | ||
{ filename: './foo/index.js', importSources: ['/bar/stub-api'] } | ||
); | ||
|
||
expect(actual).toInclude('@compiled/react/runtime'); | ||
}); | ||
|
||
it('should pick up custom package import source', () => { | ||
const actual = transform( | ||
` | ||
import { css } from '@af/compiled'; | ||
const styles = css({ color: 'red' }); | ||
<div css={styles} /> | ||
`, | ||
{ filename: './foo/index.js', importSources: ['@af/compiled'] } | ||
); | ||
|
||
expect(actual).toInclude('@compiled/react/runtime'); | ||
}); | ||
|
||
it("should handle custom package sources that aren't found", () => { | ||
expect(() => | ||
transform( | ||
` | ||
import { css } from '@af/compiled'; | ||
const styles = css({ color: 'red' }); | ||
<div css={styles} /> | ||
`, | ||
{ filename: './foo/index.js', importSources: ['asdasd2323'] } | ||
) | ||
).not.toThrow(); | ||
}); | ||
|
||
it('should throw error explaining resolution steps when using custom import source that hasnt been configured', () => { | ||
expect(() => | ||
transform( | ||
` | ||
/** @jsxImportSource @compiled/react */ | ||
import { css } from '@private/misconfigured'; | ||
const styles = css({ color: 'red' }); | ||
<div css={styles} /> | ||
`, | ||
{ filename: '/foo/index.js', highlightCode: false } | ||
) | ||
).toThrowErrorMatchingInlineSnapshot(` | ||
"/foo/index.js: This CallExpression was unable to have its styles extracted — no Compiled APIs were found in scope, if you're using createStrictAPI make sure to configure importSources (5:23). | ||
3 | import { css } from '@private/misconfigured'; | ||
4 | | ||
> 5 | const styles = css({ color: 'red' }); | ||
| ^^^^^^^^^^^^^^^^^^^^^ | ||
6 | | ||
7 | <div css={styles} /> | ||
8 | " | ||
`); | ||
}); | ||
}); |
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
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
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
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
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
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
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
13 changes: 13 additions & 0 deletions
13
packages/react/src/create-strict-api/__tests__/__fixtures__/strict-api.ts
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,13 @@ | ||
import { createStrictAPI } from '@compiled/react'; | ||
|
||
const { css, XCSSProp, cssMap, cx } = createStrictAPI<{ | ||
'&:hover': { | ||
color: 'var(--ds-text-hover)'; | ||
background: 'var(--ds-surface-hover)' | 'var(--ds-surface-sunken-hover)'; | ||
}; | ||
color: 'var(--ds-text)'; | ||
background: 'var(--ds-surface)' | 'var(--ds-surface-sunken)'; | ||
bkgrnd: 'red' | 'green'; | ||
}>(); | ||
|
||
export { css, XCSSProp, cssMap, cx }; |
Oops, something went wrong.