Skip to content

Latest commit

 

History

History
523 lines (352 loc) · 30.7 KB

bud-documentation.md

File metadata and controls

523 lines (352 loc) · 30.7 KB

Introduction

This documentation provides an in-depth understanding of the Bud.js build configuration file, which is responsible for defining how the project's source code is transformed, optimized, and bundled into production-ready assets.

Bud.js Overview

Bud.js is a powerful build tool used to streamline and enhance the development workflow of JavaScript projects. It offers various features, including asset management, code splitting, file watching, optimization, and more. The configuration file presented here leverages the capabilities of Bud.js to tailor the build process to the project's requirements.

Custom String Function

The configuration file defines a custom string prototype function, toKebabCase(), which converts a string to kebab case. This function employs regular expressions to transform strings containing camel case or other capitalization styles into kebab case.

String.prototype.toKebabCase = function () {
  // Regular expression-based transformation to kebab case
};

The configuration includes a function, bladefiles(), responsible for processing Blade template files. Blade files are parsed to extract JavaScript and CSS entry points for different application modules.

The bladefiles() function extracts JavaScript and CSS entry points from Blade template files and organizes them into module-specific collections. Each module's entry points are stored in the moduleEnPoints object.

The app.entry() method is used to define application entry points for JavaScript and CSS files. Entry points represent distinct features or components of the application. Each entry point consists of an array of file paths.

The app.assets() method specifies which directories should be included in the asset compilation process. In this configuration, the images directory is included for processing.

The app.watch() method defines specific directories to watch for changes. When files within these directories are modified, the development server will trigger a page reload.

Several methods are employed to optimize the output of the build process. These include minimizing output files, disabling file hashing, enabling code splitting, and configuring runtime behavior.

The configuration sets up a development environment using the app.proxy() and app.serve() methods. It proxies requests to a specified origin and serves the project on a development server.

The app.setPublicPath() method defines the URI path where compiled assets will be publicly accessible. This is useful for specifying the location of assets within a WordPress theme.

The app.wpjson.settings() method configures the generation of a theme.json file for WordPress block editor styling. Various configuration options are provided for customizing colors, spacing, and typography.

The configuration is enabled using the app.enable() method. Once enabled, the Bud.js configuration settings take effect, and the build process begins according to the defined rules.

Blade File Processing

The bladefiles() function is a crucial part of the build configuration, responsible for extracting JavaScript and CSS entry points from Blade template files. Blade is a templating engine commonly used with Laravel, and this function helps integrate JavaScript and CSS dependencies defined within Blade files into the build process.

Function Overview

The bladefiles() function iterates through Blade template files located in the @src/views/blocks/ directory. For each file, it extracts the module's name, JavaScript entry points, and CSS entry points. These entry points are then organized into a structured object, moduleEnPoints, where each module's name corresponds to its associated entry points.

Process Steps

  • Glob Blade Files: The function starts by using the app.glob() method to retrieve an array of Blade template file paths from the @src/views/blocks/ directory.
  • Iterate Through Files: For each Blade file path, the function performs the following steps:
    • Extracts the module's name from the file name by converting it to kebab case using the custom toKebabCase() string prototype function.
    • Reads the content of the Blade file using the app.fs.read() method.
  • Extract Entry Points: The function uses regular expressions to extract JavaScript and CSS entry points from the Blade file's content. These entry points are then concatenated and split into arrays for further processing.
  • Organize Data: The extracted entry points are stored in the moduleEnPoints object using the module name as the key. Each module entry contains an array of JavaScript and CSS file names.

Example

Suppose you have a Blade template file named MyComponent.blade.php in the @src/views/blocks/ directory. Inside the file, you have the following script and style references:

