Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #11 from craigmarvelley/build-profile-configuration

Added a build profile configuration option
  • Loading branch information...
commit 7c92e5e9f33b9ae02a2372ae2aa3704f6845de5b 2 parents f050e1f + 341329c
Kevin Montag kmontag authored
25 DependencyInjection/Configuration.php
View
@@ -3,22 +3,22 @@
/**
* Copyright (c) 2011 Hearsay News Products, Inc.
*
- * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
- * The above copyright notice and this permission notice shall be included in
+ * The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
@@ -78,6 +78,9 @@ public function getConfigTreeBuilder()
->cannotBeEmpty()
->isRequired()
->end()
+ ->scalarNode('build_profile')
+ ->defaultNull()
+ ->end()
->booleanNode('hide_unoptimized_assets')
->defaultFalse()
->end()
9 DependencyInjection/HearsayRequireJSExtension.php
View
@@ -62,13 +62,18 @@ public function load(array $configs, ContainerBuilder $container)
// Set optimizer options
$container->setParameter('hearsay_require_js.r.path', $this->getRealPath($config['optimizer']['path'], $container));
+ $filter = $container->getDefinition('hearsay_require_js.optimizer_filter');
+
+ if (isset($config['optimizer']['build_profile'])) {
+ $buildProfile = $this->getRealPath($config['optimizer']['build_profile'], $container);
+ $filter->addMethodCall('setBuildProfile', array($buildProfile));
+ }
foreach ($config['optimizer']['excludes'] as $exclude) {
- $filter = $container->getDefinition('hearsay_require_js.optimizer_filter');
$filter->addMethodCall('addExclude', array($exclude));
}
foreach ($config['optimizer']['options'] as $name => $settings) {
$value = $settings['value'];
- $container->getDefinition('hearsay_require_js.optimizer_filter')->addMethodCall('setOption', array($name, $value));
+ $filter->addMethodCall('setOption', array($name, $value));
}
} else {
// If the optimizer config isn't provided, don't provide the filter
51 Filter/RequireJSOptimizerFilter.php
View
@@ -50,6 +50,11 @@ class RequireJSOptimizerFilter implements FilterInterface
*/
protected $baseUrl = null;
/**
+ * Path to an optional build profile containing optimizer configuration
+ * @var string
+ */
+ protected $buildProfile = null;
+ /**
* Extension to add to temporary files for processing.
* @var string
*/
@@ -78,6 +83,14 @@ public function __construct($nodePath, $rPath, $baseUrl)
}
/**
+ * @param string $path
+ */
+ public function setBuildProfile($path)
+ {
+ $this->buildProfile = $path;
+ }
+
+ /**
* Set a RequireJS path which should be excluded from the build.
* @param string $path The excluded path.
*/
@@ -139,18 +152,30 @@ public function filterDump(AssetInterface $asset)
$name = md5($input);
$pb = new $pb_class();
$pb
- ->add($this->nodePath)
- ->add($this->rPath)
- ->add('-o') // Optimize
- // Configure the primary input
- ->add('paths.' . $name . '=' . $input)
- ->add('name=' . $name)
-
- // Configure the output
- ->add('out=' . $output)
-
- // Configure the input base URL
- ->add('baseUrl=' . $this->baseUrl)
+ ->add($this->nodePath)
+ ->add($this->rPath)
+ ->add('-o') // Optimize
+ ;
+
+ // Build profile path, if set, needs to be provided after the optimize flag
+ if ($this->buildProfile) {
+ $buildProfile = $this->buildProfile;
+ if (! file_exists($buildProfile)) {
+ throw new \RuntimeException("Build profile does not exist at ".$buildProfile);
+ }
+ $pb->add($buildProfile);
+ }
+
+ $pb
+ // Configure the primary input
+ ->add('paths.' . $name . '=' . $input)
+ ->add('name=' . $name)
+
+ // Configure the output
+ ->add('out=' . $output)
+
+ // Configure the input base URL
+ ->add('baseUrl=' . $this->baseUrl)
;
$excludesString = '';
@@ -181,7 +206,7 @@ public function filterDump(AssetInterface $asset)
if (!$proc->isSuccessful()) {
$message = "Optimization failed";
-
+
$output = $proc->getErrorOutput();
if (strlen($output) === 0) {
$output = $proc->getOutput();
21 Resources/doc/index.rst
View
@@ -23,7 +23,7 @@ To install the bundle manually:
));
3. Add the bundle to your kernel::
-
+
// app/AppKernel.php
public function registerBundles()
{
@@ -38,7 +38,7 @@ Configuration
=============
You can expose directories of Javascript modules for access via ``require``.
-You must expose one root directory (from which files will be ``require``'d by
+You must expose one root directory (from which files will be ``require``'d by
default), and you may expose as many additional namespaces as you like. Given a
directory structure like::
@@ -128,7 +128,7 @@ Optimization
============
The bundle provides an Assetic filter to create minified Javascript files using
-the RequireJS optimizer. This also inlines any module definitions required by
+the RequireJS 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::
@@ -150,9 +150,20 @@ Note that your configured path definitions will be incorporated into the
optimizer filter, including the exclusion of external dependencies from the
built file.
+If you wish to provide configuration using a `build profile <http://github.com/jrburke/r.js/blob/master/build/example.build.js>`_::
+
+ # app/config/config.yml
+ hearsay_require_js:
+ optimizer:
+ path: /path/to/r.js
+ build_profile: /path/to/app.build.js # Build profile location (filename is arbitrary)
+ options: { skipModuleInsertion: true } # Additional options to pass to the optimizer (optional)
+
+Note that any command line options will take precedence over matching corresponding build profile configuration.
+
If you wish to prevent unoptimized assets from being served (in e.g. a
production environment), you can suppress them::
-
+
# app/config/config.yml
hearsay_require_js:
optimizer:
@@ -162,5 +173,5 @@ production environment), you can suppress them::
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
+options) - otherwise, you may see certain assets available in development, but
not production.
24 Tests/DependencyInjection/HearsayRequireJSExtensionTest.php
View
@@ -68,7 +68,7 @@ public function testNamespacesMapped()
// Check the namespace mapping
$mapping = $container->getDefinition('hearsay_require_js.namespace_mapping');
$methods = $mapping->getMethodCalls();
-
+
$this->assertEquals(3, count($methods), 'Incorrect number of method calls on namespace mapping');
$this->assertContains(array(
'registerNamespace', array($namespace_dir, 'namespace', true),
@@ -187,6 +187,28 @@ public function testOptimizerOptionsSet()
), $methods, 'Did not find expected method call');
}
+ public function testOptimizerBuildProfileSet()
+ {
+ $config = array(
+ 'base_directory' => '/home/user/base',
+ 'optimizer' => array(
+ 'path' => '/path/to/r.js',
+ 'build_profile' => 'path/to/app.build.js'
+ ),
+ );
+ $container = $this->getContainerBuilder();
+ $loader = new HearsayRequireJSExtension();
+
+ $loader->load(array($config), $container);
+
+ $optimizer = $container->getDefinition('hearsay_require_js.optimizer_filter');
+ $methods = $optimizer->getMethodCalls();
+ $this->assertEquals(1, count($methods), 'Incorrect number of method calls on optimizer');
+ $this->assertContains(array(
+ 'setBuildProfile', array('path/to/app.build.js'),
+ ), $methods, 'Did not find expected method call');
+ }
+
public function testExceptionOnUnrecognizedBundle()
{
$config = array(
Please sign in to comment.
Something went wrong with that request. Please try again.