Skip to content

Commit aba4156

Browse files
authored
Merge pull request #1023 from Kit/refactor-frontend-scss
Refactor Frontend SCSS
2 parents 1c6e21f + ae06fdc commit aba4156

20 files changed

Lines changed: 515 additions & 403 deletions

DEVELOPMENT.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,17 @@ Typically, packages listed in this section would be internal development tools f
6060
- PHPStan
6161
- Codeception
6262

63+
### Build
64+
65+
Run `npm run build` to compile frontend CSS and JS in one step.
66+
6367
### CSS
6468

6569
Run `npm run watch:css` to compile frontend CSS to `resources/frontend/css/frontend.css` when working on SCSS in the `resources/frontend/scss` folder.
6670

67-
`resources/frontend/css/frontend.css` is excluded from the repo by `.gitignore`; it is built before tests and deployment automatically using GitHub actions.
71+
### JS
72+
73+
Run `npm run watch:js` to compile frontend JS to `resources/frontend/js/dist/frontend-min.js` when working on JS in the `resources/frontend/js` folder.
6874

6975
## Committing Work
7076

SETUP.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ In the Plugin's directory, at the command line, run `npm install`.
9494
This sets up:
9595
- JS linting / coding standards using WordPress recommended configurations (https://developer.wordpress.org/block-editor/reference-guides/packages/packages-scripts/#lint-js)
9696
- SCSS and CSS linting / coding standards using WordPress recommended configurations (https://developer.wordpress.org/block-editor/reference-guides/packages/packages-scripts/#lint-style)
97-
- SASS compilation
97+
- JS compilation and minification
98+
- SASS compilation and minification
9899

99100
### Configure wp-config.php
100101

TESTING.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,23 @@ The following Composer commands can be used:
3232
| `composer watch-css` | `composer watch-css` | Builds the frontend CSS file when changes are made to SCSS files |
3333
| `composer js-coding-standards` | `composer lint-js` | Runs WordPress JS Coding Standards on JS files |
3434
| `composer fix-js-coding-standards` | `composer fix-js` | Fixes JS files to meet WordPress JS Coding Standards |
35+
| `composer build-js` | `composer build-js` | Builds the frontend JS file |
36+
| `composer watch-js` | `composer watch-js` | Builds the frontend JS file when changes are made to frontend JS files |
37+
| `composer build` | `composer build` | Builds frontend CSS and JS |
3538
| `composer static-analysis` | `composer phpstan` | Runs PHPStan static analysis with increased memory limit |
3639
| `composer test` | `composer test` | Builds and runs end-to-end tests with `fail-fast` enabled |
3740
| `composer test-integration` | `composer test-integration` | Builds and runs integration tests with `fail-fast` enabled |
3841

42+
The following npm commands can be used, if preferred:
43+
44+
| Command | Description |
45+
|---------|-------------|
46+
| `npm run build:css` | Builds the frontend CSS file |
47+
| `npm run watch:css` | Builds the frontend CSS file when changes are made to SCSS files |
48+
| `npm run build:js` | Builds the frontend JS file |
49+
| `npm run watch:js` | Builds the frontend JS file when changes are made to frontend JS files |
50+
| `npm run build` | Builds frontend CSS and JS |
51+
3952
## Write (or modify) a test
4053

4154
If your work creates new functionality, write a test.

composer.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,14 @@
3131
"watch-css": "npm run watch:css",
3232
"lint-css": "npm run lint:css",
3333
"fix-css": "npm run fix:css",
34+
"build-js": "npm run build:js",
35+
"watch-js": "npm run watch:js",
3436
"lint-js": "npm run lint:js",
3537
"fix-js": "npm run fix:js",
38+
"build": [
39+
"npm run build:css",
40+
"npm run build:js"
41+
],
3642
"phpstan": "vendor/bin/phpstan analyse --memory-limit=1250M",
3743
"php-coding-standards": "vendor/bin/phpcs ./ -s -v",
3844
"fix-php-coding-standards": "vendor/bin/phpcbf ./ -s -v",

includes/blocks/class-convertkit-block-broadcasts.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,12 @@ public function enqueue_scripts() {
115115
// Get ConvertKit Settings.
116116
$settings = new ConvertKit_Settings();
117117

118-
wp_enqueue_script( 'convertkit-' . $this->get_name(), CONVERTKIT_PLUGIN_URL . 'resources/frontend/js/broadcasts.js', array(), CONVERTKIT_PLUGIN_VERSION, true );
118+
// Enqueue frontend JS.
119+
convertkit_enqueue_frontend_js();
120+
121+
// Define variables.
119122
wp_localize_script(
120-
'convertkit-' . $this->get_name(),
123+
'convertkit-js',
121124
'convertkit_broadcasts',
122125
array(
123126
// REST API URL endpoint.
@@ -127,6 +130,7 @@ public function enqueue_scripts() {
127130
'debug' => $settings->debug_enabled(),
128131
)
129132
);
133+
130134
}
131135

132136
/**

includes/class-convertkit-output-restrict-content.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1431,9 +1431,11 @@ private function get_call_to_action( $post_id ) { // phpcs:ignore Generic.CodeAn
14311431
// Only load scripts if the Disable Scripts option is off.
14321432
if ( ! $this->settings->scripts_disabled() ) {
14331433
// Enqueue scripts.
1434-
wp_enqueue_script( 'convertkit-restrict-content', CONVERTKIT_PLUGIN_URL . 'resources/frontend/js/restrict-content.js', array(), CONVERTKIT_PLUGIN_VERSION, true );
1434+
convertkit_enqueue_frontend_js();
1435+
1436+
// Define variables.
14351437
wp_localize_script(
1436-
'convertkit-restrict-content',
1438+
'convertkit-js',
14371439
'convertkit_restrict_content',
14381440
array(
14391441
'nonce' => wp_create_nonce( 'wp_rest' ),
@@ -1442,7 +1444,6 @@ private function get_call_to_action( $post_id ) { // phpcs:ignore Generic.CodeAn
14421444
'debug' => $this->settings->debug_enabled(),
14431445
)
14441446
);
1445-
14461447
}
14471448

14481449
// Output code form if this request is after the user entered their email address,

includes/class-convertkit-output.php

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -801,14 +801,10 @@ public function enqueue_scripts() {
801801
return;
802802
}
803803

804-
// Register scripts that we might use.
805-
wp_register_script(
806-
'convertkit-js',
807-
CONVERTKIT_PLUGIN_URL . 'resources/frontend/js/convertkit.js',
808-
array(),
809-
CONVERTKIT_PLUGIN_VERSION,
810-
true
811-
);
804+
// Enqueue frontend JS.
805+
convertkit_enqueue_frontend_js();
806+
807+
// Define variables.
812808
wp_localize_script(
813809
'convertkit-js',
814810
'convertkit',
@@ -820,9 +816,6 @@ public function enqueue_scripts() {
820816
)
821817
);
822818

823-
// Enqueue.
824-
wp_enqueue_script( 'convertkit-js' );
825-
826819
}
827820

828821
/**

includes/functions.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,23 @@ function convertkit_enqueue_frontend_css() {
540540

541541
}
542542

543+
/**
544+
* Helper method to enqueue the frontend JS file.
545+
*
546+
* @since 3.2.0
547+
*/
548+
function convertkit_enqueue_frontend_js() {
549+
550+
wp_enqueue_script(
551+
'convertkit-js',
552+
CONVERTKIT_PLUGIN_URL . 'resources/frontend/js/dist/frontend.min.js',
553+
array(),
554+
CONVERTKIT_PLUGIN_VERSION,
555+
true
556+
);
557+
558+
}
559+
543560
/**
544561
* Helper method to enqueue Select2 scripts for use within the ConvertKit Plugin.
545562
*

package.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@
55
"build:css": "sass resources/frontend/scss/frontend.scss resources/frontend/css/frontend.css --no-source-map --style=compressed",
66
"watch:css": "sass resources/frontend/scss/frontend.scss resources/frontend/css/frontend.css --watch --style=compressed",
77
"lint:css": "wp-scripts lint-style 'resources/frontend/scss/**/*.scss' 'resources/backend/css/**/*.css' --max-warnings=0",
8-
"fix:css": "wp-scripts lint-style 'resources/frontend/scss/**/*.scss' 'resources/backend/css/**/*.css' --fix",
9-
"lint:js": "wp-scripts lint-js --max-warnings=0",
10-
"fix:js": "wp-scripts lint-js --fix"
8+
"fix:css": "prettier --write 'resources/frontend/scss/**/*.scss' && wp-scripts lint-style 'resources/frontend/scss/**/*.scss' 'resources/backend/css/**/*.css' --fix",
9+
"build:js": "wp-scripts build resources/frontend/js/frontend.js --output-path=resources/frontend/js/dist --output-filename=frontend.min.js",
10+
"watch:js": "wp-scripts start resources/frontend/js/frontend.js --output-path=resources/frontend/js/dist --output-filename=frontend.min.js",
11+
"lint:js": "wp-scripts lint-js 'resources/frontend/js/*.js' --max-warnings=0",
12+
"fix:js": "wp-scripts lint-js 'resources/frontend/js/*.js' --fix",
13+
"build": "npm run build:css && npm run build:js"
1114
},
1215
"prettier": "@wordpress/prettier-config",
1316
"devDependencies": {

resources/frontend/css/frontend.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)