UNPKG

@invisit/webpack-aws-lambda-auto-deploy-plugin

Version:

Uploads compiled assets to s3 after build

119 lines 4.63 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (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 (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __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 }); const Path = __importStar(require("path")); const lodash_1 = require("lodash"); const AWS = __importStar(require("aws-sdk")); const webpack_1 = __importDefault(require("webpack")); const helpers_1 = require("./helpers"); const prelude_ts_1 = require("@3fv/prelude-ts"); const log = helpers_1.getLogger(); const compileError = (compilation, err) => { compilation.errors.push(err instanceof Error ? err : new Error(err)); }; class AWSLambdaAutoDeployPlugin extends webpack_1.default.Plugin { constructor(config, awsConfig = config.aws?.config ?? {}, entryMap = prelude_ts_1.asOption(config.mappings) .map(it => (Array.isArray(it) ? it : [{ fn: it, entry: ["default"] }])) .get() .reduce((map, { fn, entry }) => ({ ...map, [entry]: { entry, fn: [...(map[entry]?.fn ?? []), ...fn] } }), {})) { super(); this.config = config; this.awsConfig = awsConfig; this.entryMap = entryMap; this.pkg = require(Path.join(helpers_1.RootPluginDir, "package.json")); this.name = this.pkg.name; this.clients = { s3: undefined, lambda: undefined }; /** * Deploy the compilation to the configured * entry <-> lambda mappings * * @param {webpack.compilation.Compilation} compilation * @param {EntryLambdaMapping} entryMapping * @returns {Promise<void>} */ this.deploy = async ([compilation, { entry, fn }]) => { log.info(`Deploying entry (${entry}) to functions ${fn.join(", ")}`, compilation.entrypoints, compilation.entries); }; /** * Process done compilation event * * @param {webpack.Stats | webpack.compilation.MultiStats} statsOrMultiStats * @returns {Promise<void>} */ this.onDone = async (statsOrMultiStats) => { const { entryMap } = this; const allStats = helpers_1.isMultiStats(statsOrMultiStats) ? statsOrMultiStats.stats : [statsOrMultiStats]; const pendingDeployments = lodash_1.uniq(allStats .map(({ compilation }) => [ compilation, prelude_ts_1.asOption(compilation.compiler?.name) .map(name => entryMap[name]) .getOrCall(() => Object.values(entryMap)[0]) ]) .filter(([, entry]) => Boolean(entry))); try { await Promise.all(pendingDeployments.map(this.deploy)); } catch (err) { log.error(`AutoDeploy failed`, err); } }; } /** * Entries that have configured functions * * @returns {string[]} */ get entryNames() { return Object.keys(this.entryMap); } get s3() { return prelude_ts_1.asOption(this.clients.s3).getOrCall(() => (this.clients.s3 = new AWS.S3(this.awsConfig ?? {}))); } get lambda() { return prelude_ts_1.asOption(this.clients.lambda).getOrCall(() => (this.clients.lambda = new AWS.Lambda(this.awsConfig ?? {}))); } apply(compiler) { compiler.hooks.done.tapPromise(this.name, this.onDone); } async handleErrors(error, compilation) { compileError(compilation, `AWSLambdaAutoDeployPlugin: ${error}`); throw error; } } exports.default = AWSLambdaAutoDeployPlugin; //# sourceMappingURL=AWSLambdaAutoDeployPlugin.js.map