Skip to content

Commit eaeed47

Browse files
committed
feat: Added Prettier & imageminimizerPlugin
1 parent 2c7db24 commit eaeed47

12 files changed

+152
-38
lines changed

README.md

+18-7
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ ____
4444
4545
**Images**
4646

47-
>- [**ImageMinimizerWebpackPlugin**](https://webpack.js.org/plugins/image-minimizer-webpack-plugin/) for Minification/optimization of images
47+
>- [**ImageMinimizerWebpackPlugin**](https://webpack.js.org/plugins/image-minimizer-webpack-plugin/) + [**CopyWebpackPlugin**](https://webpack.js.org/plugins/copy-webpack-plugin/)
48+
to optimize (compress) all images using
4849
>- _File types: `.png`, `.jpg`, `.jpeg`, `.gif`, `.svg`_
4950
5051
**Translation**
@@ -58,6 +59,10 @@ ____
5859
>- [**BrowserSync**](https://browsersync.io/), synchronising URLs, interactions and code changes across devices and automatically refreshes all the browsers on al devices
5960
>- [**webPackBar**](https://github.com/nuxt/webpackbar) so you can get a real progress bar while development which also includes a **profiler**
6061
62+
**Configuration**
63+
>- All configuration files (`.eslintrc.js`, `.stylelintrc.js`, `babel.config.js`, `postcss.config.js`) are organised in a single folder
64+
>- The Webpack configuration is divided into 2 sub configuration files for the development and production build/environment
65+
6166
## Requirements
6267

6368
- Node.js
@@ -78,14 +83,16 @@ ____
7883
│ ├── wordpress-webpack.pot # Boilerplate POT File that gets overwritten by WP-Pot
7984
└──assets
8085
├── src # Holds all the source files
86+
│ ├── images # Uncompressed images
8187
│ ├── scss # Holds the SCSS files
82-
│ │ ├─ frontend.scss
83-
│ │ └─ backend.scss
88+
│ │ ├─ frontend.scss # For front-end styling
89+
│ │ └─ backend.scss # For back-end / wp-admin styling
8490
│ └── js # Holds the JS files
85-
│ ├─ frontend.js
86-
│ └─ backend.js
91+
│ ├─ frontend.js # For front-end scripting
92+
│ └─ backend.js # For back-end / wp-admin scripting
8793
8894
└── public
95+
├── images # Iptimized (compressed) images
8996
├── css # Compiled CSS files with be generated here
9097
└── js # Compiled JS files with be generated here
9198
```
@@ -125,8 +132,6 @@ Or run with watcher & browserSync
125132
```bash
126133
npm run prod:watch
127134
```
128-
This will open a browser, watch all files (php, scss, js, etc) and reload the
129-
browser when you press save.
130135

131136
## More Scripts/Tasks
132137

@@ -147,4 +152,10 @@ npm run stylelint
147152

148153
# To find fix problems in your sass/css code
149154
npm run stylelint:fix
155+
156+
# To make sure files in assets/src/js are formatted
157+
npm run prettier
158+
159+
# To fix and format the js files in assets/src/js
160+
npm run prettier:fix
150161
```

assets/src/images/gif-placeholder.gif

3.75 KB
Loading

assets/src/images/jpg-placeholder.jpg

1.34 KB
Loading

assets/src/images/png-placeholder.png

373 Bytes
Loading

assets/src/images/svg-placeholder.svg

+5
Loading

assets/src/js/backend.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* SASS
33
*/
4-
import '../sass/backend.scss';
4+
import "../sass/backend.scss";
55

66
/**
77
* JavaScript

assets/src/js/frontend.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* SASS
33
*/
4-
import '../sass/frontend.scss';
4+
import "../sass/frontend.scss";
55

66
/**
77
* JavaScript

package.json

+32-11
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,10 @@
11
{
22
"name": "wordpress-webpack-workflow",
3-
"version": "1.0.0",
3+
"version": "1.0.2",
44
"author": "WP-Strap",
55
"license": "MIT",
66
"homepage": "https://github.com/wp-strap/wordpress-webpack-workflow",
7-
"description": "WordPress webpack/postcss/tailwindcss workflow to use in WordPress projects (plugins or themes)",
8-
"keywords": [
9-
"wordpress",
10-
"workflow",
11-
"webpack",
12-
"theme",
13-
"plugin"
14-
],
7+
"description": "Modern WebPack workflow for WordPress front-end development and testing (plugins & themes) with handy tools included.\n",
158
"main": "index.js",
169
"repository": {
1710
"type": "git",
@@ -30,7 +23,9 @@
3023
"eslint:fix": "eslint assets/src/js/**/*.js --fix",
3124
"stylelint": "stylelint assets/src/sass/**/*.{css,scss}",
3225
"stylelint:fix": "stylelint assets/src/sass/**/*.{css,scss} --fix",
33-
"pot": "wp-pot --src '**/**/**/**/*.php' --dest-file 'languages/wordpress-webpack.pot' --package 'wordpress-webpack' --domain 'wordpress-webpack-text-domain' --last-translator '{{author_name}} <{{author_email}}>' --team '{{author_name}} <{{author_email}}>' --bug-report '{{author_url}}'"
26+
"prettier": "prettier --check assets/src/js/**/*.js",
27+
"prettier:fix": "prettier --write assets/src/js/**/*.js",
28+
"translate": "wp-pot --src '**/**/**/*.php' --dest-file 'languages/wordpress-webpack.pot' --package 'wordpress-webpack' --domain 'wordpress-webpack-text-domain' --last-translator '{{author_name}} <{{author_email}}>' --team '{{author_name}} <{{author_email}}>' --bug-report '{{author_url}}'"
3429
},
3530
"babel": {
3631
"extends": "./webpack/babel.config.js"
@@ -47,6 +42,9 @@
4742
"extends": [
4843
"./webpack/.stylelintrc.js"
4944
]
45+
},
46+
"prettier": {
47+
5048
},
5149
"dependencies": {},
5250
"devDependencies": {
@@ -59,10 +57,16 @@
5957
"babel-loader": "^8.2.2",
6058
"browser-sync": "^2.26.13",
6159
"browser-sync-webpack-plugin": "^2.3.0",
60+
"copy-webpack-plugin": "^7.0.0",
6261
"css-loader": "^5.0.1",
6362
"eslint": "^7.17.0",
6463
"eslint-webpack-plugin": "^2.4.1",
6564
"glob-all": "^3.2.1",
65+
"image-minimizer-webpack-plugin": "^2.2.0",
66+
"imagemin-gifsicle": "^7.0.0",
67+
"imagemin-jpegtran": "^7.0.0",
68+
"imagemin-optipng": "^8.0.0",
69+
"imagemin-svgo": "^8.0.0",
6670
"mini-css-extract-plugin": "^1.3.3",
6771
"node-sass-magic-importer": "^5.3.2",
6872
"postcss": "^8.2.4",
@@ -78,5 +82,22 @@
7882
"webpack-cli": "^4.3.1",
7983
"webpackbar": "^5.0.0-3",
8084
"wp-pot-cli": "^1.5.0"
81-
}
85+
},
86+
"keywords": [
87+
"wordpress",
88+
"workflow",
89+
"webpack",
90+
"theme",
91+
"plugin",
92+
"WebPack",
93+
"BrowserSync",
94+
"PostCSS",
95+
"Autoprefixer",
96+
"PurgeCSS",
97+
"BabelJS",
98+
"Eslint",
99+
"Stylelint",
100+
"SCSS",
101+
"WP-Pot"
102+
]
82103
}

webpack.config.js

+31-9
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,32 @@ const path = require( 'path' );
77

88
// Paths to find our files and provide BrowserSync functionality.
99
const projectPaths = {
10-
projectDir: __dirname, // Current project directory absolute path.
11-
projectJsPath: path.resolve( __dirname, 'assets/src/js' ),
12-
projectScssPath: path.resolve( __dirname, 'assets/src/scss' ),
13-
projectOutput: path.resolve( __dirname, 'assets/public' ),
14-
projectWebpack: path.resolve( __dirname, 'webpack' ),
10+
projectDir: __dirname, // Current project directory absolute path.
11+
projectJsPath: path.resolve( __dirname, 'assets/src/js' ),
12+
projectScssPath: path.resolve( __dirname, 'assets/src/scss' ),
13+
projectImagesPath: path.resolve( __dirname, 'assets/src/images' ),
14+
projectOutput: path.resolve( __dirname, 'assets/public' ),
15+
projectWebpack: path.resolve( __dirname, 'webpack' ),
1516
// BrowserSync settings
1617
browserSync: {
1718
enable: true, // enable or disable browserSync
1819
host: 'localhost',
19-
port: 3000,
20+
port: 3000,
2021
mode: 'proxy', // proxy | server
2122
server: { baseDir: [ 'public' ] }, // can be ignored if using proxy
2223
proxy: 'https://wp-strap.lndo.site',
2324
// BrowserSync will automatically watch for changes to any files connected to our entry,
2425
// including both JS and Sass files. We can use this property to tell BrowserSync to watch
2526
// for other types of files, in this case PHP files, in our project.
26-
files: '**/**/**.php',
27+
files: '**/**/**.php',
2728
reload: true, // Set false to prevent BrowserSync from reloading and let Webpack Dev Server take care of this
2829
// browse to http://localhost:3000/ during development,
2930
}
3031
};
3132

3233
// Javascript files to bundle
3334
const projectFiles = {
34-
projectJs: {
35+
projectJs: {
3536
babelJs: __dirname + '/webpack/.eslintrc.js',
3637
eslint: true, // enable or disable eslint | this is only enabled in development env.
3738
filename: 'js/[name].js',
@@ -45,7 +46,7 @@ const projectFiles = {
4546
}
4647
}
4748
},
48-
projectCss: {
49+
projectCss: {
4950
postCss: projectPaths.projectWebpack + '/postcss.config.js',
5051
stylelint: true, // enable or disable stylelint | this is only enabled in development env.
5152
filename: 'css/[name].css',
@@ -63,6 +64,27 @@ const projectFiles = {
6364
__dirname + '/*',
6465
]
6566
}
67+
},
68+
projectImages: {
69+
rules: {
70+
image: {
71+
test: /\.(jpe?g|png|gif|svg)$/i,
72+
}
73+
},
74+
minimizerOptions: {
75+
// Lossless optimization with custom option
76+
// Feel free to experiment with options for better result for you
77+
plugins: [
78+
[ 'gifsicle', { interlaced: true } ],
79+
[ 'jpegtran', { progressive: true } ],
80+
[ 'optipng', { optimizationLevel: 5 } ],
81+
[ 'svgo', {
82+
plugins: [
83+
{ removeViewBox: false, },
84+
],
85+
}, ],
86+
],
87+
}
6688
}
6789
}
6890

webpack/.prettierrc.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* Prettier config file
3+
* as configured in package.json under eslintConfig.extends
4+
*
5+
* @docs Prettier: https://prettier.io/
6+
* @since 1.0.0
7+
*/
8+
module.exports = {
9+
trailingComma: "es5",
10+
tabWidth: 4,
11+
semi: false,
12+
singleQuote: true,
13+
};

webpack/config.development.js

+27-5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
* @type MiniCssExtractPlugin : Extracts the CSS files into public/css https://webpack.js.org/plugins/mini-css-extract-plugin/
99
* @type BrowserSyncPlugin : Synchronising URLs, interactions and code changes across devices https://github.com/Va1/browser-sync-webpack-plugin
1010
* @type WebpackBar : Display elegant progress bar while building or watch https://github.com/nuxt-contrib/webpackbar
11+
* @type ImageMinimizerPlugin : To optimize (compress) all images using https://webpack.js.org/plugins/image-minimizer-webpack-plugin/
12+
* @type CopyPlugin : For WordPress we need to copy images from src to public to be able to optimize them https://webpack.js.org/plugins/copy-webpack-plugin/
1113
*
1214
* Only in development environment:
1315
* @type ESLintPlugin : Find and fix problems in your JavaScript code https://eslint.org/
@@ -19,6 +21,8 @@ const ESLintPlugin = require( 'eslint-webpack-plugin' );
1921
const StylelintPlugin = require( 'stylelint-webpack-plugin' )
2022
const BrowserSyncPlugin = require( 'browser-sync-webpack-plugin' )
2123
const WebpackBar = require( 'webpackbar' );
24+
const ImageMinimizerPlugin = require( 'image-minimizer-webpack-plugin' );
25+
const CopyPlugin = require( "copy-webpack-plugin" );
2226

2327
module.exports = ( projectOptions ) => {
2428
// Set environment level to 'development'
@@ -54,25 +58,43 @@ module.exports = ( projectOptions ) => {
5458
use: 'babel-loader' // Configurations in "webpack/babel.config.js"
5559
};
5660

61+
// Images rules
62+
const imageRules = {
63+
test: projectOptions.projectImages.rules.image.test,
64+
use: [
65+
{
66+
loader: 'file-loader',// Or `url-loader` or your other loader
67+
},
68+
],
69+
}
70+
5771
// Optimization rules
5872
const optimizations = {};
5973

6074
// Plugins
6175
const plugins = [
62-
new WebpackBar(
76+
new WebpackBar( // Adds loading bar during builds
6377
// Uncomment this to enable profiler https://github.com/nuxt-contrib/webpackbar#options
6478
// { reporters: [ 'profile' ], profile: true }
6579
),
66-
new MiniCssExtractPlugin( {
80+
new MiniCssExtractPlugin( { // Extracts CSS files
6781
filename: projectOptions.projectCss.filename
6882
} ),
83+
new CopyPlugin( { // Copies images from src to public
84+
patterns: [
85+
{ from: projectOptions.projectImagesPath, to: projectOptions.projectOutput + '/images' },
86+
],
87+
} ),
88+
new ImageMinimizerPlugin( { // Optimizes images
89+
minimizerOptions: projectOptions.projectImages.minimizerOptions,
90+
} ),
6991
];
7092
// Add browserSync to plugins if enabled
7193
if ( projectOptions.browserSync.enable === true ) {
7294
const browserSyncOptions = {
7395
files: projectOptions.browserSync.files,
74-
host: projectOptions.browserSync.host,
75-
port: projectOptions.browserSync.port,
96+
host: projectOptions.browserSync.host,
97+
port: projectOptions.browserSync.port,
7698
}
7799
if ( projectOptions.browserSync.mode === 'server' ) {
78100
Object.assign( browserSyncOptions, {
@@ -108,7 +130,7 @@ module.exports = ( projectOptions ) => {
108130
// set this to false if you don't need it as it may slow down the build process
109131
devtool: 'source-map',
110132
optimization: optimizations,
111-
module: { rules: [ cssRules, jsRules, ], },
133+
module: { rules: [ cssRules, jsRules, imageRules ], },
112134
plugins: plugins,
113135
}
114136
}

0 commit comments

Comments
 (0)