4466ba436b
## Description This PR ensures that the default prettier config is used for examples and templates. This config is compatible with `prettier@3` as well (upgrading prettier is bigger change that can be a future PR). ## Changes - Updated `.prettierrc.json` in root with `"trailingComma": "es5"` (will be needed upgrading to prettier@3) - Added `examples/.prettierrc.json` with default config (this will change every example) - Added `packages/create-next-app/templates/.prettierrc.json` with default config (this will change every template) ## Related - Fixes #54402 - Closes #54409
138 lines
3.7 KiB
TypeScript
138 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);
|