Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[docs] generate code paths for webpack raw-loader efficiency #27301

Merged
merged 6 commits into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions docs/docs-beta/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"scripts": {
"docusaurus": "docusaurus",
"start": "docusaurus start -p 3050",
"build": "docusaurus build",
"build": "yarn generate-code-imports && docusaurus build",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we also need to generate code imports before running yarn start ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, I can add it there too! was a little worried about performance impact, but it would be a much better user experience.

"swizzle": "docusaurus swizzle",
"deploy": "docusaurus deploy",
"clear": "docusaurus clear",
Expand All @@ -16,7 +16,8 @@
"vale": "vale ./docs --ext=.md,.mdx",
"lint": "eslint . --ext=.tsx,.ts,.js,.md,.mdx --fix",
"lint-and-vale": "yarn run lint && yarn run vale",
"sync-api-docs": "/bin/sh scripts/vercel-sync-api-docs.sh"
"sync-api-docs": "/bin/sh scripts/vercel-sync-api-docs.sh",
"generate-code-imports": "node scripts/generate-code-imports.js"
},
"dependencies": {
"@docusaurus/core": "^3.7.0",
Expand Down
71 changes: 71 additions & 0 deletions docs/docs-beta/scripts/generate-code-imports.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
const fs = require('fs');
const path = require('path');

const DOCUMENTATION_DIRECTORY = 'docs';
const VALID_DOCUMENT_EXTENSIONS = ['.md', '.mdx'];
const CODE_EXAMPLE_PATH_REGEX = /<CodeExample\s+[^>]*path=["']([^"']+)["'][^>]*>/g;

/**
* Returns a list of file paths for a given `dir`.
*/
function getAllDocuments(dir) {
let results = [];
const list = fs.readdirSync(dir);

list.forEach((file) => {
file = path.join(dir, file);
const stat = fs.statSync(file);
if (stat && stat.isDirectory()) {
results = results.concat(getAllDocuments(file)); // Recurse into subdirectory
} else {
if (VALID_DOCUMENT_EXTENSIONS.indexOf(path.extname(file)) !== -1) {
results.push(file); // Add file to results
}
}
});

return results;
}

/**
* Extracts all `regex` group `1` matches found in a list of `files`.
*/
function getUniqueRegexMatches(files, regex) {
const matches = new Set();

files.forEach((file) => {
const content = fs.readFileSync(file, 'utf-8');
let foundMatches;
while ((foundMatches = regex.exec(content)) !== null) {
matches.add(foundMatches[1]); // Extract group 1
}
});

return Array.from(matches);
}

/**
* Helper function to create a `raw-loader` import from a `path`.
*/
function pathToImport(path) {
return `import('!!raw-loader!/../../examples/${path}')`;
}

const files = getAllDocuments('docs');

const uniqueMatches = getUniqueRegexMatches(files, CODE_EXAMPLE_PATH_REGEX);

const _module = `
/// THIS FILE IS AUTOMATICALLY GENERATED BY \`yarn generate-code-imports\` DO NOT MODIFY ///
//
export const CODE_EXAMPLE_PATH_MAPPINGS = \{
${uniqueMatches.map((path) => ` '${path}': () => ${pathToImport(path)},`).join('\n')}
\};

/// THIS FILE IS AUTOMATICALLY GENERATED BY \`yarn generate-code-imports\` DO NOT MODIFY ///
`;

fs.writeFile('src/code-examples-content.js', _module, (err) => {
if (err) throw err;
console.log(`Succesfully generated mappings for ${uniqueMatches.length} references...`);
});
Loading
Loading