@angular-buildx/webpack
Version:
A custom Angular CLI builder for extending Webpack configuration in Angular projects.
56 lines (55 loc) • 2.7 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.extractWebpackConfiguration = exports.getCustomWebpackConfiguration = void 0;
const util_1 = __importDefault(require("util"));
const path_1 = __importDefault(require("path"));
const process_1 = require("process");
const fs_1 = __importDefault(require("fs"));
const getCustomWebpackConfiguration = (options, context) => {
if (options.webpack) {
const webpackPath = path_1.default.resolve(context.workspaceRoot, options.webpack);
if (fs_1.default.existsSync(webpackPath)) {
try {
context.logger.info(`\nLoaded custom Webpack configuration from: ${webpackPath}`);
return require(webpackPath);
}
catch (error) {
context.logger.error(`\nError loading custom Webpack config at: ${webpackPath}`);
throw error;
}
}
else {
context.logger.error(`\nCustom Webpack config file does not exist at: ${webpackPath}`);
throw Error(`\nCustom Webpack config file does not exist at: ${webpackPath}`);
}
}
};
exports.getCustomWebpackConfiguration = getCustomWebpackConfiguration;
const extractWebpackConfiguration = (config, context, options, webpackCallback) => {
const snapshotDirectory = path_1.default.join((0, process_1.cwd)(), ".webpack");
const webpackConfigString = (newConfig) => util_1.default.inspect(newConfig, {
depth: Infinity,
compact: false,
});
const fileConfigOriginalPath = path_1.default.join(snapshotDirectory, "webpack.original.snapshot");
const fileConfigMergedPath = path_1.default.join(snapshotDirectory, "webpack.merged.snapshot");
if (!fs_1.default.existsSync(snapshotDirectory)) {
fs_1.default.mkdirSync(snapshotDirectory);
}
if (options.webpack) {
context.logger.info(`\nWrite original config snapshot`);
fs_1.default.writeFileSync(fileConfigOriginalPath, `module.exports = ${webpackConfigString(config)};`);
const finalConfig = webpackCallback(config, context);
context.logger.info(`\nWrite merged config snapshot callback`);
fs_1.default.writeFileSync(fileConfigMergedPath, `module.exports = ${webpackConfigString(finalConfig ? finalConfig : config)};`);
if (finalConfig) {
context.logger.warn(`\nYou already use your own config`);
return finalConfig;
}
}
return config;
};
exports.extractWebpackConfiguration = extractWebpackConfiguration;