<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Eduardo Orive Vinuesa</title>
    <description>The latest articles on DEV Community by Eduardo Orive Vinuesa (@edorka).</description>
    <link>https://dev.to/edorka</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F318554%2Ff3fea059-262f-44af-928b-70346dff0cb4.jpeg</url>
      <title>DEV Community: Eduardo Orive Vinuesa</title>
      <link>https://dev.to/edorka</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/edorka"/>
    <language>en</language>
    <item>
      <title>Preact SPA into a Wordpress theme</title>
      <dc:creator>Eduardo Orive Vinuesa</dc:creator>
      <pubDate>Tue, 18 Aug 2020 12:25:50 +0000</pubDate>
      <link>https://dev.to/edorka/preact-spa-into-a-wordpress-theme-lge</link>
      <guid>https://dev.to/edorka/preact-spa-into-a-wordpress-theme-lge</guid>
      <description>&lt;p&gt;Updated: Aug 19th, Found a solution that, however manual it's pretty clean and doesn't require adding new dependencies.&lt;/p&gt;

&lt;h1&gt;
  
  
  Why?
&lt;/h1&gt;

&lt;p&gt;Recently I was asked to run a small web to hub the contents of a poetry event. Wordpress or Strapi were my first options but I don't feel confident with PHP to fully customize wordpress and I had no practice with Strapi at all.&lt;/p&gt;

&lt;p&gt;I have ran some test projects with Preact and it's CLI (&lt;a href="https://github.com/preactjs/preact-cli/"&gt;https://github.com/preactjs/preact-cli/&lt;/a&gt;) makes it pretty easy to start a SPA.&lt;/p&gt;

&lt;h1&gt;
  
  
  What?
&lt;/h1&gt;

&lt;p&gt;Given we have done an SPA capable of fetch Wordpress posts from it's API, and categories and all that; we still will have to manage with deployment and issues like CORS.&lt;/p&gt;

&lt;p&gt;The obvious solution is to use your SPA as a Wordpress Theme.&lt;/p&gt;

&lt;h1&gt;
  
  
  How to do it myself?
&lt;/h1&gt;

&lt;p&gt;Imagine that our theme formal name is &lt;code&gt;preact-spa&lt;/code&gt;. We will store the output into the folder &lt;code&gt;wp-theme&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Then, step by step:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Create a &lt;code&gt;preact-wp.config.js&lt;/code&gt; with this content:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import previous from './preact.config.js';

export default function (config, env, helpers) {
    config.output.publicPath = '/wp-content/themes/preact-spa/';
    previous(config, env, helpers);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Now we have to build it:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;yarn build -c preact-wp.config.js --dest wp-theme
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;you can also run &lt;code&gt;npm run build ...&lt;/code&gt; if not using &lt;code&gt;yarn&lt;/code&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Copy or rename &lt;code&gt;wp-theme/index.html&lt;/code&gt; as &lt;code&gt;wp-theme/index.php&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;If you are using css urls you may notice that the path to these files is not set to be under wp-content/... a barbarian but practical solution is to install &lt;code&gt;string-replace-loader&lt;/code&gt; as a development dependency and expand our &lt;code&gt;preact-wp.config.js&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import previous from './preact.config.js';

export default function (config, env, helpers) {
    const publicPath = '/wp-content/themes/nsp/';
    config.output.publicPath = publicPath;
    config.module.rules.push({
        test: /\.css/,
        loader: 'string-replace-loader',
        options: {
          search: /url\('\/assets\/(.*)?'\)/ig, 
          replace: (_match, resource) =&amp;gt; `url('${publicPath}assets/${resource}')`,
          flags: 'g',
        }
      });
    previous(config, env, helpers);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;create a new &lt;code&gt;wp-theme/style.css&lt;/code&gt; file:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/*
Theme Name: Preact SPA
Author: Your Name
Author URI: https://github.com/your-github
Description: Create React WP Theme in no time
Version: 0.0.1
License: GPL
License URI: http://www.gnu.org/licenses/gpl-2.0.html
Tags: generated
Text Domain: your-domain

This theme, like WordPress, is licensed under the GPL.
*/
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;[optional] Consider some cleanup before shipping:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;rm wp-theme/*map wp-theme/sw-debug.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Zip it to your Wordpress site and switch to this theme.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h1&gt;
  
  
  Issues?
&lt;/h1&gt;

&lt;p&gt;Sure, Creating a theme from running &lt;code&gt;yarn dev&lt;/code&gt; so it updates it self as the code changes. However I don't consider it really necessary.&lt;/p&gt;

&lt;h1&gt;
  
  
  Yes... But?
&lt;/h1&gt;

&lt;p&gt;Questions? Comments? Please comment below I would be happy to help.&lt;/p&gt;

</description>
      <category>preact</category>
      <category>wordpress</category>
      <category>theme</category>
      <category>node</category>
    </item>
  </channel>
</rss>
