Laravel¿ª·¢£ºÔõÑùʹÓÃLaravel Elixir±àÒëÇ°¶Ë×ÊÔ´£¿
Ëæ×ÅÇ°¶Ë¿ª·¢µÄÒ»Ö±Éú³¤£¬Ç°¶Ë×ÊÔ´µÄ¿ª·¢¡¢µ÷ÊÔºÍÓÅ»¯µÈÊÂÇéÒ²±äµÃÔ½À´Ô½ÖØ´ó¡£Îª´Ë£¬Ðí¶àÇ°¶Ë¿ª·¢Õ߶¼×îÏȽÓÄÉһЩ×Ô¶¯»¯¹¹½¨¹¤¾ßÀ´¼ò»¯ÕâЩÊÂÇéµÄÁ÷³Ì£¬laravel elixirÕýÊÇÆäÖÐÖ®Ò»¡£
Laravel ElixirÊÇÒ»¸ö»ùÓÚGulpµÄ×Ô¶¯»¯¹¹½¨¹¤¾ß£¬Ëü¿ÉÒÔ×ÊÖú¿ª·¢Õß×Ô¶¯»¯µØ±àÒ롢ѹËõºÍºÏ²¢Ç°¶Ë×ÊÔ´£¬ºÃ±ÈÑùʽ±í¡¢JavaScriptÎļþºÍͼƬµÈ¡£±¾ÎĽ«ÏÈÈÝÔõÑùʹÓÃLaravel ElixirÀ´±àÒëÇ°¶Ë×ÊÔ´¡£
Ò»¡¢×°ÖÃ
ÔÚʹÓÃLaravel Elixir֮ǰ£¬ÐèÒªÏÈÔÚLaravelÓ¦ÓóÌÐòÖÐ×°ÖÃÏà¹ØµÄnpmÒÀÀµ°ü¡£×°ÖÃÀú³ÌÈçÏ£º
×°ÖÃ[Laravel Elixir](https://github.com/laravel/elixir)¡£
npm install --save-dev laravel-elixir
µÇ¼ºó¸´ÖÆ
×°ÖÃ[gulp](http://gulpjs.com/)¡£
npm install --global gulp npm install --save-dev gulp
µÇ¼ºó¸´ÖÆ
×°ÖÃÏà¹ØÒÀÀµ°ü¡£
npm install --save-dev gulp-sass gulp-less gulp-concat gulp-uglify gulp-sourcemaps gulp-imagemin
µÇ¼ºó¸´ÖÆ
¶þ¡¢±àдGulpfile
Á¬Ã¦Ñ§Ï°¡°Ç°¶ËÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
Laravel ElixirµÄÉèÖÃÎļþÊÇÒ»¸öGulpfile£¬ËüÖ÷ÒªÓÃÓÚ½ç˵×Ô¶¯»¯¹¹½¨Ê¹Ãü¡£ÔÚLaravelÓ¦ÓÃÖУ¬¿ÉÒÔÔÚĿ¼ÏÂн¨Gulpfile.jsÎļþ£¬È»ºó±àдÏà¹ØµÄGulpʹÃü¡£ÈçÏÂÊÇÒ»¸ö»ù±¾µÄGulpfileÉèÖôúÂ룺
// ÒýÈëGulpÒÀÀµ var gulp = require('gulp'); // ÒýÈëLaravel ElixirÒÀÀµ var elixir = require('laravel-elixir'); // ½ç˵×ÊԴ·¾¶ var assetsPath = { 'js': 'resources/assets/js', 'css': 'resources/assets/css', 'images': 'resources/assets/images', 'fonts': 'resources/assets/fonts' }; // ½ç˵±àÒëÊä³ö·¾¶ var publicPath = { 'css': 'public/css', 'js': 'public/js', 'images': 'public/images', 'fonts': 'public/fonts' }; // ±àÒëCSSʹÃü gulp.task('compile-css', function() { elixir(function(mix) { mix.sass(assetsPath.css + '/app.scss') .version([publicPath.css + '/app.css']); }); }); // ±àÒëJSʹÃü gulp.task('compile-js', function() { elixir(function(mix) { mix.scripts([ assetsPath.js + '/jquery.js', assetsPath.js + '/app.js' ], publicPath.js + '/app.js') .version([publicPath.js + '/app.js']); }); }); // ѹËõͼƬʹÃü gulp.task('compress-images', function() { return gulp.src(assetsPath.images + '/**') .pipe(elixir(function(mix) { mix.imagemin() .output(publicPath.images); })); }); // ºÏ²¢×ÖÌåʹÃü gulp.task('merge-fonts', function() { return gulp.src(assetsPath.fonts + '/**') .pipe(gulp.dest(publicPath.fonts)); }); // Ö´Ðй¹½¨Ê¹Ãü gulp.task('build', ['compile-css', 'compile-js', 'compress-images', 'merge-fonts'], function() {}); // ½ç˵ĬÈÏʹÃü gulp.task('default', ['build']);
µÇ¼ºó¸´ÖÆ
ÔÚGulpfileÖУ¬Ê×ÏÈÐèÒªÒýÈëgulpºÍLaravel ElixirÒÀÀµ¡£È»ºó½ç˵×ÊԴ·¾¶ºÍ±àÒëÊä³ö·¾¶£¬ÒÔ±ãÔÚʹÃüÖÐʹÓ᣽ÓÏÂÀ´£¬»®·Ö½ç˵ÁËËĸöÇ°¶Ë×ÊÔ´±àÒëʹÃü£¬°üÀ¨±àÒëCSS¡¢±àÒëJS¡¢Ñ¹ËõͼƬºÍºÏ²¢×ÖÌå¡£×îºó£¬½ç˵ÁËÒ»¸öbuildʹÃü£¬ÓÃÓÚÖ´ÐÐËùÓбàÒëʹÃü¡£Í¬Ê±£¬»¹½ç˵ÁËÒ»¸öĬÈÏʹÃü£¬Ëü»á×Ô¶¯Ö´ÐÐbuildʹÃü¡£
Èý¡¢Ê¹ÓÃLaravel Elixir±àÒëÇ°¶Ë×ÊÔ´
ÔÚ±àдºÃGulpfileÖ®ºó£¬ÎÒÃǾͿÉÒÔʹÓÃLaravel ElixirÀ´±àÒëÇ°¶Ë×ÊÔ´ÁË¡£Ê¹ÓÃÈçÏÂÏÂÁîÀ´Æô¶¯¹¹½¨£º
gulp
µÇ¼ºó¸´ÖÆ
Æô¶¯¹¹½¨Ö®ºó£¬Laravel Elixir»á×Ô¶¯Ö´ÐÐËùÓнç˵ºÃµÄʹÃü£¬´Ó¶øÍê³ÉÇ°¶Ë×ÊÔ´µÄ±àÒ롢ѹËõºÍºÏ²¢µÈÊÂÇé¡£ÎÒÃÇÎÞÐèÊÖ¶¯Ö´ÐÐÕâЩʹÃü£¬´Ó¶øÌá¸ßÁË¿ª·¢Ð§ÂÊ¡£
ͬʱ£¬Laravel Elixir»¹ÌṩÁËһЩ¿ì½Ý·½·¨£¬ËüÃÇ¿ÉÒÔ×ÊÖúÎÒÃÇÔ½·¢Àû±ãµØ±àÒëÇ°¶Ë×ÊÔ´¡£ºÃ±È£¬ÎÒÃÇ¿ÉÒÔʹÓÃÈçÏÂÏÂÁîÀ´±àÒëSassÎļþ£º
elixir(function(mix) { mix.sass('app.scss'); });
µÇ¼ºó¸´ÖÆ
ÉÏÊö´úÂë»á×Ô¶¯±àÒëresources/assets/sassϵÄapp.scssÎļþ£¬²¢½«±àÒëºóµÄÎļþÊä³öµ½public/cssĿ¼Ï¡£ÎÒÃÇ»¹¿ÉÒÔʹÓÃÀàËƵķ½·¨À´±àÒëJavaScriptÎļþ¡¢Ñ¹ËõͼƬµÈ¡£
×ܽá
ÒÔÉϾÍÊÇÔõÑùʹÓÃLaravel Elixir±àÒëÇ°¶Ë×ÊÔ´µÄÒªÁ졣ͨ¹ýʹÓÃLaravel Elixir£¬ÎÒÃÇ¿ÉÒÔ¼ò»¯Ç°¶Ë¿ª·¢ÖеÄһЩ·±ËöÊÂÇ飬Ìá¸ß¿ª·¢Ð§ÂÊ£¬²¢Ê¹µÃÕû¸ö¿ª·¢Àú³ÌÔ½·¢Á÷ͨ¡£ÖµµÃÒ»ÌáµÄÊÇ£¬Laravel ElixirµÄѧϰ±¾Ç®ºÜµÍ£¬½öÐèÏàʶһЩ»ù±¾µÄGulp֪ʶ£¬¾Í¿ÉÒÔÇáËÉÉÏÊÖʹÓá£
ÒÔÉϾÍÊÇLaravel¿ª·¢£ºÔõÑùʹÓÃLaravel Elixir±àÒëÇ°¶Ë×ÊÔ´£¿µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