Installation Add the package via Composer:
composer require dawen/config-to-js-bundle
No Symfony-specific AppKernel registration is needed in Laravel (this is a legacy Symfony bundle). Instead, use Laravel's service provider and command registration.
Service Provider
Register the package in config/app.php under providers:
Dawen\Bundle\ConfigToJsBundle\ConfigToJsBundle::class,
Publish Config
Publish the default config to config/config-to-js.php:
php artisan vendor:publish --provider="Dawen\Bundle\ConfigToJsBundle\ConfigToJsBundle" --tag="config"
Edit the config to define your output_path (e.g., public/js/config.js) and config array.
First Use Case Run the command to generate the JS file:
php artisan config:js:dump
This outputs a JavaScript module (e.g., export default { imageLocation: 'http://...', appVersion: '1.1.0' }) to your specified path.
Dynamic Configuration
Use Laravel’s config system to define values in config/config-to-js.php:
'config' => [
'imageLocation' => config('app.url') . '/images',
'appVersion' => config('app.version'),
],
This keeps your JS config in sync with Laravel’s environment-aware settings.
Environment-Specific Outputs
Override output_path per environment in .env:
CONFIG_TO_JS_OUTPUT_PATH=public/js/config.prod.js
Then reference it in config/config-to-js.php:
'output_path' => env('CONFIG_TO_JS_OUTPUT_PATH', 'public/js/config.js'),
Integration with Frontend Builds
define or require in your build config:
// vite.config.js
define({
__APP_CONFIG__: require('./public/js/config.js'),
});
mix.js:
mix.js('resources/js/app.js', 'public/js')
.copy('public/js/config.js');
Caching and Invalidation
config.[hash].js) using Laravel’s mix-version or a custom helper.app/Console/Kernel.php:
protected function schedule(Schedule $schedule) {
$schedule->command('config:js:dump')->hourly();
}
config() helper to merge dynamic values:
'config' => array_merge(
config('app.defaults'),
config('app.overrides')
),
config array in config/config-to-js.php:
'config' => [
'imageLocation' => ['required', 'url'],
'appVersion' => ['required', 'string'],
],
Use a validator (e.g., Illuminate\Support\Facades\Validator) before dumping.Symfony Legacy Assumptions
AppKernel.php steps; use the service provider instead.output_path is an absolute path (e.g., public/js/config.js resolves to storage/framework/views/public/js/config.js if not prefixed with public/).File Permissions
Laravel’s storage/ directory may block writes. Set permissions:
chmod -R 775 storage/
Or configure output_path to write to public/ (e.g., public/js/config.js).
Type Limitation
type: 'module' option only supports ES6 modules. For global variables, modify the bundle’s template or use a wrapper:
// public/js/config.js
window.AppConfig = { ... };
Command Not Found
If php artisan config:js:dump fails:
console.log in your frontend to verify values:
console.log(AppConfig); // or `import config from './config.js'`
config/config-to-js.php:
\Log::debug('Config-to-JS config:', [
'output_path' => config('config-to-js.output_path'),
'config' => config('config-to-js.config'),
]);
Custom Templates Override the JS template by publishing the bundle’s assets:
php artisan vendor:publish --provider="Dawen\Bundle\ConfigToJsBundle\ConfigToJsBundle" --tag="templates"
Edit resources/views/config-to-js/module.js.twig to modify the output format.
Pre/Post-Processing
Use Laravel’s events to hook into the dump process. Example:
// app/Providers/EventServiceProvider.php
protected $listen = [
'config-to-js.dump' => [
\App\Listeners\LogConfigDump::class,
],
];
Dynamic Config Sources Fetch config from a database or API before dumping:
// In a service provider's boot method
$this->app->afterResolving('config-to-js', function ($bundle) {
$bundle->setConfig(array_merge(
config('config-to-js.config'),
\App\Models\Settings::first()->toArray()
));
});
uglify-js) to minify config.js:
// webpack.mix.js
mix.js('resources/js/app.js', 'public/js')
.js('public/js/config.js', 'public/js/config.min.js');
// Load config.js dynamically
import('./config.js').then(config => {
window.AppConfig = config.default;
});
How can I help you explore Laravel packages today?