<?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: Paul Loveridge</title>
    <description>The latest articles on DEV Community by Paul Loveridge (@drstrangelug).</description>
    <link>https://dev.to/drstrangelug</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%2F238871%2Fe3282d57-e447-41e5-84b4-5434079ec59b.jpg</url>
      <title>DEV Community: Paul Loveridge</title>
      <link>https://dev.to/drstrangelug</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/drstrangelug"/>
    <language>en</language>
    <item>
      <title>Webpack output based upon environment variable     </title>
      <dc:creator>Paul Loveridge</dc:creator>
      <pubDate>Wed, 12 Aug 2020 07:20:14 +0000</pubDate>
      <link>https://dev.to/drstrangelug/webpack-output-based-upon-environment-variable-34h5</link>
      <guid>https://dev.to/drstrangelug/webpack-output-based-upon-environment-variable-34h5</guid>
      <description>&lt;p&gt;My first dev.to post - I've been wondering what to write for a while. But recently had a problem at work and found what I thought was a nifty solution. So I thought I'd share and help out others and perhaps even be told what I'd done wrong.&lt;/p&gt;

&lt;p&gt;For my day job I'm a developer on a mature Java webapp.  (And when I say mature I mean it's old enough to remember the Christopher Eccleston as Doctor Who.)&lt;/p&gt;

&lt;p&gt;So as part of this we've decided to start building a React based client project to replace our older parts of written with JSPs, JSTL and JQuery.  I wanted the React code to be quickly editable and viewable during development app with as small a delay as possible.&lt;/p&gt;

&lt;p&gt;To keep the react client seperate it's partitioned under {appname}/react so it doesnt interfere with any of the older client code.&lt;/p&gt;

&lt;p&gt;I could get the project deployed as part of the webapp but to refresh anything when react source changed required a tomcat restart, which would be a cycle time of 45 to 60 seconds, plus I'd have to login again.  &lt;/p&gt;

&lt;p&gt;Not a good way to write UI code.  What I needed was the compiled react app to get updated within the running tomcat as it changed (and I could use webpack's watch option to automate that).&lt;/p&gt;

&lt;p&gt;The first issue was to know where to put it.  Eclipse doesnt deploy to tomcat but runs tomcat with apps deployed somewhere in whatever tomcat integration plugin it's using.  Fortunately you can change that by editing the server settings in Eclipse.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--hrmDyGTn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/nGTZGuQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--hrmDyGTn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.imgur.com/nGTZGuQ.png" alt="Eclipse webapps destination folder"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The solution was the edit where Eclipse deployed the webapps (so I knew where it was) and have webpack build stright into the /react folder there.&lt;/p&gt;

&lt;p&gt;My &lt;strong&gt;webpack.config.js&lt;/strong&gt; looked like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;output : {
    path :path.join(__dirname, "../.metadata/eclipse_wtp/wtpwebapps/app/react/dist")
},
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;But that wasnt ideal. It used a hardcoded output folder that was only applicable on &lt;strong&gt;my&lt;/strong&gt; work machine, not any other developers and (more importantly) not for Jenkins.&lt;/p&gt;

&lt;p&gt;The solution was to use an environment variable to specify where the output folder was and use a default if it wasnt set.  I choose &lt;strong&gt;APP_REACT_DIR&lt;/strong&gt; for my variable name .&lt;/p&gt;

&lt;p&gt;Just ahead of my module exports I defined a new constant&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const distFolder = process.env.APP_REACT_DIR ?
  path.join(__dirname, process.env.APP_REACT_DIR ) 
  : 'dist';
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Then inside &lt;strong&gt;webpack.config.js&lt;/strong&gt; the output config then looked like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;output : {
    path : distFolder
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Bingo!  Now setting whenever webpack is run in watch mode the updates happen in seconds.  It will deploy to &lt;strong&gt;APP_REACT_DIR&lt;/strong&gt; if set, but if not to the project's dist folder.&lt;/p&gt;

</description>
      <category>webpack</category>
    </item>
  </channel>
</rss>
