markdown Rails Asset Pipeline:`require`指令

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了markdown Rails Asset Pipeline:`require`指令相关的知识,希望对你有一定的参考价值。

#### Asset Path
* In your manifest files, you will need to require certain files in order to render their content on
to your page
* Rails will look in the `assets` path for these files. There are **three** default locations 
where your rails app contains an **assets** folder:
    * `app/assets/`
    * `lib/assets/`
    * `vendor/assets/`
* Within these directories, Rails will also take a look into the following sub-directories:
    * `images`
    * `javascripts`
    * `stylesheets`
* Any **files** that are directly located within any of these directories can be referenced in you
manifest file as follows:

    * a file located in `app/assets/javascripts/slider.js`, can be referenced in your (js) 
    manifest file as:

```javascript 
//= require slider
```

* If a file that you want to include in your app is contained in **another** sub-directory, for 
example `app/assets/javascripts/parm/slider.js`, is referenced a bit differently:

```javascript 
//= require parm/slider
```

* Sprockets use manifest files to differentiate which files to include and serve, these manifest
files contains **directives**: instructions that tell Sprockets which files to require in order
to build a single css or javascript file.
* In js files, sprocket directives begin with `//=`
* `require file` is used to tell sprockets to just require this single file (named file) in your app
* `require_tree .` is used to tell sprockets to recursively **include all javascript files and all
directories into the current directory**, these files are loaded alphabetically from top to bottom!
* A css **manifest** file will contain the following:

```css
/*
*= require_self
*= require_tree .
*/
```

* `require_tree` means the same for css as it does for js files, include **all** stylesheet
files that are located in the current directory.
* `require_self` means to load any css that is contained in the same manifest file at the exact
moment the `require_self` is called! (In summit app, in the css manifest file: application.css,
you are loading sass variables and @importing sass files, these will be loaded at the time and
location as when `require_self` was called)

* `require_directory .` requires every file in the current directory
* `require_directory ./parm` requires all the files in a folder called parm in the current
directory
* `stub filename` ignores file named filename
* More [info](https://github.com/rails/sprockets)

以上是关于markdown Rails Asset Pipeline:`require`指令的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Rails 4 中指定asset_host?

sass-rails 助手“image-url”、“asset-url”在 rails 3.2.1 中不起作用

在 Rails 6 中,如何在 js.erb 文件中使用asset_path?

Rails Asset Pipeline 预编译,如何正确执行?

如何加快 Rails Asset Pipeline 预编译过程?

禁用 Asset Pipeline/Sprockets Rails 4.1