Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
michaljurecko committed Aug 14, 2017
0 parents commit 32aeb91
Show file tree
Hide file tree
Showing 225 changed files with 8,521 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/tests export-ignore
/examples export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
/.scrutinizer.yml export-ignore
/.travis.yml export-ignore
/README.md export-ignore
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/vendor/
/tests/tmp
/composer.lock
/coverage.xml
*.actual
*.expected
*.log
12 changes: 12 additions & 0 deletions .scrutinizer.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
checks:
php:
code_rating: true

filter:
excluded_paths:
- 'tests/*'
- 'examples/*'

tools:
external_code_coverage:
timeout: 1000
75 changes: 75 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
language: php
php:
- 5.6
- 7.0
- 7.1

matrix:
include:
- php: 5.6
env: composerArgs="--prefer-lowest --prefer-stable"
- php: 7.1
env: composerArgs="--prefer-lowest --prefer-stable"
- php: 7.1
env: coverage=on coding_standard=on

addons:
apt:
packages:
- shunit2

sudo: false

script:
# PHP Tests
- vendor/bin/tester tests -s -c tests/php-unix.ini $coverageArgs
# Nette Code Checker
- >
if [ "$coding_standard" == "on" ]; then
php temp/code-checker/src/code-checker.php --short-arrays -d src;
fi
# Examples - tests
- bash shunit2 tests/ExamplesTests/gulp_tests.sh
- bash shunit2 tests/ExamplesTests/grunt_tests.sh
- bash shunit2 tests/ExamplesTests/webpack_tests.sh
# Examples - npm-check-updates
- ncu --packageFileDir --packageFile "${TRAVIS_BUILD_DIR}/examples/gulp/package.json"
- ncu --packageFileDir --packageFile "${TRAVIS_BUILD_DIR}/examples/grunt/package.json"
- ncu --packageFileDir --packageFile "${TRAVIS_BUILD_DIR}/examples/webpack/package.json"

after_failure:
- for i in $(find tests -name \*.actual); do echo "--- $i"; cat $i; echo; echo; done
- for i in $(find examples -name \*.log); do echo "--- $i"; cat $i; echo; echo; done

before_script:
# Install composerArgs
- travis_retry composer update --no-interaction --prefer-dist $composerArgs
# Install CodeChecker
- >
if [ "$coding_standard" == "on" ]; then
travis_retry composer create-project nette/code-checker temp/code-checker ~2.5 --no-interaction;
fi
# Install jq 1.5
- mkdir -p "$HOME/bin"
- curl -o "$HOME/bin/jq" -L https://github.com/stedolan/jq/releases/download/jq-1.5/jq-linux64
- chmod +x "$HOME/bin/jq"
- export PATH=$PATH:$HOME/bin
# Install nodeJs
- nvm install node
- npm install -g npm@3
- npm config set loglevel error
- npm install -s -q -g gulp grunt-cli webpack npm-check-updates
# Code coverage arguments
- if [ "$coverage" == "on" ]; then coverageArgs="-p phpdbg --coverage ./coverage.xml --coverage-src ./src"; fi

after_script:
# Report Code Coverage
- >
if [ "$coverage" == "on" ]; then
wget https://scrutinizer-ci.com/ocular.phar;
php ocular.phar code-coverage:upload --format=php-clover coverage.xml;
fi
cache:
directories:
- $HOME/.composer/cache
22 changes: 22 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
MIT License

Copyright (c) 2017 Michal Jurečko

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

200 changes: 200 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
# Webrouse/n-asset-macro

