From 3ac82cf76aff5b66bfecb74b65859709eb94c30d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20B=C3=B6hmer?= Date: Fri, 3 Nov 2023 23:01:23 +0100 Subject: [PATCH] The frontend dependencies can now detect their path automatically. Therefore it does not need to be configured in the webpack.config.js before compilation. This should help to make things like issue #426 easier to resolve. --- README.md | 20 +++---- .../WebpackAutoPathSubscriber.php | 57 +++++++++++++++++++ webpack.config.js | 15 ++--- 3 files changed, 71 insertions(+), 21 deletions(-) create mode 100644 src/EventSubscriber/WebpackAutoPathSubscriber.php diff --git a/README.md b/README.md index ad2197dc..7409ddd2 100644 --- a/README.md +++ b/README.md @@ -101,24 +101,20 @@ for a detailed guide how to install Part-DB.** In bigger instances with concurrent accesses, MySQL is more performant. This can not be changed easily later, so choose wisely. 4. Install composer dependencies and generate autoload files: `composer install -o --no-dev` -5. If you have put Part-DB into a subdirectory on your server (like `part-db/`), you have to edit the file - `webpack.config.js` and uncomment the lines (remove the `//` before the lines) `.setPublicPath('/part-db/build')` ( - line 43) and - `.setManifestKeyPrefix('build/')` (line 44). You have to replace `/part-db` with your own path on line 44. -6. Install client side dependencies and build it: `yarn install` and `yarn build` -7. _Optional_ (speeds up first load): Warmup cache: `php bin/console cache:warmup` -8. Upgrade database to new scheme (or create it, when it was empty): `php bin/console doctrine:migrations:migrate` and +5. Install client side dependencies and build it: `yarn install` and `yarn build` +6. _Optional_ (speeds up first load): Warmup cache: `php bin/console cache:warmup` +7. Upgrade database to new scheme (or create it, when it was empty): `php bin/console doctrine:migrations:migrate` and follow the instructions given. During the process the password for the admin is user is shown. Copy it. **Caution**: This steps tamper with your database and could potentially destroy it. So make sure to make a backup of your database. -9. You can configure Part-DB via `config/parameters.yaml`. You should check if settings match your expectations, after +8. You can configure Part-DB via `config/parameters.yaml`. You should check if settings match your expectations, after you installed/upgraded Part-DB. Check if `partdb.default_currency` matches your mainly used currency (this can not be changed after creating price information). Run `php bin/console cache:clear` when you changed something. -10. Access Part-DB in your browser (under the URL you put it) and login with user *admin*. Password is the one outputted - during DB setup. - If you can not remember the password, set a new one with `php bin/console app:set-password admin`. You can create - new users with the admin user and start using Part-DB. +9. Access Part-DB in your browser (under the URL you put it) and login with user *admin*. Password is the one outputted + during DB setup. + If you can not remember the password, set a new one with `php bin/console app:set-password admin`. You can create + new users with the admin user and start using Part-DB. When you want to upgrade to a newer version, then just copy the new files into the folder and repeat the steps 4. to 7. diff --git a/src/EventSubscriber/WebpackAutoPathSubscriber.php b/src/EventSubscriber/WebpackAutoPathSubscriber.php new file mode 100644 index 00000000..f040a646 --- /dev/null +++ b/src/EventSubscriber/WebpackAutoPathSubscriber.php @@ -0,0 +1,57 @@ +. + */ + +declare(strict_types=1); + + +namespace App\EventSubscriber; + +use Symfony\Component\EventDispatcher\EventSubscriberInterface; +use Symfony\WebpackEncoreBundle\Event\RenderAssetTagEvent; + +/** + * This class fixes the wrong pathes generated by webpack using the auto publicPath mode. + * Basically it replaces the wrong /auto/ part of the path with the correct /build/ in all encore entrypoints. + */ +class WebpackAutoPathSubscriber implements EventSubscriberInterface +{ + public static function getSubscribedEvents(): array + { + return [ + RenderAssetTagEvent::class => 'onRenderAssetTag' + ]; + } + + public function onRenderAssetTag(RenderAssetTagEvent $event): void + { + if ($event->isScriptTag()) { + $event->setAttribute('src', $this->resolveAuto($event->getUrl())); + } + if ($event->isLinkTag()) { + $event->setAttribute('href', $this->resolveAuto($event->getUrl())); + } + } + + private function resolveAuto(string $path): string + { + //Replace the first occurence of /auto/ with /build/ to get the correct path + return preg_replace('/\/auto\//', '/build/', $path, 1); + } +} \ No newline at end of file diff --git a/webpack.config.js b/webpack.config.js index 857ca728..1eab3652 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -36,16 +36,9 @@ if (!Encore.isRuntimeEnvironmentConfigured()) { Encore // directory where compiled assets will be stored .setOutputPath('public/build/') - // public path used by the web server to access the output path + // This value doesn't matter, as the public path is set to auto later down. This is just to prevent a warning .setPublicPath('/build') - // only needed for CDN's or subdirectory deploy - //.setManifestKeyPrefix('build/') - - /** - * If you are putting Part-DB into a sub directory you have to uncomment these lines and - * replace "part-db/" with your path to Part-DB - */ - //.setPublicPath('/part-db/build') + // only needed for CDN's or subdirectory deploy (this should not be needeed, as we use auto public path) //.setManifestKeyPrefix('build/') /* @@ -189,3 +182,7 @@ if (Encore.isDev()) { module.exports = Encore.getWebpackConfig(); + +//Enable webpack auto public path (this only works in combination with WebpackAutoPathSubscriber!!) +//We do it here to supress a warning caused by webpack Encore +module.exports.output.publicPath = 'auto'; \ No newline at end of file