Skip to content
This repository was archived by the owner on Nov 20, 2020. It is now read-only.

Commit 6bdc1de

Browse files
committed
Merge pull request #3 from haroldiedema/feature/twig-enhancement
Fixed readme & added BabelLoader
2 parents 308fbc6 + 54a9d63 commit 6bdc1de

File tree

4 files changed

+181
-2
lines changed

4 files changed

+181
-2
lines changed

README.md

+81-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
- [Bundle configuration](#bundle-configuration)
1111
- [Shared dependencies](#shared-dependencies)
1212
- [Asset output directories](#asset-output-directories)
13+
- [Ideal configuration](#ideal-configuration)
1314
- [Loaders](#loaders)
1415
- [CSS](#css)
1516
- [Less](#less)
@@ -234,7 +235,7 @@ webpack:
234235
webpack:
235236
output:
236237
path: '%kernel.root_dir%/../web'
237-
dump_dir: '%kernel.root_dir%/../web/bundles'
238+
dump_path: '%kernel.root_dir%/../web/bundles'
238239
public_path: '/'
239240
```
240241
@@ -244,6 +245,65 @@ that exists inside the DOCUMENT_ROOT directory.
244245
For example, if the `output.path` value is `%kernel.root_dir/../web/packed`, the value of `output.public_path` must be
245246
set to `/packed`.
246247
248+
### Ideal configuration
249+
250+
The following configuration requires the following modules to be present in your `node_modules` directory.
251+
252+
- webpack-extract-text-plugin
253+
- style-loader
254+
- css-loader
255+
- less-loader
256+
- url-loader
257+
- babel-loader
258+
259+
Because we're creating shared chunks of javascript files, you'll need to include '`/compiled/shared.js`' manually in
260+
your base template. The same might also be the case for your CSS files, depending on what you include and where you do
261+
it.
262+
263+
config.yml
264+
```yaml
265+
webpack:
266+
node:
267+
binary: '/path/to/node'
268+
node_modules_path: '%kernel.root_dir%/../node_modules'
269+
output:
270+
path: '%kernel.root_dir%/../web/compiled'
271+
dump_path: '%kernel.root_dir%/../web/bundles'
272+
public_path: '/compiled'
273+
common_id: 'shared'
274+
loaders:
275+
css:
276+
enabled: true
277+
all_chunks: true
278+
filename: '[name].css'
279+
less:
280+
enabled: true
281+
all_chunks: true
282+
filename: '[name].css'
283+
url:
284+
enabled: true
285+
babel:
286+
enabled: true
287+
```
288+
289+
base.html.twig
290+
```html
291+
<head>
292+
<script src="/compiled/shared.js"></script>
293+
</head>
294+
```
295+
296+
Somewhere in your twig templates
297+
```twig
298+
{% webpack js "@YourBundle/SomeModule.js" %}
299+
<script src="{{ asset }}"></script>
300+
{% endwebpack %}
301+
302+
{% webpack css "@YourBundle/SomeModule.js" %}
303+
<link rel="stylesheet" href="{{ asset }}">
304+
{% endwebpack %}
305+
```
306+
247307
## Loaders
248308
249309
Loaders allow you to `require` files other than javascript. This package comes with 3 default loaders.
@@ -258,6 +318,8 @@ Each loader has its own configuration under the `loaders` section.
258318
259319
Enables loading CSS files.
260320
321+
> You need the `css-loader` and `style-loader` node module for this to work.
322+
261323
```yaml
262324
webpack:
263325
loaders:
@@ -283,6 +345,8 @@ Enables loading less files.
283345
284346
This plugin shares the exact same configuration settings as the CSS loader.
285347
348+
> You need the `less-loader`, `css-loader` and `style-loader` node modules for this to work.
349+
286350
```webpack
287351
webpack:
288352
loaders:
@@ -300,10 +364,25 @@ This plugin only has an `enabled` setting. It is disabled by default.
300364
```yaml
301365
webpack:
302366
loaders:
303-
less:
367+
url:
304368
enabled: true
305369
```
306370
371+
### Babel
372+
373+
The Babel Loader transpiles ECMAScript 6 code to ECMAScript 5 code, allowing it to run in older browsers. The loader
374+
compiles `.jsx` files instead of `.js` files, because not all files need to be compiled. Once ES6 hits mainstream, all
375+
you would need to do is gradually rename your jsx files to js files and everything _should_ still work.
376+
377+
> You need the `babel-loader` node module for this to work.
378+
379+
```yaml
380+
webpack:
381+
loaders:
382+
babel:
383+
enabled: true
384+
```
385+
307386
## Plugins
308387
309388
The webpack-bundle package comes with an easy way to develop and use plugins. A plugin simply writes pieces of code

src/Bundle/Resources/config/loaders.yml

+4
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,7 @@ services:
1111
class: Hostnet\Component\Webpack\Configuration\Loader\LessLoader
1212
tags:
1313
- { name: hostnet_webpack.config_extension }
14+
hostnet_webpack.loader.babel:
15+
class: Hostnet\Component\Webpack\Configuration\Loader\BabelLoader
16+
tags:
17+
- { name: hostnet_webpack.config_extension }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
namespace Hostnet\Component\Webpack\Configuration\Loader;
3+
4+
use Hostnet\Component\Webpack\Configuration\CodeBlock;
5+
use Hostnet\Component\Webpack\Configuration\ConfigExtensionInterface;
6+
use Symfony\Component\Config\Definition\Builder\NodeBuilder;
7+
8+
final class BabelLoader implements LoaderInterface, ConfigExtensionInterface
9+
{
10+
/**
11+
* @var array
12+
*/
13+
private $config;
14+
15+
/**
16+
* @param array $config
17+
*/
18+
public function __construct(array $config = [])
19+
{
20+
$this->config = isset($config['loaders']['babel'])
21+
? $config['loaders']['babel']
22+
: ['enabled' => false];
23+
}
24+
25+
/** {@inheritdoc} */
26+
public static function applyConfiguration(NodeBuilder $node_builder)
27+
{
28+
$node_builder
29+
->arrayNode('babel')
30+
->canBeDisabled()
31+
->end();
32+
}
33+
34+
/** {@inheritdoc} */
35+
public function getCodeBlocks()
36+
{
37+
if (! $this->config['enabled']) {
38+
return [new CodeBlock()];
39+
}
40+
41+
return [(new CodeBlock())->set(
42+
CodeBlock::LOADER,
43+
'{ test: /\.jsx$/, loader: \'babel-loader?cacheDirectory\' }'
44+
)];
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
namespace Hostnet\Component\Webpack\Configuration\Loader;
3+
4+
use Hostnet\Component\Webpack\Configuration\CodeBlock;
5+
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
6+
7+
/**
8+
* @covers Hostnet\Component\Webpack\Configuration\Loader\UrlLoader
9+
* @author Harold Iedema <[email protected]>
10+
*/
11+
class BabelLoaderTest extends \PHPUnit_Framework_TestCase
12+
{
13+
public function testConfigTreeBuilder()
14+
{
15+
$tree = new TreeBuilder();
16+
$node = $tree->root('webpack')->children();
17+
18+
BabelLoader::applyConfiguration($node);
19+
$node->end();
20+
21+
$config = $tree->buildTree()->finalize([]);
22+
23+
$this->assertArrayHasKey('babel', $config);
24+
$this->assertArrayHasKey('enabled', $config['babel']);
25+
}
26+
27+
public function testGetCodeBlockDefault()
28+
{
29+
$config = new BabelLoader();
30+
$block = $config->getCodeBlocks()[0];
31+
32+
$this->assertFalse($block->has(CodeBlock::LOADER));
33+
}
34+
35+
public function testGetCodeBlockDisabled()
36+
{
37+
$config = new BabelLoader(['loaders' => ['babel' => ['enabled' => false]]]);
38+
$block = $config->getCodeBlocks()[0];
39+
40+
$this->assertFalse($block->has(CodeBlock::LOADER));
41+
}
42+
43+
public function testGetCodeBlock()
44+
{
45+
$config = new BabelLoader(['loaders' => ['babel' => ['enabled' => true]]]);
46+
$block = $config->getCodeBlocks()[0];
47+
48+
$this->assertTrue($block->has(CodeBlock::LOADER));
49+
}
50+
}

0 commit comments

Comments
 (0)