|
| 1 | +Yii2 Webpack integration |
| 2 | +======================== |
| 3 | + |
| 4 | +This extension allow the developper to use [Webpack 2](https://webpack.js.org/) as the asset manager. |
| 5 | + |
| 6 | +Webpack2 comes preconfigured with the following loaders |
| 7 | + * javascript |
| 8 | + * typescript |
| 9 | + * sass |
| 10 | + * less |
| 11 | + * css |
| 12 | + |
| 13 | + |
| 14 | +[](https://packagist.org/packages/sweelix/yii2-webpack) |
| 15 | +[](https://travis-ci.org/pgaultier/yii2-webpack) |
| 16 | +[](https://scrutinizer-ci.com/g/pgaultier/yii2-webpack/?branch=master) |
| 17 | +[](https://scrutinizer-ci.com/g/pgaultier/yii2-webpack/?branch=master) |
| 18 | +[](https://packagist.org/packages/sweelix/yii2-webpack) |
| 19 | + |
| 20 | +[](https://packagist.org/packages/sweelix/yii2-webpack) |
| 21 | +[](https://travis-ci.org/pgaultier/yii2-webpack) |
| 22 | +[](https://scrutinizer-ci.com/g/pgaultier/yii2-webpack/?branch=devel) |
| 23 | +[](https://scrutinizer-ci.com/g/pgaultier/yii2-webpack/?branch=devel) |
| 24 | + |
| 25 | +Installation |
| 26 | +------------ |
| 27 | + |
| 28 | +If you use Packagist for installing packages, then you can update your `composer.json like this : |
| 29 | + |
| 30 | +``` json |
| 31 | +{ |
| 32 | + "require": { |
| 33 | + "sweelix/yii2-webpack": "*" |
| 34 | + } |
| 35 | +} |
| 36 | +``` |
| 37 | + |
| 38 | +Howto use it |
| 39 | +------------ |
| 40 | + |
| 41 | +Add extension to your console configuration to enable CLI commands |
| 42 | + |
| 43 | +``` php |
| 44 | +return [ |
| 45 | + // add webpack to console bootstrap to attach console controllers |
| 46 | + 'bootstrap' => ['log', 'webpack'], |
| 47 | + //.... |
| 48 | + 'modules' => [ |
| 49 | + 'webpack' => [ |
| 50 | + 'class' => 'sweelix\webpack\Module', |
| 51 | + ], |
| 52 | + // ... |
| 53 | + ], |
| 54 | +]; |
| 55 | +``` |
| 56 | + |
| 57 | +### Generate everything from scratch (init webpack stuff) |
| 58 | + |
| 59 | +``` |
| 60 | +php protected/yii webpack |
| 61 | +``` |
| 62 | + |
| 63 | + * **Generating package.json** |
| 64 | + * **Relative path to composer.json ?** If you are in main directory, this is probably `composer.json` |
| 65 | + * **Application name ?** Application name which will be used in package.json |
| 66 | + * **Author ?** Your name |
| 67 | + * **Description ?** Description of package.json |
| 68 | + * **License ?** License of the application |
| 69 | + |
| 70 | + * **Generating webpack-yii2.json** |
| 71 | + * **Webpack assets path relative to package.json** This is where you will write your front app (`protected/assets/webpack` for example) |
| 72 | + * **Webpack assets source directory ?** Name of the directory (inside webpack assets relative path) where you will create sources `src` |
| 73 | + * **Webpack assets distribution directory ?** Name of the directory (inside webpack assets relative path) where bundles will be generated `dist` |
| 74 | + * **Webpack assets distribution scripts directory ?** Name of the directory (inside `dist`) where scripts will be stored (`js`) |
| 75 | + * **Webpack assets distribution styles directory ?** Name of the directory (inside `dist`) where styles will be stored (`css`) |
| 76 | + * **Webpack catalog filename ?** Name of catalog file which will allow the asset manager to find the bundles |
| 77 | + |
| 78 | + * **Generating webpack.config.js** |
| 79 | + |
| 80 | +if you need to regenerate one of the files, you can use the following CLI commands : |
| 81 | + |
| 82 | + * `php protected/yii webpack/generate-config` : regenerate `webpack-yii2.json` |
| 83 | + * `php protected/yii webpack/generate-package` : regenerate `package.json` |
| 84 | + * `php protected/yii webpack/generate-webpack` : regenerate `webpack.config.js` |
| 85 | + |
| 86 | +### Sample application structure |
| 87 | + |
| 88 | +If your application has the following directory structure : |
| 89 | + |
| 90 | + * *index.php* |
| 91 | + * *composer.json* |
| 92 | + * **protected** |
| 93 | + * *yii* (console script) |
| 94 | + * **assets** |
| 95 | + * *WebpackAsset.php* |
| 96 | + * **webpack** |
| 97 | + * **src** |
| 98 | + * *app.ts* |
| 99 | + * **css** |
| 100 | + * *app.css* |
| 101 | + * *...* |
| 102 | + |
| 103 | +#### Run webpack init to prepare application |
| 104 | + |
| 105 | +The typical answer when running `php protected/yii webpack` would be : |
| 106 | + |
| 107 | + * **Generating package.json** |
| 108 | + * **Relative path to composer.json ?** Leave default value |
| 109 | + * **Application name ?** Leave default value |
| 110 | + * **Author ?** Leave default value |
| 111 | + * **Description ?** Leave default value |
| 112 | + * **License ?** Leave default value |
| 113 | + |
| 114 | + * **Generating webpack-yii2.json** |
| 115 | + * **Webpack assets path relative to package.json** `protected/assets/webpack` |
| 116 | + * **Webpack assets source directory ?** Leave default value |
| 117 | + * **Webpack assets distribution directory ?** Leave default value |
| 118 | + * **Webpack assets distribution scripts directory ?** Leave default value |
| 119 | + * **Webpack assets distribution styles directory ?** Leave default value |
| 120 | + * **Webpack catalog filename ?** Leave default value |
| 121 | + |
| 122 | + * **Generating webpack.config.js** |
| 123 | + |
| 124 | +Application structure with generated files will be |
| 125 | + |
| 126 | + * *index.php* |
| 127 | + * *composer.json* |
| 128 | + * *package.json* |
| 129 | + * *webpack-yii2.json* |
| 130 | + * *webpack.config.js* |
| 131 | + * **protected** |
| 132 | + * *yii* (console script) |
| 133 | + * **assets** |
| 134 | + * *WebpackAsset.php* |
| 135 | + * **webpack** |
| 136 | + * *assets-catalog.json* -> generated by webpack |
| 137 | + * **dist** -> generated by webpack |
| 138 | + * **js** |
| 139 | + * *js bundles* |
| 140 | + * **css** |
| 141 | + * *css bundles* |
| 142 | + * **src** |
| 143 | + * *app.ts* |
| 144 | + * **css** |
| 145 | + * *app.css* |
| 146 | + * *...* |
| 147 | + |
| 148 | +#### Create Webpack aware asset |
| 149 | + |
| 150 | +```php |
| 151 | +namespace app\assets; |
| 152 | + |
| 153 | +use sweelix\webpack\WebpackAssetBundle; |
| 154 | + |
| 155 | +class WebpackAsset extends WebpackAssetBundle |
| 156 | +{ |
| 157 | + /** |
| 158 | + * @var string base webpack alias (do not add /src nor /dist, they are automagically handled) |
| 159 | + */ |
| 160 | + public $webpackPath = '@app/assets/webpack'; |
| 161 | + |
| 162 | + /** |
| 163 | + * @var array list of webpack bundles to publish (these are the entries from webpack) |
| 164 | + * the bundles (except for the manifest one which should be in first position) must be defined |
| 165 | + * in the webpack-yii2.json configuration file |
| 166 | + */ |
| 167 | + public $webpackBundles = [ |
| 168 | + 'manifest', |
| 169 | + 'app' |
| 170 | + ]; |
| 171 | + |
| 172 | +} |
| 173 | +``` |
| 174 | + |
| 175 | +#### Generate the assets |
| 176 | + |
| 177 | +For development run |
| 178 | + |
| 179 | +``` |
| 180 | +webpack |
| 181 | +``` |
| 182 | +or to enable automatic build on file change |
| 183 | +``` |
| 184 | +webpack --watch |
| 185 | +``` |
| 186 | + |
| 187 | + |
| 188 | +For production run |
| 189 | + |
| 190 | +``` |
| 191 | +webpack -p |
| 192 | +``` |
| 193 | + |
| 194 | +#### Add files to your repository |
| 195 | + |
| 196 | +When your assets are ready, you have to make sure following files are added to your repository : |
| 197 | + |
| 198 | + * `package.json` node package management |
| 199 | + * `webpack.config.js` needed to run webpack |
| 200 | + * `webpack-yii2.json` needed by webpack.config.js to define you app entry points and the target directories |
| 201 | + |
| 202 | + * `<appdir>/assets/webpack/assets-catalog.json` to let the webpack aware asset find the dist files |
| 203 | + * `<appdir>/assets/webpack/dist` to keep the assets (they are not dynamically generated when asset is registered) |
| 204 | + * `<appdir>/assets/webpack/src` because you want to keep your sources :-) |
| 205 | + |
| 206 | + |
| 207 | + |
| 208 | +Contributing |
| 209 | +------------ |
| 210 | + |
| 211 | +All code contributions - including those of people having commit access - |
| 212 | +must go through a pull request and approved by a core developer before being |
| 213 | +merged. This is to ensure proper review of all the code. |
| 214 | + |
| 215 | +Fork the project, create a [feature branch ](http://nvie.com/posts/a-successful-git-branching-model/), and send us a pull request. |
0 commit comments