Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #65 from guilhermeblanco/declare-module-name

Implement a flag that controls if defines will be declared as their module names
  • Loading branch information...
commit e988f07ebefdea91de1df5a3871c6f27f7e65086 2 parents 3a1a414 + e7b3315
@IgorTimoshenko IgorTimoshenko authored
View
25 Assetic/Filter/RJsFilter.php
@@ -98,17 +98,26 @@ class RJsFilter extends BaseNodeFilter
protected $almondPath;
/**
+ * Flag that controls if defines will be declared as their module names
+ *
+ * @var boolean
+ */
+ protected $declareModuleName;
+
+ /**
* The constructor method
*
- * @param string $nodePath The absolute path to the node.js
- * @param string $rPath The absolute path to the r.js
- * @param string $baseUrl The base URL
+ * @param string $nodePath The absolute path to the node.js
+ * @param string $rPath The absolute path to the r.js
+ * @param string $baseUrl The base URL
+ * @param boolean $declareModuleName Flag that allows defines to be declared as their module names
*/
- public function __construct($nodePath, $rPath, $baseUrl)
+ public function __construct($nodePath, $rPath, $baseUrl, $declareModuleName)
{
- $this->nodePath = $nodePath;
- $this->rPath = $rPath;
- $this->baseUrl = $baseUrl;
+ $this->nodePath = $nodePath;
+ $this->rPath = $rPath;
+ $this->baseUrl = $baseUrl;
+ $this->declareModuleName = $declareModuleName;
}
/**
@@ -264,7 +273,7 @@ protected function makeBuildProfile($input, $output, $moduleName)
{
$buildProfile = tempnam(sys_get_temp_dir(), 'build_profile') . '.js';
- $name = md5($input);
+ $name = $this->declareModuleName ? $moduleName : md5($input);
// The basic build profile
$content = (object) array(
View
3  DependencyInjection/Configuration.php
@@ -128,6 +128,9 @@ public function getConfigTreeBuilder()
->booleanNode('hide_unoptimized_assets')
->defaultFalse()
->end()
+ ->booleanNode('declare_module_name')
+ ->defaultFalse()
+ ->end()
->arrayNode('exclude')
->defaultValue(array())
->prototype('scalar')
View
7 DependencyInjection/HearsayRequireJSExtension.php
@@ -56,6 +56,13 @@ public function load(array $configs, ContainerBuilder $container)
? false
: $config['optimizer']['hide_unoptimized_assets'];
+ // By using this flag as true, defines will be declared as their module names
+ $declareModuleName = !isset($config['optimizer']['declare_module_name'])
+ ? false
+ : $config['optimizer']['declare_module_name'];
+
+ $container->setParameter('hearsay_require_js.declare_module_name', $declareModuleName);
+
foreach ($config['paths'] as $path => $settings) {
$location = $settings['location'];
View
8 README.md
@@ -72,7 +72,7 @@ more.
* [options](#options)
* [timeout](#timeout)
* [almond_path](#almond_path)
-
+ * [declare_module_name](#declare_module_name)
#### *require_js_src* ####
@@ -189,6 +189,12 @@ This determines the node.js process timeout, in seconds.
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 ###
```yaml
View
1  Resources/config/services.yml
@@ -65,6 +65,7 @@ services:
- %assetic.node.bin%
- %hearsay_require_js.r.path%
- %hearsay_require_js.base_dir%
+ - %hearsay_require_js.declare_module_name%
calls:
- [ addNodePath, [%assetic.node.bin%] ]
tags:
View
51 Tests/Assetic/Filter/RJsFilterTest.php
@@ -25,6 +25,11 @@
class RJsFilterTest extends \PHPUnit_Framework_TestCase
{
/**
+ * @var string
+ */
+ private $nodePath;
+
+ /**
* @var RJsFilter
*/
private $filter;
@@ -36,10 +41,15 @@ public function setUp()
{
parent::setUp();
- $nodePath = $this->getNodePath();
+ $this->nodePath = $this->getNodePath();
+ $this->filter = new RJsFilter(
+ $this->nodePath,
+ __DIR__ . '/../../../r.js',
+ __DIR__ . '/modules/base',
+ false
+ );
- $this->filter = new RJsFilter($nodePath, __DIR__ . '/../../../r.js', __DIR__ . '/modules/base');
- $this->filter->addNodePath($nodePath);
+ $this->filter->addNodePath($this->nodePath);
}
/**
@@ -191,6 +201,41 @@ public function testOptimizerInclusionsIncluded()
}
/**
+ * Tests that optimizer correctly handles module includes
+ *
+ * @covers Hearsay\RequireJSBundle\Assetic\Filter\RJsFilter::filterDump
+ * @covers Hearsay\RequireJSBundle\Assetic\Filter\RJsFilter::addModule
+ * @covers Hearsay\RequireJSBundle\Assetic\Filter\RJsFilter::addOption
+ */
+ public function testOptimizerInclusionsIncludedWithModuleName()
+ {
+ $this->filter = new RJsFilter(
+ $this->nodePath,
+ __DIR__ . '/../../../r.js',
+ __DIR__ . '/modules/base',
+ true
+ );
+
+ $this->filter->addNodePath($this->nodePath);
+
+ $this->filter->addOption('preserveLicenseComments', false);
+ $this->filter->addOption('skipModuleInsertion', true);
+
+ // registering optimizer modules config
+ $this->filter->addModule("module/file2", array("include" => array("module/file")));
+
+ $asset = new FileAsset(__DIR__ . '/modules/base/module/file2.js');
+ $asset->ensureFilter($this->filter);
+
+ // expecting result contains both module and module2 although module 2 doesn't depend on module
+ $this->assertRegExp(
+ '/^define\("module\/file2",\{js:"got it twice"\}\),define\("module\/file",\{js:"got it"\}\);$/',
+ $asset->dump(),
+ 'Defined inclusions included incorrectly'
+ );
+ }
+
+ /**
* Tests that optimizer correctly excludes exclude-modules (configured in optimizer options for current module)
* and their includes
*
Please sign in to comment.
Something went wrong with that request. Please try again.