import fs from "fs" ; import { sync as globSync } from "glob" ; import { join } from "path" ; import { Compiler , Plugin } from "webpack" ; import VirtualModulesPlugin from "webpack-virtual-modules" ; const defaultModule = `const { load, getDefaultOptions } = require('@expo/auto-nav');

export default load({
    // for pages
    context: require.context('./', false, /^((?!_nav).)*\\.[jt]sx?$/, 'sync'),
    // for folders
    deep: require.context('./', true, /\\/*.\\/_nav\\.[jt]sx?$/, 'sync'),
    config: () => {
      const ctx = require.context('./', false, /_nav\.json$/, 'sync');
      return ctx.keys().length > 0 ? ctx(ctx.keys()[0]) : getDefaultOptions();
    },
})` ; /**
 * Convert any asset type to a JS code block that uses React Native's AssetRegistry module.
 */ export class VirtualNavigationPlugin implements Plugin { constructor ( private config ) { } apply ( compiler ) { const plugin = new VirtualModulesPlugin ( ) ; const screensPath = join ( this . config . projectRoot , "screens" ) ; compiler . options . plugins = ( compiler . options . plugins || [ ] ) . concat ( plugin ) ; plugin . apply ( compiler ) ; compiler . hooks . compilation . tap ( "MyPlugin" , ( compilation ) => { const getSubDirectoriesRecursive = ( path ) => { const dirs = globSync ( `${ path }/**/*` ) ; const subDirs = dirs . filter ( ( dir ) => fs . lstatSync ( dir ) . isDirectory ( ) ) . filter ( ( dir ) => ! fs . existsSync ( join ( dir , "_nav.js" ) ) && ! fs . existsSync ( join ( dir , "_nav.ts" ) ) && ! fs . existsSync ( join ( dir , "_nav.tsx" ) ) ) ; return subDirs ; } ; console . log ( "all:" , getSubDirectoriesRecursive ( screensPath ) ) ; const addScreen = ( at ) => { console . log ( "[virtual] adding:" , at ) ; plugin . writeModule ( join ( at , "_nav.js" ) , defaultModule ) ; } ; getSubDirectoriesRecursive ( screensPath ) . forEach ( addScreen ) ; } ) ; } }