@0xc/serverless-config-transformer 中文文档教程
serverless-config-transformer
用于在构建时转换无服务器配置的简单包。 这是 最初用于 gulpfile,但应该适用于其他情况。 原本的 想法是允许本地开发插件与用于 生产部署。
How to use
此文件是基于生产 gulpfile 的简化示例。
import { dest, series, src, task } from "gulp";
import ts from "gulp-typescript";
import merge from "merge2";
import {
Serverless,
ServerlessConfigTransformer,
} from "@0xc/serverless-config-transformer";
/* eslint-disable @typescript-eslint/no-var-requires */
const del = require("del");
const serverlessGulp = require("serverless-gulp");
const currentFolder = ".";
const backupFolder = ".backup";
const slsFile = "serverless.yml";
const slsFilePath = `${currentFolder}/${slsFile}`;
const backupSlsFilePath = `${backupFolder}/${slsFile}`;
const overwrite = { overwrite: true };
const serverlessConfigTransformer = new ServerlessConfigTransformer({
converter: config => {
config.plugins = ["serverless-webpack"];
config.provider.package.include = ["dist/**"];
config.custom = { webpack: config.custom!.webpack };
config.functions = Object.keys(config.functions).reduce(
(functions, key) => ({
...functions,
[key]: {
...config.functions[key],
handler: config.functions[key].handler.replace(/^src\//, "dist/"),
},
}),
{} as Serverless.Functions,
);
return config;
},
});
function backupConfig() {
return src(slsFilePath).pipe(dest(backupFolder));
}
function updateConfig() {
return src(slsFilePath, { read: true }).pipe(serverlessConfigTransformer);
}
function restoreConfig() {
return src(backupSlsFilePath).pipe(dest(currentFolder, overwrite));
}
const tsProject = ts.createProject("tsconfig.build.json");
const outDir = "dist";
function compile(includeSourceMaps = false) {
return () => {
const srcs = tsProject.src().pipe(tsProject());
if (includeSourceMaps) {
return merge(srcs.js.pipe(dest(outDir)), srcs.dts.pipe(dest(outDir)));
}
return srcs.js.pipe(dest(outDir));
};
}
function clean() {
return del([outDir]);
}
task("clean", clean);
task("build", series("clean", compile(false)));
task("config:backup", backupConfig);
task("config:update", updateConfig);
task("config:restore", restoreConfig);
task("predeploy", series("config:backup", "config:update", "build"));
task("postdeploy", series("config:restore"));
这个包只处理无服务器配置的转换,这是一个 文字文件转换。 您应该备份并恢复原始配置 你自己。 或者至少,不要提交所做的更改。
serverless-config-transformer
A simple package for transforming the Serverless config at build time. This was used for a gulpfile initially, but should be applicable otherwise. The original idea was to allow local development plugins to be different from those used for the production deployment.
How to use
This file is a reduced example based on a production gulpfile.
import { dest, series, src, task } from "gulp";
import ts from "gulp-typescript";
import merge from "merge2";
import {
Serverless,
ServerlessConfigTransformer,
} from "@0xc/serverless-config-transformer";
/* eslint-disable @typescript-eslint/no-var-requires */
const del = require("del");
const serverlessGulp = require("serverless-gulp");
const currentFolder = ".";
const backupFolder = ".backup";
const slsFile = "serverless.yml";
const slsFilePath = `${currentFolder}/${slsFile}`;
const backupSlsFilePath = `${backupFolder}/${slsFile}`;
const overwrite = { overwrite: true };
const serverlessConfigTransformer = new ServerlessConfigTransformer({
converter: config => {
config.plugins = ["serverless-webpack"];
config.provider.package.include = ["dist/**"];
config.custom = { webpack: config.custom!.webpack };
config.functions = Object.keys(config.functions).reduce(
(functions, key) => ({
...functions,
[key]: {
...config.functions[key],
handler: config.functions[key].handler.replace(/^src\//, "dist/"),
},
}),
{} as Serverless.Functions,
);
return config;
},
});
function backupConfig() {
return src(slsFilePath).pipe(dest(backupFolder));
}
function updateConfig() {
return src(slsFilePath, { read: true }).pipe(serverlessConfigTransformer);
}
function restoreConfig() {
return src(backupSlsFilePath).pipe(dest(currentFolder, overwrite));
}
const tsProject = ts.createProject("tsconfig.build.json");
const outDir = "dist";
function compile(includeSourceMaps = false) {
return () => {
const srcs = tsProject.src().pipe(tsProject());
if (includeSourceMaps) {
return merge(srcs.js.pipe(dest(outDir)), srcs.dts.pipe(dest(outDir)));
}
return srcs.js.pipe(dest(outDir));
};
}
function clean() {
return del([outDir]);
}
task("clean", clean);
task("build", series("clean", compile(false)));
task("config:backup", backupConfig);
task("config:update", updateConfig);
task("config:restore", restoreConfig);
task("predeploy", series("config:backup", "config:update", "build"));
task("postdeploy", series("config:restore"));
This package only handles transformation of the Serverless config, which is a literal file transformation. You should backup and restore the original config yourself. Or at the very least, don't commit the changes made.
更多
你可能也喜欢
- @0xproject/assert 中文文档教程
- @168-cli/utils 中文文档教程
- @1o1w1/react-scripts 中文文档教程
- @20minutes/node-google-dfp 中文文档教程
- @44north/cookies 中文文档教程
- @4geit/rct-link-project 中文文档教程
- @555platform/utility-belt.js 中文文档教程
- @7oussem/react-native-couchbaselite 中文文档教程
- @abramstyle/deploy-tools 中文文档教程
- @activepipe/react-chartjs-2 中文文档教程