{ "name": "symfony-standard-requirejs", "private": true, "ignore": [ "**/.*", "node_modules", "bower_components", "test", "tests" ], "dependencies": { "almond": "0.3.0", "requirejs": "2.1.15", "rjs": "2.1.15" } }
{ "name": "symfony-standard-requirejs", "private": true, "dependencies": { "gulp": "3.8.8", "yargs": "1.3.2" } }
var gulp = require('gulp'), exec = require('child_process').exec, argv = require('yargs').argv; gulp.task('copy', function () { // almond gulp.src('bower_components/almond/almond.js') .pipe(gulp.dest('web/app/vendor/almond')); // requirejs gulp.src('bower_components/requirejs/require.js') .pipe(gulp.dest('web/app/vendor/requirejs')); // rjs gulp.src('bower_components/rjs/dist/r.js') .pipe(gulp.dest('.')); }); gulp.task('rjs', function (cb) { var env = argv.env ? argv.env : 'dev', cmd = [ 'php app/console cache:clear --env=' + env, 'php app/console assets_version:increment --env=' + env, 'php app/console assetic:dump --env=' + env, 'node r.js -o web/app/app.build.js' ]; exec(cmd.join(' && '), function (err, stdout, stderr) { console.log(stdout); console.log(stderr); cb(err); }); }); gulp.task('build', ['copy', 'rjs']); gulp.task('default', ['build']);
npm install bower install
{% extends "AppBundle::layout.html.twig" %} {% block javascripts %} {% if app.environment == 'prod' %} <script src="{{ asset('app/dist/main.js') }}"></script> {% else %} <script>var require = {urlArgs: 'bust=' + (new Date()).getTime()};</script> <script data-main="app/main" src="{{ asset('app/vendor/requirejs/require.js') }}"></script> {% endif %} <script> requirejs.config({ config: { 'src/config': { user: { id: 1, username: 'John Doe' } } } }); </script> {% endblock %}
It should be noted here that the client part has some structure in the file system and in order to have fewer questions, I will write about it briefly. The sources are located in the public web directory, namely in the web / app directory, which has the following structure:βββ app.build.js βββ dist β βββ .gitkeep βββ main.js βββ specs β βββ .gitkeep βββ src β βββ app.js β βββ config.js βββ vendor
requirejs.config({ baseUrl: 'app' }); require([ 'src/app', 'src/config' ], function (App, config) { App.start(config); });
({ baseUrl: '.', mainConfigFile: 'main.js', wrapShim: true, name: 'vendor/almond/almond', include: 'main', out: 'dist/main.js', findNestedDependencies: true, preserveLicenseComments: false })
In the file example.build.js you can read more about each parameter.Let's write a simple Hello application:
define([ ], function () { 'use strict'; var App = {}; App.start = function (config) { console.log('Hello, ' + config.user.username + '!'); }; window.App = App; return App; });
define([ 'module' ], function (module) { 'use strict'; return module.config(); });
Symfony2 may require additional edits in the controller, in the example I am using Symfony Standard Edition.Open the main application page in the browser, the message βHello, John Doe!β Should appear in the browser console.
npm install bower install gulp build --env=prod
/bower_components/ /node_modules/ /web/app/dist/ /web/app/vendor/ /r.js
Source: https://habr.com/ru/post/245911/
All Articles