Webpack adapter for Nette framework consisting of:
- DI extension
- entry point chunks resolver (uses webpack manifest.json)
- UI components to render assets <script>and<link>tags
- webpack config helper to manage your setup consistently with neonfiles
Install the DI extension via Composer.
composer require wavevision/nette-webpackThe webpack helper can be installed via Yarn
yarn add --dev @wavevision/nette-webpackor npm
npm install --save-dev @wavevision/nette-webpackRegister DI extension in your app config.
extensions:
    webpack: Wavevision\NetteWebpack\DI\Extension(%debugMode%, %consoleMode%)You can configure the extension as follows (default values).
webpack:
    debugger: %debugMode%
    devServer:
        enabled: %debugMode%
        url: http://localhost:9006
    dir: %wwwDir%/dist
    dist: dist
    entries: []
    manifest: manifest.json- debugger: booleanβ enable Tracy panel with useful development information
- devServer.enabled: booleanβ serve assets from- webpack-dev-server
- devServer.url: stringβ- webpack-dev-serverpublic URL
- dir: stringβ absolute path to webpack build directory
- dist: stringβ webpack build directory name
- entries: Record<string, boolean>β webpack entry points that should be considered when resolving assets
- manifest: stringβ webpack manifest name
Then, setup entry chunks.
use Nette\Application\UI\Presenter;
use Wavevision\NetteWebpack\InjectResolveEntryChunks;
use Wavevision\NetteWebpack\UI\Components\Assets\AssetsComponent;
final class AppPresenter extends Presenter
{
    use AssetsComponent;
    use InjectResolveEntryChunks;
    public function actionDefault(): void
    {
        $this
            ->getAssetsComponent()
            ->setChunks($this->resolveEntryChunks->process('entry'));
    }
}Note: Entry chunks are resolved based on webpack
manifest.json. You can also set chunks manually and/or separately withsetScriptsandsetStylesmethods.
Finally, render assets in your layout.
<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8">
		<meta name="viewport" content="width=device-width">
		<title>Wavevision Nette Webpack</title>
		{control assets:styles}
	</head>
	<body>
		{include content}
		{control assets:scripts}
	</body>
</html>Should you need it, you can inject and use following services to further customize your setup:
- NetteWebpackβ provides basic methods to work with the extension
- FormatAssetNameβ formats and resolves asset URL based on provided name
- FormatChunkNameβ formats chunk names for specific content types and resolves their URL
This simple utility will help you to manage your project setup and webpack configs consistently. It will also provide
you with pre-configured webpack-manifest-plugin to
generate manifest.json
with extra chunks property that is used to dynamically resolve entry chunks in your application.
import { WebpackHelper } from '@wavevision/nette-webpack';The helper constructor accepts following arguments:
- neonPath?: stringβ path to a- neonin which- webpackis configured (if not provided, default values will be used)
- wwwDir: stringβ mandatory path to application- wwwDir
- manifestOptions?: WebpackManifestPlugin.Optionsβ if you need to customize manifest plugin setup, you can do it here
The returned class exposes following methods:
- createManifestPlugin(): WebpackManifestPluginβ creates manifest plugin instance
- getDevServerPublicPath(): stringβ returns resolved- webpack-dev-serverURL with- distincluded in path
- getDevServerUrl(): UrlWithParsedQueryβ returns- webpack-dev-serverparsed URL object
- getDist(): stringβ returns- distparameter
- getEntries(): Record<string, boolean>β returns records of configured webpack entries
- getEnabledEntries(): string[]β returns list of webpack entries that have- trueconfigured
- getManifest(): stringβ returns webpack manifest file name
- getOutputPath(): stringβ returns resolved path to webpack output directory
- parseNeonConfig<T extends NeonConfig>(): Tβ returns parsed- neonconfig (throws error if- neonPathis not defined)
Note: You can also import
Neonhelper if you want to parse and work with moreneonfiles.
See example webpack config to see it all in action.
ManyοΈ π to JiΕΓ Pudil for his WebpackNetteAdapter which we used in our projects and served as an inspiration for this library.
