-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement non-React / vanilla Compiled styles
- Loading branch information
Showing
19 changed files
with
536 additions
and
42 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
93 changes: 93 additions & 0 deletions
93
packages/babel-plugin-strip-runtime/src/__tests__/vanilla-css.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,93 @@ | ||
import { writeFileSync } from 'fs'; | ||
|
||
import { transform } from './transform'; | ||
|
||
// Mock out FS to avoid writing to disk | ||
// We aren't processing the result anyway, so no need for specifying the response | ||
jest.mock('fs'); | ||
|
||
describe('babel-plugin-strip-runtime with vanillaCss', () => { | ||
describe('with the classic runtime', () => { | ||
const runtime = 'classic'; | ||
|
||
const styles = `{ | ||
danger: { | ||
color: 'red', | ||
backgroundColor: 'red' | ||
}, | ||
success: { | ||
color: 'green', | ||
backgroundColor: 'green' | ||
} | ||
}`; | ||
|
||
it('should transform vanillaCss to ax', () => { | ||
const actual = transform( | ||
` | ||
import { cssMap, vanillaCss } from '@compiled/react'; | ||
const someStyles = cssMap(${styles}); | ||
function someFunctionCall(_obj) {} | ||
export const bap = someFunctionCall({ | ||
// node DOM constructor | ||
toDOM(node) { | ||
const { localId, state } = node.attrs; | ||
// injectCompiledCss should be added right before \`attrs\` at build time. | ||
const attrs = { | ||
'data-task-local-id': localId || 'local-task', | ||
'data-task-state': state || 'TODO', | ||
// vanillaCss function will hint Babel to inject styles on run time, and extract styles on build time | ||
class: vanillaCss([someStyles.base, state === "DONE" && someStyles.done]), | ||
}; | ||
// construct a div node | ||
return ['div', attrs, 0]; | ||
}, | ||
}); | ||
`, | ||
{ | ||
run: 'both', | ||
runtime, | ||
extractStylesToDirectory: { source: 'src/', dest: 'dist/' }, | ||
} | ||
); | ||
|
||
expect(actual).toMatchInlineSnapshot(` | ||
"/* app.tsx generated by @compiled/babel-plugin v0.0.0 */ | ||
import './app.compiled.css'; | ||
import * as React from 'react'; | ||
import { ax, ix } from '@compiled/react/runtime'; | ||
const someStyles = { | ||
danger: '_syaz5scu _bfhk5scu', | ||
success: '_syazbf54 _bfhkbf54', | ||
}; | ||
function someFunctionCall(_obj) {} | ||
export const bap = someFunctionCall({ | ||
// node DOM constructor | ||
toDOM(node) { | ||
const { localId, state } = node.attrs; | ||
// injectCompiledCss should be added right before \`attrs\` at build time. | ||
const attrs = { | ||
'data-task-local-id': localId || 'local-task', | ||
'data-task-state': state || 'TODO', | ||
// vanillaCss function will hint Babel to inject styles on run time, and extract styles on build time | ||
class: ax([someStyles.base, state === 'DONE' && someStyles.done]), | ||
}; | ||
// construct a div node | ||
return ['div', attrs, 0]; | ||
}, | ||
}); | ||
" | ||
`); | ||
|
||
expect(writeFileSync).toBeCalledWith( | ||
expect.stringContaining('app.compiled.css'), | ||
'._bfhk5scu{background-color:red}\n' + | ||
'._bfhkbf54{background-color:green}\n' + | ||
'._syaz5scu{color:red}\n' + | ||
'._syazbf54{color:green}' | ||
); | ||
}); | ||
}); | ||
}); |
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
17 changes: 17 additions & 0 deletions
17
packages/babel-plugin-strip-runtime/src/utils/is-inject-css.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,17 @@ | ||
import * as t from '@babel/types'; | ||
|
||
/** | ||
* Return true if (and only if) the current node is a | ||
* `injectCompiledCss()` function call. | ||
* | ||
* @param node | ||
* @returns if the node is `injectCompiledCss()` | ||
*/ | ||
export const isInjectCompiledCss = (node: t.Node): boolean => { | ||
return ( | ||
// TODO: update other injectGlobalCss usages in other places | ||
t.isCallExpression(node) && | ||
t.isIdentifier(node.callee) && | ||
node.callee.name === 'injectCompiledCss' | ||
); | ||
}; |
17 changes: 17 additions & 0 deletions
17
packages/babel-plugin-strip-runtime/src/utils/is-inject-global-css.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,17 @@ | ||
import * as t from '@babel/types'; | ||
|
||
/** | ||
* Return true if (and only if) the current node is a | ||
* `injectGlobalCss()` function call. | ||
* | ||
* @param node | ||
* @returns if the node is `injectGlobalCss()` | ||
*/ | ||
export const isInjectGlobalCss = (node: t.Node): boolean => { | ||
return ( | ||
// TODO: update other injectGlobalCss usages in other places | ||
t.isCallExpression(node) && | ||
t.isIdentifier(node.callee) && | ||
node.callee.name === 'injectGlobalCss' | ||
); | ||
}; |
14 changes: 0 additions & 14 deletions
14
packages/babel-plugin-strip-runtime/src/utils/is-inject-globalcss.ts
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.