From 9f860b8dfc8e54297752d9c661e16aa8c640eaac Mon Sep 17 00:00:00 2001 From: Nicolas Gallagher Date: Tue, 22 Jan 2019 18:38:26 -0800 Subject: [PATCH] [change] StyleSheet: compile styles directly to CSS MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Introduces a centralized compiler for "atomic" and "classic" CSS output. The "classic" compiler is for internal use only and offers no CSS safety guarantees. The "atomic compiler is used to implement the public-facing StyleSheet API. The atomic compiler now maps the React style declarations, rather than CSS style declarations, to CSS rules. This avoids having to convert React styles to CSS styles before being able to lookup classNames. And it reduces the number of CSS rules needed by each DOM element. Before: { paddingHorizontal: 0; } ↓ .paddingLeft-0 { padding-left: 0; } .paddingRight-0 { padding-right: 0; } After: { paddingHorizontal: 0; } ↓ .paddingHorizontal-0 { padding-left: 0; padding-right: 0 } Overview of previous StyleSheet resolver: 1. Localise styles 2. Transform to CSS styles 3. Expand short-form properties 4a. Lookup Atomic CSS for each declaration 4b. Compile Atomic CSS for each static declaration i. Vendor prefix ii. Insert CSS rules 4c. Create inline style for each dynamic-only declaration i. Vendor prefix Overview of new StyleSheet design: 1. Localise styles 2a. Lookup Atomic CSS for each declaration 2b. Compile Atomic CSS for each static declarations i. Transform to CSS styles ii. Expand short-form properties iii. Vendor prefix iiii. Insert CSS rules 2c. Create inline style for each dynamic-only declaration i. Transform to CSS styles ii. Expand short-form properties iii. Vendor prefix Ref #1136 --- .watchmanconfig | 1 + .../__snapshots__/index-test.js.snap | 12 +- .../src/exports/ActivityIndicator/index.js | 2 +- .../__snapshots__/index-test.js.snap | 5 +- .../__snapshots__/index-test.js.snap | 2 +- .../src/exports/ProgressBar/index.js | 2 +- .../StyleSheet/ReactNativeStyleResolver.js | 137 ++++--- .../exports/StyleSheet/StyleSheetManager.js | 99 ----- .../__tests__/StyleSheetManager-test.js | 42 -- .../ReactNativeStyleResolver-test.js.snap | 36 +- .../StyleSheetManager-test.js.snap | 3 - .../__snapshots__/compile-test.js.snap | 127 ++++++ .../__snapshots__/createAtomicRules.js.snap | 30 -- .../createRuleBlock-test.js.snap | 3 - .../StyleSheet/__tests__/compile-test.js | 67 ++++ .../StyleSheet/__tests__/createAtomicRules.js | 25 -- .../__tests__/createCompileableStyle-test.js | 120 ++++++ .../__tests__/createReactDOMStyle-test.js | 137 +------ .../__tests__/createRuleBlock-test.js | 16 - .../StyleSheet/__tests__/index-test.js | 21 +- .../__tests__/normalizeValue-test.js | 13 - .../normalizeValueWithProperty-test.js | 20 + .../src/exports/StyleSheet/compile.js | 262 +++++++++++++ .../src/exports/StyleSheet/constants.js | 25 ++ .../exports/StyleSheet/createAtomicRules.js | 82 ---- .../exports/StyleSheet/createCSSStyleSheet.js | 31 ++ .../StyleSheet/createCompileableStyle.js | 64 +++ .../StyleSheet/createKeyframesRules.js | 37 -- .../exports/StyleSheet/createReactDOMStyle.js | 366 +++++++----------- .../src/exports/StyleSheet/createRuleBlock.js | 35 -- .../exports/StyleSheet/createStyleResolver.js | 0 .../src/exports/StyleSheet/modality.js | 6 +- .../src/exports/StyleSheet/normalizeValue.js | 19 - .../StyleSheet/normalizeValueWithProperty.js | 33 ++ .../exports/StyleSheet/resolveShadowValue.js | 8 +- .../__snapshots__/index-test.js.snap | 6 +- .../src/modules/AnimationPropTypes/index.js | 32 +- .../__snapshots__/index-test.js.snap | 4 +- .../storybook/1-components/View/ViewScreen.js | 4 +- scripts/babel/preset.js | 2 +- 40 files changed, 1055 insertions(+), 881 deletions(-) create mode 100644 .watchmanconfig delete mode 100644 packages/react-native-web/src/exports/StyleSheet/StyleSheetManager.js delete mode 100644 packages/react-native-web/src/exports/StyleSheet/__tests__/StyleSheetManager-test.js delete mode 100644 packages/react-native-web/src/exports/StyleSheet/__tests__/__snapshots__/StyleSheetManager-test.js.snap create mode 100644 packages/react-native-web/src/exports/StyleSheet/__tests__/__snapshots__/compile-test.js.snap delete mode 100644 packages/react-native-web/src/exports/StyleSheet/__tests__/__snapshots__/createAtomicRules.js.snap delete mode 100644 packages/react-native-web/src/exports/StyleSheet/__tests__/__snapshots__/createRuleBlock-test.js.snap create mode 100644 packages/react-native-web/src/exports/StyleSheet/__tests__/compile-test.js delete mode 100644 packages/react-native-web/src/exports/StyleSheet/__tests__/createAtomicRules.js create mode 100644 packages/react-native-web/src/exports/StyleSheet/__tests__/createCompileableStyle-test.js delete mode 100644 packages/react-native-web/src/exports/StyleSheet/__tests__/createRuleBlock-test.js delete mode 100644 packages/react-native-web/src/exports/StyleSheet/__tests__/normalizeValue-test.js create mode 100644 packages/react-native-web/src/exports/StyleSheet/__tests__/normalizeValueWithProperty-test.js create mode 100644 packages/react-native-web/src/exports/StyleSheet/compile.js create mode 100644 packages/react-native-web/src/exports/StyleSheet/constants.js delete mode 100644 packages/react-native-web/src/exports/StyleSheet/createAtomicRules.js create mode 100644 packages/react-native-web/src/exports/StyleSheet/createCSSStyleSheet.js create mode 100644 packages/react-native-web/src/exports/StyleSheet/createCompileableStyle.js delete mode 100644 packages/react-native-web/src/exports/StyleSheet/createKeyframesRules.js delete mode 100644 packages/react-native-web/src/exports/StyleSheet/createRuleBlock.js create mode 100644 packages/react-native-web/src/exports/StyleSheet/createStyleResolver.js delete mode 100644 packages/react-native-web/src/exports/StyleSheet/normalizeValue.js create mode 100644 packages/react-native-web/src/exports/StyleSheet/normalizeValueWithProperty.js diff --git a/.watchmanconfig b/.watchmanconfig new file mode 100644 index 00000000..0967ef42 --- /dev/null +++ b/.watchmanconfig @@ -0,0 +1 @@ +{} diff --git a/packages/react-native-web/src/exports/ActivityIndicator/__tests__/__snapshots__/index-test.js.snap b/packages/react-native-web/src/exports/ActivityIndicator/__tests__/__snapshots__/index-test.js.snap index 5fe49c57..ba46fa39 100644 --- a/packages/react-native-web/src/exports/ActivityIndicator/__tests__/__snapshots__/index-test.js.snap +++ b/packages/react-native-web/src/exports/ActivityIndicator/__tests__/__snapshots__/index-test.js.snap @@ -17,7 +17,7 @@ exports[`components/ActivityIndicator prop "animating" is "false" 1`] = ` Object { "animationDuration": "0.75s", "animationIterationCount": "infinite", - "animationName": Array [ + "animationKeyframes": Array [ Object { "0%": Object { "transform": Array [ @@ -97,7 +97,7 @@ exports[`components/ActivityIndicator prop "animating" is "true" 1`] = ` Object { "animationDuration": "0.75s", "animationIterationCount": "infinite", - "animationName": Array [ + "animationKeyframes": Array [ Object { "0%": Object { "transform": Array [ @@ -211,7 +211,7 @@ exports[`components/ActivityIndicator prop "hidesWhenStopped" is "false" 1`] = ` Object { "animationDuration": "0.75s", "animationIterationCount": "infinite", - "animationName": Array [ + "animationKeyframes": Array [ Object { "0%": Object { "transform": Array [ @@ -290,7 +290,7 @@ exports[`components/ActivityIndicator prop "hidesWhenStopped" is "true" 1`] = ` Object { "animationDuration": "0.75s", "animationIterationCount": "infinite", - "animationName": Array [ + "animationKeyframes": Array [ Object { "0%": Object { "transform": Array [ @@ -370,7 +370,7 @@ exports[`components/ActivityIndicator prop "size" is "large" 1`] = ` Object { "animationDuration": "0.75s", "animationIterationCount": "infinite", - "animationName": Array [ + "animationKeyframes": Array [ Object { "0%": Object { "transform": Array [ @@ -448,7 +448,7 @@ exports[`components/ActivityIndicator prop "size" is a number 1`] = ` Object { "animationDuration": "0.75s", "animationIterationCount": "infinite", - "animationName": Array [ + "animationKeyframes": Array [ Object { "0%": Object { "transform": Array [ diff --git a/packages/react-native-web/src/exports/ActivityIndicator/index.js b/packages/react-native-web/src/exports/ActivityIndicator/index.js index 76d81634..68d62ea6 100644 --- a/packages/react-native-web/src/exports/ActivityIndicator/index.js +++ b/packages/react-native-web/src/exports/ActivityIndicator/index.js @@ -86,7 +86,7 @@ const styles = StyleSheet.create({ }, animation: { animationDuration: '0.75s', - animationName: [ + animationKeyframes: [ { '0%': { transform: [{ rotate: '0deg' }] }, '100%': { transform: [{ rotate: '360deg' }] } diff --git a/packages/react-native-web/src/exports/AppRegistry/__tests__/__snapshots__/index-test.js.snap b/packages/react-native-web/src/exports/AppRegistry/__tests__/__snapshots__/index-test.js.snap index d5b6eb6f..8bcfdeac 100644 --- a/packages/react-native-web/src/exports/AppRegistry/__tests__/__snapshots__/index-test.js.snap +++ b/packages/react-native-web/src/exports/AppRegistry/__tests__/__snapshots__/index-test.js.snap @@ -11,10 +11,13 @@ exports[`AppRegistry getApplication returns "element" and "getStyleElement" 1`] exports[`AppRegistry getApplication returns "element" and "getStyleElement" 2`] = ` "" `; diff --git a/packages/react-native-web/src/exports/Picker/__tests__/__snapshots__/index-test.js.snap b/packages/react-native-web/src/exports/Picker/__tests__/__snapshots__/index-test.js.snap index a5a042ce..0d7419ca 100644 --- a/packages/react-native-web/src/exports/Picker/__tests__/__snapshots__/index-test.js.snap +++ b/packages/react-native-web/src/exports/Picker/__tests__/__snapshots__/index-test.js.snap @@ -10,7 +10,7 @@ exports[`components/Picker prop "children" items 1`] = ` exports[`components/Picker prop "children" renders items 1`] = `