UNPKG

webpack-serve-certificate-creator

Version:
55 lines (54 loc) 1.85 kB
import { resolve } from 'node:path'; import { getCertificate } from './utils.js'; export const PLUGIN_NAME = 'webpack-serve-certificate-creator'; export class WebpackServeCertificateCreator { options; constructor(options = {}) { this.options = options; } get defaultOptions() { return { enabled: process.env.WEBPACK_SERVE === 'true', outDir: '.certificate-cache', commonName: 'localhost', options: { days: 30, keySize: 2048, }, }; } apply(compiler) { const infraLogger = compiler.getInfrastructureLogger(PLUGIN_NAME); const baseDir = compiler.options.context ?? './'; const options = { ...this.defaultOptions, ...this.options, options: { ...this.defaultOptions.options, ...this.options.options, }, }; if (!options.enabled) { infraLogger.debug(`Not enabled`); return; } const cacheDirectory = resolve(baseDir, options.outDir); const dataPath = resolve(cacheDirectory, `${options.commonName}.json`); const context = { cacheDirectory, dataPath, logger: infraLogger, }; const certificate = getCertificate(options, context); if (typeof compiler.options.devServer !== 'object' || !compiler.options.devServer) { compiler.options.devServer = {}; } compiler.options.devServer.server ??= {}; compiler.options.devServer.server.type = 'https'; compiler.options.devServer.server.options = { ...compiler.options.devServer.server.options, cert: certificate.cert, key: certificate.private, }; } }