Grunt-contrib-copy syntax for confusing process parameters

I am trying to replace some placeholders in different files when copying. My grunt file works fine, but it adds the ability to make replacements to the process, it just doesn't work. Below is the relevant section of my grunt file:

grunt.initConfig({ copy: { js: { files: [{ expand: true, cwd: 'src/wp-content/themes/pilau-starter/', src: ['**/*.js'], dest: 'public/wp-content/themes/pilau-starter/' }], options: { process: function ( content ) { console.log( content ); content = content.replace( /pilauBreakpointLarge/g, breakpoints.large ); content = content.replace( /pilauBreakpointMedium/g, breakpoints.medium ); return content; } } }, } }); 

The paths can be understood in the context of the GitHub code: https://github.com/pilau/starter (the shared directory is not tied to the repo, because this is the starting topic). These paths are variables in my source Gruntfile and work fine in all other tasks.

All varnas are configured OK. I turned on console.log( content ) to check if this function of the process really works - it seems this is not so, so I assume this is the basic syntax.

There is an answer ( https://stackoverflow.com/a/165168/ ) that seems to address this, but as far as I can tell, this way of doing it is just bad JS syntax - not sure how it is indicated as correctly.

--verbose output to start the copy task:

 Running "copy:js" (copy) task Verifying property copy.js exists in config...OK Files: src/wp-content/themes/pilau-starter/js/admin.js -> public/wp-content/themes/pilau-starter/js/admin.js Files: src/wp-content/themes/pilau-starter/js/flickity.js -> public/wp-content/themes/pilau-starter/js/flickity.js Files: src/wp-content/themes/pilau-starter/js/global.js -> public/wp-content/themes/pilau-starter/js/global.js Files: src/wp-content/themes/pilau-starter/js/modernizr.js -> public/wp-content/themes/pilau-starter/js/modernizr.js Files: src/wp-content/themes/pilau-starter/js/picturefill.js -> public/wp-content/themes/pilau-starter/js/picturefill.js Files: src/wp-content/themes/pilau-starter/js/respond.js -> public/wp-content/themes/pilau-starter/js/respond.js Options: processContent=false, processContentExclude=[], process=undefined Options: processContent=false, processContentExclude=[], process=undefined Copying src/wp-content/themes/pilau-starter/js/admin.js -> public/wp-content/themes/pilau-starter/js/admin.js Reading src/wp-content/themes/pilau-starter/js/admin.js...OK Writing public/wp-content/themes/pilau-starter/js/admin.js...OK 
+6
source share
2 answers

Your version of grunt-contrib-copy is 0.4.0. How to properly mark @nemesv over the name of the property that will be used in this version will be processContent not process .

I cloned your repo and switched to the json-breakpoints branch. And ran grunt copy:js and it replaced the contents.

original-contentreplaced-content

Now when you run grunt copy:js --verbose , it will show it anyway

cli

processContent registered undefined because grunt uses JSON.stringify to register the value. And JSON.stringify returns undefined when you pass it a function definition.


If you are interested, here is the method responsible for registering the entire option

  Log.prototype.writeflags = function(obj, prefix) { var wordlist; if (Array.isArray(obj)) { wordlist = this.wordlist(obj); } else if (typeof obj === 'object' && obj) { wordlist = this.wordlist(Object.keys(obj).map(function(key) { var val = obj[key]; return key + (val === true ? '' : '=' + JSON.stringify(val)); })); } this._writeln((prefix || 'Flags') + ': ' + (wordlist || '(none)'.cyan)); return this; }; 
+2
source

This is not a problem with the process parameter at all, but more of a problem with srcThemeDir . I would run it to make sure that you know exactly what it is, because it seems that the copy task does not find the files (and therefore does not call the function of the process).

0
source

Source: https://habr.com/ru/post/988163/


All Articles