內容簡介
WP CoffeeScript是一個WordPress外掛,讓Enqueue CoffeeScript 的過程,跟Enqueue JavaScript一樣簡單。你只需要使用enqueue_coffeescript()而不是wp_enqueue_script(),並且使用方式也幾乎一模一樣的參數。唯一的區別是第二個參數應該是檔案的路徑,而非網址。如果你想要編譯多個CoffeeScript檔案成為一個JS檔案,可以使用檔案路徑的陣列作為第二個參數。
請注意,CoffeeScript執行檔必須安裝在伺服器上。你也可以設定自訂的執行檔路徑(參考範例)。
如果你想取得發行版,查看新功能或瀏覽原始碼,請參閱GitHub存儲庫。
Examples
將位於佈景主題目錄中的CoffeeScript檔案排入佇列:
enqueue_coffeescript('my-handle', get_template_directory().'/my-script.coffee');
排入多個CoffeeScript檔案,編譯成為一個單一的輸出檔案:
$script1 = get_template_directory().'/script1.coffee';
$script2 = get_template_directory().'/script2.coffee';
enqueue_coffeescript('my-handle', array($script1, $script2));
在頁腳排入具有相依性的CS檔案(參數完全與wp_enqueue_script()相同):
enqueue_coffeescript('my-handle', get_template_directory().'/my-script.coffee', array('dep1', 'dep2'), false, true);
設定CS執行檔的自訂路徑(預設值為coffeescript):
add_filter('wpcs_executable', 'set_wpcs_executable');
function set_wpcs_executable($path) {
return '/my/path/to/coffeescript';
}
在每次頁面載入時編譯CS(預設行為僅在修改JS時編譯):
add_filter('wpcs_caching_enabled', 'disable_wpcs_caching');
function disable_wpcs_caching($is_enabled) {
return false;
}
外掛標籤
開發者團隊
原文外掛簡介
WP CoffeeScript is a WordPress plugin that makes enqueueing CoffeeScript as easy as enqueueing JavaScript. Instead of using wp_enqueue_script(), as you would for JS, you just use enqueue_coffeescript(), which takes almost exactly the same arguments. The only difference is that the second argument should be the file path instead of the URL. If you’d like to compile multiple CS files into a single JS file, you can use an array of file paths as the second argument.
Please note that the CoffeeScript executable must be installed on the server. You can also set a custom path to the executable (see the examples).
If you’d like to grab development releases, see what new features are being added, or browse the source code please visit the GitHub repo.
Examples
Enqueue a CoffeeScript file that’s in the theme directory:
enqueue_coffeescript('my-handle', get_template_directory().'/my-script.coffee');
Enqueue multiple CS files, compiling them into a single output file:
$script1 = get_template_directory().'/script1.coffee';
$script2 = get_template_directory().'/script2.coffee';
enqueue_coffeescript('my-handle', array($script1, $script2));
Enqueue a CS file in the footer with dependencies (the arguments are exactly the same as in wp_enqueue_script()):
enqueue_coffeescript('my-handle', get_template_directory().'/my-script.coffee', array('dep1', 'dep2'), false, true);
Set a custom path to the CS executable (the default value is coffeescript):
add_filter('wpcs_executable', 'set_wpcs_executable');
function set_wpcs_executable($path) {
return '/my/path/to/coffeescript';
}
Compile the CS on every page load (the default behavior is to only compile when the JS has been modified):
add_filter('wpcs_caching_enabled', 'disable_wpcs_caching');
function disable_wpcs_caching($is_enabled) {
return false;
}
