简体   繁体   中英

How to edit an object within a very simple JS file using Node.js

Whilst this question is related to Workbox and Webpack, it does not require any prior knowledge of either library.

Background (skip if not familiar with Workbox)

I am currently utilising the InjectManifest plugin from Workbox 4.3.1 ( workbox-webpack-plugin ). This version of the library offers an option called manifestTransforms , but unfortunately, the transformations are not applied to assets within the webpack compilation (this is a known issue ).

Whilst this has been fixed in Workbox v5+, I am unable to upgrade due to another library in my build process requiring webpack v3 ( Dynamic Importing in Laravel Mix )

The reason I mention the above is because unforunately the solution is not to upgrade to workbox v5+.

The Problem

I have an auto-generated file that looks like this:

self.__precacheManifest = (self.__precacheManifest || []).concat([
    {
        "revision": "68cd3870a6400d76a16c",
        "url": "//css/app.css"
    },
    // etc...
]);

I need to somehow extract the the contents of the object stored within self.__precacheManifest , apply my own transformations, and then save it back to the file.

What I have Tried...

This is as far as I have got:

// 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 => filename.startsWith('precache-manifest'))[0];

require('./dist/js/' + manifest);

// This does not fire because of thrown error...
console.log(self.__precacheManifest);

This throws the following error:

self is not defined

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?

Bear in mind that once I have applied the transformations to the object, I then need to save the updated object to the file...

Since self refers to window and window does not exist in node.js a way around is needed. One thing that should work is to define the variable self in Node's global scope and let the require statement populate the content of the variable, like this:

global['self'] = {};
require('./dist/js/' + manifest);
console.log(self.__precacheManifest);

To save the modified contents back to the file

const newPrecacheManifest = JSON.stringify(updatedArray);
fs.writeFileSync('./dist/js/' + manifest, `self.__precacheManifest = (self.__precacheManifest || []).concat(${newPrecachedManifes});`, 'utf8');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM