@alleyinteractive/build-tool
Version:
An opinionated set of build configurations for wp-scripts
138 lines • 5.04 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.hasProjectFile = exports.hasArgInCLI = exports.getUserWebpackConfig = exports.getUserWebpackConfigFilePath = exports.getWebpackConfig = exports.getDefaultArgs = exports.getArgsFromCLI = exports.getArgFromCLI = exports.fromProjectRoot = void 0;
const node_fs_1 = require("node:fs");
const node_process_1 = require("node:process");
const path_1 = __importDefault(require("path"));
/**
* Get the absolute path to a file from the project root.
*
* @param fileName - The file name to get the absolute path to.
*
* @returns The absolute path to the file.
*/
const fromProjectRoot = (fileName) => path_1.default.join((0, node_process_1.cwd)(), fileName);
exports.fromProjectRoot = fromProjectRoot;
/**
* Check if a file exists in the project root.
*
* @param fileName - The file name to check for.
*/
const hasProjectFile = (fileName) => (0, node_fs_1.existsSync)(fromProjectRoot(fileName));
exports.hasProjectFile = hasProjectFile;
/**
* Get all arguments passed to the CLI.
*/
const getArgsFromCLI = () => {
const args = process.argv.slice(2);
return args;
};
exports.getArgsFromCLI = getArgsFromCLI;
/**
* Get the value of an argument passed to the CLI.
*
* @param arg - The argument to get the value of.
*/
const getArgFromCLI = (arg) => {
let argValue;
getArgsFromCLI().forEach((cliArg) => {
const [name, value] = cliArg.split('=');
if (name === arg) {
// return the value if it exists, otherwise return the name.
argValue = value || name;
}
});
return argValue;
};
exports.getArgFromCLI = getArgFromCLI;
/**
* Check if an argument is present in the CLI.
*
* @param arg - The argument to check for.
*/
const hasArgInCLI = (arg) => getArgFromCLI(arg) !== undefined;
exports.hasArgInCLI = hasArgInCLI;
/**
* Get the path to the user's webpack config file.
*/
const getUserWebpackConfigFilePath = () => {
if (hasProjectFile('webpack.config.js')) {
return fromProjectRoot('webpack.config.js');
}
if (hasArgInCLI('--config')) {
const args = getArgsFromCLI();
// Get the --config flag that is not the one from this package.
const userCLIConfigArg = args.filter((arg) => arg.startsWith('--config')
&& !arg.includes('build-tool/dist'));
if (typeof userCLIConfigArg[0] !== 'undefined') {
// Get the value of the --config flag.
const configPath = userCLIConfigArg[0].split('=')[1];
if (typeof configPath !== 'undefined') {
return path_1.default.join((0, node_process_1.cwd)(), configPath);
}
}
}
return undefined;
};
exports.getUserWebpackConfigFilePath = getUserWebpackConfigFilePath;
/**
* Get the path to the webpack config file.
*/
const getWebpackConfig = () => path_1.default.join(__dirname, '../config/extended.config.js');
exports.getWebpackConfig = getWebpackConfig;
/**
* Get the user's webpack configuration.
*/
const getUserWebpackConfig = () => {
const webpackConfigFilePath = getUserWebpackConfigFilePath();
if (typeof webpackConfigFilePath === 'undefined') {
return {};
}
try {
// eslint-disable-next-line global-require, import/no-dynamic-require
const webpackConfig = require(require.resolve(webpackConfigFilePath));
return webpackConfig;
}
catch (error) {
// eslint-disable-next-line no-console
console.error(`Failed to load webpack config from ${webpackConfigFilePath}:`, error);
return {};
}
};
exports.getUserWebpackConfig = getUserWebpackConfig;
/**
* Get the default arguments to pass to wp-scripts.
*
* @returns {string[]}
*/
const getDefaultArgs = () => {
/**
* The default arguments to pass to wp-scripts.
*/
const defaultArgs = [];
/**
* If the `build` or `start` command is used, add the necessary wp-scripts args.
*/
if (hasArgInCLI('build') || hasArgInCLI('start')) {
defaultArgs.push(`--config=${getWebpackConfig()}`);
// Include the --webpack-copy-php flag explicitly.
if (!hasArgInCLI('--webpack-copy-php')) {
defaultArgs.push('--webpack-copy-php');
}
/**
* The default directory where wp-scripts will detect block.json files.
* Explicitly set the webpack source directory to "blocks" unless specified.
*
* @see https://github.com/WordPress/gutenberg/tree/trunk/packages/scripts#automatic-blockjson-detection-and-the-source-code-directory
*/
const webpackSrcDir = hasArgInCLI('--webpack-src-dir')
? getArgFromCLI('--webpack-src-dir') : 'blocks';
defaultArgs.push(`--webpack-src-dir=${webpackSrcDir}`);
}
return defaultArgs;
};
exports.getDefaultArgs = getDefaultArgs;
//# sourceMappingURL=index.js.map