From b791abc478faaa7d78b3eae229668d436d58471a Mon Sep 17 00:00:00 2001
From: Brian Donovan <1938+eventualbuddha@users.noreply.github.com>
Date: Thu, 23 Jun 2022 19:34:24 -0700
Subject: [PATCH] build(ps): switch from CRA to vite
Refs #1314
---
frontends/bas/package.json | 2 +-
frontends/bmd/package.json | 2 +-
frontends/bsd/package.json | 2 +-
frontends/election-manager/package.json | 2 +-
frontends/precinct-scanner/.eslintignore | 4 +
frontends/precinct-scanner/config/env.js | 106 +++
.../config/jest/cssTransform.js | 14 +
frontends/precinct-scanner/config/paths.js | 73 ++
frontends/precinct-scanner/index.html | 16 +
frontends/precinct-scanner/jest.config.js | 32 +
frontends/precinct-scanner/package.json | 38 +-
.../precinct-scanner/prodserver/setupProxy.js | 20 +-
frontends/precinct-scanner/scripts/test.js | 53 ++
frontends/precinct-scanner/src/index.tsx | 2 +-
frontends/precinct-scanner/src/polyfills.ts | 10 +
.../precinct-scanner/src/react-app-env.d.ts | 1 -
frontends/precinct-scanner/vite.config.ts | 84 ++
pnpm-lock.yaml | 835 ++++++++++++++----
18 files changed, 1066 insertions(+), 230 deletions(-)
create mode 100644 frontends/precinct-scanner/config/env.js
create mode 100644 frontends/precinct-scanner/config/jest/cssTransform.js
create mode 100644 frontends/precinct-scanner/config/paths.js
create mode 100644 frontends/precinct-scanner/index.html
create mode 100644 frontends/precinct-scanner/jest.config.js
create mode 100644 frontends/precinct-scanner/scripts/test.js
create mode 100644 frontends/precinct-scanner/src/polyfills.ts
delete mode 100644 frontends/precinct-scanner/src/react-app-env.d.ts
create mode 100644 frontends/precinct-scanner/vite.config.ts
diff --git a/frontends/bas/package.json b/frontends/bas/package.json
index 9a6f2a142ff..6364fd2aced 100644
--- a/frontends/bas/package.json
+++ b/frontends/bas/package.json
@@ -144,7 +144,7 @@
"ts-pnp": "1.2.0",
"typescript": "4.6.3",
"url-loader": "4.1.1",
- "vite": "^2.9.9"
+ "vite": "^2.9.12"
},
"vx": {
"isBundled": true,
diff --git a/frontends/bmd/package.json b/frontends/bmd/package.json
index 6e3b8d40c16..6307dad11b3 100644
--- a/frontends/bmd/package.json
+++ b/frontends/bmd/package.json
@@ -193,7 +193,7 @@
"stylelint-config-styled-components": "^0.1.1",
"stylelint-processor-styled-components": "^1.10.0",
"ts-jest": "26",
- "vite": "^2.9.9"
+ "vite": "^2.9.12"
},
"engines": {
"node": ">= 12"
diff --git a/frontends/bsd/package.json b/frontends/bsd/package.json
index 80dbcf6dfaa..ed783b8cfac 100644
--- a/frontends/bsd/package.json
+++ b/frontends/bsd/package.json
@@ -144,7 +144,7 @@
"stylelint-config-styled-components": "^0.1.1",
"stylelint-processor-styled-components": "^1.10.0",
"type-fest": "^0.18.0",
- "vite": "^2.9.9",
+ "vite": "^2.9.12",
"zip-stream": "^3.0.1"
},
"vx": {
diff --git a/frontends/election-manager/package.json b/frontends/election-manager/package.json
index 100e2840581..bf66374819d 100644
--- a/frontends/election-manager/package.json
+++ b/frontends/election-manager/package.json
@@ -205,7 +205,7 @@
"stylelint-config-prettier": "^8.0.1",
"stylelint-config-styled-components": "^0.1.1",
"stylelint-processor-styled-components": "^1.10.0",
- "vite": "^2.9.9"
+ "vite": "^2.9.12"
},
"vx": {
"isBundled": true,
diff --git a/frontends/precinct-scanner/.eslintignore b/frontends/precinct-scanner/.eslintignore
index 5e4306738b3..0217cd62443 100644
--- a/frontends/precinct-scanner/.eslintignore
+++ b/frontends/precinct-scanner/.eslintignore
@@ -5,4 +5,8 @@
/cypress/support
/prodserver
/src/**/*.js
+/config
+/scripts
*.d.ts
+*.config.js
+*.config.ts
diff --git a/frontends/precinct-scanner/config/env.js b/frontends/precinct-scanner/config/env.js
new file mode 100644
index 00000000000..3d1411bd0bd
--- /dev/null
+++ b/frontends/precinct-scanner/config/env.js
@@ -0,0 +1,106 @@
+'use strict';
+
+const fs = require('fs');
+const path = require('path');
+const paths = require('./paths');
+
+// Make sure that including paths.js after env.js will read .env variables.
+delete require.cache[require.resolve('./paths')];
+
+const NODE_ENV = process.env.NODE_ENV;
+if (!NODE_ENV) {
+ throw new Error(
+ 'The NODE_ENV environment variable is required but was not specified.'
+ );
+}
+
+// https://github.com/bkeepers/dotenv#what-other-env-files-can-i-use
+const dotenvFiles = [
+ `${paths.dotenv}.${NODE_ENV}.local`,
+ // Don't include `.env.local` for `test` environment
+ // since normally you expect tests to produce the same
+ // results for everyone
+ NODE_ENV !== 'test' && `${paths.dotenv}.local`,
+ `${paths.dotenv}.${NODE_ENV}`,
+ paths.dotenv,
+].filter(Boolean);
+
+// Load environment variables from .env* files. Suppress warnings using silent
+// if this file is missing. dotenv will never modify any environment variables
+// that have already been set. Variable expansion is supported in .env files.
+// https://github.com/motdotla/dotenv
+// https://github.com/motdotla/dotenv-expand
+dotenvFiles.forEach(dotenvFile => {
+ if (fs.existsSync(dotenvFile)) {
+ require('dotenv-expand')(
+ require('dotenv').config({
+ path: dotenvFile,
+ })
+ );
+ }
+});
+
+// We support resolving modules according to `NODE_PATH`.
+// This lets you use absolute paths in imports inside large monorepos:
+// https://github.com/facebook/create-react-app/issues/253.
+// It works similar to `NODE_PATH` in Node itself:
+// https://nodejs.org/api/modules.html#modules_loading_from_the_global_folders
+// Note that unlike in Node, only *relative* paths from `NODE_PATH` are honored.
+// Otherwise, we risk importing Node.js core modules into an app instead of webpack shims.
+// https://github.com/facebook/create-react-app/issues/1023#issuecomment-265344421
+// We also resolve them to make sure all tools using them work consistently.
+const appDirectory = fs.realpathSync(process.cwd());
+process.env.NODE_PATH = (process.env.NODE_PATH || '')
+ .split(path.delimiter)
+ .filter(folder => folder && !path.isAbsolute(folder))
+ .map(folder => path.resolve(appDirectory, folder))
+ .join(path.delimiter);
+
+// Grab NODE_ENV and REACT_APP_* environment variables and prepare them to be
+// injected into the application via DefinePlugin in webpack configuration.
+const REACT_APP = /^REACT_APP_/i;
+
+function getClientEnvironment(publicUrl) {
+ const raw = Object.keys(process.env)
+ .filter(key => REACT_APP.test(key))
+ .reduce(
+ (env, key) => {
+ env[key] = process.env[key];
+ return env;
+ },
+ {
+ // Useful for determining whether we’re running in production mode.
+ // Most importantly, it switches React into the correct mode.
+ NODE_ENV: process.env.NODE_ENV || 'development',
+ // Useful for resolving the correct path to static assets in `public`.
+ // For example,
.
+ // This should only be used as an escape hatch. Normally you would put
+ // images into the `src` and `import` them in code to get their paths.
+ PUBLIC_URL: publicUrl,
+ // We support configuring the sockjs pathname during development.
+ // These settings let a developer run multiple simultaneous projects.
+ // They are used as the connection `hostname`, `pathname` and `port`
+ // in webpackHotDevClient. They are used as the `sockHost`, `sockPath`
+ // and `sockPort` options in webpack-dev-server.
+ WDS_SOCKET_HOST: process.env.WDS_SOCKET_HOST,
+ WDS_SOCKET_PATH: process.env.WDS_SOCKET_PATH,
+ WDS_SOCKET_PORT: process.env.WDS_SOCKET_PORT,
+ // Whether or not react-refresh is enabled.
+ // react-refresh is not 100% stable at this time,
+ // which is why it's disabled by default.
+ // It is defined here so it is available in the webpackHotDevClient.
+ FAST_REFRESH: process.env.FAST_REFRESH !== 'false',
+ }
+ );
+ // Stringify all values so we can feed into webpack DefinePlugin
+ const stringified = {
+ 'process.env': Object.keys(raw).reduce((env, key) => {
+ env[key] = JSON.stringify(raw[key]);
+ return env;
+ }, {}),
+ };
+
+ return { raw, stringified };
+}
+
+module.exports = getClientEnvironment;
diff --git a/frontends/precinct-scanner/config/jest/cssTransform.js b/frontends/precinct-scanner/config/jest/cssTransform.js
new file mode 100644
index 00000000000..8f65114812a
--- /dev/null
+++ b/frontends/precinct-scanner/config/jest/cssTransform.js
@@ -0,0 +1,14 @@
+'use strict';
+
+// This is a custom Jest transformer turning style imports into empty objects.
+// http://facebook.github.io/jest/docs/en/webpack.html
+
+module.exports = {
+ process() {
+ return 'module.exports = {};';
+ },
+ getCacheKey() {
+ // The output is always the same.
+ return 'cssTransform';
+ },
+};
diff --git a/frontends/precinct-scanner/config/paths.js b/frontends/precinct-scanner/config/paths.js
new file mode 100644
index 00000000000..59c6d044b74
--- /dev/null
+++ b/frontends/precinct-scanner/config/paths.js
@@ -0,0 +1,73 @@
+'use strict';
+
+const path = require('path');
+const fs = require('fs');
+const getPublicUrlOrPath = require('react-dev-utils/getPublicUrlOrPath');
+
+// Make sure any symlinks in the project folder are resolved:
+// https://github.com/facebook/create-react-app/issues/637
+const appDirectory = fs.realpathSync(process.cwd());
+const resolveApp = relativePath => path.resolve(appDirectory, relativePath);
+
+// We use `PUBLIC_URL` environment variable or "homepage" field to infer
+// "public path" at which the app is served.
+// webpack needs to know it to put the right
+