<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/">
	<channel>
		<title>Squeezing Webpack into Backend Frameworks | Infinum</title>
		<atom:link href="https://infinum.com/blog/squeezing-webpack-into-backend-frameworks/feed/" rel="self" type="application/rss+xml" />
		<link>https://infinum.com/blog/squeezing-webpack-into-backend-frameworks/</link>
		<description>Building digital products</description>
		<lastBuildDate>Thu, 16 Apr 2026 15:26:52 +0000</lastBuildDate>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>

					<item>
				<image>
					<url>8079https://infinum.com/uploads/2017/02/squeezing-webpack-into-backend-frameworks-0.webp</url>
				</image>
				<title>Squeezing Webpack into Backend Frameworks</title>
				<link>https://infinum.com/blog/squeezing-webpack-into-backend-frameworks/</link>
				<pubDate>Tue, 24 Jan 2017 10:46:00 +0000</pubDate>
				<dc:creator>Darko Kukovec</dc:creator>
				<guid isPermaLink="false">https://infinum.com/the-capsized-eight/squeezing-webpack-into-backend-frameworks/</guid>
				<description>
					<![CDATA[<p>In order for a page to be fast, it’s a good idea to concatenate all your JavaScript assets into one file and all your CSS assets into another. </p>
<p>The post <a href="https://infinum.com/blog/squeezing-webpack-into-backend-frameworks/">Squeezing Webpack into Backend Frameworks</a> appeared first on <a href="https://infinum.com">Infinum</a>.</p>
]]>
				</description>
				<content:encoded>
					<![CDATA[<div
	class="wrapper"
	data-id="es-266"
	 data-animation-target='inner-items'>
		
			<div class="wrapper__inner">
			<div class="block-blog-content js-block-blog-content">
	
<div class="block-blog-content-sidebar" data-id="es-92">
	</div>

<div class="block-blog-content-main">
	
<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-95"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-paragraph" data-id="es-93">
	<p	class='typography typography--size-16-text-roman js-typography block-paragraph__paragraph'
	data-id='es-94'
	>
	If you ever made a website, it probably contained some assets like images, CSS and JavaScript. In order for a page to be fast, it’s a good idea to concatenate all your JavaScript assets into one file and all your CSS assets into another. Caching might also be tricky…</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-98"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-heading" data-id="es-96">
	<h2	class='typography typography--size-52-default js-typography block-heading__heading'
	data-id='es-97'
	>
	Working with assets 101</h2></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-101"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-paragraph" data-id="es-99">
	<p	class='typography typography--size-16-text-roman js-typography block-paragraph__paragraph'
	data-id='es-100'
	>
	If you want to concatenate files, use SASS or make sure your files are not cached when they shouldn’t be, you’ll need to use a tool that will take care of that. Most web frameworks have tools that work out of the box, but that might not be good enough for everyone.</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-104"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-heading" data-id="es-102">
	<h3	class='typography typography--size-36-text js-typography block-heading__heading'
	data-id='es-103'
	>
	The asset pipeline</h3></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-107"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-paragraph" data-id="es-105">
	<p	class='typography typography--size-16-text-roman js-typography block-paragraph__paragraph'
	data-id='es-106'
	>
	The asset pipeline is Rails’ way of preparing files for production. Other frameworks might use different names for it, but they all mostly do the same thing.</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-110"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-media">
	<div	class="media block-media__media media__border--none media__align--center-center"
	data-id="es-108"
	 data-media-type='image'>

	<figure class="image block-media__image-figure image--size-stretch" data-id="es-109">
	<picture class="image__picture block-media__image-picture">
												<img
					src="https://infinum.com/uploads/2017/02/squeezing-webpack-into-backend-frameworks-2.webp"
					class="image__img block-media__image-img"
					alt=""
										height="661"
															width="961"
										loading="lazy"
					 />
					</picture>

			<figcaption class="image__figcaption block-media__image-figcaption">
			 The basic Ruby on Rails request flow		</figcaption>
	</figure></div></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-113"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-heading" data-id="es-111">
	<h3	class='typography typography--size-36-text js-typography block-heading__heading'
	data-id='es-112'
	>
	Module bundlers</h3></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-116"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-paragraph" data-id="es-114">
	<p	class='typography typography--size-16-text-roman js-typography block-paragraph__paragraph'
	data-id='es-115'
	>
	A module bundler does a similar thing, but it is generally more flexible. The final product (files) might be similar, but it does more things in between.</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-119"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-heading" data-id="es-117">
	<h2	class='typography typography--size-52-default js-typography block-heading__heading'
	data-id='es-118'
	>
	Webpack – the good parts</h2></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-122"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-paragraph" data-id="es-120">
	<p	class='typography typography--size-16-text-roman js-typography block-paragraph__paragraph'
	data-id='es-121'
	>
	Webpack is a module bundler that is getting more popular day by day. Although this is mostly because of the <a href="https://facebook.github.io/react/">React</a> community, it gained a lot traction in other communities, e.g. the <a href="https://github.com/angular/angular-cli">Angular 2 CLI</a> also switched to Webpack recently.<br>The reason why it’s so popular is that it’s very fast and extendable. Therefore, it can be adapted to a lot of different use-cases.</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-125"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-paragraph" data-id="es-123">
	<p	class='typography typography--size-16-text-roman js-typography block-paragraph__paragraph'
	data-id='es-124'
	>
	Here are some things that Webpack can do with very little configuration that would be impossible or very hard in the Rails asset pipeline:</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-128"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="lists" data-id="es-126">
	<ul	class='typography typography--size-16-text-roman js-typography lists__typography'
	data-id='es-127'
	>
	<li>Dead code removal – remove unused code to decrease the final JS size</li><li>Code splitting – break the code into multiple parts that can be loaded only when needed</li><li>Linting – stop the build process if there are some errors that might cause issues</li><li>Treat assets as modules – every asset (images, fonts, etc.) can be required and used anywhere</li></ul></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-131"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-paragraph" data-id="es-129">
	<p	class='typography typography--size-16-text-roman js-typography block-paragraph__paragraph'
	data-id='es-130'
	>
	Webpack handles assets in a different way, which might be weird at first, but is very powerful. Your application has only one entry point (in most cases this will be a JavaScript file), and from there, you can require all other files as you need them. You can include other JavaScript files, but also CSS, JPG, PNG, or any other format you want. The important thing is to define a loader for a specific file format, and you’re good to go.</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-134"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-heading" data-id="es-132">
	<h2	class='typography typography--size-52-default js-typography block-heading__heading'
	data-id='es-133'
	>
	What is out there</h2></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-137"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-paragraph" data-id="es-135">
	<p	class='typography typography--size-16-text-roman js-typography block-paragraph__paragraph'
	data-id='es-136'
	>
	When we decided to use Webpack with Rails, we looked at the available options and tried a few. Although they were good, none of them covered all our use cases:</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-140"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="lists" data-id="es-138">
	<ul	class='typography typography--size-16-text-roman js-typography lists__typography'
	data-id='es-139'
	>
	<li>The Pixelated approach – It’s very similar to our approach, but the difference being in how assets are handled in production. Since they’re using the same fingerprint for all files, it means that it will change when any of the files changes. What that means is – if you change one icon and redeploy your site, the visitors will need to download all the assets again, even if nothing has changed for them. We think this is very inefficient, especially if you’re doing multiple deploys per day.</li><li><a href="https://github.com/mipearson/webpack-rails">webpack-rails</a> gem – It requires you to change how you’re using the assets from the Rails side. It also has a very specific (tightly coupled) setup that might limit us in some situations.</li><li><strong>webpack-assets gem</strong> – The whole configuration is done through Rails and it makes some assumptions about your client-side setup</li><li><a href="https://github.com/danethurber/webpack-manifest-plugin">webpack-manifest-plugin</a> – this plugin was our starting point, but we realized there were some features missing, like the very powerful <a href="https://github.com/infinum/webpack-asset-pipeline/blob/master/documentation/options.md#mapassetpath"><code>mapAssetPath</code></a> that allows us to do manual adjustments in some cases that aren’t covered out of the box.</li></ul></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-143"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-heading" data-id="es-141">
	<h2	class='typography typography--size-52-default js-typography block-heading__heading'
	data-id='es-142'
	>
	Rails 5.1</h2></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-146"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-paragraph" data-id="es-144">
	<p	class='typography typography--size-16-text-roman js-typography block-paragraph__paragraph'
	data-id='es-145'
	>
	It <a href="https://github.com/rails/rails/pull/26836#issuecomment-264242030">was announced</a> a few weeks ago that Rails 5.1 will get an official (but experimental) support for Webpack. The release date for Rails 5.1 is still unknown. Given that Rails 5 was released in June, and the <a href="https://github.com/rails/rails/milestones">current progress of 5.1</a> is just above 50%, it might still be a while before you’ll be able to use it.</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-149"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-heading" data-id="es-147">
	<h2	class='typography typography--size-52-default js-typography block-heading__heading'
	data-id='es-148'
	>
	The missing link</h2></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-152"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-paragraph" data-id="es-150">
	<p	class='typography typography--size-16-text-roman js-typography block-paragraph__paragraph'
	data-id='es-151'
	>
	Our approach was simple – find a common ground for communication between Webpack and Rails. With this approach, we’re avoiding the tight coupling – Rails doesn’t know much about Webpack, and Webpack doesn’t know a lot about Rails. There are two parts of the solution:</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-155"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-heading" data-id="es-153">
	<h3	class='typography typography--size-36-text js-typography block-heading__heading'
	data-id='es-154'
	>
	The Webpack part</h3></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-158"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-paragraph" data-id="es-156">
	<p	class='typography typography--size-16-text-roman js-typography block-paragraph__paragraph'
	data-id='es-157'
	>
	Webpack will be responsible for all the assets in the project. Since it will also add fingerprints to the files, we’ll need to let Rails know what the new files are, and to which folders they’re saved. To make this part simple, we created <a href="https://github.com/infinum/webpack-asset-pipeline">webpack-asset-pipeline</a>. The plugin will intercept all requires and imports from your JS and CSS and add them to the manifest JSON file:</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-160"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-code">
	<pre class="phiki language-javascript github-light" data-language="javascript" style="background-color: #fff;color: #24292e;"><code><span class="line"><span class="token" style="color: #d73a49;">var</span><span class="token"> </span><span class="token" style="color: #24292e;">WebpackAssetPipeline</span><span class="token"> </span><span class="token" style="color: #d73a49;">=</span><span class="token"> </span><span class="token" style="color: #6f42c1;">require</span><span class="token">(</span><span class="token">’</span><span class="token" style="color: #24292e;">webpack</span><span class="token" style="color: #d73a49;">-</span><span class="token" style="color: #24292e;">asset</span><span class="token" style="color: #d73a49;">-</span><span class="token" style="color: #24292e;">pipeline</span><span class="token">’</span><span class="token">)</span><span class="token">;</span><span class="token">
</span></span><span class="line"><span class="token">
</span></span><span class="line"><span class="token" style="color: #005cc5;">module</span><span class="token">.</span><span class="token" style="color: #005cc5;">exports</span><span class="token"> </span><span class="token" style="color: #d73a49;">=</span><span class="token"> </span><span class="token">{</span><span class="token">
</span></span><span class="line"><span class="token">  </span><span class="token" style="color: #6a737d;">//</span><span class="token" style="color: #6a737d;"> The usual Webpack configuration</span><span class="token">
</span></span><span class="line"><span class="token">  </span><span class="token">entry</span><span class="token">:</span><span class="token"> ’</span><span class="token" style="color: #24292e;">main</span><span class="token" style="color: #24292e;">.j</span><span class="token">s</span><span class="token">’</span><span class="token">,</span><span class="token">
</span></span><span class="line"><span class="token">  </span><span class="token" style="color: #6a737d;">//</span><span class="token" style="color: #6a737d;"> etc.</span><span class="token">
</span></span><span class="line"><span class="token">
</span></span><span class="line"><span class="token">  </span><span class="token">plugins</span><span class="token">:</span><span class="token"> </span><span class="token">[</span><span class="token">
</span></span><span class="line"><span class="token">    </span><span class="token" style="color: #d73a49;">new</span><span class="token"> </span><span class="token" style="color: #6f42c1;">WebpackAssetPipeline</span><span class="token">(</span><span class="token">)</span><span class="token">
</span></span><span class="line"><span class="token">  </span><span class="token">]</span><span class="token">,</span><span class="token">
</span></span><span class="line"><span class="token">  </span><span class="token">output</span><span class="token">:</span><span class="token"> </span><span class="token">{</span><span class="token">
</span></span><span class="line"><span class="token">    </span><span class="token">path</span><span class="token">:</span><span class="token"> ’</span><span class="token" style="color: #24292e;">public</span><span class="token" style="color: #d73a49;">/</span><span class="token">’</span><span class="token">,</span><span class="token">
</span></span><span class="line"><span class="token">    </span><span class="token">publicPath</span><span class="token">:</span><span class="token"> ’</span><span class="token" style="color: #d73a49;">/</span><span class="token">’</span><span class="token">,</span><span class="token">
</span></span><span class="line"><span class="token">    </span><span class="token">filename</span><span class="token">:</span><span class="token"> ’</span><span class="token">[</span><span class="token" style="color: #24292e;">name</span><span class="token">]</span><span class="token" style="color: #24292e;">.j</span><span class="token">s</span><span class="token">’
</span></span><span class="line"><span class="token">  </span><span class="token">}</span><span class="token">
</span></span><span class="line"><span class="token">}</span><span class="token">
</span></span><span class="line"><span class="token">
</span></span></code></pre></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-163"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-paragraph" data-id="es-161">
	<p	class='typography typography--size-16-text-roman js-typography block-paragraph__paragraph'
	data-id='es-162'
	>
	When you run Webpack, the plugin will create a <code>manifest.json</code> file that will contain all the asset mappings.</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-166"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-paragraph" data-id="es-164">
	<p	class='typography typography--size-16-text-roman js-typography block-paragraph__paragraph'
	data-id='es-165'
	>
	An example of JavaScript code with requires:</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-168"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-code">
	<pre class="phiki language-javascript github-light" data-language="javascript" style="background-color: #fff;color: #24292e;"><code><span class="line"><span class="token" style="color: #6a737d;">//</span><span class="token" style="color: #6a737d;"> application.js</span><span class="token">
</span></span><span class="line"><span class="token">
</span></span><span class="line"><span class="token" style="color: #6f42c1;">require</span><span class="token">(</span><span class="token">’</span><span class="token" style="color: #24292e;">application</span><span class="token" style="color: #24292e;">.cs</span><span class="token">s</span><span class="token">’</span><span class="token">)</span><span class="token">;</span><span class="token">
</span></span><span class="line"><span class="token" style="color: #6f42c1;">require</span><span class="token">(</span><span class="token">’</span><span class="token" style="color: #24292e;">images</span><span class="token" style="color: #d73a49;">/</span><span class="token" style="color: #24292e;">file1</span><span class="token" style="color: #24292e;">.jp</span><span class="token">g</span><span class="token">’</span><span class="token">)</span><span class="token">;</span><span class="token">
</span></span><span class="line"><span class="token" style="color: #6f42c1;">require</span><span class="token">(</span><span class="token">’</span><span class="token" style="color: #24292e;">images</span><span class="token" style="color: #d73a49;">/</span><span class="token" style="color: #24292e;">file2</span><span class="token" style="color: #24292e;">.jp</span><span class="token">g</span><span class="token">’</span><span class="token">)</span><span class="token">;</span><span class="token">
</span></span><span class="line"><span class="token" style="color: #6f42c1;">require</span><span class="token">(</span><span class="token">’</span><span class="token" style="color: #24292e;">images</span><span class="token" style="color: #d73a49;">/</span><span class="token" style="color: #24292e;">file3</span><span class="token" style="color: #24292e;">.jp</span><span class="token">g</span><span class="token">’</span><span class="token">)</span><span class="token">;</span><span class="token">
</span></span><span class="line"><span class="token">
</span></span></code></pre></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-171"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-paragraph" data-id="es-169">
	<p	class='typography typography--size-16-text-roman js-typography block-paragraph__paragraph'
	data-id='es-170'
	>
	The mappings you’ll get in the manifest file:</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-173"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-code">
	<pre class="phiki language-javascript github-light" data-language="javascript" style="background-color: #fff;color: #24292e;"><code><span class="line"><span class="token">{</span><span class="token">
</span></span><span class="line"><span class="token">  </span><span class="token" style="color: #032f62;">&quot;</span><span class="token" style="color: #032f62;">application.js</span><span class="token" style="color: #032f62;">&quot;</span><span class="token">: </span><span class="token" style="color: #032f62;">&quot;</span><span class="token" style="color: #032f62;">application-63eb3290f4d9a09812716b989a0808f3.js</span><span class="token" style="color: #032f62;">&quot;</span><span class="token">,</span><span class="token">
</span></span><span class="line"><span class="token">  </span><span class="token" style="color: #032f62;">&quot;</span><span class="token" style="color: #032f62;">application.css</span><span class="token" style="color: #032f62;">&quot;</span><span class="token">: </span><span class="token" style="color: #032f62;">&quot;</span><span class="token" style="color: #032f62;">application-07eb3299a08b981290763eb32962808f.css</span><span class="token" style="color: #032f62;">&quot;</span><span class="token">,</span><span class="token">
</span></span><span class="line"><span class="token">  </span><span class="token" style="color: #032f62;">&quot;</span><span class="token" style="color: #032f62;">images/file1.jpg</span><span class="token" style="color: #032f62;">&quot;</span><span class="token">: </span><span class="token" style="color: #032f62;">&quot;</span><span class="token" style="color: #032f62;">963eb32907744d9a0d6b98127162808f.jpg</span><span class="token" style="color: #032f62;">&quot;</span><span class="token">,</span><span class="token">
</span></span><span class="line"><span class="token">  </span><span class="token" style="color: #032f62;">&quot;</span><span class="token" style="color: #032f62;">images/file2.jpg</span><span class="token" style="color: #032f62;">&quot;</span><span class="token">: </span><span class="token" style="color: #032f62;">&quot;</span><span class="token" style="color: #032f62;">162808f4d9a0963eb3290774127d6b98.jpg</span><span class="token" style="color: #032f62;">&quot;</span><span class="token">,</span><span class="token">
</span></span><span class="line"><span class="token">  </span><span class="token" style="color: #032f62;">&quot;</span><span class="token" style="color: #032f62;">images/file3.jpg</span><span class="token" style="color: #032f62;">&quot;</span><span class="token">: </span><span class="token" style="color: #032f62;">&quot;</span><span class="token" style="color: #032f62;">d6b98127162969a0808f3eb32907744d.jpg</span><span class="token" style="color: #032f62;">&quot;</span><span class="token">
</span></span><span class="line"><span class="token">}</span><span class="token">
</span></span><span class="line"><span class="token">
</span></span></code></pre></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-176"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-paragraph" data-id="es-174">
	<p	class='typography typography--size-16-text-roman js-typography block-paragraph__paragraph'
	data-id='es-175'
	>
	To run Webpack on every relevant file change (for development), you should run it with the <code>&#8211;watch</code> flag:</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-178"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-code">
	<pre class="phiki language-shellscript github-light" data-language="shellscript" style="background-color: #fff;color: #24292e;"><code><span class="line"><span class="token" style="color: #6f42c1;">webpack</span><span class="token"> </span><span class="token" style="color: #005cc5;">-</span><span class="token" style="color: #005cc5;">-watch</span><span class="token">
</span></span><span class="line"><span class="token">
</span></span></code></pre></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-181"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-heading" data-id="es-179">
	<h3	class='typography typography--size-36-text js-typography block-heading__heading'
	data-id='es-180'
	>
	The Rails part</h3></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-184"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-paragraph" data-id="es-182">
	<p	class='typography typography--size-16-text-roman js-typography block-paragraph__paragraph'
	data-id='es-183'
	>
	The only thing Ruby on Rails needs to do is to replace the assets helpers. This can be done in one of two ways (depending on your preferences). In both cases, you should remove Rails sprockets because they will not be used, and you won’t be able to use their methods by mistake. The easiest way to do this is with the following command when you’re creating your app:</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-186"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-code">
	<pre class="phiki language-shellscript github-light" data-language="shellscript" style="background-color: #fff;color: #24292e;"><code><span class="line"><span class="token" style="color: #6f42c1;">rails</span><span class="token"> </span><span class="token" style="color: #032f62;">new</span><span class="token"> </span><span class="token" style="color: #032f62;">app</span><span class="token"> </span><span class="token" style="color: #005cc5;">-</span><span class="token" style="color: #005cc5;">-skip-sprockets</span><span class="token">
</span></span><span class="line"><span class="token">
</span></span></code></pre></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-189"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-heading" data-id="es-187">
	<h4	class='typography typography--size-24-text js-typography block-heading__heading'
	data-id='es-188'
	>
	Adding a new <code>webpack_asset_url</code> helper</h4></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-192"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-paragraph" data-id="es-190">
	<p	class='typography typography--size-16-text-roman js-typography block-paragraph__paragraph'
	data-id='es-191'
	>
	You’ll need a helper to replace the asset pipeline. Here is an example of how you can do this:</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-194"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-code">
	<pre class="phiki language-ruby github-light" data-language="ruby" style="background-color: #fff;color: #24292e;"><code><span class="line"><span class="token" style="color: #6a737d;">#</span><span class="token" style="color: #6a737d;"> app/helpers/webpack_helper.rb</span><span class="token" style="color: #6a737d;">
</span></span><span class="line"><span class="token">
</span></span><span class="line"><span class="token" style="color: #d73a49;">module</span><span class="token"> </span><span class="token" style="color: #6f42c1;">WebpackHelper</span><span class="token">
</span></span><span class="line"><span class="token">  </span><span class="token" style="color: #d73a49;">def</span><span class="token"> </span><span class="token" style="color: #6f42c1;">webpack_asset_url</span><span class="token">(</span><span class="token">asset</span><span class="token">)</span><span class="token">
</span></span><span class="line"><span class="token">    </span><span class="token" style="color: #032f62;">&quot;</span><span class="token" style="color: #032f62;">/assets/</span><span class="token" style="color: #032f62;">#{</span><span class="token" style="color: #032f62;">manifest</span><span class="token" style="color: #032f62;">.</span><span class="token" style="color: #6f42c1;">fetch</span><span class="token" style="color: #032f62;">(</span><span class="token" style="color: #032f62;">asset</span><span class="token" style="color: #032f62;">)</span><span class="token" style="color: #032f62;">}</span><span class="token" style="color: #032f62;">&quot;</span><span class="token">
</span></span><span class="line"><span class="token">  </span><span class="token" style="color: #d73a49;">end</span><span class="token">
</span></span><span class="line"><span class="token">
</span></span><span class="line"><span class="token">  </span><span class="token" style="color: #d73a49;">def</span><span class="token"> </span><span class="token" style="color: #6f42c1;">manifest</span><span class="token">
</span></span><span class="line"><span class="token">    </span><span class="token" style="color: #24292e;">@</span><span class="token" style="color: #24292e;">manifest</span><span class="token"> </span><span class="token" style="color: #d73a49;">||=</span><span class="token"> </span><span class="token" style="color: #005cc5;">JSON</span><span class="token">.</span><span class="token" style="color: #6f42c1;">parse</span><span class="token">(</span><span class="token" style="color: #005cc5;">File</span><span class="token">.</span><span class="token" style="color: #6f42c1;">read</span><span class="token">(</span><span class="token">’manifest</span><span class="token">.</span><span class="token" style="color: #6f42c1;">json</span><span class="token">’</span><span class="token">)</span><span class="token">)</span><span class="token">
</span></span><span class="line"><span class="token">  </span><span class="token" style="color: #d73a49;">rescue</span><span class="token">
</span></span><span class="line"><span class="token">    </span><span class="token" style="color: #d73a49;">fail</span><span class="token"> ’</span><span class="token" style="color: #005cc5;">Please</span><span class="token"> run webpack’
</span></span><span class="line"><span class="token">  </span><span class="token" style="color: #d73a49;">end</span><span class="token">
</span></span><span class="line"><span class="token" style="color: #d73a49;">end</span><span class="token">
</span></span><span class="line"><span class="token">
</span></span></code></pre></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-197"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-paragraph" data-id="es-195">
	<p	class='typography typography--size-16-text-roman js-typography block-paragraph__paragraph'
	data-id='es-196'
	>
	An example how to use the helper:</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-199"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-code">
	<pre class="phiki language-ruby github-light" data-language="ruby" style="background-color: #fff;color: #24292e;"><code><span class="line"><span class="token" style="color: #d73a49;">&lt;</span><span class="token">img src</span><span class="token" style="color: #d73a49;">=</span><span class="token" style="color: #032f62;">&quot;</span><span class="token" style="color: #032f62;">#{</span><span class="token" style="color: #6f42c1;">webpack_asset_url</span><span class="token" style="color: #032f62;">(</span><span class="token" style="color: #032f62;">’logo</span><span class="token" style="color: #032f62;">.</span><span class="token" style="color: #6f42c1;">svg</span><span class="token" style="color: #032f62;">’</span><span class="token" style="color: #032f62;">)</span><span class="token" style="color: #032f62;">}</span><span class="token" style="color: #032f62;">&quot;</span><span class="token"> alt</span><span class="token" style="color: #d73a49;">=</span><span class="token" style="color: #032f62;">&quot;</span><span class="token" style="color: #032f62;">logo</span><span class="token" style="color: #032f62;">&quot;</span><span class="token"> </span><span class="token" style="color: #d73a49;">/</span><span class="token" style="color: #d73a49;">&gt;</span><span class="token">
</span></span><span class="line"><span class="token">
</span></span></code></pre></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-202"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-paragraph" data-id="es-200">
	<p	class='typography typography--size-16-text-roman js-typography block-paragraph__paragraph'
	data-id='es-201'
	>
	You’ll need to add the main JavaScript file to your layout with this helper. Also, keep in mind that you shouldn’t use Rails <code>image_tag</code>, <code>stylesheet_link_tag</code> or <code>javascript_include_tag</code> helpers.</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-205"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-heading" data-id="es-203">
	<h4	class='typography typography--size-24-text js-typography block-heading__heading'
	data-id='es-204'
	>
	Monkey patching Rails assets helpers</h4></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-208"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-paragraph" data-id="es-206">
	<p	class='typography typography--size-16-text-roman js-typography block-paragraph__paragraph'
	data-id='es-207'
	>
	The second solution means a little more work in the start, but your application code will stay the same. Although removing sprockets is optional in the first method, in this case you have to remove them. The code for monkey patching is available <a href="https://github.com/infinum/webpack-asset-pipeline/blob/master/documentation/integrations/rails.md">in the plugin documentation</a>.</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-211"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-paragraph" data-id="es-209">
	<p	class='typography typography--size-16-text-roman js-typography block-paragraph__paragraph'
	data-id='es-210'
	>
	After you’re done with monkey patching, you’ll be able to use the assets in the same way you used the asset pipeline:</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-213"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-code">
	<pre class="phiki language-ruby github-light" data-language="ruby" style="background-color: #fff;color: #24292e;"><code><span class="line"><span class="token" style="color: #d73a49;">=</span><span class="token"> </span><span class="token" style="color: #6f42c1;">image_tag</span><span class="token">(</span><span class="token">’logo</span><span class="token">.</span><span class="token" style="color: #6f42c1;">svg</span><span class="token">’</span><span class="token">)</span><span class="token">
</span></span><span class="line"><span class="token">
</span></span><span class="line"><span class="token" style="color: #d73a49;">=</span><span class="token"> stylesheet_link_tag ’application’</span><span class="token">,</span><span class="token"> </span><span class="token" style="color: #005cc5;">m</span><span class="token" style="color: #005cc5;">e</span><span class="token" style="color: #005cc5;">dia:</span><span class="token"> ’all’
</span></span><span class="line"><span class="token" style="color: #d73a49;">=</span><span class="token"> javascript_include_tag ’application’
</span></span><span class="line"><span class="token">
</span></span></code></pre></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-216"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-media">
	<div	class="media block-media__media media__border--none media__align--center-center"
	data-id="es-214"
	 data-media-type='image'>

	<figure class="image block-media__image-figure image--size-stretch" data-id="es-215">
	<picture class="image__picture block-media__image-picture">
												<img
					src="https://infinum.com/uploads/2017/02/squeezing-webpack-into-backend-frameworks-4.webp"
					class="image__img block-media__image-img"
					alt=""
										height="680"
															width="960"
										loading="lazy"
					 />
					</picture>

			<figcaption class="image__figcaption block-media__image-figcaption">
			 Updated flow in combination with Webpack		</figcaption>
	</figure></div></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-219"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-heading" data-id="es-217">
	<h3	class='typography typography--size-36-text js-typography block-heading__heading'
	data-id='es-218'
	>
	Hot reloading</h3></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-222"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-paragraph" data-id="es-220">
	<p	class='typography typography--size-16-text-roman js-typography block-paragraph__paragraph'
	data-id='es-221'
	>
	If you want to use all the features Webpack can offer, you can. One of the biggest features is hot reloading. Since JavaScript hot reloading depends on the JavaScript framework, this is out of scope for this blog post, but CSS hot reloading can be set up with relative ease:</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-225"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-heading" data-id="es-223">
	<h4	class='typography typography--size-24-text js-typography block-heading__heading'
	data-id='es-224'
	>
	Configuring Webpack</h4></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-228"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-paragraph" data-id="es-226">
	<p	class='typography typography--size-16-text-roman js-typography block-paragraph__paragraph'
	data-id='es-227'
	>
	To use hot reloading, all assets will need to be served from the Webpack server. To work correctly with the rest of the setup, you’ll need to proxy the rest through the Webpack server to your original app server. For that, you can use <a href="https://webpack.js.org/guides/development/#webpack-dev-server"><code>webpack-dev-server</code></a>, or if you need more control, <a href="https://webpack.js.org/guides/development/#webpack-dev-middleware"><code>webpack-dev-middleware</code></a> in combination with <a href="http://expressjs.com/"><code>express</code></a>. An example of how to configure the <code>webpack-dev-middleware</code> to enable CSS hot reloading is available in the <a href="https://github.com/DarkoKukovec/webpack-express-dev-server-boilerplate"><code>webpack-express-dev-server-boilerplate</code></a>.</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-231"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-paragraph" data-id="es-229">
	<p	class='typography typography--size-16-text-roman js-typography block-paragraph__paragraph'
	data-id='es-230'
	>
	You’ll also need to set the <a href="https://github.com/infinum/webpack-asset-pipeline/blob/master/documentation/options.md"><code>writeToFileEmit</code></a> property in <code>webpack-asset-pipeline</code> to <code>true</code> to ensure the manifest is saved to disk and therefore accessible by the app server.</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-234"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-media">
	<div	class="media block-media__media media__border--none media__align--center-center"
	data-id="es-232"
	 data-media-type='image'>

	<figure class="image block-media__image-figure image--size-stretch" data-id="es-233">
	<picture class="image__picture block-media__image-picture">
												<img
					src="https://infinum.com/uploads/2017/02/squeezing-webpack-into-backend-frameworks-5.webp"
					class="image__img block-media__image-img"
					alt=""
										height="661"
															width="961"
										loading="lazy"
					 />
					</picture>

			<figcaption class="image__figcaption block-media__image-figcaption">
			 Request flow during development with hot reloading		</figcaption>
	</figure></div></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-237"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-heading" data-id="es-235">
	<h2	class='typography typography--size-52-default js-typography block-heading__heading'
	data-id='es-236'
	>
	Caveats</h2></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-240"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-paragraph" data-id="es-238">
	<p	class='typography typography--size-16-text-roman js-typography block-paragraph__paragraph'
	data-id='es-239'
	>
	There are still some issues we didn’t solve, but they’re just features that are nice to have – nothing essential:</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-243"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-heading" data-id="es-241">
	<h3	class='typography typography--size-36-text js-typography block-heading__heading'
	data-id='es-242'
	>
	Running the server</h3></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-246"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-paragraph" data-id="es-244">
	<p	class='typography typography--size-16-text-roman js-typography block-paragraph__paragraph'
	data-id='es-245'
	>
	Since both Webpack and Rails have their servers or watch commands, we’ll need to run them both at the same time. We can do this either by having two terminals running the processes or by using something like the <a href="https://github.com/ddollar/foreman">foreman</a> gem.</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-249"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-heading" data-id="es-247">
	<h3	class='typography typography--size-36-text js-typography block-heading__heading'
	data-id='es-248'
	>
	Requiring assets</h3></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-252"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-paragraph" data-id="es-250">
	<p	class='typography typography--size-16-text-roman js-typography block-paragraph__paragraph'
	data-id='es-251'
	>
	A great thing about Webpack is that it only processes those files that are explicitly referenced in JavaScript or CSS. That works great as long as you don’t need the assets somewhere else (e.g. in Rails). If you need an asset in Rails, but not in JS/CSS, you’ll still need to require the file in JS so Webpack will know it has to process it. The best way to do this is to have a separate JS file where you require all the assets that are used in Rails, but not in JS/CSS.</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-255"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-heading" data-id="es-253">
	<h2	class='typography typography--size-52-default js-typography block-heading__heading'
	data-id='es-254'
	>
	What if I’m using some other framework instead of Rails?</h2></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-258"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-paragraph" data-id="es-256">
	<p	class='typography typography--size-16-text-roman js-typography block-paragraph__paragraph'
	data-id='es-257'
	>
	You might not be using Rails on your projects, but the plugin could work just as well with other server-side frameworks like Laravel, Django, Spring or ASP.NET MVC – the only thing you’ll need to do yourself is write the helper that will be used to fetch the asset names from the manifest file.</p></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-261"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-heading" data-id="es-259">
	<h2	class='typography typography--size-52-default js-typography block-heading__heading'
	data-id='es-260'
	>
	Want to contribute?</h2></div>	</div>

<div
	class="wrapper wrapper__use-simple--true"
	data-id="es-264"
	 data-animation='slideFade' data-animation-target='inner-items'>
		
			<div class="block-paragraph" data-id="es-262">
	<p	class='typography typography--size-16-text-roman js-typography block-paragraph__paragraph'
	data-id='es-263'
	>
	We’re looking to cover as many platforms as possible, so if you created helpers for your framework, you can send us a pull request with instructions on how to set it up. I’m sure others will appreciate it.</p></div>	</div>
</div>
</div>		</div>
	</div><p>The post <a href="https://infinum.com/blog/squeezing-webpack-into-backend-frameworks/">Squeezing Webpack into Backend Frameworks</a> appeared first on <a href="https://infinum.com">Infinum</a>.</p>
]]>
				</content:encoded>
			</item>
		
	</channel>
</rss>