Make JS Preprocessing Pluggable
mfer - December 31, 2008 - 16:45
| Project: | Drupal |
| Version: | 8.x-dev |
| Component: | javascript |
| Category: | feature request |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | needs work |
| Issue tags: | JavaScript, Patch Spotlight |
Description
Currently drupal_build_js_cache() preforms the JavaScript preprocessing to roll all of the js files into one file. While the method in core works well for most sites there are cases where developers would like to use other methods. For example, see the sf_cache module.
So, let's make JavaScript preprocessing a pluggable system like #259103: make pluggable password hashing framework more generic and use class auto-loading. and #331180: fix pluggable smtp/mail framework. Depending on when this goes in we may want to take advantage of handles in core. See http://www.garfieldtech.com/blog/handlers-rfc-2. I don't know of an actually issue for this, yet.

#1
Here is my first pass at a pluggable JavaScript preprocessor.
#2
Updated the interface name to JSPreprocessingInterface for consistency and fixed some comments.
#3
Looks pretty good to me. Some formatting issues:
+ *+ * @param $files
+ * An array of JavaScript files.
+ *
+ * @return
+ * html for preprocessed JavaScript files.
need to indent two spaces under the @s. That goes for just about all of the PHPDocs.
I know these bits are just getting moved but that's no reason not to improve them:
+ // Create the js/ within the files folder.I'd say "Ensure there's a js directory with in the files directory that it is writable."
+ file_check_directory($jspath, FILE_CREATE_DIRECTORY);I think you should also make it writable:
+ file_check_directory($jspath, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);#4
Thanks for the feedback, drewish. Updated.
#5
The last submitted patch failed testing.
#6
Previous patch still applies with no issues for me.
#7
Any chance you can name preprocessing to something else? The theme system has kinda taken over that name.
#8
@moshe weitzman - I'm open to a name change. Drupal 6 already uses preprocess when refering to JavaScript so it would be a name change from an existing convention. Though, this topic is out of the scope of this patch which is just to take an existing system (with an existing name) and make it pluggable. It's primarily a re-factoring of code.
#9
Stuck up a note about this issue in the JavaScript Aggregator module: #356596: Port to Drupal 7.
Would be nice to have a test case. I'll see what I can do....
#10
Had to make the preprocessor able to lazy load classes from different files. This meant moving JSPreprocessingInterface to common.inc, and changing "preprocess_js_system" to:
<?phparray(
'name' => 'DrupalPreprocessJS',
'file' => 'includes/preprocess.inc'
)
?>
....When the preprocessor is loaded, it will check "file" and include that file before creating the interface.
As for the tests, I got pretty far, but couldn't get it to work all the time. Help appreciated.
#11
....... Forgot the patch.
#12
@rob - thats D6 style code. We no longer have to specify files, with the code registry.
#13
Why are we telling it what file to load? The registry takes care of loading files for classes and interfaces. If it's in the includes folder it just finds it. If it's a module you define what module files to scan. See http://drupal.org/node/224333#registry and http://api.drupal.org/api/group/registry/7.
#14
Fixed the tests, but am having troubles with the registry. Assistance would be nice.
#15
Ahhh, had to add common.test to the registry.
#16
I updated the test to remember the configured class for the JS Preprocessing and restore it after the test.
2 Questions:
#17
I'd love for it to be referenced somehow else, but can't think of anything since it's running on a different process and the class isn't loaded. Any other ideas would be awesome.
I don't think it really matters, but we could move it to a different class if that's prefered.
#18
The attached patch moves including common.test to a javascript_test module in the tests folder. The javascript_test.info file includes the module. The javascript_test.module file is a dummy file. If we change core to not require module files this can go away. The setup function for the javascript tests enables this module.
#19
This is looking pretty much RTBC, but Crell just posted #363787: Plugins: Swappable subsystems for core, so I think we should see how it grows...
#20
Great work! The same should be applied to CSS preprocessing.
Subscribing.
#21
#22
nice, subscribing
#23
I don't think the plugins/handlers system will make it into D7. So, here is an update to the previous series of patches that store the class in a variable.
This is the overall layout of the functionality but the tests could use some work.
#24
The last submitted patch failed testing.
#25
Rerolled the patch. Didn't test it, just a straight re-roll.
#26
The last submitted patch failed testing.
#27
Sub
#28