【问题标题】:Elixir + Gulp + Laravel with differents JS inclusions depending on URLElixir + Gulp + Laravel,根据 URL 包含不同的 JS
【发布时间】:2016-05-10 04:46:25
【问题描述】:

我想开始使用 Elixir + Gulp。在我的项目中,我只是使用 CSS + JS。

今天,我是这样使用它的:我有一个包含在所有文件中的 dashboard.blade.php。

然后,根据页面,我对每个库都有一个条件包含:

@if (Request::is("tournaments") || Request::is("invites"))
    {!! Html::script('js/plugins/tables/footable/footable.min.js') !!}
@endif
@if (Request::is("tournaments/create"))
    {!! Html::script('js/plugins/forms/inputs/duallistbox.min.js') !!}
    {!! Html::script('js/plugins/pickers/pickadate/picker.js') !!}
    {!! Html::script('js/plugins/pickers/pickadate/picker.date.js') !!}

@endif
....

我想要的是使用 elixir,并在 elixir 端做这些事情,这样我就可以简化我的 dashboard.blade.php 并且总是只有 2 个包含:app.css 和 app.js

我只是不知道最好的方法是什么。

有什么想法吗?

编辑1:

在我的 gulp.js 中,我定义了所有页面通用的一般样式:

mix.styles([
  'icons/icomoon/styles.css',
  'bootstrap.css',
  'components.css',
  'colors.css'

], 'public/css/app.css');

然后,在我的页面中,我像这样包含它:

    {!! Html::style('css/app.css')!!}

我可以用chrome打开文件,所以链接似乎没问题。

但是当我打开我的网站时,一切都出错了,我在 Chrome 控制台中:

Uncaught SyntaxError: Unexpected token <
jquery.min.js:1 Uncaught SyntaxError: Unexpected token <
bootstrap.min.js:1 Uncaught SyntaxError: Unexpected token <
blockui.min.js:1 Uncaught SyntaxError: Unexpected token <
app.js:1 Uncaught SyntaxError: Unexpected token <
pnotify.min.js:1 Uncaught SyntaxError: Unexpected token <
switch.min.js:1 Uncaught SyntaxError: Unexpected token <
(index):1 Failed to decode downloaded font:     http://laravel.dev/css/fonts/icomoon.woff?3p0rtw
(index):1 OTS parsing error: invalid version tag
(index):1 Failed to decode downloaded font:     http://laravel.dev/css/fonts/icomoon.ttf?3p0rtw
(index):1 OTS parsing error: invalid version tag

我做错了什么???

【问题讨论】:

  • 我在特定视图的其他刀片之一中使用@section('scripts')。因此,如果您有一个特定于 tournaments/create 视图的刀片,您可以在该刀片中执行 @section('scripts')&lt;script src="{{ elixir('....') }}&gt;&lt;/script&gt;@endsection,这会将脚本添加到您的脚本标签中。
  • 样式也一样...@section('styles')
  • 但是长生不老药都在 gulp.js 中,不是吗?如何获得统一和缩小的脚本和样式?
  • 使用gulpfile.js,我创建了多个mix.scripts()mix.styles(),并创建了mix.version()。然后你刚刚吞下的那些“包”可以使用上面的代码获得。
  • 好的,所以我 gulp.js,你需要为每个刀片制作 1 个 mix.script,对吗?

标签: javascript php css laravel laravel-elixir


【解决方案1】:

在您的gulpfile.js 中,您可以创建一个大型脚本,例如:

/**
 * Tournaments create package
 */
mix.scripts([
    'forms/inputs/dualistbox.min.js',
    'pickers/pickadate/picker.js',
    'pickers/pickadate/picker.date.js'
    // ↑ these are the files you are wanting to merge
], 'js/directory/where/you/want/script/saved/tournamentCreate.js', 'js/plugins');
//  ↑ this is directory you want the merged file to be in           ↑ this is the directory to look in for the scripts above

/**
 * Version control (might use, might not...I do)
 */
mix.version([
    'js/directory/from/above/tournamentCreate.js'
]);

现在您有一个包含脚本的合并脚本,您可以使用elixir 将其拉入。请注意,您需要在控制台中运行gulp 来创建文件。

在你的 tournementcreate.blade.php

@section('scripts')
<script src="{{ elixir('js/directory/from/above/tournamentCreate.js') }}"></script>
@endsection

您可以根据需要创建任意数量。您只需使用相同布局的新mix.scripts()。希望一切都有意义!

【讨论】:

  • Tx,我做到了,它似乎可以工作,我创建了一个独特的 app.css,但是随后,chrome 控制台中的一切都失败了: Uncaught SyntaxError: Unexpected token laravel.dev/css/fonts/icomoon.woff?3p0rtw (index):1 OTS 解析错误:无效版本标签 (index): 1 解码下载字体失败:laravel.dev/css/fonts/icomoon.ttf?3p0rtw(索引):1 OTS 解析错误:版本标签无效
  • 我会质疑的
  • 只有铬?缓存问题?我认为字体解码可能与formatting 有关,尽管我并不肯定。您是否执行了mix.version(),因为那里似乎也存在一些错误。
  • 你检查了我上面提供的链接吗?其他人在处理字体时也遇到了同样的问题,仅限 Chrome。
  • 是的,我正在检查它。
猜你喜欢
  • 2017-01-21
  • 2015-05-05
  • 2018-08-23
  • 1970-01-01
  • 2015-04-27
  • 1970-01-01
  • 2016-07-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多