yoniholmes / grunt-text-replace Goto Github PK
View Code? Open in Web Editor NEWGeneral purpose text replacement for grunt. Allows you to replace text in files using strings, regexs or functions.
License: MIT License
General purpose text replacement for grunt. Allows you to replace text in files using strings, regexs or functions.
License: MIT License
Currently, only the first occurance of a pattern is being replaced...
Is it possible to do something like:
from: /"<%= pkg.name %>"\.[a-z0-9.]+\.css/g,
?
Putting them package name in manually works, however it would be useful if it could parse the template variable so it didn't need to be changed for each project.
Hey all,
Is there a way to just remove a line, instead of replacing it with '' ? I think it'd be fairly helpful in dev to prod situations.
Hope this helps!
N
This would be useful in the overwrite mode.
Are multi-line replacements supported? I'm struggling to get them working...
{
from: "foo\nbar",
to: "foobar"
}
I've installed and configured the package correctly. Upon running "grunt replace" it just sits there. I ran "grunt replace -v" and it shows that it's just constantly running "replace" task over and over indefinitely. It never actually makes any changes or ends the stask.
Here's the console.
Here's my setup. I changed the source links and regex to generics to maintain privacy of the project I'm working on. Note, I excluded "dest" because I want to overwrite. I also tried with "dest" and had the same results
Hello there,
For some reason my regexp search and replace is no longer matching my selection - not sure which version this started happening with, however it used to work just fine. Nothing changed my side. ( I am using latest grunt / grunt-cli / replace ) .
Here is my replace grunt task:
replace: {
removeextend: {
src: ['bin/js/**/*.js'],
overwrite: true, // overwrite matched source files
replacements: [{
from: /var __extends[\W\S\D]*__\(\);\s};/g,
to: ""
}]
}
},...
and an example file: http://kurst.co.uk/transfer/JSONLoader.js
Here is the Regex in action: http://www.regexr.com/39u9o
Not sure why this stopped working - that query used to work. Any insight into why this stopped working would be welcome.
Thank you for your time.
Hi,
I'm trying to handle text substitution based on a environment task variable. But it doesn't work..Is it supposed to work ?
Thanks
//What I try to do :
replace: {
dev : {
'host' : {
src: ['app/js/app.min.js'],
overwrite: true,
replacements: [{
from: 'http://$domain',
to: 'http://domain.dev'
}]
},
},
prod: {
'host' : {
src: ['app/js/app.min.js'],
overwrite: true,
replacements: [{
from: 'http://$domain',
to: 'http://domain.com'
}]
}
}
}
// +
grunt.registerTask('package-prod', ['replace:host:prod']);
//What is working but ugly :
replace: {
'host-dev' : {
src: ['app/js/app.min.js'],
overwrite: true,
replacements: [{
from: 'http://$domain',
to: 'http://domain.dev'
}]
},
'host-prod' : {
src: ['app/js/app.min.js'],
overwrite: true,
replacements: [{
from: 'http://$domain',
to: 'http://domain.com'
}]
}
}
// +
grunt.registerTask('package-prod', ['replace:host-prod']);
I receive a trivial error: "Warning: Error while processing "app/scripts/cfg.js" file."
What I want to achieve is to increment the last part of a version number in my cfg.js file.
It's difficult for me to tell what's wrong, because the warning doesn't give me any details.
replace: {
versionnumber: {
src: ['<%= yeoman.app %>/scripts/cfg.js'],
overwrite: true, // overwrite matched source files
replacements: [{
from: /'version' : '([0-9]+)\.([0-9]+)\.([0-9]+)'/g,
to: function (matchedWord, index, fullText, regexMatches) {
return (
'\'version\' : \'' +
regexMatches[1] + '.' +
regexMatches[2] + '.' +
(parseInt(regexMatches[3])+1) +
'\''
);
}
}]
}
},
I am trying to run a replace with overwrite option set to true. This works but for all files including the ones where there were no matches it will change the file encoding from UTF8 to unicode. Any ideas why this is happening? Ideally I would not want the encoding to change at all, especially for files with no matches should not be touched.
Thanks
I'd like to do something like:
replacements: [{
from: /foo/,
to: function(matchedWord, index, fullText, regexMatches, sourceFile) {
grunt.log.writeln('Found "foo" in ' + sourceFile + ' replacing with "bar".');
return 'bar';
}
}]
Maybe I'm missing something and this is available through something like grunt.task
, but the closest I've found is grunt.task.current.file.src
which gives me the unexpanded path (something like ['**/*.js']
).
Hey everybody,
as mentioned (gruntjs/grunt#667) I wonder if it's possible to build in an option to disable template processing.
grunt.initConfig({
replace: {
stylesheets: {
src: "build/stylesheets/**",
overwrite: true,
replacements: [{
from: /url\([^)]*\)/gi,
to: function(matched) {
// some processing going on here
return 'url(<%' + matched + '%>)';
}
}]
}
}
});
};
The problem is the return value. Jonathan told me, that every return value gets processed, so it would be nice, if I could disable it.
My output should look like this: url(<%something%>)
Kind Regards,
Kristof
It be very nice to maintain the directory structure from src wildcard matches.
For example let's say I have:
replace: {
html: {
src: ['src/**/*.html'],
dest: 'dist/'
// the rest
}
}
In this case the dist gets a flattened version of the src files, losing directories and possibly overwriting files, for example a directory with index.html would overwrite the root index.html. It'd be nice to have an expand option that would build the directories as needed.
Currently, there's no way to change the cwd when using grunt-text-replace
, and the destination ignores the path of the source file relative to the cwd, which makes it cumbersome to do a replace when using a more complex file structure.
If you want to, I'll submit a pull request, I've already got it working on my project.
Hello,
I will be nice if you tag each version of this plugin.
Many thanks before
P.
When running replace on a source target that contains a folder with a period in the title, the following error is thrown.
Warning: Unable to read "../testing/foo.bar" file (Error code: EISDIR). Use --force to continue.
In this case, foo.bar
is a folder, not a file, but the script is attempting to open it as if it were a file.
I've found that the issue is caused by the way that grunt.file.expand
is called and then iterated through in the replaceFileMultiple
function. When I updated the grunt.file.expand
call to include only files it fixes the issue. The updated line should look like this:
var sourceFiles = grunt.file.expand({'filter': 'isFile'}, settings.src);
When I look at the generated spritesheet, I see that all the sprites are in one vertical line, instead of being smartly arranged to minimize space. This results in a spritesheet that is about 4x larger than a spritesheet I generated from the same images using glue. I'd prefer to use your plugin because it outputs such nice SCSS -- but this inefficiency is killing it for me. Is there some configuration I should be changing, or is smarter arrangement just not built into the plugin?
Hello,
I'm currently busy to create my own grunt workflow but I've some trouble when I have to replace some strings. I'm not so good using Javascript but I understand it in a general way (sorry guys :/)
Everything in my worlflow works (at least I don't have any errors in my shell) and the task ends in a good way : "Done, without errors." (hourra)
Except that the path to my img folder and the path to my Js folder never change in my file, but the css path well. Here is the part for the "text-replace"
If I can have some advice from people ? Or the explanation of my mistakes ? Thanks a lot for the people who will answer to it.
Have a nice day and Merry Christmas (yeah a little bit in advance :) )
replace:{
img_css:{
src:'<%= project.src %>/css/.css',
overwrite:true,
replacements:[{
from:['<%= project.src %>/img/','../<%= project.src %>/img/'],
to:'<%= project.app %>/img/'
}],
},
img:{
src:['.html','.php','__/.php'],
overwrite:true,
replacements:[{
from:['<%= project.src %>/img/','../<%= project.src %>/img/'],
to:'<%= project.app %>/img/'
}],
},
css:{
src:['.html','.php','/.php'],
overwrite:true,
replacements:[{
from:'',
to:''
}],
},
js:{
src:['.html','*.php','/.php','app/inc/.php'],
overwrite:true,
replacements:[{
from:['<script src="<%= project.src %>/js/lib/_.js"></script>','<script src="<%= project.src %>/js/_.js"></script>'],
to:'<script src="<%= project.app %>/js/app.min.js"></script>'
}],
},
}
replace: {
lang: {
src: ['index.html'],
dest: ['dist/index.html'],
replacements: [{
from: /%([^}]*)%/, //Match %test%
to: function(matchedWord) {
matchedWord = matchedWord.substring(1, matchedWord.length - 1);
return strings.index[matchedWord] ? strings.index[matchedWord] : matchedWord;
}
}]
}
},
I have 100+ strings I want to replace using config files to change the language of my app.
I'm not sure, but I don't think this could be done using grunt-text-replace, am I wrong?
I'm finding this plugin really helpful in my current workflow for my university's CMS, thank you! I was wondering though if there was a way to load replacements from a .json file instead of directly stating them in the .gruntfile? Currently I have about 40 replacements so my gruntfile is getting quite large.
I'm using src with an array of files, and then overwrite. But how do I specify which replacements are related to which files? Do all the replacements occur to all the files?
I'm posting this issue to let you know that we will be publishing Grunt 0.4 on Monday, February 18th.
If your plugin is not already Grunt 0.4 compatible, would you please consider updating it? For an overview of what's changed, please see our migration guide.
If you'd like to develop against the final version of Grunt before Monday, please specify "grunt": "0.4.0rc8"
as a devDependency in your project. After Monday's release, you'll be able to use "grunt": "~0.4.0"
to actually publish your plugin. If you depend on any plugins from the grunt-contrib series, please see our list of release candidates for compatible versions. All of these will be updated to final status when Grunt 0.4 is published.
Also, in an effort to reduce duplication of effort and fragmentation in the developer community, could you review the grunt-contrib series of plugins to see if any of your functionality overlaps significantly with them? Grunt-contrib is community maintained with 40+ contributors—we'd love to discuss any additions you'd like to make.
Finally, we're working on a new task format that doesn't depend on Grunt: it's called node-task. Once this is complete, there will be one more conversion, and then we'll never ask you to upgrade your plugins to support our changes again. Until that happens, thanks for bearing with us!
If you have any questions about how to proceed, please respond here, or join us in #grunt on irc.freenode.net.
Thanks, we really appreciate your work!
Would it be possible to add an encoding option? My source files are UTF-8 and but after running the replace my files were converted to ANSI. Maybe you can add an encoding option like the one available for https://www.npmjs.org/package/grunt-replace?
It would also be nice to have an option like the "force" option on that other package too. It seems running this package causes all my files to be modified even though the contents of the file have actually not changed. This makes it a bit of a pain for git since it shows that I basically need to commit all files that I run the replace for.
I have the following configuration:
"style-image-refs": {
src: ["app/assets/stylesheets/**/*.css"],
overwrite: true,
replacements: [
{ from: "url(\"/images/", to: "url(\"/assets/" },
{ from: "url(\"../images/", to: "url(\"/assets/" }
]
}
to replace image URL paths in CSS files. The overwrite
attribute seems to be causing the replace functionality to break. If I modify the configuration to write the target files to a different directory, like this:
"style-image-refs": {
src: ["app/assets/stylesheets/**/*.css"],
dest: "app/assets/stylesheets/fixed/",
replacements: [
{ from: "url(\"/images/", to: "url(\"/assets/" },
{ from: "url(\"../images/", to: "url(\"/assets/" }
]
}
the files get written to the fixed
directory, and the target strings are replaced as expected. But when I use the overwrite
attribute it doesn't work.
Anyone else experiencing this? Is my configuration setup wrong or something? Any help would be greatly appreciated.
Thanks.
Hi,
Can I anchor a regular expression to match a complete line? The from in the example below (/^x_queue$/) never matches. If I drop and ^ and $ it works fine, but I have cases where I want this (else I could end up with situations like ####x_queue).
Thanks.
disableQueue: {
src: "db/config/plugins",
overwrite: true,
replacements: [{
from: /^x_queue$/,
to: "#x_queue"
}]
First, thanks for your plugin, I'm using it to great effect on my sites.
I have a case where I want to apply the same search/replaced to multiple files, but each with a distinct destination (with a prefix appended). It'd be convenient if I could do something like this:
replace: {
ssl_html: {
files: [
{src: '<%= pkg.template_path %>common_html.group/html_head_end.html', dest: '<%= pkg.template_path %>common_html.group/ssl_html_head_end.html'},
{src: '<%= pkg.template_path %>common_html.group/main_menu.html', dest: '<%= pkg.template_path %>common_html.group/ssl_main_menu.html'},
{src: '<%= pkg.template_path %>common_html.group/info_footer.html', dest: '<%= pkg.template_path %>common_html.group/ssl_info_footer.html'},
{src: '<%= pkg.template_path %>common_html.group/free_content_banner.html', dest: '<%= pkg.template_path %>common_html.group/ssl_free_content_banner.html'},
{src: '<%= pkg.template_path %>common_html.group/free_content_guts.html', dest: '<%= pkg.template_path %>common_html.group/ssl_free_content_guts.html'},
{src: '<%= pkg.template_path %>common_html.group/free_content_tabs.html', dest: '<%= pkg.template_path %>common_html.group/ssl_free_content_tabs.html'},
],
replacements: [
{
from: 'http://cssjs.crushlivepoker.net/',
to: '/site/'
},
{
from: 'http://cssjs.CrushLivePoker.net/',
to: '/site/'
},
},
],
},
},
...where it can accept an array of files with src: and dest: but the same replacements applied to each. Unless I'm doing something wrong, it appears to not be possible currently?
I've got the followring error message:
Warning: Task "replace" not found. Use --force to continue.
Aborted due to warnings.
My package.json:
{
"name": "master-project",
"version": "0.1.0",
"devDependencies": {
"grunt": ">=0.4.5",
"grunt-contrib-nodeunit": ">=0.4.1",
"grunt-contrib-watch": ">=0.6.1",
"grunt-contrib-connect": ">=0.9.0",
"grunt-contrib-concat": ">=0.5.0",
"grunt-autoprefixer": ">=2.0.0",
"grunt-eslint": ">=2.0.0",
"grunt-contrib-stylus": ">=0.20.0",
"grunt-contrib-uglify": ">=0.6.0",
"grunt-contrib-cssmin": ">=0.10.0",
"grunt-text-replace": ">=0.4.0"
}
}
The "grunt-text-replace" is installed in my project's "node_modules" folder.
My Gruntfile.js:
'use strict';
module.exports = function(grunt) {
// Project configuration.
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
uglify: {
build: {
src: 'js/src/global.js', // Input - Unknown filename operator: <%= pkg.name %>
dest: 'js/global.min.js' // Output - Unknown filename operator: <%= pkg.name %>
}
},
replace: {
makethisforme: {
src: ['js/*.min.js'],
overwrite: true, // overwrite matched source files
replacements: [{
from: /\"/g,
to: /\'/g
}]
}
}
});
// Load the plugin that provides the "uglify" task.
grunt.loadNpmTasks('grunt-contrib-uglify', 'grunt-text-replace');
// Default task(s).
// http://gruntjs.com/creating-tasks
grunt.registerTask('default', ['uglify', 'replace']);
};
I want to use this plugin, because I want to change back the duble-quotes to single, which is changed by UglifyJS. The nicer solutaion would be to watch if uglifyjs runs, than take action. But I'm not a Javascript programmer.
hello I would like to use regular expression to replace a specific function call with something else.
specifically I made a function called isUndefined
which returns true if the input is not defined.
ex:
if (isUndefined(PARAM)) {
// do blah
but it appears that some browsers do not like this if PARAM has never been mentioned before
for such reasons I would like to add a build step to replace the function call with
typeof PARAM === "undefined"
the problem is that PARAM could contains parenthesis and I need it to match to the end of the matching parenthesis.
Regular expressions does have a recursive element to it. but that does not exist in javascript apparently.
So I was wondering if you had any advice on how to accomplish using your tool
Example of success:
isUndefined(funcThatMayReturnUndefined())
// changes to
typeof funcThatMayReturnUndefined() === "undefined"
Looks awesome, would love to use this in place of another string replace plugin I am currently using, but can't unless it supports 4.0. Thx...
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.