Y
Y
Yura Komarov2020-06-23 11:07:46
Node.js
Yura Komarov, 2020-06-23 11:07:46

How to write to a file using node js and gulp?

Hello. Tell me how to write data from one file to another using gulp and nodejs.
That is, I have a js file and I want to write it to a php file that needs to be created and which will be included on the page.

script.js file

export const phpReplace = done => {
    const script = fs.readFileSync('public/scripts/chunk-vendors.min.js', 'utf8');
    const str = '<script>\n' + script + '\n</script>';
    fs.writeFile('public/includes/app_min_js.php', str);
    done();
};

default.js file
import {templates, phpReplace} from './script';
gulp.task(
    'build',
    gulp.series(templates, gulp.parallel(phpReplace ))
);

Well, that is, I want that when I run the npm run build command, the phpReplace function will work for me, which will read the data from the chunk-vendors.min.js file, create the app_min_js.php file and write everything from the read file there.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
Yura Komarov, 2020-06-23
@Yurajun

Thanks to everyone who wanted to help solve the problem.
I replaced the writeFile function with writeFileSync and it seems like the file was created with the script written.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question