From 0678dad2d68ab4a0300e19a15493326fd105d3c3 Mon Sep 17 00:00:00 2001 From: Laurent Cozic Date: Wed, 4 Jan 2023 20:39:33 +0000 Subject: [PATCH] Tools: Speed up building mobile CodeMirror when nothing has changed --- .../CodeMirror/CodeMirror.bundle.js.md5 | 1 + packages/app-mobile/package.json | 1 + packages/app-mobile/tools/buildInjectedJs.ts | 18 +++++++++++++++++- yarn.lock | 1 + 4 files changed, 20 insertions(+), 1 deletion(-) create mode 100644 packages/app-mobile/components/NoteEditor/CodeMirror/CodeMirror.bundle.js.md5 diff --git a/packages/app-mobile/components/NoteEditor/CodeMirror/CodeMirror.bundle.js.md5 b/packages/app-mobile/components/NoteEditor/CodeMirror/CodeMirror.bundle.js.md5 new file mode 100644 index 0000000000..2c9eb93aa8 --- /dev/null +++ b/packages/app-mobile/components/NoteEditor/CodeMirror/CodeMirror.bundle.js.md5 @@ -0,0 +1 @@ +0e1c132c199587530715a478bdb29a74 \ No newline at end of file diff --git a/packages/app-mobile/package.json b/packages/app-mobile/package.json index 709fe10561..b2fdaf8634 100644 --- a/packages/app-mobile/package.json +++ b/packages/app-mobile/package.json @@ -101,6 +101,7 @@ "jest-environment-jsdom": "29.3.1", "jetifier": "2.0.0", "jsdom": "20.0.0", + "md5-file": "5.0.0", "metro-react-native-babel-preset": "0.67.0", "nodemon": "2.0.20", "ts-jest": "29.0.3", diff --git a/packages/app-mobile/tools/buildInjectedJs.ts b/packages/app-mobile/tools/buildInjectedJs.ts index 300303e45d..609a3033b4 100644 --- a/packages/app-mobile/tools/buildInjectedJs.ts +++ b/packages/app-mobile/tools/buildInjectedJs.ts @@ -3,8 +3,9 @@ // files: First here we convert the JS file to a plain string, and that string // is then loaded by eg. the Mermaid plugin, and finally injected in the WebView. -import { mkdirp, readFile, writeFile } from 'fs-extra'; +import { mkdirp, pathExists, readFile, writeFile } from 'fs-extra'; import { dirname, extname, basename } from 'path'; +const md5File = require('md5-file'); const execa = require('execa'); // We need this to be transpiled to `const webpack = require('webpack')`. @@ -77,19 +78,34 @@ class BundledFile { resolve: { extensions: ['.tsx', '.ts', '.js'], }, + cache: { + type: 'filesystem', + }, }; return config; } private async uglify() { + const md5Path = `${this.bundleOutputPath}.md5`; + const newMd5 = await md5File(this.bundleOutputPath); + const previousMd5 = await pathExists(md5Path) ? await readFile(md5Path, 'utf8') : ''; + + if (newMd5 === previousMd5 && await pathExists(this.bundleMinifiedPath)) { + console.info('Bundle has not changed - skipping minifying...'); + return; + } + console.info(`Minifying bundle: ${this.bundleName}...`); + await execa('yarn', [ 'run', 'uglifyjs', '--compress', '-o', this.bundleMinifiedPath, this.bundleOutputPath, ]); + + await writeFile(md5Path, newMd5, 'utf8'); } private handleErrors(err: Error | undefined | null, stats: webpack.Stats | undefined): boolean { diff --git a/yarn.lock b/yarn.lock index c3e18a4c7d..bc3b3bb5e0 100644 --- a/yarn.lock +++ b/yarn.lock @@ -4713,6 +4713,7 @@ __metadata: jsc-android: 241213.1.0 jsdom: 20.0.0 md5: 2.3.0 + md5-file: 5.0.0 metro-react-native-babel-preset: 0.67.0 nodemon: 2.0.20 prop-types: 15.8.1