From b1fd7f7796c4e47348e2f6f66fdfde3c58b60715 Mon Sep 17 00:00:00 2001 From: HowardBraham Date: Wed, 30 Nov 2022 07:19:45 -0800 Subject: [PATCH] Fixes 'yarn start' on Windows - LavaPack is not defined (#13318) (#16550) Co-authored-by: legobeat <109787230+legobeat@users.noreply.github.com> --- development/build/scripts.js | 2 +- development/build/static.js | 25 +++++++++++++++---------- development/build/utils.js | 24 ++++++++++++++++++++++++ test/e2e/seeder/smart-contracts.js | 2 +- 4 files changed, 41 insertions(+), 12 deletions(-) diff --git a/development/build/scripts.js b/development/build/scripts.js index 1e2ec7957..a961dca92 100644 --- a/development/build/scripts.js +++ b/development/build/scripts.js @@ -27,10 +27,10 @@ const terser = require('terser'); const bifyModuleGroups = require('bify-module-groups'); +const phishingWarningManifest = require('@metamask/phishing-warning/package.json'); const { streamFlatMap } = require('../stream-flat-map'); const { BuildType } = require('../lib/build-type'); const { generateIconNames } = require('../generate-icon-names'); -const phishingWarningManifest = require('../../node_modules/@metamask/phishing-warning/package.json'); const { BUILD_TARGETS, ENVIRONMENT } = require('./constants'); const { getConfig, getProductionConfig } = require('./config'); const { diff --git a/development/build/static.js b/development/build/static.js index af1b55c93..305b152a5 100644 --- a/development/build/static.js +++ b/development/build/static.js @@ -8,6 +8,7 @@ const { BuildType } = require('../lib/build-type'); const { TASKS } = require('./constants'); const { createTask, composeSeries } = require('./task'); +const { getPathInsideNodeModules } = require('./utils'); const EMPTY_JS_FILE = './development/empty.js'; @@ -125,7 +126,7 @@ function getCopyTargets(shouldIncludeLockdown, shouldIncludeSnow) { dest: `images`, }, { - src: `./node_modules/@metamask/contract-metadata/images/`, + src: getPathInsideNodeModules('@metamask/contract-metadata', 'images/'), dest: `images/contract`, }, { @@ -137,11 +138,14 @@ function getCopyTargets(shouldIncludeLockdown, shouldIncludeSnow) { dest: `vendor`, }, { - src: `./node_modules/@fortawesome/fontawesome-free/webfonts/`, + src: getPathInsideNodeModules( + '@fortawesome/fontawesome-free', + 'webfonts/', + ), dest: `fonts/fontawesome`, }, { - src: `./node_modules/react-responsive-carousel/lib/styles`, + src: getPathInsideNodeModules('react-responsive-carousel', 'lib/styles/'), dest: 'react-gallery/', }, { @@ -154,7 +158,7 @@ function getCopyTargets(shouldIncludeLockdown, shouldIncludeSnow) { dest: `loading.html`, }, { - src: `./node_modules/globalthis/dist/browser.js`, + src: getPathInsideNodeModules('globalthis', 'dist/browser.js'), dest: `globalthis.js`, }, { @@ -169,7 +173,7 @@ function getCopyTargets(shouldIncludeLockdown, shouldIncludeSnow) { }, { src: shouldIncludeLockdown - ? `./node_modules/ses/dist/lockdown.umd.min.js` + ? getPathInsideNodeModules('ses', 'dist/lockdown.umd.min.js') : EMPTY_JS_FILE, dest: `lockdown-install.js`, }, @@ -190,13 +194,11 @@ function getCopyTargets(shouldIncludeLockdown, shouldIncludeSnow) { dest: `lockdown-more.js`, }, { - // eslint-disable-next-line node/no-extraneous-require - src: require.resolve('@lavamoat/lavapack/src/runtime-cjs.js'), + src: getPathInsideNodeModules('@lavamoat/lavapack', 'src/runtime-cjs.js'), dest: `runtime-cjs.js`, }, { - // eslint-disable-next-line node/no-extraneous-require - src: require.resolve('@lavamoat/lavapack/src/runtime.js'), + src: getPathInsideNodeModules('@lavamoat/lavapack', 'src/runtime.js'), dest: `runtime-lavamoat.js`, }, ]; @@ -210,7 +212,10 @@ function getCopyTargets(shouldIncludeLockdown, shouldIncludeSnow) { for (const tag of languageTags) { allCopyTargets.push({ - src: `./node_modules/@formatjs/intl-relativetimeformat/dist/locale-data/${tag}.json`, + src: getPathInsideNodeModules( + '@formatjs/intl-relativetimeformat', + `dist/locale-data/${tag}.json`, + ), dest: `intl/${tag}/relative-time-format-data.json`, }); } diff --git a/development/build/utils.js b/development/build/utils.js index f783c6d40..eea601a2b 100644 --- a/development/build/utils.js +++ b/development/build/utils.js @@ -1,3 +1,4 @@ +const path = require('path'); const semver = require('semver'); const { BuildType } = require('../lib/build-type'); const { BUILD_TARGETS, ENVIRONMENT } = require('./constants'); @@ -117,10 +118,33 @@ function logError(error) { console.error(error.stack || error); } +/** + * Get the path of a file or folder inside the node_modules folder + * + * require.resolve was causing errors on Windows, once the paths were fed into fast-glob + * (The backslashes had to be converted to forward-slashes) + * This helper function was written to fix the Windows problem, and also end reliance on writing paths that start with './node_modules/' + * + * @see {@link https://github.com/MetaMask/metamask-extension/pull/16550} + * @param {string} packageName - The name of the package, such as '@lavamoat/lavapack' + * @param {string} pathToFiles - The path of the file or folder inside the package, optionally starting with / + */ +function getPathInsideNodeModules(packageName, pathToFiles) { + let targetPath = path.dirname(require.resolve(`${packageName}/package.json`)); + + targetPath = path.join(targetPath, pathToFiles); + + // Force POSIX separators + targetPath = targetPath.split(path.sep).join(path.posix.sep); + + return targetPath; +} + module.exports = { getBrowserVersionMap, getEnvironment, isDevBuild, isTestBuild, logError, + getPathInsideNodeModules, }; diff --git a/test/e2e/seeder/smart-contracts.js b/test/e2e/seeder/smart-contracts.js index f7cc148c5..3c61cf062 100644 --- a/test/e2e/seeder/smart-contracts.js +++ b/test/e2e/seeder/smart-contracts.js @@ -7,7 +7,7 @@ const { collectiblesBytecode, failingContractAbi, failingContractBytecode, -} = require('../../../node_modules/@metamask/test-dapp/dist/constants.json'); +} = require('@metamask/test-dapp/dist/constants.json'); const hstFactory = { initialAmount: 100,