question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

react-tokens build optimizations

See original GitHub issue

cc @evwilkin @redallen @karelhala

React tokens are currently built into a single file. This causes issues in non-esm environments. Since there were great optimizations for pf4 components packages, which greatly improved bundle sizes of consumer apps (so far it was around 50% - 80%, depends on use-case), I think we can do the same with tokens

Here is an example of how this can affect the build of app using pf4: Screenshot from 2020-02-03 15-00-43

This is one of the cloud services app. As you can see, react-tokens has almost the same parsed size as react-core.

basically the solution to this would be the same as with the rest of pf4 packages. Build each token into one file (while keeping the index with all tokens), and use absolute import path on pf4 react packages.

The code change would be rather simple. Here are some examples.

Here is something that can be added to the template files

diff --git a/packages/patternfly-4/react-tokens/src/templates/cjs.js b/packages/patternfly-4/react-tokens/src/templates/cjs.js
index 9cc1b1313..683e4f2ff 100644
--- a/packages/patternfly-4/react-tokens/src/templates/cjs.js
+++ b/packages/patternfly-4/react-tokens/src/templates/cjs.js
@@ -3,5 +3,7 @@ const { join } = require('path');
 module.exports = {
   getOutputPath: ({ outDir }) => join(outDir, 'js/index.js'),
   getContent: ({ tokens }) =>
-    Object.keys(tokens).reduce((acc, key) => `${acc}module.exports.${key} = ${JSON.stringify(tokens[key])}\n`, '')
+    Object.keys(tokens).reduce((acc, key) => `${acc}module.exports.${key} = ${JSON.stringify(tokens[key])}\n`, ''),
+  getSingleOutputPath: ({ outDir, tokenName }) => join(outDir, `js/${tokenName}.js`),
+  getSingleContent: ({ tokenValue }) => `module.exports = ${JSON.stringify(tokenValue)}\n`
 };

And in the generating function:

diff --git a/packages/patternfly-4/react-tokens/src/generateTokens.js b/packages/patternfly-4/react-tokens/src/generateTokens.js
index 83d4d3942..1177e8118 100644
--- a/packages/patternfly-4/react-tokens/src/generateTokens.js
+++ b/packages/patternfly-4/react-tokens/src/generateTokens.js
@@ -51,4 +51,7 @@ cssFiles.forEach(filePath => {
 readdirSync(templateDir).forEach(templateFile => {
   const template = require(join(templateDir, templateFile));
   outputFileSync(template.getOutputPath({ outDir }), template.getContent({ tokens }));
+  Object.entries(tokens).forEach(([tokenName, tokenValue]) => {
+    outputFileSync(template.getSingleOutputPath({ outDir, tokenName }), template.getSingleContent({ tokenName, tokenValue }));
+  })
 });

Could you consider something along these lines? Thanks.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
redallencommented, Feb 4, 2020

Looking good @Hyperkid123 ! I’ll review it sometime today.

1reaction
redallencommented, Feb 3, 2020

Yes, we can create multiple files instead of a singular index.js and we can also rewrite our React imports to use the CJS version like we did with react-icons.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Optimizing Performance - React
If you're benchmarking or experiencing performance problems in your React apps, make sure you're testing with the minified production build.
Read more >
Faster! Optimizing React app to the bone - Tolgee
How to optimize React app and make it faster. How to identify slow components and optimize infinite scrolling in your application.
Read more >
Two Quick Ways To Reduce React App's Size In Production
File size Development v/s Production. 2. Serve gzipped file in production. You can dramatically reduce the size by gzipping the bundle.js.
Read more >
Create React App & Passing Tokens
Create React App takes care of all the tooling and configurations that React apps require, so we can focus on building a cool...
Read more >
React Redux Performance Techniques and Optimizations
Learn React Redux performance techniques and optimizations as we look at our blog project to see when components are re-rendering and why.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found