Skip to content

Commit 0708c7b

Browse files
committed
Initial commit
0 parents  commit 0708c7b

14 files changed

Lines changed: 5870 additions & 0 deletions

.gitignore

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# dependencies
2+
/node_modules
3+
node_modules/
4+
5+
# testing
6+
/coverage
7+
8+
# misc
9+
package-lock.json
10+
completions.json
11+
.DS_Store
12+
.history
13+
*not_in_use*
14+
/packages/dnb-ui-lib
15+
16+
# pkm
17+
npm-debug.log*
18+
yarn-debug.log*
19+
yarn-error.log*
20+
.yarn/*
21+
.yarn/cache
22+
!.yarn/patches
23+
!.yarn/plugins
24+
!.yarn/releases
25+
!.yarn/sdks
26+
!.yarn/versions
27+
28+
# enviroment
29+
.env
30+
.env.*

.prettierrc

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"printWidth": 75,
3+
"tabWidth": 2,
4+
"singleQuote": true,
5+
"bracketSpacing": true,
6+
"useTabs": false,
7+
"semi": false,
8+
"bracketSameLine": false,
9+
"trailingComma": "es5",
10+
"overrides": [
11+
{
12+
"files": ".yarnrc.yml",
13+
"options": {
14+
"singleQuote": false
15+
}
16+
}
17+
]
18+
}

.yarn/releases/yarn-3.2.0.cjs

Lines changed: 785 additions & 0 deletions
Large diffs are not rendered by default.

.yarnrc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
2+
# yarn lockfile v1
3+
4+
5+
yarn-path ".yarn/releases/yarn-1.22.19.cjs"

.yarnrc.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
defaultSemverRangePrefix: ""
2+
3+
enableGlobalCache: false
4+
5+
nodeLinker: node-modules
6+
7+
pnpMode: loose
8+
9+
yarnPath: .yarn/releases/yarn-3.2.0.cjs

LICENSE

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"Commons Clause" License Condition v1.0
2+
3+
The Software is provided to you by the Licensor under the License,
4+
as defined below, subject to the following condition.
5+
6+
Without limiting other conditions in the License,
7+
the grant of rights under the License will not include,
8+
and the License does not grant to you,
9+
the right to Sell the Software.
10+
11+
For purposes of the foregoing, “Sell” means practicing any
12+
or all of the rights granted to you under the License
13+
to provide to third parties,
14+
for a fee or other consideration
15+
(including without limitation fees for hosting
16+
or consulting/ support services related to the Software),
17+
a product or service whose value derives,
18+
entirely or substantially, from the functionality of the Software.
19+
Any license notice or attribution required by
20+
the License must also include this
21+
Commons Cause License Condition notice.
22+
23+
https://commonsclause.com/
24+
25+
Software: DNB Design System (Eufemia)
26+
License: Apache 2.0 with Commons Clause
27+
Licensor: DNB Bank ASA, Org nr. 984 851 006
28+
29+
Copyright (c) 2018-present DNB.no, https://www.dnb.no/
30+
31+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
32+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
33+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
34+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
35+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
36+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
37+
SOFTWARE.

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Babel Plugin to enhance React Live syntax
2+
3+
[React Live](https://github.com/FormidableLabs/react-live) only supports code as a string. This Babel Plugin uses AST to transform JavaScript and TypeScript code to a string.
4+
5+
This allows the given "source code" to be fully typed (TypeScript).
6+
7+
## How to use
8+
9+
Install `babel-plugin-react-live` and add it to your Babel config.
10+
11+
```json
12+
{
13+
"plugins": [
14+
[
15+
"babel-plugin-react-live",
16+
{
17+
"componentName": "YourComponent",
18+
"filesToMatch": ["Examples.tsx"],
19+
"prettierPath": "./.prettierrc"
20+
}
21+
]
22+
]
23+
}
24+
```
25+
26+
## How it works
27+
28+
It uses AST to transform related code to a string.

__mocks__/Examples.tsx

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import React from 'react'
2+
3+
const ComponentBox = ({ children }) => children
4+
5+
export const MockNoInlineWithComponent = () => {
6+
return (
7+
<ComponentBox data-test="id">
8+
{() => {
9+
const DemoComponent = () => {
10+
return <>content</>
11+
}
12+
13+
return <DemoComponent />
14+
}}
15+
</ComponentBox>
16+
)
17+
}
18+
19+
export const MockNoInlineWithText = () => {
20+
return (
21+
<ComponentBox data-test="id">
22+
{() => {
23+
return <>content</>
24+
}}
25+
</ComponentBox>
26+
)
27+
}
28+
29+
export const MockOneChilds = () => {
30+
return (
31+
<ComponentBox data-test="id">
32+
<div>content</div>
33+
</ComponentBox>
34+
)
35+
}
36+
37+
export const MockManyChilds = () => {
38+
return (
39+
<ComponentBox data-test="id">
40+
<div>content 1</div>
41+
<div>content 2</div>
42+
<div>content 3</div>
43+
</ComponentBox>
44+
)
45+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/**
2+
* Babel Plugin Test
3+
*
4+
*/
5+
6+
import { transformFileAsync } from '@babel/core'
7+
import nodePath from 'path'
8+
import prettier from 'prettier'
9+
import babelPluginReactLive from '../babelPluginReactLive'
10+
11+
const targetFile = nodePath.resolve(__dirname, '../__mocks__/Examples.tsx')
12+
const prettierPath = nodePath.resolve(__dirname, '../.prettierrc')
13+
const pluginOptions = {
14+
componentName: 'ComponentBox',
15+
filesToMatch: ['Examples.tsx'],
16+
prettierPath,
17+
}
18+
19+
it('babelPluginReactLive', async () => {
20+
const babelFileResult = await transformFileAsync(targetFile, {
21+
code: true,
22+
presets: [
23+
[
24+
'@babel/preset-env',
25+
{
26+
modules: false,
27+
targets: { firefox: '100' },
28+
},
29+
],
30+
],
31+
plugins: [[babelPluginReactLive, pluginOptions]],
32+
})
33+
34+
const code = String(babelFileResult?.code)
35+
36+
const formattedCode = prettier.format(code, {
37+
filepath: 'file.tsx',
38+
semi: false,
39+
})
40+
41+
expect(formattedCode).toMatchInlineSnapshot(`
42+
"const ComponentBox = ({ children }) => children
43+
export const MockNoInlineWithComponent = () => {
44+
return (
45+
<ComponentBox data-test="id" noInline>{\`const DemoComponent = () => {
46+
return <>content</>
47+
}
48+
render(<DemoComponent />)
49+
\`}</ComponentBox>
50+
)
51+
}
52+
export const MockNoInlineWithText = () => {
53+
return (
54+
<ComponentBox data-test="id" noInline>{\`render(<>content</>)
55+
\`}</ComponentBox>
56+
)
57+
}
58+
export const MockOneChilds = () => {
59+
return (
60+
<ComponentBox data-test="id">{\`<div>content</div>
61+
\`}</ComponentBox>
62+
)
63+
}
64+
export const MockManyChilds = () => {
65+
return (
66+
<ComponentBox data-test="id">{\`<>
67+
<div>content 1</div>
68+
<div>content 2</div>
69+
<div>content 3</div>
70+
</>
71+
\`}</ComponentBox>
72+
)
73+
}
74+
"
75+
`)
76+
77+
expect(formattedCode.match(/noInline/g)).toHaveLength(2)
78+
expect(formattedCode.match(/\{`/g)).toHaveLength(4)
79+
expect(formattedCode.match(/`\}/g)).toHaveLength(4)
80+
})

babel.config.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module.exports = {
2+
presets: [
3+
['@babel/preset-env', { targets: { node: 'current' } }],
4+
['@babel/preset-typescript', { isTSX: true, allExtensions: true }],
5+
'@babel/preset-react',
6+
],
7+
}

0 commit comments

Comments
 (0)