Giter Club home page Giter Club logo

ashstevens-dev / webpack-integration-example-wp-theme Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 26 KB

Simple example template illustrating how you can modify a WordPress theme to incorporate Webpack. Could be modified slightly for non-WordPress builds. Includes SASS & JS compilation/bundling/minification, Babel transpilation, Font Awesome, and extraction/inclusion of Gutenberg styles to minify them with the SASS.

License: MIT License

PHP 17.08% JavaScript 67.56% SCSS 7.86% CSS 7.51%
webpack4 wordpress wordpress-optimization wordpress-performance babel sass module-bundler npm

webpack-integration-example-wp-theme's Introduction

webpack-integration-example-wp-theme

Simple example template illustrating how you can modify a WordPress theme to incorporate Webpack. Includes SASS and JS compilation/bundling/minification, the use of Babel to transpile JS code so it's compatible with older browsers, and the inclusion of Font Awesome. It also shows you how to unhook Gutenberg styles from being loaded so you can import them into your SASS file instead, to further save on performance in your site.

Getting Started

  1. Add package.json to your theme root and edit the name and author values as appropriate
  2. Analyze/edit your style.css file. It should only have the header comment section at the top. Any other styles should be placed into a .scss file. You can add in nesting, mixins, partials, functions, etc. to harness the full power of Sass later. It will work as-is (with plain CSS)
  3. Reference the scss file in this example. It illustrates how to include background images on elements; and it has the line you need to use to include Gutenberg's block styles
  4. Ensure your theme has a matching directory structure to what's illustrated here (Sass in src/assets/styles, JS in src/assets/scripts, images referenced by the stylesheet in src/assets/images)
  5. Add the index.js file into the src/ directory and update the imports to include your stylesheets/javascript files (don't touch the Font Awesome block unless you want to remove it or modify it to utilize tree-shaking methods)
  6. Edit your functions.php file as illustrated. Modify your enqueues to simply include the bundled css and js (and any additional scripts/styles if you choose not to bundle them with the rest). Add the block of code to unhook Gutenberg
  7. If you need/want multiple bundles, edit the webpack.config.js file to add more entries (see line 16) and then create the additional js file(s) and include them in src/

Install the packages

npm install

Run Webpack in development mode (unminified output)

npm run build

Run Webpack in production mode

npm run dist

Run Webpack in watch mode

npm run watch

This will recompile code as you go, making a full build unnecessary while you're developing/making updates.

Use Font Awesome in your theme

Include the <i> tag inline where you want it to appear in pages, posts, theme files, etc. Reference the online library to find the free icon you want to add https://fontawesome.com/icons?d=gallery&m=free

โœจ Moving on

There are many other Webpack features I did not include or go over in this example including code-splitting, working with other types of files (JSON, locally-included fonts, SVG images, etc.), linting, and more. This is just a very simple example that could either be expanded & improved upon, modified to work with other project builds (non-WordPress), or used as-is for simple WordPress website projects.

webpack-integration-example-wp-theme's People

Contributors

ashstevens-dev avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.