Home > PHP Framework > Laravel > body text

what is laravel elixir

青灯夜游
Release: 2022-02-14 14:57:32
Original
2143 people have browsed it

Laravel Elixir is an API that integrates Gulp and provides a simple solution for compiling Less, Sass, CoffeeScript and handling many other daily tasks in Laravel projects, thereby reducing the hassle of writing the above tedious tasks time, effectively improving programming efficiency.

what is laravel elixir

The operating environment of this tutorial: Windows 7 system, Laravel version 8.5, Dell G3 computer.

Laravel aims to make PHP development easy and enjoyable, so starting from Laravel 5, a new API called LaravelElixir is provided. This API integrates Gulp, providing a simple solution for compiling Less, Sass, CoffeeScript in Laravel projects and handling many other daily tasks, thereby reducing the time of writing the above tedious tasks and effectively improving programming efficiency.

Laravel Elixir provides a simple and smooth API that allows you to define basic Gulp tasks in your Laravel application. Elixir supports many common CSS and JavaScript preprocessors and even includes testing tools. Using chain calls, Elixir allows you to define the development process smoothly, for example:

elixir(function(mix) {
    mix.sass('app.scss')
       .coffee('app.coffee');
});
Copy after login

If you have ever been confused about getting started with Gulp and compiling resource files, then you will fall in love with Laravel Elixir , but Laravel does not force you to use Elixir, you are free to choose the automated development process tool you like.

Installation and Configuration

#Installing Node

Before you start using Elixir, you must first make sure that your machine Have Node.js installed.

node -v
Copy after login