[![Build Status](https://travis-ci.org/webrouse/n-asset-macro.svg?branch=master)](https://travis-ci.org/webrouse/n-asset-macro)
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/webrouse/n-asset-macro/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/webrouse/n-asset-macro/?branch=master)
[![Code Coverage](https://scrutinizer-ci.com/g/webrouse/n-asset-macro/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/webrouse/n-asset-macro/?branch=master)
[![Latest stable](https://img.shields.io/packagist/v/webrouse/n-asset-macro.svg)](https://packagist.org/packages/webrouse/n-asset-macro)

Asset macro for [Latte](https://latte.nette.org) and [Nette Framework](https://nette.org).

Useful for assets [cache busting](https://www.keycdn.com/support/what-is-cache-busting)
with [gulp](https://github.com/webrouse/n-asset-macro/tree/master/examples/gulp "Gulp example"), [webpack](https://github.com/webrouse/n-asset-macro/tree/master/examples/webpack "Webpack example"), [grunt](https://github.com/webrouse/n-asset-macro/tree/master/examples/grunt "Grunt example") and other similar tools.

## Installation

The best way to install **webrouse/n-asset-macro** is using [Composer](http://getcomposer.org/):

```sh
$ composer require webrouse/n-asset-macro
```

Then register the extension in configuration:
```yaml
# app/config/config.neon
extensions:
assetMacro: Webrouse\AssetMacro\DI\Extension
```
## Usage
Macro can by used in any presenter or control template:
```latte
{* app/presenters/templates/@layout.latte *}
<script src="{asset resources/vendor.js}"></script>
<script src="{asset resources/main.js}"></script>
```

Asset macro prepend path with ```$basePath``` and load revision from the [revision manifest](#revision-manifest):

```html
<script src="/base/path/resources/vendor.d78da025b7.js"></script>
<script src="/base/path/resources/main.34edebe2a2.js"></script>
```

See the [examples](#examples) for usage with [gulp](https://github.com/webrouse/n-asset-macro/tree/master/examples/gulp "Gulp example"), [webpack](https://github.com/webrouse/n-asset-macro/tree/master/examples/webpack "Webpack example"), [grunt](https://github.com/webrouse/n-asset-macro/tree/master/examples/grunt "Grunt example").

## Configuration

Default configuration, which usually doesn't need to be changed:

```yaml
# app/config/config.neon
assetMacro:
# Path to revision manifest or asset => revision pairs,
# if set, the autodetection is switched off
revManifest: null # %wwwDir%/assets.json
# File names for automatic detection of revision manifest
autodetect:
- assets.json
- busters.json
- versions.json
- manifest.json
- rev-manifest.json
# Action if missing asset file: exception, notice, or ignore
missingAsset: notice
# Action if missing manifest file: exception, notice, or ignore
missingManifest: notice
# Action if missing asset revision in manifest: exception, notice, or ignore
missingRevision: ignore
```
## Revision manifest
**Revision manifest is a JSON file that contains the revision (path or version) of asset.**
This file can be generated by various asset processors [gulp](https://github.com/webrouse/n-asset-macro/tree/master/examples/gulp), [grunt](https://github.com/webrouse/n-asset-macro/tree/master/examples/grunt) and [webpack](https://github.com/webrouse/n-asset-macro/tree/master/examples/webpack), [see examples](#examples).
This asset macro searches for the revision manifest in the asset directory or in any parent directory up to `%wwwDir%`.

By default is rev. manifest expected in: `assets.json`, `busters.json`, `versions.json`, `manifest.json` or `rev-manifest.json` file.

**Instead of autodetection, the path to revision manifest can be set directly:**
```yaml
# app/config/config.neon
assetMacro:
revManifest: %wwwDir%/assets.json
```

**Or you can specify `asset => revision` pairs directly in config file:**

```yaml
# app/config/config.neon
assetMacro:
revManifest:
'js/vendor.js': 16016edc74d # or js/vendor.16016edc74d.js
'js/main.js': 4b82916016 # or js/main.4b82916016.js
```

Revision manifest may contains asset version or the asset path. Both ways are supported.

### Revision manifest with asset paths

With this method, the files have a **different name at each change**.

Example revision manifest:
```json
{
"js/app.js": "js/app.234a81ab33.js",
"js/vendor.js": "js/vendor.d67fbce193.js",
"js/locales/en.js": "js/locales/en.d78da025b7.js",
"js/locales/sk.js": "js/locales/sk.34edebe2a2.js",
"css/app.css": "css/app.04b5ff0b97.js"
}
```

With the example manifest, the expr. `{asset "js/app.js"}` generates: `/base/path/js/app.234a81ab33.js`.

### Revision manifest with asset versions

This approach looks better at first glance. The **asset path is still the same**, and only the parameter in the query changes.

However, it can [cause problems](http://www.stevesouders.com/blog/2008/08/23/revving-filenames-dont-use-querystring/) with some cache servers, which don't take the URL parameters into account.

Example revision manifest:
```json
{
"js/app.js": "234a81ab33",
"js/vendor.js": "d67fbce193",
"js/locales/en.js": "d78da025b7",
"js/locales/sk.js": "34edebe2a2",
"css/app.css": "04b5ff0b97"
}
```

With the example manifest, the expr. `{asset "js/app.js"}` generates: `/base/path/js/app.js?v=234a81ab33`.

Asset macro automatically detects which of these two formats of revision manifest is used.

## Custom format

It is possible to specify the custom format using placeholders:

| Placeholder | Example output |
| -------------|---------------------------------------------------------------------------|
| `%url%` | `/base/path/js/main.js?v=8c48f58df` or `/base/path/js/main.8c48f58df.js` |
| `%path%` | `js/main.js` or `js/main.8c48f58df.js` |
| `%raw%` | `8c48f58df` or `js/main.8c48f58df.js` |
| `%basePath%` | `/base/path` |

The format is defined by the second macro parameter or using the `format` key.
```latte
{* app/presenters/templates/@layout.latte *}
{asset 'js/vendor.js', '<script src="%url%"></script>'}
<script src="{asset 'js/livereload.js', format => '%path%?host=localhost&v=%raw%'}"></script>
```

## Error handling

Error handling can be set using the [configuration keys](https://github.com/webrouse/n-asset-macro/blob/master/README.md#configuration): `missingAsset`, `missingManifest`, `missingRevision`.

It can be mutted by third macro parameter or key `needed` (default `TRUE`).

Argument `needed => FALSE` will cause the missing file or the missing revision record will be ignored.

Empty string will be result for missing asset. Missing version will be replaced with `unknown` string.

**Example of `needed` parameter**
* `absent.js` file doesn't exist.
* `missing_rev.js` exists but doesn't have revision in manifest (or the manifest has not been found).

```latte
{asset 'js/absent.js', '<script src="%url%"></script>', FALSE}
{asset 'js/missing_rev.js', format => '<script src="%url%"></script>', needed => FALSE}
```

Generated output:
```html
<script src="/base/path/js/missing_rev.js?v=unknown"></script>
```

## Caching

If you don't want to load the revision manifest in the production mode, you can use the `cache` macro:

```latte
{cache "scripts-$locale", if => ! \Tracy\Debugger::isEnabled()}
<script src="{asset 'js/vendor.js'}" defer></script>
<script src="{asset "js/locales/$locale.js"}" defer></script>
<script src="{asset 'js/main.js'}" defer></script>
{/cache}
```

## Examples

Examples based on [nette/sandbox](https://github.com/nette/sandbox):

* [Gulp + Asset macro](https://github.com/webrouse/n-asset-macro/tree/master/examples/gulp)
* [Grunt + Asset macro](https://github.com/webrouse/n-asset-macro/tree/master/examples/grunt)
* [Webpack + Asset macro](https://github.com/webrouse/n-asset-macro/tree/master/examples/webpack)

## License

N-asset-macro is under the MIT license. See the [LICENSE](LICENSE.md) file for details.
28 changes: 28 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "webrouse/n-asset-macro",
"license": "MIT",
"description": "Asset macro for Latte and Nette Framework useful for assets cache busting (with gulp, webpack, grunt, etc.)",
"type": "library",
"authors": [
{
"name": "Michal Jurečko",
"email": "[email protected]"
}
],
"require": {
"php": ">=5.6",
"latte/latte": ">=2.4",
"nette/di": ">=2.3",
"nette/utils": ">=2.3"
},
"require-dev": {
"nette/tester": "^2.0",
"nette/nette": ">=2.3"
},
"autoload": {
"psr-0": {
"Webrouse": "src"
}
},
"minimum-stability": "dev"
}
9 changes: 9 additions & 0 deletions examples/grunt/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/log/*
/temp/*
/vendor/*
/node_modules/*
/www/dist/*
!.gitignore
!.htaccess
!web.config
/composer.lock
Loading

0 comments on commit 32aeb91

Please sign in to comment.