Skip to content

Commit 89f07cc

Browse files
committed
Add explanation & test for usage with jsx-pragmatic
1 parent 45edf5d commit 89f07cc

File tree

3 files changed

+59
-8
lines changed

3 files changed

+59
-8
lines changed

packages/babel-plugin-transform-jsx-to-tagged-templates/README.md

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ npm i -D babel-plugin-transform-jsx-to-tagged-templates
2323
```js
2424
"plugins": [
2525
"babel-plugin-transform-jsx-to-tagged-templates"
26-
]
2726
]
2827
```
2928

@@ -33,15 +32,45 @@ There's only one option: `tag`. It lets you specify the function to use for pref
3332

3433
```js
3534
"plugins": [
36-
[
37-
"babel-plugin-transform-jsx-to-tagged-templates",
38-
{
39-
"tag": "custom.html"
40-
}
41-
]
42-
]
35+
["babel-plugin-transform-jsx-to-tagged-templates", {
36+
"tag": "custom.html"
37+
}]
4338
]
4439
```
4540

41+
## Auto-importing the tag
42+
43+
Want to automatically import `html` into any file that uses JSX? It works the same as with JSX!
44+
Just use [babel-plugin-jsx-pragmatic]:
45+
46+
```js
47+
"plugins": [
48+
["babel-plugin-jsx-pragmatic", {
49+
// the module to import:
50+
"module": "lit-html",
51+
// a named export to use from that module:
52+
"export": "html",
53+
// what to call it locally: (should match your "tag" option)
54+
"import": "$$html"
55+
}],
56+
["babel-plugin-transform-jsx-to-tagged-templates", {
57+
"tag": "$$html"
58+
}]
59+
]
60+
```
61+
62+
The above will produce files that look like:
63+
64+
```js
65+
import { html as $$html } from 'lit-html';
66+
67+
export default $$html`<h1>hello</h1>`
68+
```
69+
70+
### License
71+
72+
Apache 2
73+
4674
[htm]: https://github.com/developit/htm
4775
[lit-html]: https://github.com/polymer/lit-html
76+
[babel-plugin-jsx-pragmatic]: https://github.com/jmm/babel-plugin-jsx-pragmatic

packages/babel-plugin-transform-jsx-to-tagged-templates/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
"@babel/preset-env": "^7.2.0",
5353
"babel-core": "^7.0.0-bridge.0",
5454
"babel-jest": "^23.6.0",
55+
"babel-plugin-jsx-pragmatic": "^1.0.2",
5556
"eslint": "^5.10.0",
5657
"eslint-config-developit": "^1.1.1",
5758
"jest": "^23.6.0",

packages/babel-plugin-transform-jsx-to-tagged-templates/test/index.test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,25 @@ describe('babel-plugin-transform-jsx-to-tagged-templates', () => {
4545
}).code
4646
).toBe('const Foo = props => html`<ul>${props.items.map(item => html`<li>\n ${item}\n </li>`)}</ul>`;');
4747
});
48+
49+
test('integration with babel-plugin-jsx-pragmatic', () => {
50+
expect(
51+
transform('const Foo = props => <div>hello</div>;', {
52+
babelrc: false,
53+
plugins: [
54+
['babel-plugin-jsx-pragmatic', {
55+
// module to import:
56+
module: 'lit-html',
57+
// the name of the export to use:
58+
export: 'html',
59+
// whatever you specified for the "tag" option:
60+
import: '$$html'
61+
}],
62+
[transformJsxToTaggedTemplatesPlugin, {
63+
tag: '$$html'
64+
}]
65+
]
66+
}).code
67+
).toBe('import { html as $$html } from "lit-html";\n\nconst Foo = props => $$html`<div>hello</div>`;');
68+
});
4869
});

0 commit comments

Comments
 (0)