File tree Expand file tree Collapse file tree 3 files changed +59
-8
lines changed
packages/babel-plugin-transform-jsx-to-tagged-templates Expand file tree Collapse file tree 3 files changed +59
-8
lines changed Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change 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" ,
Original file line number Diff line number Diff 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} ) ;
You can’t perform that action at this time.
0 commit comments