Skip to content

Commit 79ffc8a

Browse files
nbbeekengribnoysup
andauthored
fix(components): always force speedy mode for emotion to avoid performance issues in dev mode COMPASS-9828 (#7305)
--------- Co-authored-by: Sergey Petushkov <[email protected]>
1 parent 7bed6e3 commit 79ffc8a

File tree

4 files changed

+66
-12
lines changed

4 files changed

+66
-12
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { sheet } from '@leafygreen-ui/emotion';
2+
/**
3+
* Emotion will dynamically decide which style insertion method to use based on
4+
* the "env" it is built for: in "development" mode it uses a method of
5+
* inserting literal style tags with css as text inside of them for every `css`
6+
* method call to apply styles to the page. This method is really slow, every
7+
* single style tag insertion causes style recalculation that can end up
8+
* blocking the main thread for multiple seconds, when accumulated this can
9+
* result in minutes of unresponsive page behavior. In "production" mode the
10+
* style insertion is done using a modern JS API that doesn't result in such
11+
* drastic performance issues.
12+
*
13+
* Specifically when embedding compass-web in mms, there is a massive
14+
* performance hit that can be observed when emotion is not running in "speedy"
15+
* mode, so to work around that we are always forcing emotion to enable it.
16+
*
17+
* Historically "speedy" mode was only active in production because editing
18+
* styles in the browser devtools didn't work otherwise, nowadays there is no
19+
* reason to not use it always, so there should be no downsides to doing this.
20+
*
21+
* See also https://github.com/10gen/compass-data-explorer/pull/11 where we
22+
* already ran into a similar issue.
23+
*/
24+
sheet.speedy(true);

packages/compass-components/src/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1-
export * from './components/leafygreen';
1+
// IMPORTANT: this import should always be the first one in compass-component
2+
// main entrypoint to ensure that emotion is reconfigured before any component
3+
// modules generate their stylesheets
4+
import './force-emotion-speedy';
5+
// -------------------------------
26

7+
export * from './components/leafygreen';
38
export {
49
default as emotion,
510
flush,

packages/compass-web/scripts/sync-dist-to-mms.js

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const fs = require('fs');
33
const path = require('path');
44
const child_process = require('child_process');
55
const os = require('os');
6+
const util = require('util');
67
const { debounce } = require('lodash');
78

89
if (!process.env.MMS_HOME) {
@@ -58,11 +59,42 @@ const webpackWatchProcess = child_process.spawn('npm', ['run', 'watch'], {
5859
stdio: 'inherit',
5960
});
6061

62+
const failProofRunner = () =>
63+
new (class FailProofRunner extends Array {
64+
append(...fns) {
65+
this.push(...fns);
66+
return this;
67+
}
68+
69+
run() {
70+
const errors = this.map((f) => {
71+
try {
72+
f();
73+
} catch (e) {
74+
return e;
75+
}
76+
}).filter((e) => e);
77+
78+
if (errors.length) {
79+
fs.writeSync(
80+
process.stdout.fd,
81+
util.inspect(errors, { depth: 20 }) + '\n'
82+
);
83+
}
84+
85+
return errors.length;
86+
}
87+
})();
88+
6189
function cleanup(signalName) {
62-
distWatcher.close();
63-
webpackWatchProcess.kill(signalName);
64-
fs.cpSync(tmpDir, destDir, { recursive: true });
65-
fs.rmSync(tmpDir, { recursive: true, force: true });
90+
const errorCount = failProofRunner()
91+
.append(() => distWatcher.close())
92+
.append(() => webpackWatchProcess.kill(signalName))
93+
.append(() => fs.cpSync(tmpDir, destDir, { recursive: true }))
94+
.append(() => fs.rmSync(tmpDir, { recursive: true, force: true }))
95+
.run();
96+
fs.writeSync(process.stdout.fd, 'Exit compass-web sync...\n');
97+
process.exit(errorCount);
6698
}
6799

68100
for (const evt of ['SIGINT', 'SIGTERM']) {

packages/compass-web/webpack.config.js

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -294,13 +294,6 @@ module.exports = (env, args) => {
294294
tls: 'commonjs2 tls',
295295
},
296296
plugins: [
297-
// Always package dist with NODE_ENV set to production, otherwise @emotion
298-
// dev mode behavior completely hangs code in the browser when applying
299-
// dev build to locally running mms
300-
new webpack.DefinePlugin({
301-
'process.env.NODE_ENV': JSON.stringify('production'),
302-
}),
303-
304297
// Only applied when running webpack in --watch mode. In this mode we want
305298
// to constantly rebuild d.ts files when source changes, we also don't
306299
// want to fail and stop compilation if we failed to generate definitions

0 commit comments

Comments
 (0)