By default, Laravel Homestead will include everything you need; however, if you are not using Vagrant, then you can simply browse Node’s download page(http://nodejs.org/download/) to install. [Related recommendations: laravel video tutorial]

Gulp

Next, you need to install Gulp globally (http: //gulpjs.com) NPM extension package:

npm install --global gulp
Copy after login

Laravel Elixir

#The last step is to install Elixir! In every newly installed Laravel code, you will find a file named package.json in the root directory. Think of it like your composer.json file, except that it defines Node's dependent extension package instead of PHP's. You can use the following command to install dependent extension packages:

npm install
Copy after login

If you are developing on a Windows system or running a VM on a Windows host system, you need to run npm Turn on --no-bin-links when installing the install command:

npm install --no-bin-links
Copy after login

Run Elixir

#Elixir is built on top of Gulp, so to run your Elixir tasks, just run the gulp command on the command line. Adding the --production flag to the command will tell Elixir to compress your CSS and JavaScript files:

// 运行所有任务...
gulp

// 运行所有任务并压缩所有 CSS 及 JavaScript...
gulp --production
Copy after login

Monitor resource file modifications

Because it is quite inconvenient to run the gulp command on the command line every time you modify your resource file, so you can use the gulp watch command. This command will be run from your command line and monitor resource files for any modifications. When modifications occur, a new file will be automatically compiled:

gulp watch
Copy after login

#. js contains all your Elixir tasks. Elixir tasks can be chained together to define how your resource files should be compiled.

Less

#To compile Less to CSS, you can use the less method. The less method will assume that your Less files are saved in the resources/assets/less folder. By default, this example task will place the compiled CSS in public/css/app.css:

elixir(function(mix) {
    mix.less('app.less');
});
Copy after login

You may want to merge multiple Less file to a single CSS file. Similarly, the generated CSS will be placed in public/css/app.css:

elixir(function(mix) {
    mix.less([
        'app.less',
        'controllers.less'
    ]);
});
Copy after login

If you want to customize the output location of the compiled CSS, you can Pass the second parameter to the less method:

elixir(function(mix) {
    mix.less('app.less', 'public/stylesheets');
});

// 指定输出的文件名称...
elixir(function(mix) {
    mix.less('app.less', 'public/stylesheets/style.css');
});
Copy after login

Sass

sass 方法让你能编译 Sass 至 CSS。Sass 文件的默认读取路径是 resources/assets/sass,你可以使用此方法:

elixir(function(mix) {
    mix.sass('app.scss');
});
Copy after login

同样的,如同 less 方法,你可以编译多个 Sass 文件至单个的 CSS 文件,甚至可以自定义生成的 CSS 的输出目录:

elixir(function(mix) {
    mix.sass([
        'app.scss',
        'controllers.scss'
    ], 'public/assets/css');
});
Copy after login

纯 CSS

如果你只是想将一些纯 CSS 样式合并成单个的文件,你可以使用 styles 方法。此方法的默认路径为 resources/assets/css 目录,而生成的 CSS 会被放置于 public/css/all.css

elixir(function(mix) {
    mix.styles([
        'normalize.css',
        'main.css'
    ]);
});
Copy after login

当然,你也可以通过传递第二个参数至 styles 方法,将生成的文件输出至指定的位置:

elixir(function(mix) {
    mix.styles([
        'normalize.css',
        'main.css'
    ], 'public/assets/css');
});
Copy after login

Source Maps

Source maps 在默认情况下是开启的。因此,针对每个被编译的文件,同目录内都会伴随着一个 *.css.map 文件。这个文件能够让你在浏览器调试时,可以追踪编译后的样式选择器至原始的 Sass 或 Less 位置。

如果你不想为你的 CSS 生成 source maps,你可以使用一个简单的配置选项关闭它们:

elixir.config.sourcemaps = false;

elixir(function(mix) {
    mix.sass('app.scss');
});
Copy after login

使用脚本

Elixir 也提供了一些函数来帮助你使用 JavaScript 文件,像是编译 ECMAScript 6、编译 CoffeeScript、Browserify、压缩、及简单的串联纯 JavaScript 文件。

CoffeeScript

coffee 方法可以用于编译 CoffeeScript 至纯 JavaScript。coffee 函数接收一个相对于 resources/assets/coffee 目录的 CoffeeScript 文件名字符串或数组,接着在 public/js 目录生成单个的 app.js 文件:

elixir(function(mix) {
    mix.coffee(['app.coffee', 'controllers.coffee']);
});
Copy after login

Browserify

Elixir 还附带了一个 browserify 方法,给予你在浏览器引入模块及 ECMAScript 6 的有用的特性。

此任务假设你的脚本都保存在 resources/assets/js,并会将生成的文件放置于 public/js/main.js

elixir(function(mix) {
    mix.browserify('main.js');
});
Copy after login

虽然 Browserify 附带了 Partialify 及 Babelify 转换器,但是只要你愿意,你可以随意安装并增加更多的转换器:

npm install aliasify --save-dev
Copy after login
elixir.config.js.browserify.transformers.push({
    name: 'aliasify',
    options: {}
});

elixir(function(mix) {
    mix.browserify('main.js');
});
Copy after login

Babel

babel 方法可被用于编译 ECMAScript 6 与 7 至纯 JavaScript。此函数接收一个相对于 resources/assets/js 目录的文件数组,接着在 public/js 目录生成单个的 all.js 文件:

elixir(function(mix) {
    mix.babel([
        'order.js',
        'product.js'
    ]);
});
Copy after login

若要选择不同的输出位置,只需简单的指定你希望的路径作为第二个参数。该方法除了 Babel 的编译外,特色与功能同等于 mix.scripts()

Scripts

如果你想将多个 JavaScript 文件合并至单个文件,你可以使用 scripts 方法。

scripts 方法假设所有的路径都相对于 resources/assets/js 目录,且默认会将生成的 JavaScript 放置于 public/js/all.js

elixir(function(mix) {
    mix.scripts([
        'jquery.js',
        'app.js'
    ]);
});
Copy after login

如果你想多个脚本的集合合并成不同文件,你可以使用调用多个 scripts 方法。给予该方法的第二个参数会为每个串联决定生成的文件名称:

elixir(function(mix) {
    mix.scripts(['app.js', 'controllers.js'], 'public/js/app.js')
       .scripts(['forum.js', 'threads.js'], 'public/js/forum.js');
});
Copy after login

如果你想合并指定目录中的所有脚本,你可以使用 scriptsIn 方法。生成的 JavaScript 会被放置在 public/js/all.js

elixir(function(mix) {
    mix.scriptsIn('public/js/some/directory');
});
Copy after login

复制文件与目录

copy 方法可以复制文件与目录至新位置。所有操作路径都相对于项目的根目录:

elixir(function(mix) {
    mix.copy('vendor/foo/bar.css', 'public/css/bar.css');
});

elixir(function(mix) {
    mix.copy('vendor/package/views', 'resources/views');
});
Copy after login

版本与缓存清除

许多的开发者会在它们编译后的资源文件中加上时间戳或是唯一的 token,强迫浏览器加载全新的资源文件以取代提供的旧版本代码副本。你可以使用 version 方法让 Elixir 处理它们。

version 方法接收一个相对于 public 目录的文件名称,接着为你的文件名称加上唯一的哈希值,以防止文件被缓存。举例来说,生成出来的文件名称可能像这样:all-16d570a7.css

elixir(function(mix) {
    mix.version('css/all.css');
});
Copy after login

在为文件生成版本之后,你可以在你的 视图 中使用 Laravel 的全局 elixir PHP 辅助函数来正确加载名称被哈希后的文件。elixir 函数会自动判断被哈希的文件名称:

<link rel="stylesheet" href="{{ elixir(&#39;css/all.css&#39;) }}">
Copy after login

为多个文件生成版本

你可以传递一个数组至 version 方法来为多个文件生成版本:

elixir(function(mix) {
    mix.version([&#39;css/all.css&#39;, &#39;js/app.js&#39;]);
});
Copy after login

一旦该文件被加上版本,你需要使用 elixir 辅助函数来生成哈希文件的正确链接。切记,你只需要传递未哈希文件的名称至 elixir 辅助函数。此函数会自动使用未哈希的名称来判断该文件为目前的哈希版本:

<link rel="stylesheet" href="{{ elixir(&#39;css/all.css&#39;) }}">

Copy after login

BrowserSync

当你对前端资源进行修改后,BrowserSync 会自动刷新你的网页浏览器。你可以使用 browserSync 方法来告知 Elixir,当你运行 gulp watch 命令时启动 BrowserSync 服务器:

elixir(function(mix) {
    mix.browserSync();
});
Copy after login

一旦你运行 gulp watch,就能使用连接端口 3000 启用浏览器同步并访问你的网页应用程序:http://homestead.app:3000。如果你在本机开发所使用的域名不是 homestead.app,那么你可以传递一个 选项 的数组作为 browserSync 方法的第一个参数:

elixir(function(mix) {
    mix.browserSync({
        proxy: &#39;project.app&#39;    
    });
});
Copy after login

调用既有的 Gulp 任务

如果你需要在 Elixir 调用一个既有的 Gulp 任务,你可以使用 task 方法。举个例子,假设你有一个 Gulp 任务,当你调用时会输出一些简单的文本:

gulp.task(&#39;speak&#39;, function() {
    var message = &#39;Tea...Earl Grey...Hot&#39;;

    gulp.src(&#39;&#39;).pipe(shell(&#39;say &#39; + message));
});
Copy after login

如果你希望在 Elixir 中调用这个任务,使用 mix.task 方法并传递该任务的名称作为该方法唯一的参数:

elixir(function(mix) {
    mix.task(&#39;speak&#39;);
});
Copy after login

自定义监控器

如果你想注册一个监控器让你的自定义任务能在每次文件改变时就运行,只需传递一个正则表达式作为 task 方法的第二个参数:

elixir(function(mix) {
    mix.task(&#39;speak&#39;, &#39;app/**/*.php&#39;);
});
Copy after login

编写 Elixir 扩展功能

如果你需要比 Elixir 的 task 方法更灵活的方案,你可以创建自定义的 Elixir 扩展功能。Elixir 扩展功能允许你传递参数至你的自定义任务。举例来说,你可以编写一个扩展功能,像是:

// 文件:elixir-extensions.js

var gulp = require(&#39;gulp&#39;);
var shell = require(&#39;gulp-shell&#39;);
var Elixir = require(&#39;laravel-elixir&#39;);

var Task = Elixir.Task;

Elixir.extend(&#39;speak&#39;, function(message) {

    new Task(&#39;speak&#39;, function() {
        return gulp.src(&#39;&#39;).pipe(shell(&#39;say &#39; + message));
    });

});

// mix.speak(&#39;Hello World&#39;);
Copy after login

就是这样!注意,你的 Gulp 具体的逻辑必须被放置在 Task 第二个参数传递的构造器函数里面。你可以将此扩展功能放置在 Gulpfile 的上方,取而代之也可以导出至一个自定义任务的文件。举个例子,如果你将你的扩展功能放置在 elixir-extensions.js 文件中,那么你可以在 Gulpfile 中像这样引入该文件:

// 文件:Gulpfile.js

var elixir = require(&#39;laravel-elixir&#39;);

require(&#39;./elixir-extensions&#39;)

elixir(function(mix) {
    mix.speak(&#39;Tea, Earl Grey, Hot&#39;);
});
Copy after login

自定义监控器

如果你想在运行 gulp watch 时能够重新触发你的自定义任务,你可以注册一个监控器:

new Task(&#39;speak&#39;, function() {
    return gulp.src(&#39;&#39;).pipe(shell(&#39;say &#39; + message));
})
.watch(&#39;./app/**&#39;);
Copy after login

相关推荐:最新的五个Laravel视频教程

The above is the detailed content of what is laravel elixir. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!