与Laravel和Elixir合作

时间:2015-06-21 12:59:33

标签: laravel sass zurb-foundation laravel-elixir

如何在Laravel中使用Foundation?

我以为我会使用vendorbower install foundation文件夹中安装Foundation。这导致我有一个vendor/bower_components文件夹,我有基础和所有必需的库,如jQuery。

我应该在gulpfile.js中为Elixir添加什么才能正确解释?应该可以

  • 更新Bower组件
  • 安装新的Bower套餐
  • 修改Foundation Sass变量,而不会在更新时覆盖这些变量
  • 使用Compass

在非Laravel项目中,我将运行Ruby gem foundation new my_project并手动包含已编译的文件。但是,在这种情况下,该命令会创建许多不需要工作的文件。

2 个答案:

答案 0 :(得分:20)

Laravel Elixir包含Libsass,因此您不需要Ruby从Laravel编译您的Foundation Sass文件。所有你需要的是凉亭和Laravel Elixir。您也不需要将文件从bower_components文件夹复制到resources/assets文件夹。

首先关注official instrucctions安装Elixir。

然后使用以下内容在Laravel项目的根目录中创建文件.bowerrc

{
    "directory": "vendor/bower_components"
}

然后使用以下内容在Laravel项目的根目录中创建文件bower.json

{
    "name": "laravel-and-foundation",
    "private": "true",
    "dependencies": {
        "foundation": "latest"
    }
}

然后安装凉亭和基础:

npm install --global bower
bower install # This will install Foundation into vendor/bower_components

然后使用以下内容创建文件resources/assets/sass/_settings.scss

// Custom settings for Zurb Foundation. Default settings can be found at
// vendor/bower_components/foundation/scss/foundation/_settings.scss

然后使用以下内容编辑文件resources/assets/sass/app.scss

@import "normalize";

@import "settings";

// Include all foundation
@import "foundation";

// Or selectively include components
// @import
//   "foundation/components/accordion",
//   "foundation/components/alert-boxes",
//   "foundation/components/block-grid",
//   "foundation/components/breadcrumbs",
//   "foundation/components/button-groups",
//   "foundation/components/buttons",
//   "foundation/components/clearing",
//   "foundation/components/dropdown",
//   "foundation/components/dropdown-buttons",
//   "foundation/components/flex-video",
//   "foundation/components/forms",
//   "foundation/components/grid",
//   "foundation/components/inline-lists",
//   "foundation/components/joyride",
//   "foundation/components/keystrokes",
//   "foundation/components/labels",
//   "foundation/components/magellan",
//   "foundation/components/orbit",
//   "foundation/components/pagination",
//   "foundation/components/panels",
//   "foundation/components/pricing-tables",
//   "foundation/components/progress-bars",
//   "foundation/components/reveal",
//   "foundation/components/side-nav",
//   "foundation/components/split-buttons",
//   "foundation/components/sub-nav",
//   "foundation/components/switches",
//   "foundation/components/tables",
//   "foundation/components/tabs",
//   "foundation/components/thumbs",
//   "foundation/components/tooltips",
//   "foundation/components/top-bar",
//   "foundation/components/type",
//   "foundation/components/offcanvas",
//   "foundation/components/visibility";

使用以下内容配置文件gulpfile.js

elixir(function(mix) {

    // Compile CSS
    mix.sass(
        'app.scss', // Source files
        'public/css', // Destination folder
        {includePaths: ['vendor/bower_components/foundation/scss']}
    );

    // Compile JavaScript
    mix.scripts(
        ['vendor/modernizr.js', 'vendor/jquery.js', 'foundation.min.js'], // Source files. You can also selective choose only some components
        'public/js/app.js', // Destination file
        'vendor/bower_components/foundation/js/' // Source files base directory
    );


});

只需遵循官方文档即可构建:

gulp # Run all tasks...
gulp --production # Run all tasks and minify files
gulp watch # Watch for changes and run all tasks on the fly

您的已编译文件位于public/css/app.csspublic/js/app.js

要更新到最新的Zurb Foundation版本,请运行:

bower update

答案 1 :(得分:1)

复制基金> scss文件夹到资源> assets文件夹,将scss重命名为sass,在你的gulpfile.js中添加以下内容

elixir(function(mix) {
    mix.sass('foundation.scss');
});

运行gulp,它将公开生成foundation.css文件> css文件夹,在项目中包含该文件。

对于js文件,你可以简单地使用这样的东西来复制文件

mix.copy('resources/assets/foundation/js/app.js', 'public/js/app.js');