Currently Available: Need a skilled Software Developer for your next project?
Categories
Laravel

Building Packages Across Laravel, PHP, and JavaScript

When you build reusable code for Laravel applications, you first have to decide which parts work with PHP alone, which parts depend on Laravel, and which JavaScript files other projects will install and import. A package is code you distribute so other projects can install and use it. Composer installs PHP packages, Laravel connects framework-specific features to an application, and npm-compatible tools distribute JavaScript packages.

You can store all of these pieces in one repository, though each tool still expects its own installation method, metadata, compatibility rules, and tests. Decide separately what each consumer will install. You can build a standalone PHP library if the core code works outside Laravel, add a Laravel integration layer when the code relies on framework features, and publish an npm package when other projects need to import your JavaScript.

Choose the package boundary first

Start by mapping out what a consumer will install and use. A PHP library holds framework-independent code, so Laravel applications can use it without forcing the library to depend on Laravel. A Laravel package adds framework features like service-container bindings, routes, views, commands, or migrations. A JavaScript package gives frontend projects code they can import. Static files that Laravel copies into an application’s public directory follow a different path.

These boundaries determine which dependencies each package pulls in and which PHP, Laravel, and JavaScript versions you have to support. If the core logic only needs PHP, keep Laravel out of its runtime requirements. Add a Laravel integration layer only when the code needs framework services or resources, and publish an npm package only when another project needs to import your JavaScript as a library.

You can keep your PHP and JavaScript packages in one repository, or you can split them into separate repositories. Your choice depends on how you want to maintain and release them, because the ecosystem documentation does not treat either layout as universally best. Whichever layout you pick, document which packages consumers should install and which versions each package supports.

Set up the PHP package with Composer

Composer installs PHP dependencies and generates the autoloader that maps namespaces to source files. In composer.json, you declare the package name, supported PHP versions, runtime dependencies, and autoload rules. PSR-4 is a PHP standard that maps namespaces to directories, which lets consumers load your classes without manually including every file. Put test-only namespace mappings under autoload-dev and development tools under development requirements so production users do not install them at runtime. Composer documents these fields in its package schema.

Test the package from a separate consumer project in addition to running tests inside the package directory. During local development, you can add a Composer path repository in a consumer project to point directly to your package directory on disk. Composer links that directory when the system allows it and copies it when linking is unavailable. You must declare the path repository in the consumer project’s root composer.json, because Composer does not load repository declarations recursively from dependencies. You can read the details in the path repository documentation.

When you manage a library in version control, publish version tags and let Composer infer the version from the repository. Composer advises library authors not to hard-code a version in composer.json. Testing from a separate consumer project confirms that Composer can install the package and load its classes the way your documentation describes.

Connect Laravel features through a service provider

A Laravel service provider connects your package to a host Laravel application by registering its framework features. Inside the provider’s register method, you bind services into Laravel’s container so application code can request them through Laravel’s dependency system. Inside boot, you load resources such as routes, views, translations, and migrations. Laravel’s package development documentation explains these hooks along with package discovery, which lets Laravel register your provider automatically from Composer metadata.

Include only the integration features your package uses. Publish configuration files when users should edit package settings, and publish public assets when Laravel should copy files into the host application’s public directory. If you merge package defaults with user configuration overrides, keep in mind that Laravel’s mergeConfigFrom merges only the first level of an array. When a user overrides part of a nested configuration array, you have to handle those nested defaults explicitly so they are not lost.

Unit tests for your PHP classes alone will not catch framework integration problems, so test the provider inside a Laravel-like application context. A consumer fixture lets you verify provider discovery, configuration loading, routes, and published files. Laravel’s package documentation also describes a package skeleton that includes development tooling and a workbench application. You can start from that skeleton to save setup work, then remove any features your package does not use.

Choose how JavaScript reaches consumers

Laravel asset publishing and npm distribution solve different problems. When you use asset publishing, Laravel copies files such as JavaScript, CSS, or images directly into the host application’s public directory, which works well for static files the application serves as-is. When you publish an npm package, you give another project metadata and JavaScript entry points it can install and import. Keep in mind that a standard Vite application build compiles the host application’s assets and does not produce a reusable npm library on its own.

To build a library that consumers can import, configure Vite’s library mode. You specify the entry points consumers will import, the output formats, and the package metadata. You also decide which dependencies to bundle and which to leave out. If you externalize a dependency, you omit it from the built files and require consumers to install it themselves. If you bundle a dependency, you include it in the library and take responsibility for shipping it. Make that call based on your consumers’ build setups and the compatibility behavior you plan to support.

Before you publish to npm, generate a package archive (a tarball) and inspect what is inside. Check that your entry points and built files are present, and make sure tests, secrets, and development-only files are left out. You can run npm pack to produce a tarball for review; npm documents both local package testing and package creation. Once the archive looks right, install it into a small frontend fixture and run the exact import statement from your documentation.

Test what consumers install

A package’s own test suite verifies your code, but it does not prove that consumers can install the published files or use the documented entry points. Test every boundary a user crosses. Cover PHP logic with unit tests, Laravel integration with feature tests, and JavaScript behavior with library tests. Add build checks for generated files, and use a consumer fixture to verify your installation and setup steps.

Make sure those fixtures match the environments you claim to support. A Laravel fixture should install the package through Composer and run through provider discovery and any documented publish commands. A JavaScript fixture should install the local package and import its public entry point. Run your package-content checks against the actual artifacts you plan to release instead of assuming that every file in your repository will end up in the published package.

Version PHP and JavaScript as separate release surfaces

Composer and npm each have their own manifest, dependency graph, and publishing workflow. You can release both packages under one coordinated version number, or you can version each package on its own schedule. Base that policy on whether your PHP and JavaScript APIs change together, and write it down so consumers know which releases work together.

Semantic Versioning uses major, minor, and patch numbers to communicate changes to a package’s public API, meaning the features and entry points you document for consumers. You increment the major version for breaking changes, the minor version for backward-compatible additions, and the patch version for backward-compatible fixes. Apply those rules to all documented behavior, including installation steps and public entry points, and write clear release notes that state which package changed and what consumers need to update.

For PHP, Laravel, Node, and browser version ranges, list only the versions you have actually tested. Tracking those commitments in a Laravel package support matrix helps keep things clear when frameworks and runtimes release on different schedules.

What I'm building

Delegate tasks. Get software.

Give Vroni a GitHub issue, bug report, spec, or rough idea. It reads the repo, plans the change, writes code, runs checks, and works toward a review-ready pull request.

Take a look at vroni.com

Email updates

Usually a new article and a few links I found interesting.

No spam. Unsubscribe with one click.

Leave a Reply

Your email address will not be published. Required fields are marked *