Skip to content

Commit

Permalink
Fixes 'yarn start' on Windows - LavaPack is not defined (#13318)
Browse files Browse the repository at this point in the history
Also adds a getPathInsideNodeModules helper function to end reliance on writing paths that start with './node_modules/'
  • Loading branch information
HowardBraham committed Nov 18, 2022
1 parent ad4db57 commit 3ed04f1
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 12 deletions.
2 changes: 1 addition & 1 deletion development/build/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
25 changes: 15 additions & 10 deletions development/build/static.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -115,7 +116,7 @@ function getCopyTargets(shouldIncludeLockdown) {
dest: `images`,
},
{
src: `./node_modules/@metamask/contract-metadata/images/`,
src: getPathInsideNodeModules('@metamask/contract-metadata', 'images/'),
dest: `images/contract`,
},
{
Expand All @@ -127,11 +128,14 @@ function getCopyTargets(shouldIncludeLockdown) {
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/',
},
{
Expand All @@ -144,12 +148,12 @@ function getCopyTargets(shouldIncludeLockdown) {
dest: `loading.html`,
},
{
src: `./node_modules/globalthis/dist/browser.js`,
src: getPathInsideNodeModules('globalthis', 'dist/browser.js'),
dest: `globalthis.js`,
},
{
src: shouldIncludeLockdown
? `./node_modules/ses/dist/lockdown.umd.min.js`
? getPathInsideNodeModules('ses', 'dist/lockdown.umd.min.js')
: EMPTY_JS_FILE,
dest: `lockdown-install.js`,
},
Expand All @@ -170,13 +174,11 @@ function getCopyTargets(shouldIncludeLockdown) {
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`,
},
];
Expand All @@ -190,7 +192,10 @@ function getCopyTargets(shouldIncludeLockdown) {

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`,
});
}
Expand Down
29 changes: 29 additions & 0 deletions development/build/utils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const path = require('path');
const semver = require('semver');
const { BuildType } = require('../lib/build-type');
const { BUILD_TARGETS, ENVIRONMENT } = require('./constants');
Expand Down Expand Up @@ -117,10 +118,38 @@ 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`));

if (pathToFiles.charAt(0) !== '/') {
targetPath += '/';
}

targetPath += pathToFiles;

if (process.platform === 'win32') {
targetPath = targetPath.replace(/\\/gu, '/');
}

return targetPath;
}

module.exports = {
getBrowserVersionMap,
getEnvironment,
isDevBuild,
isTestBuild,
logError,
getPathInsideNodeModules,
};
2 changes: 1 addition & 1 deletion test/e2e/seeder/smart-contracts.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 3ed04f1

Please sign in to comment.