W
W
wobemah2021-07-23 13:45:17
Building projects
wobemah, 2021-07-23 13:45:17

How to get JSON from a JS module in Gulp?

I have a file "settings.js" and it exports settings, I need to transform this object with settings into a "setting.json" file and move it to another directory. (I know how to transfer, but how to make a json file from a JS module (object) - no, please help.)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2021-07-23
@wobemah

I did not find a ready-made package, you can try this, for a start.

const { src, dest, series } = require('gulp');
const through = require('through2');

const transformToJson = () => {
    return src('./src/settings.js')
        .pipe(through.obj((file, _, callback) => {
            const computed = require(file.path);

            file.extname = '.json';
            file.contents = Buffer.from(JSON.stringify(computed, null, 4));

            callback(null, file);
        }))
        .pipe(dest('./dist'));
};

module.exports.default = series(transformToJson);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question