Skip to content

Commit

Permalink
[SFT-1409]: Reducing the file-size of freeform.js (#1641)
Browse files Browse the repository at this point in the history
* feat(SFT-1409): optimizing front-end js size

* feat(SFT-1409): optimizing all freeform scripts

* removing `axios` dependency
* removing `lodash` dependency
* removing `core-js` polyfills

* chore(SFT-1409): updated changelog

---------

Co-authored-by: kjmartens <[email protected]>
  • Loading branch information
gustavs-gutmanis and kjmartens authored Nov 19, 2024
1 parent 61dec48 commit 16a3297
Show file tree
Hide file tree
Showing 19 changed files with 449 additions and 119 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# Solspace Freeform Changelog

## 5.8.0 - Unreleased

### Changed
- Optimized all front end Freeform scripts.
- Removed the `axios` dependency.
- Removed the `lodash` dependency.
- Removed the `core-js` polyfills.
- Scripts for Calculation fields now load seperately as `calculation.js` only if the field exists in the form being displayed.
- When using `freeform.loadScripts()` (for manually loading scripts) and Calculation fields, be sure to include the `field.calculation` option.

## 5.7.3 - Unreleased

### Added
Expand Down
83 changes: 29 additions & 54 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export const FormUsage: React.FC = () => {

{!isFetching && data?.length === 0 && (
<EmptyBlock
title={translate("No results found")}
title={translate('No results found')}
subtitle={translate(
'This form is currently not attached to any elements.'
)}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace Solspace\Freeform\Bundles\Fields\Implementations\CalculationField;

use Solspace\Freeform\Events\Forms\CollectScriptsEvent;
use Solspace\Freeform\Events\Forms\RenderTagEvent;
use Solspace\Freeform\Fields\Implementations\Pro\CalculationField;
use Solspace\Freeform\Form\Form;
use Solspace\Freeform\Library\Bundles\FeatureBundle;
use yii\base\Event;

class CalculationFieldBundle extends FeatureBundle
{
private const SCRIPT_PATH = 'js/scripts/front-end/fields/calculation.js';

public function __construct()
{
Event::on(
Form::class,
Form::EVENT_COLLECT_SCRIPTS,
[$this, 'collectScripts'],
);

Event::on(
Form::class,
Form::EVENT_RENDER_BEFORE_CLOSING_TAG,
[$this, 'attachScripts'],
);
}

public function collectScripts(CollectScriptsEvent $event): void
{
$event->addScript('field.calculation', self::SCRIPT_PATH);
}

public function attachScripts(RenderTagEvent $event): void
{
if (!$event->isGenerateTag()) {
return;
}

$form = $event->getForm();
if (!$form->getLayout()->hasFields(CalculationField::class)) {
return;
}

$event->addScript(self::SCRIPT_PATH);
}
}
2 changes: 1 addition & 1 deletion packages/plugin/src/Resources/js/client/client.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/plugin/src/Resources/js/client/vendor.js

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,12 +1,3 @@
/**
* @license
* Lodash <https://lodash.com/>
* Copyright OpenJS Foundation and other contributors <https://openjsf.org/>
* Released under MIT license <https://lodash.com/license>
* Based on Underscore.js 1.8.3 <http://underscorejs.org/LICENSE>
* Copyright Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors
*/

/**
* filesize
*
Expand Down
5 changes: 0 additions & 5 deletions packages/scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@
"@babel/plugin-proposal-class-properties": "^7.18.6",
"@babel/preset-env": "^7.22.10",
"@babel/preset-typescript": "^7.22.11",
"@types/axios": "^0.14.0",
"@types/grecaptcha": "^3.0.4",
"@types/jquery": "^3.5.16",
"@types/lodash.isequal": "^4.5.8",
"babel-loader": "^9.1.3",
"browserslist": "^4.21.10",
"css-loader": "^6.8.1",
Expand All @@ -38,12 +36,9 @@
},
"dependencies": {
"@stripe/stripe-js": "^2.1.9",
"axios": "^1.5.0",
"clsx": "^2.0.0",
"core-js": "^3.33.2",
"expression-language": "^1.1.4",
"filesize": "^10.0.12",
"lodash.isequal": "^4.5.0",
"locutus": "^2.0.16"
}
}
Loading

0 comments on commit 16a3297

Please sign in to comment.