Skip to content

Commit eb7a59b

Browse files
feat(react-property): rewrite build svg script
No longer generate the following from svg build script: - attributes.json - attribute-to-property.json - boolean-properties.json - overloaded-boolean-properties.json The reason is these files have redundant attribute names, which makes the final bundle size larger than expected. Also, the svg attribute names are lowercased instead of camelcased. Now the following file is generated: - properties.json It's similar to `react-dom/lib/SVGDOMPropertyConfig.js` except redundant attribute to property name mappings are deleted. Also, update svg index module to include the injection properties.
1 parent 8559c38 commit eb7a59b

File tree

2 files changed

+74
-46
lines changed

2 files changed

+74
-46
lines changed

packages/react-property/scripts/build-svg.js

Lines changed: 29 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,7 @@ const SVGDOMPropertyConfig = require('react-dom/lib/SVGDOMPropertyConfig');
55
const { LIB_DIR, SVG_DIR } = require('./constants');
66

77
/**
8-
* Creates the DOM property map via injection.
9-
*
10-
* @see https://github.com/facebook/react/blob/15-stable/src/renderers/dom/shared/DOMProperty.js#L57
11-
*/
12-
DOMProperty.injection.injectDOMPropertyConfig(SVGDOMPropertyConfig);
13-
14-
// `autofocus` is removed since it's not an applicable attribute for SVG elements
15-
delete DOMProperty.getPossibleStandardName.autofocus;
16-
17-
/**
18-
* Create output directories (if it does not exist).
8+
* Create output directories (if it doesn't exist).
199
*/
2010
try {
2111
fs.mkdirSync(LIB_DIR);
@@ -30,37 +20,38 @@ try {
3020
}
3121

3222
/**
33-
* SVG DOM property config.
23+
* Contains a mapping of React props to HTML attributes.
3424
*
35-
* Contains a mapping of SVG attributes to React props.
25+
* @see https://github.com/facebook/react/blob/15-stable/src/renderers/dom/shared/SVGDOMPropertyConfig.js
3626
*
3727
* @type {Object}
3828
*/
39-
const attributeToProperty = {};
40-
41-
/**
42-
* List of SVG DOM attributes.
43-
*
44-
* @type {Array}
45-
*/
46-
const attributes = [];
47-
48-
Object.keys(DOMProperty.getPossibleStandardName).forEach(attributeName => {
49-
const propertyName = DOMProperty.getPossibleStandardName[attributeName];
50-
51-
if (attributeName !== propertyName) {
52-
attributeToProperty[attributeName] = propertyName;
53-
}
54-
55-
attributes.push(attributeName);
56-
});
57-
58-
fs.writeFileSync(
59-
path.resolve(SVG_DIR, 'attributes.json'),
60-
JSON.stringify(attributes)
61-
);
29+
const properties = {
30+
injection: DOMProperty.injection,
31+
32+
/**
33+
* To avoid duplication, some attributes are omitted as they exist on the HTML config.
34+
*
35+
* @see https://github.com/facebook/react/blob/15-stable/src/renderers/dom/shared/SVGDOMPropertyConfig.js#L17-L33
36+
*/
37+
Properties: SVGDOMPropertyConfig.Properties,
38+
39+
/**
40+
* Remove property when key and value are the same.
41+
*/
42+
DOMAttributeNames: Object.keys(SVGDOMPropertyConfig.DOMAttributeNames).reduce(
43+
(accumulator, key) => {
44+
const value = SVGDOMPropertyConfig.DOMAttributeNames[key];
45+
if (key !== value) {
46+
accumulator[key] = value;
47+
}
48+
return accumulator;
49+
},
50+
{}
51+
)
52+
};
6253

6354
fs.writeFileSync(
64-
path.resolve(SVG_DIR, 'attribute-to-property.json'),
65-
JSON.stringify(attributeToProperty)
55+
path.resolve(SVG_DIR, 'properties.json'),
56+
JSON.stringify(properties)
6657
);
Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,51 @@
1-
var attributeToProperty = require('../../lib/svg/attribute-to-property');
2-
var attributes = require('../../lib/svg/attributes');
1+
var properties = require('./properties');
32

4-
var attributeMap = {};
3+
var injection = properties.injection;
4+
var MUST_USE_PROPERTY = injection.MUST_USE_PROPERTY;
5+
var HAS_BOOLEAN_VALUE = injection.HAS_BOOLEAN_VALUE;
6+
var HAS_NUMERIC_VALUE = injection.HAS_NUMERIC_VALUE;
7+
var HAS_POSITIVE_NUMERIC_VALUE = injection.HAS_POSITIVE_NUMERIC_VALUE;
8+
var HAS_OVERLOADED_BOOLEAN_VALUE = injection.HAS_OVERLOADED_BOOLEAN_VALUE;
9+
var Properties = properties.Properties;
10+
var DOMAttributeNames = properties.DOMAttributeNames;
11+
12+
/**
13+
* @see https://github.com/facebook/react/blob/15-stable/src/renderers/dom/shared/DOMProperty.js#L14-L16
14+
*
15+
* @param {Number} value
16+
* @param {Number} bitmask
17+
* @return {Boolean}
18+
*/
19+
function checkMask(value, bitmask) {
20+
return (value & bitmask) === bitmask;
21+
}
22+
23+
/**
24+
* Config map.
25+
*
26+
* @type {Object}
27+
*/
28+
var config = {};
529
var attributeName;
630
var propertyName;
31+
var propConfig;
32+
33+
for (propertyName in Properties) {
34+
attributeName = DOMAttributeNames[propertyName] || propertyName;
35+
propConfig = Properties[propertyName];
736

8-
for (var i = 0, len = attributes.length; i < len; i++) {
9-
attributeName = attributes[i];
10-
propertyName = attributeToProperty[attributeName] || attributeName;
11-
attributeMap[attributeName] = { propertyName: propertyName };
37+
config[attributeName] = {
38+
attributeName: attributeName,
39+
propertyName: propertyName,
40+
mustUseProperty: checkMask(propConfig, MUST_USE_PROPERTY),
41+
hasBooleanValue: checkMask(propConfig, HAS_BOOLEAN_VALUE),
42+
hasNumericValue: checkMask(propConfig, HAS_NUMERIC_VALUE),
43+
hasPositiveNumericValue: checkMask(propConfig, HAS_POSITIVE_NUMERIC_VALUE),
44+
hasOverloadedBooleanValue: checkMask(
45+
propConfig,
46+
HAS_OVERLOADED_BOOLEAN_VALUE
47+
)
48+
};
1249
}
1350

14-
module.exports = attributeMap;
51+
module.exports = config;

0 commit comments

Comments
 (0)