Skip to content

Commit 120c82b

Browse files
committed
feat: update ./codegen -> use modules, code is generated the same
1 parent 3ad6806 commit 120c82b

File tree

3 files changed

+85
-82
lines changed

3 files changed

+85
-82
lines changed

codegen/consts.js

+13-7
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
// For now, we are using a local copy of react-html-attributes.
22
// Eventually, the relative path here should be removed, and
33
// the version number of react-html-attributes should be bumped.
4-
module.exports.htmlProps = require("./react-html-attributes");
5-
module.exports.svgProps = require("svg-element-attributes");
4+
import htmlProps from "./react-html-attributes.json" with { type: "json" };
5+
6+
import { svgElementAttributes } from "svg-element-attributes";
7+
8+
export { htmlProps, svgElementAttributes }
9+
10+
export const voids = ["area", "base", "br", "col", "embed", "hr", "img", "input", "link", "meta", "param", "source", "track", "wbr"];
611

7-
module.exports.voids = ["area", "base", "br", "col", "embed", "hr", "img", "input", "link", "meta", "param", "source", "track", "wbr"];
812
// The types for certain attributes differ according to their containing element.
913
// These appear in the typesByElement object, using "*" as a default element
1014
// (required only if the default attribute type is other than `String`).
1115
// For clarity, it is an error for any attribute to be in both `types` and `typesByElement`.
12-
module.exports.typesByElement = {
16+
export const typesByElement = {
1317
"cols": {
1418
"textarea": "Int",
1519
},
@@ -23,12 +27,13 @@ module.exports.typesByElement = {
2327
"rows": {
2428
"textarea": "Int"
2529
},
26-
"size" : {
30+
"size": {
2731
"input": "Int",
2832
"select": "Int"
2933
}
3034
};
31-
module.exports.types = {
35+
36+
export const types = {
3237
"_aria": "Object String",
3338
"_data": "Object String",
3439
"allowFullScreen": "Boolean",
@@ -156,4 +161,5 @@ module.exports.types = {
156161
"tabIndex": "Int",
157162
"unselectable": "Boolean"
158163
};
159-
module.exports.reserved = ["module", "data", "type", "newtype", "class", "instance", "where", "derive", "if", "then", "else", "case", "of"];
164+
165+
export const reserved = ["module", "data", "type", "newtype", "class", "instance", "where", "derive", "if", "then", "else", "case", "of"];

codegen/index.js

+71-75
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
const fs = require("fs");
2-
const { htmlProps, svgProps, voids, types, typesByElement, reserved } = require("./consts");
3-
const changeCase = require('change-case')
1+
import fs from "fs";
2+
import { htmlProps, svgElementAttributes, voids, types, typesByElement, reserved } from "./consts.js";
3+
import * as changeCase from "change-case";
4+
45
const htmlGenFile = "../src/React/Basic/DOM/Generated.purs";
56
const htmlSimplifiedGenFile = "../src/React/Basic/DOM/Simplified/Generated.purs";
67
const svgGenFile = "../src/React/Basic/DOM/SVG.purs";
@@ -25,7 +26,6 @@ import React.Basic.DOM.Internal (CSS, unsafeCreateDOMComponent)
2526
import React.Basic.Events (EventHandler)
2627
import Unsafe.Coerce (unsafeCoerce)
2728
import Web.DOM (Node)
28-
2929
`;
3030

3131
const simplifiedHtmlHeader = `${warningHeader}
@@ -42,7 +42,6 @@ import React.Basic.Events (EventHandler)
4242
import Record as Record
4343
import Unsafe.Coerce (unsafeCoerce)
4444
import Web.DOM (Node)
45-
4645
`;
4746

4847
const propType = (e, p) => {
@@ -68,7 +67,6 @@ import React.Basic (JSX, ReactComponent, Ref, element)
6867
import React.Basic.DOM.Internal (SharedSVGProps, unsafeCreateDOMComponent)
6968
import Unsafe.Coerce (unsafeCoerce)
7069
import Web.DOM (Node)
71-
7270
`;
7371

7472
const ignoredSvgPropKeys = [
@@ -83,7 +81,7 @@ const ignoredSvgPropKeys = [
8381
]
8482

8583
const camelCaseSvgProps = Object.fromEntries(
86-
Object.entries(svgProps.svgElementAttributes)
84+
Object.entries(svgElementAttributes)
8785
.filter(([elName]) => !ignoredSvgPropKeys.includes(elName)) // Exclude ignored keys
8886
.map(([elName, attrs]) => [
8987
elName,
@@ -114,39 +112,38 @@ const generatePropTypes = (elements, props, sharedPropType) =>
114112
const propType = sharedPropType ? `(${sharedPropType} Props_${e})` : `Props_${e}`
115113

116114
return `
117-
type Props_${e} =${printRecord(e,
118-
( noChildren
119-
? reactProps
120-
: reactProps.concat("children")
121-
)
122-
.concat(props[e] || [], props["*"] || [])
123-
.sort()
124-
)}
125-
126-
${symbol}
127-
:: forall attrs attrs_
128-
. Union attrs attrs_ ${propType}
129-
=> Record attrs
130-
-> JSX
131-
${symbol} = element ${symbol}'${
132-
noChildren
133-
? ""
134-
: `
135-
136-
${e}_ :: Array JSX -> JSX
137-
${e}_ children = ${symbol} { children }`
138-
}
115+
type Props_${e} =${printRecord(e,
116+
( noChildren
117+
? reactProps
118+
: reactProps.concat("children")
119+
)
120+
.concat(props[e] || [], props["*"] || [])
121+
.sort()
122+
)}
123+
124+
${symbol}
125+
:: forall attrs attrs_
126+
. Union attrs attrs_ ${propType}
127+
=> Record attrs
128+
-> JSX
129+
${symbol} = element ${symbol}'${
130+
noChildren
131+
? ""
132+
: `
133+
134+
${e}_ :: Array JSX -> JSX
135+
${e}_ children = ${symbol} { children }`
136+
}
139137
140-
${symbol}'
141-
:: forall attrs attrs_
142-
. Union attrs attrs_ ${propType}
143-
=> ReactComponent (Record attrs)
144-
${symbol}' = unsafeCoerce _${e}'
138+
${symbol}'
139+
:: forall attrs attrs_
140+
. Union attrs attrs_ ${propType}
141+
=> ReactComponent (Record attrs)
142+
${symbol}' = unsafeCoerce _${e}'
145143
146-
_${e}'
147-
:: ReactComponent (Record ${propType})
148-
_${e}' = unsafePerformEffect (unsafeCreateDOMComponent "${e}")
149-
`;
144+
_${e}'
145+
:: ReactComponent (Record ${propType})
146+
_${e}' = unsafePerformEffect (unsafeCreateDOMComponent "${e}")`;
150147
}).map(x => x.replace(/^\n\ {4}/, "").replace(/\n\ {4}/g, "\n"))
151148
.join("\n");
152149

@@ -158,40 +155,39 @@ const generatePropTypes = (elements, props, sharedPropType) =>
158155
const propType = sharedPropType ? `(${sharedPropType} Props_${e})` : `Props_${e}`
159156

160157
return noChildren ? `` : `
161-
type Props_${e} =${printRecord(e,
162-
( reactProps.concat("children")
163-
)
164-
.concat(props[e] || [], props["*"] || [])
165-
.sort()
166-
)}
167-
168-
${symbol}
169-
:: forall attrsNoChildren attrsWithDuplicate attrs attrs_ jsx
170-
. Union attrs attrs_ ${propType}
171-
=> ToJSX jsx
172-
=> Union (children :: Array JSX) attrsNoChildren attrsWithDuplicate
173-
=> Nub (children :: Array JSX | attrsNoChildren) attrs
174-
=> Record attrsNoChildren
175-
-> jsx
176-
-> JSX
177-
${symbol} props children = element _internal${symbol} propsWithChildren
178-
where
179-
propsWithChildren :: { | attrs }
180-
propsWithChildren = Record.merge { children: toJSX children } props
181-
182-
${symbol}' :: forall jsx. ToJSX jsx => jsx -> JSX
183-
${symbol}' = ${symbol} {}
184-
185-
_internal${symbol}
186-
:: forall attrs attrs_
187-
. Union attrs attrs_ ${propType}
188-
=> ReactComponent (Record attrs)
189-
_internal${symbol} = unsafeCoerce _internal${symbol}'
190-
191-
_internal${symbol}'
192-
:: ReactComponent (Record ${propType})
193-
_internal${symbol}' = unsafePerformEffect (unsafeCreateDOMComponent "${symbol}")
194-
158+
type Props_${e} =${printRecord(e,
159+
( reactProps.concat("children")
160+
)
161+
.concat(props[e] || [], props["*"] || [])
162+
.sort()
163+
)}
164+
165+
${symbol}
166+
:: forall attrsNoChildren attrsWithDuplicate attrs attrs_ jsx
167+
. Union attrs attrs_ ${propType}
168+
=> ToJSX jsx
169+
=> Union (children :: Array JSX) attrsNoChildren attrsWithDuplicate
170+
=> Nub (children :: Array JSX | attrsNoChildren) attrs
171+
=> Record attrsNoChildren
172+
-> jsx
173+
-> JSX
174+
${symbol} props children = element _internal${symbol} propsWithChildren
175+
where
176+
propsWithChildren :: { | attrs }
177+
propsWithChildren = Record.merge { children: toJSX children } props
178+
179+
${symbol}' :: forall jsx. ToJSX jsx => jsx -> JSX
180+
${symbol}' = ${symbol} {}
181+
182+
_internal${symbol}
183+
:: forall attrs attrs_
184+
. Union attrs attrs_ ${propType}
185+
=> ReactComponent (Record attrs)
186+
_internal${symbol} = unsafeCoerce _internal${symbol}'
187+
188+
_internal${symbol}'
189+
:: ReactComponent (Record ${propType})
190+
_internal${symbol}' = unsafePerformEffect (unsafeCreateDOMComponent "${symbol}")
195191
`;
196192
}).map(x => x.replace(/^\n\ {4}/, "").replace(/\n\ {4}/g, "\n"))
197193
.join("\n");
@@ -201,12 +197,12 @@ const htmlSimplifiedTagTypes = generateSimplifiedPropTypes(htmlProps.elements.ht
201197
const svgTagTypes = generatePropTypes(Object.keys(camelCaseSvgProps), camelCaseSvgProps, 'SharedSVGProps');
202198

203199
console.log(`Writing "${htmlGenFile}" ...`);
204-
fs.writeFileSync(htmlGenFile, htmlHeader + htmlTagTypes);
200+
fs.writeFileSync(htmlGenFile, htmlHeader + htmlTagTypes + "\n");
205201

206202
console.log(`Writing "${htmlSimplifiedGenFile}" ...`);
207-
fs.writeFileSync(htmlSimplifiedGenFile, simplifiedHtmlHeader + htmlSimplifiedTagTypes);
203+
fs.writeFileSync(htmlSimplifiedGenFile, simplifiedHtmlHeader + htmlSimplifiedTagTypes + "\n");
208204

209205
console.log(`Writing "${svgGenFile}" ...`);
210-
fs.writeFileSync(svgGenFile, svgHeader + svgTagTypes);
206+
fs.writeFileSync(svgGenFile, svgHeader + svgTagTypes + "\n");
211207

212208
console.log("Done.");

codegen/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"description": "",
55
"main": "index.js",
66
"author": "",
7+
"type": "module",
78
"dependencies": {
89
"react-html-attributes": "^1.4.6",
910
"change-case": "^5.4.4",

0 commit comments

Comments
 (0)