├── .docs
├── README.md
└── debug_panel.png
├── .editorconfig
├── LICENSE.md
├── Makefile
├── README.md
├── composer.json
└── src
├── AssetLocator.php
├── AssetNameResolver
├── AssetNameResolverInterface.php
├── CannotResolveAssetNameException.php
├── DebuggerAwareAssetNameResolver.php
├── IdentityAssetNameResolver.php
├── ManifestAssetNameResolver.php
└── StaticAssetNameResolver.php
├── BasePath
├── BasePathProvider.php
└── NetteHttpBasePathProvider.php
├── BuildDirectoryProvider.php
├── DI
└── WebpackExtension.php
├── Debugging
├── WebpackPanel.php
└── templates
│ ├── WebpackPanel.panel.phtml
│ └── WebpackPanel.tab.phtml
├── DevServer
├── DevServer.php
└── Http
│ ├── Client.php
│ ├── CurlClient.php
│ └── MockClient.php
├── Latte
├── WebpackExtension.php
├── WebpackMacros.php
└── WebpackNode.php
├── Manifest
├── CannotLoadManifestException.php
├── ManifestLoader.php
├── ManifestMapper.php
└── Mapper
│ ├── AssetsWebpackPluginMapper.php
│ └── WebpackManifestPluginMapper.php
└── PublicPathProvider.php
/.docs/README.md:
--------------------------------------------------------------------------------
1 | # Webpack
2 |
3 | ## Contents
4 |
5 | - [Setup](#setup)
6 | - [Usage](#usage)
7 |
8 | ## Setup
9 |
10 | ```bash
11 | composer require contributte/webpack
12 | ```
13 |
14 | ## Usage
15 |
16 | Register the extension in your config file, and configure it. The two `build` options are mandatory:
17 |
18 | ```neon
19 | extensions:
20 | webpack: Contributte\Webpack\DI\WebpackExtension(%debugMode%, %consoleMode%)
21 |
22 | webpack:
23 | build:
24 | directory: %wwwDir%/dist
25 | publicPath: dist/
26 | ```
27 |
28 |
29 | Now you can use the `{webpack}` macro in your templates. It automatically expands the provided asset name to the full path as configured:
30 |
31 | ```latte
32 |
33 | ```
34 |
35 |
36 | ### webpack-dev-server integration
37 |
38 | You might want to use the Webpack's [dev server](https://www.npmjs.com/package/webpack-dev-server) to facilitate the development of client-side assets. But maybe once you're done with the client-side, you would like to build the back-end without having to start up the dev server.
39 |
40 | This package effectively solves this problem: it automatically serves assets from the dev server if available (i.e. it responds within a specified timeout), and falls back to the build directory otherwise. All you have to do is configure the dev server URL. The dev server is enabled automatically in debug mode; you can override this setting via `enabled` option:
41 |
42 | ```neon
43 | webpack:
44 | devServer:
45 | enabled: %debugMode% # default
46 | url: http://localhost:3000
47 | timeout: 0.1 # (seconds) default
48 | ```
49 |
50 | #### Ignored assets
51 |
52 | You can also configure a set of asset names that should be ignored (i.e. resolved to an empty data URI) if the dev-server is available. This can be helpful e.g. if you use [`style-loader`](https://www.npmjs.com/package/style-loader) in development which does not emit any CSS files.
53 |
54 | ```neon
55 | webpack:
56 | devServer:
57 | ignoredAssets:
58 | - main.css
59 | ```
60 |
61 | #### Public URL (e.g. Docker usage)
62 |
63 | Dev-server might have different URLs for different access points. For example, when running in Docker Compose setup, the Nette application accesses it via the internal Docker network, while you access it in the browser via the exposed port. For this, you can set up a different `publicUrl`.
64 |
65 | ```neon
66 | webpack:
67 | devServer:
68 | url: http://webpack-dev-server:3000 # URL over internal Docker network
69 | publicUrl: http://localhost:3030 # exposed port from the dev-server container
70 | ```
71 |
72 |
73 | ### Asset resolvers and manifest file
74 |
75 | You might want to include the Webpack's asset hash in its file name for assets caching (and automatic cache busting in new releases) in the user agent. But how do you reference the asset files in your code if their names are dynamic?
76 |
77 | This package comes to the rescue. You can employ the [`webpack-manifest-plugin`](https://www.npmjs.com/package/webpack-manifest-plugin) or some similar plugin (see below) to produce a manifest file, and then configure the adapter to use it:
78 |
79 | ```neon
80 | webpack:
81 | manifest:
82 | name: manifest.json
83 | ```
84 |
85 | This way, you can keep using the original asset names, and they get expanded automatically following the resolutions from the manifest file.
86 |
87 | This package automatically optimizes this in production environment by loading the manifest file in compile time.
88 |
89 |
90 | #### Manifest mappers
91 |
92 | By default, the manifest loader supports the aforementioned `webpack-manifest-plugin`. If you use a different plugin that produces the manifest in a different format, you can implement and configure a mapper for it. This package comes bundled with a mapper for the [`assets-webpack-plugin`](https://www.npmjs.com/package/assets-webpack-plugin):
93 |
94 | ```neon
95 | webpack:
96 | manifest:
97 | name: manifest.json
98 | mapper: Contributte\Webpack\Manifest\Mapper\AssetsWebpackPluginMapper
99 | ```
100 |
101 | You can also implement your own mapper, simply extend `Contributte\Webpack\Manifest\ManifestMapper` and implement its `map()` method. It takes the parsed JSON content of the manifest file and is expected to return a flat array mapping asset names to file names.
102 |
103 |
104 |
105 | #### Manifest loading timeout
106 |
107 | You can specify a timeout for manifest loading from webpack-dev-server. The timeout defaults to 1 second.
108 |
109 | ```neon
110 | webpack:
111 | manifest:
112 | name: manifest.json
113 | timeout: 0.5
114 | ```
115 |
116 |
117 | ### Debugger
118 |
119 | In development environment, this package registers its own debug bar panel into Tracy, giving you the overview of
120 |
121 | - what assets have been resolved and how;
122 | - the path from where the assets are served;
123 | - whether the dev server is enabled and available.
124 |
125 | 
126 |
--------------------------------------------------------------------------------
/.docs/debug_panel.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/contributte/webpack/3f4900b457853d8341cd6d352cb9ca86469236c6/.docs/debug_panel.png
--------------------------------------------------------------------------------
/.editorconfig:
--------------------------------------------------------------------------------
1 | # EditorConfig is awesome: http://EditorConfig.org
2 |
3 | root = true
4 |
5 | [*]
6 | charset = utf-8
7 | end_of_line = lf
8 | insert_final_newline = true
9 | trim_trailing_whitespace = true
10 | indent_style = tab
11 | indent_size = tab
12 | tab_width = 4
13 |
14 | [{*.json, *.yaml, *.yml, *.md}]
15 | indent_style = space
16 | indent_size = 2
17 |
--------------------------------------------------------------------------------
/LICENSE.md:
--------------------------------------------------------------------------------
1 | Copyright (c) 2017, Jiří Pudil
2 | All rights reserved.
3 |
4 | Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
5 |
6 | 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
7 |
8 | 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
9 |
10 | 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
11 |
12 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
13 |
--------------------------------------------------------------------------------
/Makefile:
--------------------------------------------------------------------------------
1 | .PHONY: install qa cs csf phpstan tests coverage-clover coverage-html
2 |
3 | install:
4 | composer update
5 |
6 | qa: phpstan cs
7 |
8 | cs:
9 | vendor/bin/ecs check src tests
10 |
11 | csf:
12 | vendor/bin/ecs check src tests --fix
13 |
14 | phpstan:
15 | vendor/bin/phpstan analyze -l 8 src
16 |
17 | tests:
18 | vendor/bin/tester -C tests
19 |
20 | coverage-clover:
21 | vendor/bin/tester -C --coverage coverage.xml --coverage-src src tests
22 |
23 | coverage-html:
24 | vendor/bin/tester -C --coverage coverage.hzml --coverage-src src tests
25 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | 
2 |
3 |