Files
next.js/examples/cms-sitecore-xmcloud/scripts/generate-plugins.ts
Arian Tron 61f56f997c
Some checks failed
Test examples / Test Examples (20) (push) Has been cancelled
Test examples / Test Examples (22) (push) Has been cancelled
Lock Threads / action (push) Has been cancelled
Trigger Release / start (push) Has been cancelled
Stale issue handler / stale (push) Has been cancelled
Update Font Data / create-pull-request (push) Has been cancelled
build-and-deploy / deploy-target (push) Has been cancelled
build-and-deploy / build (push) Has been cancelled
build-and-deploy / stable - aarch64-unknown-linux-musl - node@16 (push) Has been cancelled
build-and-deploy / stable - x86_64-unknown-linux-musl - node@16 (push) Has been cancelled
build-and-deploy / stable - aarch64-unknown-linux-gnu - node@16 (push) Has been cancelled
build-and-deploy / stable - x86_64-unknown-linux-gnu - node@16 (push) Has been cancelled
build-and-deploy / stable - aarch64-pc-windows-msvc - node@16 (push) Has been cancelled
build-and-deploy / stable - x86_64-pc-windows-msvc - node@16 (push) Has been cancelled
build-and-deploy / stable - aarch64-apple-darwin - node@16 (push) Has been cancelled
build-and-deploy / stable - x86_64-apple-darwin - node@16 (push) Has been cancelled
build-and-deploy / build-wasm (nodejs) (push) Has been cancelled
build-and-deploy / build-wasm (web) (push) Has been cancelled
build-and-deploy / Deploy preview tarball (push) Has been cancelled
build-and-deploy / Potentially publish release (push) Has been cancelled
build-and-deploy / publish-turbopack-npm-packages (push) Has been cancelled
build-and-deploy / Deploy examples (push) Has been cancelled
build-and-deploy / thank you, build (push) Has been cancelled
build-and-deploy / Upload Turbopack Bytesize metrics to Datadog (push) Has been cancelled
Rspack Next.js development integration tests / Rspack integration tests (push) Has been cancelled
Rspack Next.js production integration tests / Rspack integration tests (push) Has been cancelled
Turbopack Next.js development integration tests / Next.js integration tests (push) Has been cancelled
Turbopack Next.js production integration tests / Next.js integration tests (push) Has been cancelled
Update Rspack test manifest / Update and upload Rspack development test manifest (push) Has been cancelled
Update Rspack test manifest / Update and upload Rspack production test manifest (push) Has been cancelled
Upload bundler test manifests to areweturboyet.com / Upload test results (push) Has been cancelled
Update React / create-pull-request (push) Has been cancelled
test-e2e-project-reset-cron / reset-test-project (push) Has been cancelled
Notify about the top 15 issues/PRs/feature requests (most reacted) in the last 90 days / run (push) Has been cancelled
first commit
2026-03-10 19:37:31 +03:30

139 lines
3.7 KiB
TypeScript

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);