You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Let's say we have two css files (**styla-a.css** and **style-b.css**) and we want to bundle them into one file which name will be **my-bundle**. This bundle will be stored in a **webtemp dir** (must be accessible from a browser).
26
+
27
+
The recommended way to configure Web Loader is through neon configuration files. The first step is to create a bundle.neon.
28
+
````yaml
29
+
my-bundle:
30
+
cssFiles:
31
+
- path/to/style-a.css
32
+
- path/to/style-b.css
33
+
````
34
+
35
+
Next step is to init Web Loader, set the output dir path and tell him to create bundles from **bundle.neon**.
36
+
````PHP
37
+
$webloader = new \WebLoader\Engine('path/to/webtemp');
- Separated for **CSS** and **JS** so they can have the same name.
3
3
- They can be initialized for each file in files collection or once for whole collection after all files are loaded.
4
+
- Is possible to check whether the filter is already defined
4
5
6
+
**Filters definition**
5
7
````php
6
-
$webLoader
7
-
->addCssFilter('urlFilter', function (string $code, string $filePath) {
8
-
// filter url
9
-
return $code;
10
-
})
11
-
8
+
$webLoader ->addCssFilter('urlFilter', function (string $code, string $filePath) {
9
+
// filter url
10
+
return $code;
11
+
});
12
+
13
+
if ( ! $webLoader->getCompiler()->filterExists(Engine::JS, 'minifier') {
12
14
// If you want to run filter for each file of file collection separatelly, set third parameter to TRUE
13
-
->addJsFilter('minifier', function (string $code) {
15
+
$webloader->addJsFilter('minifier', function (string $code) {
14
16
// Minify
15
17
return $code;
16
18
}, TRUE);
19
+
}
20
+
````
21
+
22
+
**Usage (configuration in a NEON file)**
23
+
````YAML
24
+
myCollection:
25
+
cssFiles:
26
+
- somefile.css
27
+
cssFilters:
28
+
- urlFilter
29
+
30
+
jsFiles:
31
+
- somefile.js
32
+
jsFilters:
33
+
- minifier
34
+
````
35
+
36
+
**Usage (in pure PHP)**
37
+
````PHP
38
+
$webloader
39
+
->createCssFilesCollection('myCollection')
40
+
->setFiles(['somefile.css'])
41
+
->setFilters(['urlFilter']);
42
+
43
+
$webloader
44
+
->createJsFilesCollection('myCollection')
45
+
->setFiles(['somefile.js'])
46
+
->setFilters(['minifier']);
47
+
````
48
+
49
+
## Default filters
50
+
There are two filters that comes with webloader.
51
+
52
+
### Url filter
53
+
This filter modifies url in css files according to output directory for correct assets loading. Is recommended to run it for each file separatelly.
54
+
55
+
````PHP
56
+
$webloader->addCssFilter('urlFilter', function ($code, $filePath) use ($outputDir, $documentRoot) {
57
+
$filter = new CssUrlFilter($outputDir, $documentRoot);
58
+
return $filter->filter($code, $filePath);
59
+
}, TRUE);
60
+
````
61
+
62
+
### Breakpoints filter
63
+
This filter extracts css from output files and creates new files with defined prefixes and put the correct css inside. Is recommended to run it for the whole collection. You can add filter for each file that is generated in the Breakpoints filter.
64
+
65
+
````PHP
66
+
$webloader->addCssFilter('cssBreakpointsFilter', function ($code, $collectionPath) use ($cssMinifier) {
67
+
$breakpoints = [
68
+
'medium' => ['px' => [768, 1023]], // For breakpoints between 640px up to 1023px
69
+
'large' => ['*'] // For every other breakpoints
70
+
];
71
+
72
+
$filter = new CssBreakpointsFilter($breakpoints);
73
+
$filter->addOutputFilesFilter(function ($code) use ($cssMinifier) {
74
+
return $cssMinifier->run($code);
75
+
});
76
+
return $filter->filter($code, $collectionPath);
77
+
});
78
+
````
79
+
Rendering then looks like this:
80
+
````PHP
81
+
$webloaderFilesCollectionRender->css('myCss');
82
+
$webloaderFilesCollectionRender->setPrefix('medium')->css('myCss', ['media' => 'screen and (min-width: 768px)']);
83
+
$webloaderFilesCollectionRender->setPrefix('large')->css('myCss', ['media' => 'screen and (min-width: 1024px)']);
0 commit comments