Skip to content

Commit 6aa4356

Browse files
Matatiro Solutionstobias-93
authored andcommitted
JavaScript refactoring (#303)
* First commit integrating refactored JS code from Bruno Sampaio to remove reliance on Google Closure and support WebPack integration * Resolving issues with failing tests * Moving location of npm install call to match updated router_test.html * Updating documentation with WebPack information * Updating capitalisation of webpack as requested. * Adjusting spacing for note block * Updates to gulpfile, package and documentation addressing comments from @stof * Setting correct permissions on js files imported from Bruon's work * Correcting permissions, capitalisation, and version number * Adding non-static method for route setting to simplify commonJS implementation * Run a build prior to the JS test suite. Updated documentation. * Switching Travis to use the same install and test commands as documented * Updated gulpfile in an attempt to support PHP 5.3 on Travis * Updated gulpfile removing another choke point for Travis on PHP 5.3 * Setting babel parameters in gulpfile to allow removal of .babelrc to prevent unexpected dependencies in projects
1 parent 2e87528 commit 6aa4356

File tree

13 files changed

+803
-281
lines changed

13 files changed

+803
-281
lines changed

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ before_install:
2828

2929
install:
3030
- composer update $COMPOSER_FLAGS --prefer-dist
31-
- npm install google-closure-library
31+
- cd Resources && npm install && cd ../
3232

3333
script:
3434
- ./phpunit
35-
- phantomjs Resources/js/run_jsunit.js Resources/js/router_test.html
35+
- cd Resources && npm run test

CONTRIBUTING.md

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -46,19 +46,31 @@ $ ./phpunit
4646

4747
### JavaScript Test Suite
4848

49-
First, install [PhantomJS](http://phantomjs.org/) and [Google Closure
50-
Library](https://github.com/google/closure-library):
49+
First, install [PhantomJS](http://phantomjs.org/) (see the website for further
50+
details or simply use your favourite package manager) and the development dependencies using:
5151

5252
```bash
53-
$ npm install google-closure-library
53+
$ cd Resources
54+
$ npm install
5455
```
5556

56-
Run the JS test suite with:
57+
then run the JS test suite with:
5758

5859
```bash
59-
$ phantomjs Resources/js/run_jsunit.js Resources/js/router_test.html
60+
$ npm run test
6061
```
6162

63+
Because the current test suite runs against the built javascript a build is automatically
64+
run first (see 'Compiling the JavaScript files' below for further details). You can
65+
explicitly run only the test suite with:
66+
67+
```bash
68+
$ phantomjs js/run_jsunit.js js/router_test.html
69+
```
70+
71+
Alternatively you can open `Resources/js/router_test.html` in your browser which
72+
runs the same test suite with a graphical output.
73+
6274
Compiling the JavaScript files
6375
------------------------------
6476

@@ -67,19 +79,23 @@ Compiling the JavaScript files
6779
> We already provide a compiled version of the JavaScript; this section is only
6880
> relevant if you want to make changes to this script.
6981
70-
In order to re-compile the JavaScript source files that we ship with this
71-
bundle, you need the Google Closure Tools. You need the
72-
[plovr](http://plovr.com/download.html) tool, which is a Java ARchive, so you
73-
also need a working Java environment. You can re-compile the JavaScript with the
74-
following command:
82+
This project is using [Gulp](https://gulpjs.com/) to compile JavaScript files.
83+
In order to use Gulp you must install both [node](https://nodejs.org/en/) and
84+
[npm](https://www.npmjs.com/).
85+
86+
If you are not familiar with using Gulp, it is recommended that you review this
87+
[An Introduction to Gulp.js](https://www.sitepoint.com/introduction-gulp-js/)
88+
tutorial which will guide you through the process of getting node and npm installed.
89+
90+
Once you have node and npm installed:
7591

7692
```bash
77-
$ java -jar plovr.jar build Resources/config/plovr/compile.js
93+
$ cd Resources
94+
$ npm install
7895
```
7996

80-
Alternatively, you can use the JMSGoogleClosureBundle. If you install this
81-
bundle, you can re-compile the JavaScript with the following command:
97+
Then to perform a build
8298

8399
```bash
84-
$ php app/console plovr:build @FOSJsRoutingBundle/compile.js
100+
$ npm run build
85101
```

Resources/doc/usage.rst

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
Usage
22
=====
33

4-
Add these two lines in your layout:
4+
In applications not using webpack add these two lines in your layout:
55

66
.. configuration-block::
77

88
.. code-block:: html+twig
99

10-
<script src="{{ asset('bundles/fosjsrouting/js/router.js') }}"></script>
10+
<script src="{{ asset('bundles/fosjsrouting/js/router.min.js') }}"></script>
1111
<script src="{{ path('fos_js_routing_js', { callback: 'fos.Router.setData' }) }}"></script>
1212

1313
.. code-block:: html+php
@@ -17,9 +17,28 @@ Add these two lines in your layout:
1717

1818
.. note::
1919

20-
If you are not using Twig, then it is no problem. What you need is to add
20+
If you are not using Twig, then it is no problem. What you need is
2121
the two JavaScript files above loaded at some point in your web page.
2222

23+
24+
If you are using webpack and Encore to package your assets you will need to use the dump command
25+
and export your routes to json:
26+
27+
.. code-block:: bash
28+
29+
bin/console fos:js-routing:dump --format=json
30+
31+
Then within your JavaScript development you can use:
32+
33+
.. code-block:: javascript
34+
35+
const routes = require('../../web/js/fos_js_routes.json');
36+
import Routing from '../../vendor/friendsofsymfony/jsrouting-bundle/Resources/public/js/router.min.js';
37+
38+
Routing.setRoutingData(routes);
39+
Routing.generate('rep_log_list');
40+
41+
2342
Generating URIs
2443
---------------
2544

Resources/gulpfile.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const gulp = require('gulp');
2+
const babel = require('gulp-babel');
3+
const rename = require('gulp-rename');
4+
const uglify = require('gulp-uglify');
5+
const wrap = require('gulp-wrap');
6+
7+
gulp.task('js', function() {
8+
return gulp.src('js/router.js')
9+
.pipe(babel({
10+
presets: ["es2015"],
11+
plugins: ["transform-object-assign"]
12+
}))
13+
.pipe(wrap({ src: 'js/router.template.js' }))
14+
.pipe(gulp.dest('public/js'))
15+
.pipe(rename({ extname: '.min.js' }))
16+
.pipe(uglify())
17+
.pipe(gulp.dest('public/js'));
18+
});
19+
20+
gulp.task('default', function() {
21+
return gulp.start(['js']);
22+
});

Resources/js/export.js

Lines changed: 0 additions & 31 deletions
This file was deleted.

Resources/js/externs.js

Lines changed: 0 additions & 12 deletions
This file was deleted.

0 commit comments

Comments
 (0)