<?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: Cody Casey</title>
    <description>The latest articles on DEV Community by Cody Casey (@codyjamescasey).</description>
    <link>https://dev.to/codyjamescasey</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%2F200353%2F1c1957ad-86b9-4bc9-8fbb-1c601965d729.jpeg</url>
      <title>DEV Community: Cody Casey</title>
      <link>https://dev.to/codyjamescasey</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/codyjamescasey"/>
    <language>en</language>
    <item>
      <title>Can I inject environment variables after bundle splitting?</title>
      <dc:creator>Cody Casey</dc:creator>
      <pubDate>Tue, 23 Jul 2019 15:33:07 +0000</pubDate>
      <link>https://dev.to/codyjamescasey/can-i-inject-environment-variables-after-bundle-splitting-bef</link>
      <guid>https://dev.to/codyjamescasey/can-i-inject-environment-variables-after-bundle-splitting-bef</guid>
      <description>&lt;p&gt;I'm working on a browser extension that I'm building with webpack and could use some help with environment variables. Hopefully I can explain what I'm trying to do properly here, but I'm more than happy to expand on anything if I'm not being clear!&lt;/p&gt;

&lt;p&gt;TL;DR: I'm trying to get a list of files (strings of file names) that are dynamically created via webpack's bundle splitting process and inject that list of files as environment variables back into the code after the splitting/building happens.&lt;/p&gt;

&lt;p&gt;First, here is a little background about browser extensions so that you can follow what I'm trying to do: Browser extensions need a &lt;code&gt;manifest.json&lt;/code&gt; file that holds information about various files that the extension will use. A simple &lt;code&gt;manifest.json&lt;/code&gt; file might look as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
    "manifest_version":2,
    "version":"1.2.3",
    "name":"My Cool Extension",
    "background":{
        "scripts":[
            "background-script.js"
        ]
    },
    "content_scripts":[
        {
            "js":[
                "content-script.js"
            ]
        }
    ]
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Basically, &lt;code&gt;content-script.js&lt;/code&gt; is a file that runs in a browser tab. When a user is navigating on webpages, this file can be injected and ran automatically. And &lt;code&gt;background-script.js&lt;/code&gt; runs at the browser level and is a shared environment between all tabs. Extensions have a message passing system that allows the content script to communicate with the background script.&lt;/p&gt;

&lt;p&gt;Through this communication, a content script can 'ask' the background script to inject additional code onto the browser tab. Let's call this additional code &lt;code&gt;additional-code.js&lt;/code&gt;. The code that the background script runs to do this looks like this: &lt;code&gt;chrome.tabs.executeScript(tabId, { file: "additional-code.js" });&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;So, just to recap for a second, before I get into the webpack bits:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Extensions need a &lt;code&gt;manifest.json&lt;/code&gt; file.&lt;/li&gt;
&lt;li&gt;Content-script files run on a browser tab while background-script files run in the browser background and are shared by all of the tabs.&lt;/li&gt;
&lt;li&gt;Content scripts and background scripts can communicate through message passing.&lt;/li&gt;
&lt;li&gt;Background scripts can inject additional code into a browser tab through &lt;code&gt;chrome.tabs.executeScript&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So, in webpack, you can do bundle splitting to break down big files into smaller files. For example, if my &lt;code&gt;additional-code.js&lt;/code&gt; file was too big, it might be broken into &lt;code&gt;additional-code-1.js&lt;/code&gt; and &lt;code&gt;additional-code-2.js&lt;/code&gt;. Actually, the files get a crazy id at the end and look more like &lt;code&gt;additional-code-d939e436.js&lt;/code&gt;. So they are a bit less predictable. Additionally, the number of files a bundle may be split into can change if the code changes. It's very dynamic.&lt;/p&gt;

&lt;p&gt;The challenge here is for my background script code to know what these file names are so that it can properly inject all of the code it needs to when the tab asks for more code. In my case, the background script would have to be able to dynamically figure out which files the original &lt;code&gt;additional-code.js&lt;/code&gt; file was broken out into. Depending on which environment I'm building for, this file may be broken out into a different number of bundles with different names. So it is highly dependent on the build.&lt;/p&gt;

&lt;p&gt;So, that injection code might actually look like this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// files === [ "additional-code-1.js", "additional-code-2.js" ]
files.forEach(file =&amp;gt; {
    chrome.tabs.executeScript(tabId, { file }).
})
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Using the &lt;code&gt;webpack-manifest-plugin&lt;/code&gt;, I am able to hook into the build after the bundles are split. It's sorta intended to automatically build a list of files that webpack builds out, but using the &lt;code&gt;generate&lt;/code&gt; function option that the plug-in takes, you can return your own format for the manifest file.&lt;/p&gt;

&lt;p&gt;For example, I can dynamically add the files that are generated to the manifest by doing something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// generate()
(seed, files) =&amp;gt; {
    const { backgroundScripts, contentScripts } = generateScriptsFromFiles(files);
    const manifest = {
        manifest_version: 2,
        version: "1.2.3",
        name: "My Cool Extension",
        background: {
            scripts: backgroundScripts
        },
        content_scripts: contentScripts
    };
    return manifest;
};
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;To simplify the above, we can take the list of files that are generated and add the appropriate ones to the manifest. This includes our &lt;code&gt;content-script.js&lt;/code&gt; and &lt;code&gt;background-script.js&lt;/code&gt; but does not include &lt;code&gt;additional-code.js&lt;/code&gt; since we want to inject &lt;code&gt;additional-code.js&lt;/code&gt; manually and not inject it automatically via manifest magic.&lt;/p&gt;

&lt;p&gt;However, this list of files does include all of the bundle-split files (&lt;code&gt;additional-code-1.js&lt;/code&gt; and &lt;code&gt;additional-code-2.js&lt;/code&gt;, for example), so it seems like a perfect hook for what I want to do. If I can take these file names and then re-inject them back into the code, then I would be set. However, by the time this generate function runs, the build is basically split and done. I don't see a way to go back and change files at this point.&lt;/p&gt;

&lt;p&gt;Any help or guidance is greatly appreciated. This use-case is definitely on the edge of normal webpack workflows, so I'm happy to continue to elaborate or expand on anything here.&lt;/p&gt;

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