File size: 3,757 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
import fs from "fs";
import path from "path";
import { getItems } from "./utils";
/*
PLUGINS GENERATION
NOTE: pluginName: the name of the plugin in the src/lib folder
Generates the `/src/temp/{pluginName}-plugins.ts` file, which exports list of plugins
Generating the plugins is optional, and it can be maintained manually if preferred.
The default convention uses the plugin's filename (without the extension) as the first part of the component
name. For example, the file `/lib/page-props-factory/plugins/exampleName.ts` would map to plugin `exampleNamePlugin`.
This can be customized in writePlugins().
*/
enum ModuleType {
CJS,
ESM,
}
interface PluginDefinition {
listPath: string;
rootPath: string;
moduleType: ModuleType;
}
interface PluginFile {
path: string;
name: string;
}
const pluginDefinitions = [
{
listPath: "scripts/temp/config-plugins.ts",
rootPath: "scripts/config/plugins",
moduleType: ModuleType.ESM,
},
{
listPath: "src/temp/sitemap-fetcher-plugins.ts",
rootPath: "src/lib/sitemap-fetcher/plugins",
moduleType: ModuleType.ESM,
},
{
listPath: "src/temp/middleware-plugins.ts",
rootPath: "src/lib/middleware/plugins",
moduleType: ModuleType.ESM,
},
{
listPath: "src/temp/page-props-factory-plugins.ts",
rootPath: "src/lib/page-props-factory/plugins",
moduleType: ModuleType.ESM,
},
{
listPath: "src/temp/next-config-plugins.js",
rootPath: "src/lib/next-config/plugins",
moduleType: ModuleType.CJS,
},
{
listPath: "src/temp/extract-path-plugins.ts",
rootPath: "src/lib/extract-path/plugins",
moduleType: ModuleType.ESM,
},
{
listPath: "src/temp/site-resolver-plugins.ts",
rootPath: "src/lib/site-resolver/plugins",
moduleType: ModuleType.ESM,
},
];
function getPluginList(path: string, pluginName: string): PluginFile[] {
const plugins = getItems<PluginFile>({
path,
resolveItem: (path, name) => ({
path: `${path}/${name}`,
name: `${name.replace(/-./g, (x) => x[1].toUpperCase())}Plugin`,
}),
cb: (name) => console.debug(`Registering ${pluginName} plugin ${name}`),
});
return plugins;
}
/**
* Generates the plugins file and saves it to the filesystem.
* By convention, we expect to find plugins under src/lib/{pluginName}/plugins/** (subfolders are
* searched recursively). The filename, with extension and non-word characters
* stripped, is used to identify the plugin's JavaScript module definition (for adding
* new plugin to the factory).
* Modify this function to use a different convention.
*/
function writePlugins(
listPath: string,
rootPath: string,
moduleType: ModuleType,
) {
const segments = rootPath.split("/");
const pluginName = segments[segments.length - 2];
const plugins = getPluginList(rootPath, pluginName);
let fileContent = "";
fileContent = plugins
.map((plugin) => {
return moduleType === ModuleType.CJS
? `exports.${plugin.name} = require('${plugin.path.replace(
"src/",
"../",
)}');`
: `export { ${plugin.name} } from '${plugin.path}';`;
})
.join("\r\n")
.concat("\r\n");
if (!plugins.length) {
fileContent =
moduleType === ModuleType.CJS
? "module.exports = {};\r\n"
: "export {};\r\n";
}
const filePath = path.resolve(listPath);
console.log(`Writing ${pluginName} plugins to ${filePath}`);
fs.writeFileSync(filePath, fileContent, {
encoding: "utf8",
});
}
function run(definitions: PluginDefinition[]) {
definitions.forEach((definition) => {
writePlugins(
definition.listPath,
definition.rootPath,
definition.moduleType,
);
});
}
run(pluginDefinitions);
|