Search code examples
javascriptgulp

How to execute a gulp task even if the file is unchanged


EDIT: Additional addition of code for task execution

I have a question I can't answer. I need gulp to execute a stain even if the file is unchanged. Currently only when I change my source file it recreates the new destination file. But I would like it to create it even if it hasn't changed. Thank's you in advance :)

Library external : "gulp-inject-string": "^1.1.2",

    // Add timestamp in cache PWA
    function pwa_cache() {
      return src(`${paths.pwa}/serviceworker.tmp.js`)
        .pipe(inject.prepend('const CACHE_VERSION = "pwa-v-'+ new Date().getTime() +'"; \n'))
        .pipe(rename('serviceworker.js'))
        .pipe(dest(`${paths.js}/pwa/`,{overwrite:true}))
    }
    // Generate all assets
const generateAssets = parallel(
  styles,
  scripts,
  pwa_cache,
  imgCompression
)
exports.default = series(generateAssets)
exports["generate-assets"] = generateAssets

Solution

  • well I proceeded differently, if it can help someone, it's the only way I found and I don't have time to find another solution. Instead of adding I rather assign the variable in my file and then I change its value, and I no longer work on a template file but directly on the original file. Post and link that helped me find the answer: https://eureka.ykyuen.info/2015/07/13/gulp-rewrite-source-files-instead-of-creating-new-files/, How to inject variable value into JS file from GULP, Inject variable into html via Gulp Task

    gulp file :

    // Add timestamp in cache PWA
    function pwa_cache() {
        return src(`${paths.js}/pwa/serviceworker.js`, {base: './'})
        .pipe(sourcemaps.init())
        .pipe(replace(new RegExp('django-pwa-v-\\d*', 's'), 'django-pwa-v-'+ new Date().getTime()))
        .pipe(sourcemaps.write('./'))
        .pipe(dest('./'))
    
    }
    

    js file :

    // Cache version don't touch string assigned to the variable /
    CACHE_VERSION = "django-pwa-v-1580491632502";
    /////////////////////////////////////////////////////////////