Hi Drupal folks,
Couple of moths ago i came across a rare issue with the JsOptimizer the generated .JS file was empty.
After a few hours of debugging i came across the JsOptimizer::clean regex and saw that it gave NULL back as return.
$contents = preg_replace('/\/\/(#|@)\s(sourceURL|sourceMappingURL)=\s*(\S*?)\s*$/m', '', $contents);
No errors or notices in the logging what so ever, but i guess it was the "Backtracking limit" that the issue because there where a lot of javascript files that was optimised to one file. So i decide to rewrite the preg_replace to this one:
$contents = preg_replace('/\/\/(#|@)\s(sourceURL|sourceMappingURL)=\s*[^$\s]*\s*$/m', '', $contents);
And everything was working, i didn't have a test case with sourceURL|sourceMappingURL so thats needs to be tested if the preg_replace is working. I added the patch in this issue that we are using right now with success.
PHP Version: 7.1.x
Drupal version: 8.5.x
| Comment | File | Size | Author |
|---|---|---|---|
| #24 | 2990907-24-TEST-ONLY.patch | 2.11 KB | joseph.olstad |
| #24 | 2990907-24.patch | 2.75 KB | joseph.olstad |
| #13 | preg_replace-jsoptimizer-return-null-2990907-10-TEST-ONLY-FAIL.patch | 2.11 KB | codebymikey |
| #10 | preg_replace-jsoptimizer-return-null-2990907-10.patch | 2.75 KB | codebymikey |
Issue fork drupal-2990907
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
lennard westerveldComment #3
lennard westerveldComment #5
heddnYes, this is an issue. I can confirm it. In my case, I had several minified vendor js files that all had source maps. This fixed it. Still needs tests. So back to NW.
Comment #9
codebymikey commentedDid some sandboxing over various regexes, and the best way to address this is to do away with any backtracking whatsoever.
Source: https://stackoverflow.com/a/36629670
And sandbox for various regexes: https://3v4l.org/JagL4
Comment #10
codebymikey commentedAttached a patch and test for the new non-backtracking regex.
Comment #11
codebymikey commentedComment #13
codebymikey commentedUse an appropriately named test-only-fail patch so as not to trigger the "Needs work" tag.
Edit: This was a misconception on my part, Drupal apparently doesn't have a convention for skipping the "Needs work" tag trigger.
Comment #15
codebymikey commentedComment #16
codebymikey commentedThe proposed patch breaks the page if the javascript happens to contain:
e.g. https://cdn.jsdelivr.net/npm/lodash@4.17.21/lodash.min.js
The regex needs to only match against the last line rather than attempt to match potential sourceMaps located in the middle of the code.
Comment #17
codebymikey commentedSee #2400287-50: Remove all occurences of sourceMappingURL and sourceURL when JS files are aggregated
Comment #18
szeidler commentedI can confirm the issue and the observation in #16 when applying the patch.
Edit: In my case I was able to fix it, by running webpack in production mode, so that sourcemaps have not been created. But that only works as long you have problem in your own scope. The general problem still exists. In my case it was also the lodash library.
Comment #22
notfloran commentedI confirm the observation of szeidler, after running webpack in production mode the problem is gone.
Comment #24
joseph.olstadstraight reroll
Comment #25
joseph.olstadRTBC:
has tests, passes tests
tests only fails
Related issues are linked
Comment #27
catchOverall this looks good but the regexp is trying to find both sourceURL and sourceMappingURL so I think we should probably have coverage for both?
It could be an extra method with nearly identical content or maybe one method with a data provider.
Comment #28
codebymikey commentedI think we'd ideally also have test coverage for the issue raised in #16 when the source code itself contains
//# sourceURL=in a string, but should not be stripped.Comment #29
codebymikey commentedComment #30
catch@codebymikey yes I think that's needed too.
Comment #31
catchActually I wonder if this code is needed at all with Peast - if it strips this already, then we don't need the regexp?
Comment #34
dieterholvoet commentedI rebased the last patch and added it to a MR.