hearsay/require-js-bundle
最新稳定版本:v1.0.6
Composer 安装命令:
composer require hearsay/require-js-bundle
包简介
This bundle provides integration of the RequireJS library into Symfony2
README 文档
README
HearsayRequireJSBundle
Overview
This bundle provides integration of the RequireJS library into Symfony2.
Installation
1. Using Composer (recommended)
To install HearsayRequireJSBundle with Composer just add the following to
your composer.json file:
{
// ...
"require": {
// ...
"hearsay/require-js-bundle": "2.0.*@dev"
// ...
}
// ...
}
Note that the
masterbranch is under development and unstable yet. If you want to use stable version then specify the1.0.*version. However, remember that the1.0branch no longer provides new features, only bug fixes.
Then, you can install the new dependencies by running Composer's update command
from the directory where your composer.json file is located:
$ php composer.phar update hearsay/require-js-bundle
Now, Composer will automatically download all required files, and install them
for you. All that is left to do is to update your AppKernel.php file, and
register the new bundle:
<?php // in AppKernel::registerBundles() $bundles = array( // ... new Hearsay\RequireJSBundle\HearsayRequireJSBundle(), // ... );
Configuration
This bundle configured under the hearsay_require_js key in your application
configuration. This includes settings related to paths, shim, optimization, and
more.
require_js_src
type: string default: //cdnjs.cloudflare.com/ajax/libs/require.js/2.1.8/require.min.js
This is a string that represents the template name which will render the RequireJS src or an URL to the RequireJS.
initialize_template
type: string default: HearsayRequireJSBundle::initialize.html.twig
This is a string that represents the template name which will render the RequireJS initialization output. You can pass into this template any extra options.
base_url
type: string default: js
This is a string that represents the base URL where assets are served, relative to the website root directory. This URL also will exposed as an empty namespace.
base_dir
type: string default: null required
This is a string that represents the base URL for the r.js optimizer, that is generally actually a filesystem path.
paths
type: array default: null
This is a prototype that represents an array of paths. Each path:
- includes the location and the key that determines whether the path is an external;
- the location can be a string as well as an array of values (paths fallbacks);
- the location can expose a file as well as a directory, if the location is a
file then MUST NOT ends with the
.jsfile extension; - the location can referred to the files using a path like
@AcmeDemoBundle/Resources/public/js/src/modules; - by default, is not an external;
- if specified as an external, will marked as an empty for the r.js optimizer;
- will exposed as a namespace.
shim
type: array default: null
This is a prototype that represents an array of shim, corresponds to the RequireJS shim config.
options
type: array default: null
An array of key-value pairs to pass to the RequireJS. The value can have variable type.
optimizer
type: array default: null
This block includes the r.js optimizer configuration options.
path
type: string default: null
This is a string that represents the path to the r.js optimizer.
hide_unoptimized_assets
type: boolean default: false
This determines whether the r.js optimizer should suppress unoptimized files.
exclude
type: array default: []
An array of module names to exclude from the build profile.
modules
type: array default: null
This is a prototype that represents an array of modules options for r.js optimizer. Each array must contain name of the module and could contain include and exclude options that explicitly includes/excludes defined dependencies. See multipage module config for more details.
options
type: array default: null
An array of key-value pairs to pass to the r.js optimizer. The value can have variable type.
timeout
type: integer default: 60
This determines the node.js process timeout, in seconds.
almond_path
type: string default: false
This is a string that represents the path to almond.js.
declare_module_name
type: boolean default: false
This is a boolean that configures module declarations as their actual module name or if it will be an md5 representation.
Full Default Configuration
hearsay_require_js: require_js_src: //cdnjs.cloudflare.com/ajax/libs/require.js/2.1.8/require.min.js initialize_template: HearsayRequireJSBundle::initialize.html.twig base_url: js base_dir: ~ # Required paths: # Prototype path: location: ~ # Required external: false shim: # Prototype name: deps: [] exports: ~ options: # Prototype name: value: ~ # Required optimizer: path: ~ # Required hide_unoptimized_assets: false almond_path: ~ exclude: [] modules: # Prototype name: include: ~ exclude: ~ options: # Prototype name: value: ~ # Required timeout: 60
Usage
Just output the RequireJS initialization and load files normally:
{{ require_js_initialize() }}
<script type="text/javascript">require(['demo/main'])</script>
Alternately, you can specify a file to be required immediately via the
data-main attribute:
{{ require_js_initialize({ 'main' : 'demo/main' }) }}
If you need to do anything fancy with the configuration, you can do so manually by modifying the default configuration, and suppressing config output when initializing RequireJS:
<script type="text/javascript">
var require = {{ require_js.config|json_encode|raw }};
require.ready = function(){console.log('DOM ready');};
</script>
{{ require_js_initialize({ 'configure' : false }) }}
To make global changes to the configuration/initialization output, you can specify an initialization template in your configuration:
# app/config/config.yml hearsay_require_js: initialize_template: ::initialize_require_js.html.twig
To use this bundle with CoffeeScript, you can specify a path that contains
.coffee scripts. The scripts will renamed to .js scripts.
Note that the
r.jsoptimizer cannot optimize.coffeescripts. However, you can apply the Assetic filter to this scripts by the file extension.
Optimization
This bundle provides the Assetic filter to create minified Javascript files using by r.js optimizer. This also inlines any module definitions required by the file being optimized. You need to provide a path to the r.js optimizer in your configuration to use the filter:
# app/config/config.yml hearsay_require_js: optimizer: path: %kernel.root_dir%/../r.js
You can then use it like any other filter; for example, to optimize only in production:
{% javascripts
filter='?requirejs'
'@AcmeDemoBundle/Resources/public/js/src/main.js'
%}
{{ require_js_initialize({ 'main' : asset_url }) }}
{% endjavascripts %}
Note that your configured path definitions will be incorporated into the optimizer filter, including the exclusion of external dependencies from the built profile's file.
If you wish to prevent unoptimized assets from being served (in e.g. production), you can suppress them:
# app/config/config.yml hearsay_require_js: optimizer: path: %kernel.root_dir%/../r.js hide_unoptimized_assets: true
If you're doing this, be sure that all the modules you need are bundled into your optimized assets (i.e. you're not accessing any modules by dynamic name, or if you are, then you're explicitly including those modules via optimizer options), otherwise you may see certain assets available in development, but not in production.
Testing
$ make test
Contributing
Please see CONTRIBUTING for details.
Credits
License
This bundle is released under the MIT license. See the complete license in the bundle:
Resources/meta/LICENSE
hearsay/require-js-bundle 适用场景与选型建议
hearsay/require-js-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 149.68k 次下载、GitHub Stars 达 130, 最近一次更新时间为 2012 年 04 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「javascript」 「requirejs」 「require」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 hearsay/require-js-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 hearsay/require-js-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 hearsay/require-js-bundle 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Generates a Blade directive exporting all of your named Laravel routes. Also provides a nice route() helper function in JavaScript.
Caching and compression for Twig assets (JavaScript and CSS).
A pretty nice way to expose your translation messages to your JavaScript.
Vue.js Framework for TYPO3 Backend Modules
Zend Framework 2 RequireJS module
This Composer plugin adds "require-ext" command which makes using of private repositories and packages smoother.
统计信息
- 总下载量: 149.68k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 132
- 点击次数: 19
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2012-04-04