Skip to content

Commit df19150

Browse files
authored
Merge pull request pgaultier#1 from pgaultier/feature/init
Init files
2 parents fd72e96 + 05bc617 commit df19150

File tree

10 files changed

+907
-0
lines changed

10 files changed

+907
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.idea
2+
vendor
3+
tests/_output/*

LICENSE.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
Sweelix Webpack Manager is free software. It is released under the terms of the following BSD License.
2+
3+
Copyright 2010-2017 by Sweelix SAS
4+
All rights reserved.
5+
6+
Redistribution and use in source and binary forms, with or without
7+
modification, are permitted provided that the following conditions are met:
8+
9+
* Redistributions of source code must retain the above copyright notice, this
10+
list of conditions and the following disclaimer.
11+
* Redistributions in binary form must reproduce the above copyright notice,
12+
this list of conditions and the following disclaimer in the documentation
13+
and/or other materials provided with the distribution.
14+
* Neither the name of Sweelix SAS nor the names of its contributors may be used
15+
to endorse or promote products derived from this software without specific
16+
prior written permission.
17+
18+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
22+
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
25+
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

README.md

Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
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+
[![Latest Stable Version](https://poser.pugx.org/sweelix/yii2-webpack/v/stable)](https://packagist.org/packages/sweelix/yii2-webpack)
15+
[![Build Status](https://api.travis-ci.org/pgaultier/yii2-webpack.svg?branch=master)](https://travis-ci.org/pgaultier/yii2-webpack)
16+
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/pgaultier/yii2-webpack/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/pgaultier/yii2-webpack/?branch=master)
17+
[![Code Coverage](https://scrutinizer-ci.com/g/pgaultier/yii2-webpack/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/pgaultier/yii2-webpack/?branch=master)
18+
[![License](https://poser.pugx.org/sweelix/yii2-webpack/license)](https://packagist.org/packages/sweelix/yii2-webpack)
19+
20+
[![Latest Development Version](https://img.shields.io/badge/unstable-devel-yellowgreen.svg)](https://packagist.org/packages/sweelix/yii2-webpack)
21+
[![Build Status](https://travis-ci.org/pgaultier/yii2-webpack.svg?branch=devel)](https://travis-ci.org/pgaultier/yii2-webpack)
22+
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/pgaultier/yii2-webpack/badges/quality-score.png?b=devel)](https://scrutinizer-ci.com/g/pgaultier/yii2-webpack/?branch=devel)
23+
[![Code Coverage](https://scrutinizer-ci.com/g/pgaultier/yii2-webpack/badges/coverage.png?b=devel)](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.

composer.json

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"name": "sweelix/yii2-webpack",
3+
"description": "PHP 5.6+ Webpack integration for the Yii framework",
4+
"keywords": ["yii2", "webpack", "assets"],
5+
"type": "yii2-extension",
6+
"license": "BSD-3-Clause",
7+
"authors": [
8+
{
9+
"name": "Philippe Gaultier",
10+
"email": "[email protected]"
11+
}
12+
],
13+
"require": {
14+
"yiisoft/yii2": "~2.0",
15+
"php" : ">=5.6.0"
16+
},
17+
"require-dev" : {
18+
"php" : ">=5.6.0",
19+
"codeception/codeception": "~2.2"
20+
},
21+
"autoload": {
22+
"psr-4": { "sweelix\\webpack\\": "src" }
23+
},
24+
"extra": {
25+
"branch-alias": {
26+
"dev-devel": "1.0.x-dev"
27+
}
28+
}
29+
}

src/Module.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
/**
3+
* Module.php
4+
*
5+
* PHP version 5.6+
6+
*
7+
* @author Philippe Gaultier <[email protected]>
8+
* @copyright 2010-2017 Philippe Gaultier
9+
* @license http://www.sweelix.net/license license
10+
* @version XXX
11+
* @link http://www.sweelix.net
12+
* @package sweelix\webpack
13+
*/
14+
15+
namespace sweelix\webpack;
16+
17+
use yii\base\BootstrapInterface;
18+
use yii\base\Module as BaseModule;
19+
use yii\console\Application as ConsoleApplication;
20+
21+
22+
/**
23+
* Oauth2 server Module definition
24+
*
25+
* @author Philippe Gaultier <[email protected]>
26+
* @copyright 2010-2017 Philippe Gaultier
27+
* @license http://www.sweelix.net/license license
28+
* @version XXX
29+
* @link http://www.sweelix.net
30+
* @package sweelix\webpack
31+
* @since XXX
32+
*/
33+
class Module extends BaseModule implements BootstrapInterface
34+
{
35+
/**
36+
* @inheritdoc
37+
*/
38+
public function bootstrap($app)
39+
{
40+
if ($app instanceof ConsoleApplication) {
41+
$this->mapConsoleControllers($app);
42+
}
43+
}
44+
45+
/**
46+
* Update controllers map to add console commands
47+
* @param ConsoleApplication $app
48+
* @since XXX
49+
*/
50+
protected function mapConsoleControllers(ConsoleApplication $app)
51+
{
52+
$app->controllerMap['webpack'] = [
53+
'class' => 'sweelix\webpack\commands\WebpackController',
54+
];
55+
}
56+
}

0 commit comments

Comments
 (0)