Table of Contents
" >Installation and Configuration
#Installing Node
" >Gulp
" >Laravel Elixir
" >Run Elixir
Monitor resource file modifications
" >
" >Less
Sass
纯 CSS
Source Maps
使用脚本
CoffeeScript
Browserify
Babel
Scripts
复制文件与目录
版本与缓存清除
为多个文件生成版本
BrowserSync
调用既有的 Gulp 任务
自定义监控器
编写 Elixir 扩展功能
Home PHP Framework Laravel what is laravel elixir

what is laravel elixir

Feb 14, 2022 pm 02:57 PM
elixir laravel

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1653
14
PHP Tutorial
1251
29
C# Tutorial
1224
24
Laravel Eloquent ORM in Bangla partial model search) Laravel Eloquent ORM in Bangla partial model search) Apr 08, 2025 pm 02:06 PM

LaravelEloquent Model Retrieval: Easily obtaining database data EloquentORM provides a concise and easy-to-understand way to operate the database. This article will introduce various Eloquent model search techniques in detail to help you obtain data from the database efficiently. 1. Get all records. Use the all() method to get all records in the database table: useApp\Models\Post;$posts=Post::all(); This will return a collection. You can access data using foreach loop or other collection methods: foreach($postsas$post){echo$post->

Laravel Introduction Example Laravel Introduction Example Apr 18, 2025 pm 12:45 PM

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

Solve caching issues in Craft CMS: Using wiejeben/craft-laravel-mix plug-in Solve caching issues in Craft CMS: Using wiejeben/craft-laravel-mix plug-in Apr 18, 2025 am 09:24 AM

When developing websites using CraftCMS, you often encounter resource file caching problems, especially when you frequently update CSS and JavaScript files, old versions of files may still be cached by the browser, causing users to not see the latest changes in time. This problem not only affects the user experience, but also increases the difficulty of development and debugging. Recently, I encountered similar troubles in my project, and after some exploration, I found the plugin wiejeben/craft-laravel-mix, which perfectly solved my caching problem.

Laravel user login function Laravel user login function Apr 18, 2025 pm 12:48 PM

Laravel provides a comprehensive Auth framework for implementing user login functions, including: Defining user models (Eloquent model), creating login forms (Blade template engine), writing login controllers (inheriting Auth\LoginController), verifying login requests (Auth::attempt) Redirecting after login is successful (redirect) considering security factors: hash passwords, anti-CSRF protection, rate limiting and security headers. In addition, the Auth framework also provides functions such as resetting passwords, registering and verifying emails. For details, please refer to the Laravel documentation: https://laravel.com/doc

Laravel's geospatial: Optimization of interactive maps and large amounts of data Laravel's geospatial: Optimization of interactive maps and large amounts of data Apr 08, 2025 pm 12:24 PM

Efficiently process 7 million records and create interactive maps with geospatial technology. This article explores how to efficiently process over 7 million records using Laravel and MySQL and convert them into interactive map visualizations. Initial challenge project requirements: Extract valuable insights using 7 million records in MySQL database. Many people first consider programming languages, but ignore the database itself: Can it meet the needs? Is data migration or structural adjustment required? Can MySQL withstand such a large data load? Preliminary analysis: Key filters and properties need to be identified. After analysis, it was found that only a few attributes were related to the solution. We verified the feasibility of the filter and set some restrictions to optimize the search. Map search based on city

Laravel framework installation method Laravel framework installation method Apr 18, 2025 pm 12:54 PM

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

Laravel and the Backend: Powering Web Application Logic Laravel and the Backend: Powering Web Application Logic Apr 11, 2025 am 11:29 AM

How does Laravel play a role in backend logic? It simplifies and enhances backend development through routing systems, EloquentORM, authentication and authorization, event and listeners, and performance optimization. 1. The routing system allows the definition of URL structure and request processing logic. 2.EloquentORM simplifies database interaction. 3. The authentication and authorization system is convenient for user management. 4. The event and listener implement loosely coupled code structure. 5. Performance optimization improves application efficiency through caching and queueing.

How to learn Laravel How to learn Laravel for free How to learn Laravel How to learn Laravel for free Apr 18, 2025 pm 12:51 PM

Want to learn the Laravel framework, but suffer from no resources or economic pressure? This article provides you with free learning of Laravel, teaching you how to use resources such as online platforms, documents and community forums to lay a solid foundation for your PHP development journey from getting started to master.

See all articles