adamreisnz / replace-in-file

A simple utility to quickly replace contents in one or more files
580 stars 65 forks source link

How to use it with Gulp? #136

Closed brendan8c closed 3 years ago

brendan8c commented 3 years ago

I want to replace all lines 'img/sprite.png' with on strings '../img/sprite.png' The lines are in the file (style.min.css) I am doing something wrong... I have errors, please help me

0

function fixSprites() {
    const results = replace.sync({
        files: config.build.style + '/style.min.css',
        from: 'img/sprite.png',
        to: '../img/sprite.png',
        countMatches: true,
    })
    return src(config.build.style + '/style.min.css')
        .pipe(replace(results))
}

exports.fixSprites = fixSprites()
adamreisnz commented 3 years ago

This is a standalone Node module, I don't think you can just use it in Gulp like that. Instead, you need to wrap it in a helper function like so:

module.exports = function updateRedirects(done) {
  replaceInFile({
    files: path.join(build.DEST_ASSETS, '_redirects'),
    from: /%API_BASE_URL%/g,
    to: config.api.baseUrl,
  }, done);
};

And then use it in Gulp like with gulp.series or gulp.parallel

brendan8c commented 3 years ago

This is the out of the box gulp solution Replace all lines of a similar type with others. Example: replace img/sprite.png with ../img/sprite.png

2021-06-06 02_02_30-gulpfile js - start_gulp - Visual Studio Code  Не поддерживается

const { series } = require('gulp')
const replace = require('replace-in-file')

function updateRedirects(done) {
    replace({
        files: 'build/css/style.min.css',
        from: /img\/sprite.png/g, // img/sprite.png – Replace this.
        to: '../img/sprite.png', // ../img/sprite.png – Replace with this.
        countMatches: true,
    }, done)
}

exports.build = series(updateRedirects) // Start
brendan8c commented 3 years ago

Это автономный модуль Node, я не думаю, что вы можете просто так использовать его в Gulp. Вместо этого вам нужно обернуть его вспомогательной функцией следующим образом:

module.exports = function updateRedirects(done) {
  replaceInFile({
    files: path.join(build.DEST_ASSETS, '_redirects'),
    from: /%API_BASE_URL%/g,
    to: config.api.baseUrl,
  }, done);
};

А затем используйте его в Gulp, например, с помощью gulp.seriesилиgulp.parallel

Tell me can I do this with any node.js plugin? I've tried doing something similar with other node.js plugins but it didn't work. What might it depend on? What do I need to know to adopt any other node.js plugin for gulp? I am asking you because I believe you are an expert in this area.

adamreisnz commented 3 years ago

Yes, just make your own file and import it as a plugin in your main gulpfile.

E.g.

const updateRedirects = require('./build/tasks/update-redirects');

gulp.task('build', gulp.series(
  clean,
  gulp.parallel(
    gulp.series(
      copyAssets,
      updateHeaders,
      updateRedirects,
      createVersionFile
    ),
    buildConfig,
    buildAppJs,
    buildAppCss,
    buildLibJs,
  ),
  buildIndex,
  updateServiceWorker
));