Skip to content

Commit

Permalink
Support for loading CJS/MJS files (constants) when building snippets.
Browse files Browse the repository at this point in the history
  • Loading branch information
pomek committed Sep 23, 2024
1 parent 3ae8d64 commit 5c737a8
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions scripts/docs/snippetadapter.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ export default async function snippetAdapter( snippets, options, umbertoHelpers

const groupedSnippetsByLanguage = {};

const constantDefinitions = getConstantDefinitions( snippets );
const constantDefinitions = await getConstantDefinitions( snippets );

// Group snippets by language. There is no way to build different languages in a single Webpack process.
// Webpack must be called as many times as different languages are being used in snippets.
Expand Down Expand Up @@ -340,7 +340,7 @@ function filterAllowedSnippets( snippets, allowedSnippets ) {
* @param {Array.<Object>} snippets
* @returns {Object}
*/
function getConstantDefinitions( snippets ) {
async function getConstantDefinitions( snippets ) {
const knownPaths = new Set();
const constantDefinitions = {};
const constantOrigins = new Map();
Expand All @@ -355,11 +355,15 @@ function getConstantDefinitions( snippets ) {
while ( !knownPaths.has( directory ) ) {
knownPaths.add( directory );

const absolutePathToConstants = upath.join( directory, 'docs', 'constants.js' );
const importPathToConstants = upath.relative( __dirname, absolutePathToConstants );
const constantsFiles = globSync( 'constants.*js', {
absolute: true,
cwd: upath.join( directory, 'docs' )
} );

for ( const item of constantsFiles ) {
const importPathToConstants = upath.relative( __dirname, item );

if ( fs.existsSync( absolutePathToConstants ) ) {
const packageConstantDefinitions = require( './' + importPathToConstants );
const { default: packageConstantDefinitions } = await import( './' + importPathToConstants );

for ( const constantName in packageConstantDefinitions ) {
const constantValue = packageConstantDefinitions[ constantName ];
Expand Down

0 comments on commit 5c737a8

Please sign in to comment.