@atlassian/wrm-troubleshooting
Version:
A tool that can help you with troubleshooting the configuration of webpack and Atlassian P2 project.
180 lines • 8.29 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getWebpackCliInfo = getWebpackCliInfo;
exports.runWebpackBundleWithInjectedPlugin = runWebpackBundleWithInjectedPlugin;
exports.runWebpackBundle = runWebpackBundle;
const chalk_1 = __importDefault(require("chalk"));
const execa_1 = __importDefault(require("execa"));
const fs_1 = require("fs");
const path_1 = __importDefault(require("path"));
const semver = __importStar(require("semver"));
const paths_1 = require("../paths");
const types_1 = require("../steps/types");
const webpackCliV3_1 = require("./webpackCliV3");
const webpackCliV4_1 = require("./webpackCliV4");
const webpackError_1 = require("./webpackError");
var WebpackCliVersion;
(function (WebpackCliVersion) {
WebpackCliVersion[WebpackCliVersion["V3"] = 3] = "V3";
WebpackCliVersion[WebpackCliVersion["V4"] = 4] = "V4";
})(WebpackCliVersion || (WebpackCliVersion = {}));
async function getWebpackCliInfo(webpackConfigFile, commandTimeout) {
const absoluteDirname = path_1.default.resolve(path_1.default.dirname(webpackConfigFile));
const workingDirectory = await resolveCliWorkingDirectory(absoluteDirname);
if (!workingDirectory) {
return new Error(`We can't detect the location of the ${chalk_1.default.bold('webpack CLI')}. Please ensure that the project has installed the "${chalk_1.default.bold('webpack-cli')}" NPM dependency.`);
}
const cliVersion = await getWebpackCliVersion(workingDirectory, commandTimeout);
if (cliVersion instanceof Error) {
return cliVersion;
}
return {
version: cliVersion,
workingDirectory,
};
}
const versionsMap = new Map([
[3, WebpackCliVersion.V3],
[4, WebpackCliVersion.V4],
]);
/**
* Returns the version of the webpack-cli command
*/
async function getWebpackCliVersion(workingDirectory, commandTimeout) {
let cliVersion;
try {
const { stdout: output } = await (0, execa_1.default)('npx', ['webpack-cli', '--version'], {
cwd: workingDirectory,
timeout: commandTimeout,
});
// webpack-cli v3 outputs only a version but webpack-cli v4 outputs multiple versions for each of the components
cliVersion = semver.parse(output);
if (cliVersion === null && output.split('\n').length > 1) {
cliVersion = semver.parse(findCliVersionString(output));
}
}
catch (err) {
const error = err;
if (error.timedOut) {
return (0, types_1.getTimeoutError)('webpack', commandTimeout);
}
const errorMessage = error.stdout || error.message || error.shortMessage;
return new webpackError_1.WebpackError(`We can't detect the location of the ${chalk_1.default.bold('webpack CLI')}. Please ensure that the project has installed the "${chalk_1.default.bold('webpack-cli')}" NPM dependency.`, errorMessage);
}
if (cliVersion === null) {
return new Error(`The "webpack-cli" has invalid version "${cliVersion}". Please try reinstalling the "webpack-cli" NPM dependency.`);
}
const webpackCliMajorVersion = cliVersion.major;
if (!versionsMap.has(webpackCliMajorVersion)) {
const supportedVersions = [...versionsMap.keys()].join(', ');
return new Error(`The current version ${webpackCliMajorVersion} of "webpack-cli" package is not yet supported. Please try using the "webpack-cli" in one the supported versions instead: ${supportedVersions}.`);
}
// We checked if the map has the value
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
return versionsMap.get(webpackCliMajorVersion);
}
function findCliVersionString(input) {
const match = input.match(/^(?:webpack-cli):?\s+([0-9.a-z\-_]+)$/m);
return match && match[1];
}
/**
* Locates the directory where we can safely call the "npx webpack-cli" command withing the project dir
*/
async function resolveCliWorkingDirectory(dir) {
// Get a name of the root directory so we can break from the loop while we are traversing path tree
const rootDirectory = path_1.default.parse(dir).root;
let currentDir = dir;
let foundPath = null;
// Chek if we have a valid dir and we haven't reached the root directory yet
while (currentDir && currentDir !== rootDirectory) {
let webpackCliPath;
/**
* Check if we can resolve webpack-cli binary in the current directory.
*
* We need a custom resolution function since we can't simply call {@link require.resolve} with "webpack-cli" as param:
* ```js
* require.resolve('webpack-cli', { paths: [currentDir] });
* ```
* This wouldn't work with the project that is using Yarn and Workspaces that are hoisting the "webpack-cli" NPM module
* to the root directory of the monorepo project. If the webpack-cli bin file is located under node_modules in
* one of the non-project-root directories, most of the time it's the directory we are looking for.
*/
try {
webpackCliPath = path_1.default.join(currentDir, 'node_modules', '.bin', 'webpack-cli');
await fs_1.promises.access(webpackCliPath);
}
catch (e) {
currentDir = path_1.default.resolve(currentDir, '..');
continue;
}
// Check if the resolved webpack path is included in current dir
if (!(0, paths_1.isSubDirectory)(currentDir, path_1.default.dirname(webpackCliPath))) {
currentDir = path_1.default.resolve(currentDir, '..');
continue;
}
foundPath = currentDir;
break;
}
return foundPath || null;
}
function runWebpackBundleWithInjectedPlugin(webpackCliInfo, webpackConfigFile, commandTimeout) {
let command;
switch (webpackCliInfo.version) {
case WebpackCliVersion.V3:
command = (0, webpackCliV3_1.runWebpackBundleWithInjectedPluginCliV3)(webpackCliInfo, webpackConfigFile, commandTimeout);
break;
case WebpackCliVersion.V4:
command = (0, webpackCliV4_1.runWebpackBundleWithInjectedPluginCliV4)(webpackCliInfo, webpackConfigFile, commandTimeout);
break;
}
return command;
}
function runWebpackBundle(webpackCliInfo, webpackConfigFile, commandTimeout) {
let command;
switch (webpackCliInfo.version) {
case WebpackCliVersion.V3:
command = (0, webpackCliV3_1.runWebpackBundleCliV3)(webpackCliInfo, webpackConfigFile, commandTimeout);
break;
case WebpackCliVersion.V4:
command = (0, webpackCliV4_1.runWebpackBundleCliV4)(webpackCliInfo, webpackConfigFile, commandTimeout);
break;
}
return command;
}
//# sourceMappingURL=webpackCli.js.map