title: How to edit an object within a very simple JS file using Node.js
body: <p><em>Whilst this question is related to Workbox and Webpack, it does not require any prior knowledge of either library.</em></p>
  
  <h1>Background (skip if not familiar with Workbox)</h1>
  
  <p>I am currently utilising the <code>InjectManifest</code> plugin from Workbox 4.3.1 (<a href="https://developers.google.com/web/tools/workbox/modules/workbox-webpack-plugin" rel="nofollow noreferrer">workbox-webpack-plugin</a>). This version of the library offers an option called <code>manifestTransforms</code>, but unfortunately, the transformations are not applied to assets within the webpack compilation (this is a <a href="https://github.com/GoogleChrome/workbox/issues/1227" rel="nofollow noreferrer">known issue</a>).</p>
  
  <p>Whilst this has been fixed in Workbox v5+, I am unable to upgrade due to another library in my build process requiring webpack v3 (<a href="https://laravel-mix.com/docs/4.1/upgrade#notes" rel="nofollow noreferrer">Dynamic Importing in Laravel Mix</a>)</p>
  
  <p><em>The reason I mention the above is because unforunately the solution is not to upgrade to workbox v5+.</em></p>
  
  <h1>The Problem</h1>
  
  <p>I have an auto-generated file that looks like this:</p>
  
  <pre><code>self.__precacheManifest = (self.__precacheManifest || []).concat([
      {
          "revision": "68cd3870a6400d76a16c",
          "url": "//css/app.css"
      },
      // etc...
  ]);
  </code></pre>
  
  <p>I need to somehow extract the the contents of the object stored within <code>self.__precacheManifest</code>, apply my own transformations, and then save it back to the file.</p>
  
  <h1>What I have Tried...</h1>
  
  <p>This is as far as I have got:</p>
  
  <pre><code>// As the precached filename is hashed, we need to read the
  // directory in order to find the filename. Assuming there
  // are no other files called `precache-manifest`, we can assume
  // it is the first value in the filtered array. There is no
  // need to test if [0] has a value because if it doesn't
  // this needs to throw an error
  let manifest = fs
      .readdirSync(path.normalize(`${__dirname}/dist/js`))
      .filter(filename =&gt; filename.startsWith('precache-manifest'))[0];
  
  require('./dist/js/' + manifest);
  
  // This does not fire because of thrown error...
  console.log(self.__precacheManifest);
  </code></pre>
  
  <p>This throws the following error:</p>
  
  <blockquote>
    <p>self is not defined</p>
  </blockquote>
  
  <p>I understand why it is throwing the error, but I have no idea how I am going to get around this because I need to somehow read the contents of the file in order to extract the object. Can anyone advise me here?</p>
  
  <p>Bear in mind that once I have applied the transformations to the object, I then need to save the updated object to the file...</p>