<script[src="script[module.js]script"></script>
<link[rel="stylesheet"][href="style[module.css]style"]>

When processed by the bladefiles() function, the data for the MyComponent module in the moduleEnPoints object would look like this:

{
  my-component: {
    js: ['module.js'],
    css: ['module.css']
  }
}

Usage

The extracted module entry points are later used in the configuration to define application entry points. This allows for organized and efficient bundling of JavaScript and CSS resources based on the modules defined within the Blade files.

Conclusion

The bladefiles() function plays a crucial role in integrating JavaScript and CSS dependencies from Blade template files into the build process. It enables developers to define module-specific entry points, promoting modularity and efficient resource management within the project.

Developers can refer to this function's documentation to understand how Blade files are processed and how module entry points are generated for the subsequent build steps.

Module Entry Points

The concept of "Module Entry Points" refers to the process of extracting JavaScript and CSS dependencies associated with individual modules defined within Blade template files. These entry points serve as the foundation for creating efficient and organized bundles during the build process.

Understanding Modules

In the context of this build configuration, a "module" represents a distinct component, feature, or section of your application. Each module is encapsulated within a Blade template file located in the @src/views/blocks/ directory. These modules might include various JavaScript and CSS resources that are required for their functionality.

Extraction Process

  1. Glob Blade Files: The bladefiles() function utilizes the app.glob() method to scan the @src/views/blocks/ directory for Blade template files. It retrieves a list of paths to these files.

  2. Module Name Extraction: For each Blade file, the function extracts the module's name from the file name. This ensures that modules are uniquely identified. The extracted module name is then converted to kebab case using the custom toKebabCase() string prototype function.

  3. Reading Blade Content: The content of each Blade file is read using the app.fs.read() method. This allows the function to analyze the file's contents and extract JavaScript and CSS references.

  4. Entry Point Extraction: The function employs regular expressions to extract references to JavaScript and CSS resources from within the Blade file's content. These references are captured and organized into the moduleEnPoints object.

ModuleEnPoints Object

The moduleEnPoints object serves as a central repository for the extracted JavaScript and CSS entry points associated with each module. It is structured as follows:

{
  moduleA: {
    js: ['script1.js', 'script2.js'],
    css: ['style1.css', 'style2.css']
  },
  moduleB: {
    js: ['script3.js'],
    css: ['style3.css']
  },
  // ...
}

Example

Suppose you have a Blade file named HeroSection.blade.php containing the following references:

<script[src="script[hero.js]script"></script>
<link[rel="stylesheet"][href="style[hero.css]style"]>

The bladefiles() function would process this file and contribute to the moduleEnPoints object:

{
  hero-section: {
    js: ['hero.js'],
    css: ['hero.css']
  }
}

Benefits of Module Entry Points

The extraction of module-specific entry points offers several benefits:

  • Modular Bundling: Entry points allow you to bundle JavaScript and CSS resources based on specific modules, resulting in more efficient loading of assets on a per-module basis.

  • Reduced Duplication: By extracting dependencies at the module level, you can avoid duplicating references across multiple templates, which promotes maintainability and reduces the chance of errors.

  • Focused Optimization: Each module can have its own set of optimizations and transformations applied to its dependencies, tailoring the optimization process to specific requirements.

Usage

The extracted moduleEnPoints object is subsequently used within the configuration to define application entry points, guiding the Bud.js build process to bundle JavaScript and CSS resources efficiently based on the modular structure of the project.

Developers can refer to this documentation to understand how module entry points are generated and leveraged within the build configuration for optimal asset bundling.

Application Entry Points

In the context of the Bud.js build configuration, "Application Entry Points" refer to the strategic selection and organization of JavaScript and CSS files that form the foundation of your project's various features and functionalities. These entry points determine how the application's assets are bundled and loaded by the browser.

Defining Entry Points

The app.entry() method is used to define these entry points within the configuration. Each entry point is associated with an array of file paths, representing the JavaScript and CSS resources required for a specific aspect of the application.

For example:

app.entry({
  homepage: ['@scripts/home', '@styles/home'],
  about: ['@scripts/about', '@styles/about'],
  // ... Other entry points
});

In the example above, homepage and about are two distinct entry points, each containing arrays of JavaScript and CSS file paths. These paths correspond to the resources necessary for the respective pages.

Utilizing Module Entry Points

Module entry points extracted using the bladefiles() function contribute to these application entry points. The combinedModules object generated by combining and organizing module entry points can be seamlessly integrated into the app.entry() method.

For instance:

app.entry({
  homepage: ['@scripts/home', '@styles/home'],
  about: ['@scripts/about', '@styles/about'],
  ...combinedModules, // Incorporate module entry points
});

This approach maintains a modular structure, allowing you to bundle resources based on features or components while minimizing duplication and optimizing loading performance.

Benefits of Application Entry Points

  • Efficient Loading: Organizing entry points enables the browser to load only the required assets for a specific page or feature, resulting in quicker initial page loads and a more responsive user experience.

  • Separation of Concerns: By defining entry points for different application aspects, you promote a clean separation of concerns and modular development, making it easier to maintain, update, and extend the codebase.

  • Optimization Opportunities: Application entry points allow you to apply specific optimizations, such as code splitting and lazy loading, to fine-tune the loading behavior and performance of your application.

Usage

Application entry points guide the Bud.js build process by determining how the JavaScript and CSS resources are bundled and made available to the browser. Developers can refer to this documentation to understand how to define and leverage these entry points for efficient asset loading and modular development.

Asset Management

Asset management involves controlling how external files and resources, such as images, fonts, and other media, are processed and included in the build output. In the Bud.js build configuration, asset management is achieved using the app.assets() method.

Method Overview

The app.assets() method defines which directories or files should be included in the compilation process and subsequently copied to the output directory. This ensures that assets are properly processed and made available for use within the built application.

Usage

To manage assets using the app.assets() method, you specify an array of directories or files that should be included. For example:

app.assets(['images', 'fonts']);

In the example above, the images and fonts directories are designated as assets. Bud.js will process and copy the contents of these directories to the appropriate location in the build output.

Benefits of Asset Management

  • Optimized Assets: The asset management process allows you to optimize and preprocess assets as needed before they are included in the build output. For example, images can be optimized for web usage to reduce file size.

  • Structured Organization: By explicitly specifying which directories are considered assets, you ensure a clear and structured organization of your project's resources.

  • Efficient Loading: Asset management ensures that assets are included in the build output only if they are used in the application. This prevents unnecessary loading of unused resources and improves overall performance.

Usage Example

Suppose your project structure includes an images directory containing various image files. By utilizing asset management, you ensure that these images are processed and copied to the appropriate location in the build output, making them accessible to the application.

app.assets(['images']);

This configuration would instruct Bud.js to handle the assets within the images directory during the build process.

Integration with Module Entry Points

Asset management can be closely integrated with module entry points. If a module requires specific assets, those assets can be organized within the module's directory structure. By defining the module's entry points and using asset management, you ensure that the assets associated with each module are processed and included in the build output as needed.

Conclusion

Asset management is a crucial aspect of the Bud.js build configuration, ensuring that external resources are properly processed, optimized, and included in the build output. Developers can refer to this documentation to understand how to utilize the app.assets() method for efficient asset management within the project.

File Watching

File watching is a fundamental aspect of the Bud.js build configuration that enables automatic monitoring of specified directories and files for changes. When changes occur, the build process is triggered, ensuring that your application remains up to date during development.

Method Overview

The app.watch() method is used to specify which directories and files should be watched for changes. When any of the watched files are modified, added, or removed, the Bud.js development server triggers a rebuild of the project, updating the build output and reflecting the changes in the browser.

Usage

To set up file watching, you provide an array of patterns that define the files and directories to be monitored. For example:

app.watch(['resources/views', 'resources/styles/**/*', 'resources/scripts/**/*', 'app']);

In the example above, the resources/views, resources/styles, resources/scripts, and app directories, as well as their subdirectories, are watched for changes.

Benefits of File Watching

  • Real-Time Updates: File watching ensures that your application remains in sync with your changes as you edit source files. This real-time update process eliminates the need to manually trigger builds after each modification.

  • Enhanced Development Workflow: Developers can benefit from a faster and more efficient development workflow, as changes are immediately reflected in the browser without the need for manual refreshes.

  • Rapid Feedback: Rapid feedback during development allows you to catch errors and bugs early, facilitating a smoother development process.

Usage Example

Suppose you are working on the styles for your application and want to ensure that changes in the resources/styles directory are automatically reflected in the build output and the browser. By configuring file watching, you can achieve this with ease:

app.watch(['resources/styles/**/*']);

This configuration instructs Bud.js to monitor changes in the resources/styles directory and its subdirectories.

Integration with Live Reload

File watching is often used in conjunction with live reload functionality, where the Bud.js development server automatically refreshes the browser when changes are detected. This creates a seamless development experience, allowing you to see your changes immediately.

Conclusion

File watching is a critical feature of the Bud.js build configuration, enabling developers to experience real-time updates and rapid feedback during the development process. Developers can refer to this documentation to understand how to utilize the app.watch() method for efficient file watching within the project.

Optimization

Optimization is a crucial step in the Bud.js build configuration that focuses on improving the performance and efficiency of your application's output. Through various optimization techniques, you can reduce file sizes, improve loading times, and enhance overall user experience.

Method Overview

The Bud.js configuration provides several methods for optimization, including:

  • Minimization: The app.minimize() method reduces the size of JavaScript and CSS files by removing whitespace, comments, and unnecessary characters.

  • File Hashing: The app.hash() method appends a unique hash to the filenames of the build output. This technique improves caching and ensures that clients retrieve the latest version of assets.

  • Code Splitting: The app.splitChunks() method splits large bundles into smaller chunks that are loaded on demand. This reduces initial loading times and optimizes resource utilization.

  • Runtime Chunk: The app.runtime() method generates a separate runtime chunk. This can improve caching and allow for more efficient updates when code changes occur.

###Usage To apply these optimization techniques, you can simply include the corresponding methods in your Bud.js configuration. For example:

app.minimize()
   .hash()
   .splitChunks()
   .runtime();

By chaining these methods together, you ensure that the build output is optimized for better performance.

Benefits of Optimization

  • Faster Load Times: Minimizing file sizes, using hashed filenames, and employing code splitting lead to faster initial load times, contributing to a smoother user experience.

  • Efficient Caching: Hashed filenames and separated runtime chunks improve caching behavior, ensuring that clients receive the latest version of assets and reducing unnecessary re-downloads.

  • Resource Management: Code splitting and optimized chunk loading allow for efficient use of resources, as only the necessary parts of the application are loaded when required.

Usage Example

Suppose you want to ensure that your application's JavaScript and CSS files are minimized for production, and you want to use hashed filenames to improve caching. You can easily apply these optimizations as follows:

app.minimize()
   .hash();

This configuration ensures that the build output is minified and that hashed filenames are generated for improved caching.

Integration with Other Techniques

Optimization can be combined with other techniques, such as file watching and live reload, to create a seamless and efficient development environment. By applying these techniques together, you can enjoy both rapid development feedback and optimized production output.

Conclusion

Optimization is a crucial step in the Bud.js build configuration that aims to enhance the performance and efficiency of your application. Developers can refer to this documentation to understand how to utilize the various optimization methods provided by Bud.js for a more streamlined and user-friendly application.

Development Environment

The development environment is a fundamental aspect of the Bud.js build configuration, providing tools and settings that enhance the development experience. This environment ensures that developers can work efficiently, see changes in real-time, and test their code with ease.

Method Overview

In the Bud.js build configuration, the development environment is configured using the following methods:

  • Proxy Origin: The app.proxy() method allows you to define a proxy origin (e.g., a local server) to handle requests during development. This is especially useful for integrating with back-end APIs or content management systems.

  • Development Server: The app.serve() method starts a local development server, enabling you to view your application in a browser. It provides automatic page reloading and a real-time preview of changes.

Usage

To set up a development environment, you can use the app.proxy() and app.serve() methods in your Bud.js configuration. For example:

app.proxy('http://localhost:8000')
   .serve('http://0.0.0.0:3000');

In this example, the Bud.js development server is configured to proxy requests to a local server at http://localhost:8000 and serves the application at http://0.0.0.0:3000.

Benefits of the Development Environment

  • Real-Time Preview: The development server provides a real-time preview of your application, allowing you to see changes as you make them without manual refreshing.

  • Proxying APIs: Using the proxy feature, you can work with back-end APIs or services during development without worrying about CORS issues.

  • Isolated Testing: The development environment allows you to test your application in an isolated setting, making it easier to identify and fix issues before deploying to production.

Usage Example

Suppose your application interacts with a remote API that you want to test locally during development. By configuring a proxy origin, you can seamlessly proxy requests to the remote API through your local server:

app.proxy('http://api.example.com');

This configuration ensures that API requests are proxied through your local development server, allowing you to work with the API in your development environment.

Integration with Other Techniques

The development environment features can be combined with other techniques, such as file watching and optimization, to create a holistic and efficient development workflow. This integration empowers developers to build and test applications with speed and confidence.

Conclusion

The development environment in the Bud.js build configuration plays a pivotal role in providing a smooth and productive development experience. Developers can refer to this documentation to understand how to configure the development environment using the app.proxy() and app.serve() methods for a seamless and efficient development workflow.

Public Path Configuration

The public path configuration in the Bud.js build setup determines the base URL or URI from which your compiled assets, such as JavaScript and CSS files, will be served to the browser. This configuration is essential for ensuring that your assets are accessible and correctly linked in your application.

Method Overview

The app.setPublicPath() method is used to define the public path where your compiled assets will be served from. This method specifies the URI that should be used as the base for all asset URLs in your application.

Usage

To configure the public path, you use the app.setPublicPath() method and provide the desired URI. For example:

app.setPublicPath('/wp-content/themes/mauj-me/public/');

In this example, the public path is set to /wp-content/themes/mauj-me/public/, which is where the compiled assets will be accessible from.

Benefits of Public Path Configuration

  • Correct Asset Links: By configuring the public path, you ensure that asset URLs in your application are correctly formed and point to the right location. This prevents broken links and ensures assets are loaded properly.

  • Easier Asset Deployment: Setting a consistent public path simplifies asset deployment and management, especially when deploying your application to different environments or servers.

Usage Example

Suppose your application is part of a WordPress theme and you want to ensure that all asset URLs are correctly formed relative to the theme's public directory. You can configure the public path as follows:

app.setPublicPath('/wp-content/themes/your-theme-name/public/');

This configuration ensures that all asset URLs are relative to the specified public path.

Conclusion

The public path configuration in the Bud.js build setup is crucial for correctly serving and linking compiled assets in your application. Developers can refer to this documentation to understand how to use the app.setPublicPath() method to configure the public path and ensure proper asset handling in their projects.

WordPress Theme Configuration

Configuring your Bud.js build setup for a WordPress theme involves several steps to optimize your theme's integration with the WordPress ecosystem, including generating a theme.json file for the block editor (Gutenberg) and tailoring various design settings.

Method Overview

The app.wpjson.settings() method is used to define the configuration options that will be included in the theme.json file. This file provides settings for block styles, colors, typography, spacing, and other design-related aspects used by the block editor.

Usage

To configure the theme.json file, you use the app.wpjson.settings() method and provide an object representing your desired configuration. For example:

app.wpjson.settings({
  color: {
    // ... Color configuration
  },
  spacing: {
    // ... Spacing configuration
  },
  typography: {
    // ... Typography configuration
  },
});

In this example, configuration options for colors, spacing, and typography are defined within the theme.json file.

Benefits of WordPress Theme Configuration

  • Block Editor Integration: By configuring the theme.json file, you provide styling and design settings that can be used by the block editor in WordPress. This ensures a consistent and cohesive design experience for content creation.

  • Optimized Styling: Tailoring color schemes, typography, and spacing settings allows you to optimize the visual appearance of your WordPress theme to match your design requirements.

  • Simplified Theming: The theme.json configuration streamlines the process of theming for the block editor, making it easier to control the visual aspects of blocks and ensure brand consistency.

Usage Example

Suppose you are developing a WordPress theme and want to define color options for the block editor. You can use the app.wpjson.settings() method to configure color settings:

app.wpjson.settings({
  color: {
    custom: false,
    customDuotone: false,
    defaultDuotone: false,
    duotone: [],
  },
});

In this example, the color section of the theme.json file is configured to disable custom colors and duotone settings.

Integration with Tailwind CSS

If you are using Tailwind CSS with your WordPress theme, you can integrate Tailwind utilities and classes directly into your theme's design configuration by enabling the relevant methods, such as app.useTailwindColors(), app.useTailwindFontFamily(), and app.useTailwindFontSize().

Conclusion

Configuring your Bud.js build setup for a WordPress theme involves generating a theme.json file with specific design settings for the block editor. Developers can refer to this documentation to understand how to use the app.wpjson.settings() method to configure the theme.json file and optimize the styling and design aspects of their WordPress themes.

Enabling the Configuration

After setting up various aspects of your Bud.js build configuration, it's important to enable and finalize the configuration to ensure that your defined settings and optimizations take effect.

Method Overview

The app.enable() method is used to enable the entire Bud.js build configuration and initiate the build process. This method should be called at the end of your configuration script to put all the defined settings into action.

Usage

To enable the configuration, simply call the app.enable() method at the end of your configuration script. For example:

app.enable();

This step ensures that all the defined settings, such as entry points, asset management, optimization, and more, are activated and applied during the build process.

Benefits of Enabling the Configuration

  • Finalization: Enabling the configuration ensures that all your defined settings and optimizations are properly applied to your project during the build process.

  • Consistency: By enabling the configuration, you guarantee that your project's assets, styles, and scripts are generated according to the specified guidelines and best practices.

Usage Example

Suppose you have defined various settings for your project, including entry points, asset management, and optimization. To ensure that all these settings are put into action, you enable the configuration at the end of your configuration script:

app
  .entry({
    // ... Entry points
  })
  .assets(['images', 'fonts'])
  .minimize()
  .hash()
  .splitChunks()
  .runtime()
  .proxy('http://localhost:8000')
  .serve('http://0.0.0.0:3000')
  .setPublicPath('/wp-content/themes/your-theme-name/public/')
  .wpjson.settings({
    color: {
      // ... Color settings
    },
    spacing: {
      // ... Spacing settings
    },
    typography: {
      // ... Typography settings
    },
  })
  .enable();

In this example, the .enable() method is called at the end of the configuration script to activate all the defined settings and finalize the configuration.

Conclusion

Enabling the configuration is the final step in the Bud.js build setup. It ensures that all your defined settings, optimizations, and configurations are applied and put into action during the build process. Developers can refer to this documentation to understand how to use the app.enable() method to enable their Bud.js build configuration and generate their project's output with the defined settings.