Coder Social home page Coder Social logo

Comments (34)

bilalq avatar bilalq commented on September 13, 2024 2

For the benefit of anyone reading this with the same problem, there's a super simple solution. It turns out that the rename function suggested by @jonhartmann does indeed handle the dest value of the files array.

All you need to do is add:

rename: function(dest) { return dest }

as a property under your target.

Finding it in the documentation was pretty hard though, as it's only briefly mentioned in the grunt.file docs. The "inside task" and "grunt.config" pages make no mention about it.

from grunt-contrib-concat.

jahooma avatar jahooma commented on September 13, 2024 1

bilalq's solution worked for me, but let me just add my vote that something is not quite right here and probably should be fixed.

from grunt-contrib-concat.

shama avatar shama commented on September 13, 2024

What does your Gruntfile look like? cwd should be handled outside of this plugin in grunt. This task just gets the files.

from grunt-contrib-concat.

shama avatar shama commented on September 13, 2024

Closing for lack of activity. Please reopen if you're still having this issue. Thanks!

from grunt-contrib-concat.

dnlmzw avatar dnlmzw commented on September 13, 2024

Not sure if this is the right place to post, but seems like I'm experiencing the same.

concat:
options:
separator: ";"
compile:
src: ['<%= DEVELOPMENT_PATH %>/scripts//*.js','!<%= DEVELOPMENT_PATH %>//plugins/**']
dest: '<%= PRODUCTION_PATH %>/scripts/main.js'

concat:
options:
separator: ";"
compile:
cwd: '<%= DEVELOPMENT_PATH %>'
src: ['scripts//*.js','!/plugins/**']
dest: '<%= PRODUCTION_PATH %>/scripts/main.js'

Would be more consistent with how other grunt plugins work.

from grunt-contrib-concat.

shama avatar shama commented on September 13, 2024

@dnlmzw What issue are you having? All of the grunt-contrib plugins, including this one, allow the 2nd syntax you posted.

This issue is suggesting the cwd needs to be prepended within this task. It does not as all that is handled by grunt and not this task. So the original poster likely has a misconfiguration in their gruntfile (my guess).

from grunt-contrib-concat.

dnlmzw avatar dnlmzw commented on September 13, 2024

Well, I don't know if I should open it as a new issue. But all I know is that when I run this, it works:

uglify:
compile:
src: '<%= DEVELOPMENT_PATH %>/scripts/*/.js'
dest: '<%= PRODUCTION_PATH %>/scripts/main.js'

But this fails:

uglify:
compile:
cwd: '<%= DEVELOPMENT_PATH %>/scripts/'
src: '*/.js'
dest: '<%= PRODUCTION_PATH %>/scripts/main.js'

from grunt-contrib-concat.

dnlmzw avatar dnlmzw commented on September 13, 2024

Sorry! Only works with expand set to true! ;)

from grunt-contrib-concat.

shama avatar shama commented on September 13, 2024

@dnlmzw Oh good call. :)

from grunt-contrib-concat.

dnlmzw avatar dnlmzw commented on September 13, 2024

Thanks for the quick reply though, and sorry for wasting your time! :)

from grunt-contrib-concat.

shama avatar shama commented on September 13, 2024

No worries!

from grunt-contrib-concat.

dnlmzw avatar dnlmzw commented on September 13, 2024

I believe I was just unfocused on my real problem. I guess I was just tricked to believe that this was the solution, when in fact my problem was something different. Can it be that when you use the 'cwd', 'dest' is returning an array eventhough 'concat' should only return a single file? I guess this is a Grunt behaviour that cannot be ignored or?

concat:
compile:
expand: true
cwd: '<%= BUILD_PATH %>/scripts'
src: ['plugins//*.js','/*.js']
dest: '<%= BUILD_PATH %>/scripts/main.min.js'

At least, this gives me a result where 'main.min.js' is being created as a folder with all the files that should have been concatenated, rather than a single file with all the code.

Doe this make sense?

from grunt-contrib-concat.

shama avatar shama commented on September 13, 2024

Oh why not just do this then?

concat: {
  compile: {
    src: '<%= DEVELOPMENT_PATH %>/scripts/**/*.js',
    dest: '<%= PRODUCTION_PATH %>/scripts/main.min.js',
  },
},

Also just a side note, if you're concat'ing scripts, you should consider using a module bundling tool like browserify or requirejs instead. As managing the script concat order gets a bit crazy after a few files.

from grunt-contrib-concat.

dnlmzw avatar dnlmzw commented on September 13, 2024

Makes sense, but this is because I need to make sure which order my scripts are concatenated. Like so:
src: ['<%= BUILD_PATH %>/plugins//*.js','<%= BUILD_PATH %>//*.js']
This is the first time I'm working with this, and haven't made a succesfull compile yet, so it might end up not working and I will have prioritize even deeper into the folders. I guess this is what requirejs would handle for me? Sorry for making this a Q&A, but yes, as I said. First time working with this! ;)

from grunt-contrib-concat.

shama avatar shama commented on September 13, 2024

@dnlmzw Exactly. I personally use browserify with this grunt task. If you're comfortable with the node var thing = require('./thing.js') syntax, browserify makes transitioning that flow to the browser pretty easy.

You usually have one main entry point to your scripts, ie: index.js:

var plugin1 = require('./plugins/plugin1.js');
var plugin2 = require('./plugins/plugin2.js');
plugin1.doSomething();

Then in your plugins/plugin1.js:

var plugin1 = module.exports = {};
plugin1.doSomething = function() {
  console.log('I did something!');
};

Then you only ever have to point your build tool to index.js and it will build loading the files in the correct order. Here is an example site using this setup: https://github.com/shama/wesbos

requirejs is another approach. It is more geared for AMD style modules (but can load commonjs/node syntax as well). This is an example using requirejs: https://github.com/cowboy/wesbos

from grunt-contrib-concat.

stianpr avatar stianpr commented on September 13, 2024

I'm experiencing the same issue that concat tries to create a folder with the dest. file. The core files definition should work with this plugin too. So there has to be some sort of a bug?

Here is my config:

concat: {
  script: {
    cwd: 'src/js/',
    expand: true,
    src:[
        'file1.js', 'file2.js', 'file3.js'
    ],
    dest: 'src/static/js/output.js'
  }
}

Results in this error:

Warning: Unable to write "src/static/js/output.js/file1.js" file (Error code: ENOTDIR). Use --force to continue.
Aborted due to warnings.

from grunt-contrib-concat.

shama avatar shama commented on September 13, 2024

@stianpr Which version of grunt-contrib-concat and grunt are you using?

from grunt-contrib-concat.

2betop avatar 2betop commented on September 13, 2024

Hi @shama, your are right, cwd should be handled outside of this plugin in grunt. Every grunt plugin scratched from grunt init has the same issue. But it's also easy to fix it in plugin like this.
image

My Gruntfile.js is like this.

concat: {
    all: {
        cwd: 'src',
        src: [
                    'amd.js',
                    'base.js',
                    'file.js'
                    ....
                ]
    }
}

Without cwd it works.

concat: {
    all: {
        src: [
                    'src/amd.js',
                    'src/base.js',
                    'src/file.js'
                    ....
                ]
    }
}

Sorry for reply so later!

from grunt-contrib-concat.

stianpr avatar stianpr commented on September 13, 2024

@shama I use concat v0.3.0 and grunt v0.4.1

from grunt-contrib-concat.

stianpr avatar stianpr commented on September 13, 2024

bump

from grunt-contrib-concat.

shama avatar shama commented on September 13, 2024

@stianpr I cannot reproduce. Here is what I get when I copy in the config you provided above:

$ grunt concat:script
Running "concat:script" (concat) task
File "src/static/js/output.js/file1.js" created.
File "src/static/js/output.js/file2.js" created.
File "src/static/js/output.js/file3.js" created.

Done, without errors.

But I don't think the config above is what you're intending. Try this instead:

concat: {
  script: {
    src:['src/js/file1.js', 'src/js/file2.js', 'src/js/file3.js'],
    dest: 'src/static/js/output.js',
  },
},

from grunt-contrib-concat.

shama avatar shama commented on September 13, 2024

Ha nm. I exactly reproduced. Im tired. :)

But that is expected behavior as the dest src/static/js/output.js. Try the config I provided below my wrong answer :).

from grunt-contrib-concat.

stianpr avatar stianpr commented on September 13, 2024

@shama if I get you currently you want me to append src/js to all my 16 files that needs concatenation? Isn't the cwd option a much cleaner way to do it? And shouldn't the Files definition work in this grunt plugin as in any other?

from grunt-contrib-concat.

shama avatar shama commented on September 13, 2024

cwd is for building the files object dynamically as outlined here: http://gruntjs.com/configuring-tasks#building-the-files-object-dynamically I don't think we can know whether you intend your dest to be a file or dir, as you can have a dir named output.js.

I think it would be a good idea to make cwd work as expected above if expand is false. Development on the file matching API has moved to globule and I think this issue has came up before. Not sure if it was implemented there or not. I'll check and open an issue for it if not.

For now you can use patterns if you want to match multiple files src: 'src/js/*.js' or build the src array with js if you need a specific custom order:

src: (function() {
  var cwd = 'src/js/';
  var arr = [];
  // determine file order here and concat to arr
  return arr.map(function(file) { return cwd + file; });
}())

This plugin doesn't do any special handling of the files object, see https://github.com/gruntjs/grunt-contrib-concat/blob/master/tasks/concat.js#L35

from grunt-contrib-concat.

x1a0 avatar x1a0 commented on September 13, 2024

I think I am having the same problem here. Bad thing to me is my file list is defined in a json file...

Because this plugin is "concat" the dest should always be treated as a file I think... :-|

from grunt-contrib-concat.

jonhartmann avatar jonhartmann commented on September 13, 2024

This should REALLY REALLY REALLY be reopened and addressed. I'm having this exact problem, and while the solution you've provided does create a route to creating the desired outcome, it is also just plain hard to work with. I'm trying to create a reusuable grunt setup that works with multiple applications based on changing out a JSON configuration file defining the files involved in the app. This plugin is where it busts up though, because suddenly I can't just use a "cwd" to a common expected directory and then have the app specify the files inside it like I have for half a dozen other plugins. Instead I have to reverse that and place the path back into each config file which will create more room for problems and tedious copy/pasting. My only other options are to claw back some level of control by placing dynamic values into all the URLs or maybe writing some kind of rename function. Placing dynamic values like <%= path %> into my source is possible, but that still leaves the problem of needing to specify that in every path used in every application. AS for renaming?? Is it even possible with this?

I whole heartedly thank you for the work that you're doing with this plugin... but this is a problem with no good solutions other than fixing the functionality in the plugin itself.

from grunt-contrib-concat.

jonhartmann avatar jonhartmann commented on September 13, 2024

I stand by my last comment, but I'll throw this in there... it does seem easier than I expected to circumvent this with the rename function.

{
cwd: buildConfig.paths.scripts,
src: assets.scripts,
rename: function (dest, src) {
return dest;
},
expand: true,
dest: '<%= config.paths.build %><%= pkg.name %>.<%= pkg.version %>.js'
}

Seems to work just fine.

from grunt-contrib-concat.

sladex avatar sladex commented on September 13, 2024

You can set cwd like this:

js: [
  'libs/amd.js',
  'libs/jquery.js',
  'app.js',
  'main.js'
].map(function(file) { return 'web-src/js/' + file; }),

from grunt-contrib-concat.

jonhartmann avatar jonhartmann commented on September 13, 2024

That's not a terrible idea either, I just have to move it from the JSON I'm using to configure things into code used to configure Grunt itself. Thanks!

from grunt-contrib-concat.

bilalq avatar bilalq commented on September 13, 2024

Mapping through a list works, but if you're referencing a list as a template from within another config property and need to prefix it with a cwd from a property, this gets to be very messy.

Let's say we have some task that retrieves files over the network from somewhere and we then later want to concat these retrieved:

someTaskThatCopies: {
  dest: '<%= pkg.tmpDir %>/someDir',
  options: {
    someList: [
      'a/a.js',
      'a/b.js',
      'b/b.js'
    ]
  }
},
concat: {
  someTarget: {
    expand: true,
    cwd: '<%= someTaskThatCopies.dest %>',
    src: '<%= someTaskThatCopies.options.someList %>',
    dest: '<%= pkg.buildDir %>/retrieved.js',
    nonull: true
  }
}

Given the way dest is being treated, the above doesn't work. @jonhartmann, can you expand a bit on how you used that rename function? Did you just invoke it from your lodash templates, or is there some builtin special handler of the rename property?

from grunt-contrib-concat.

shama avatar shama commented on September 13, 2024

It is under building the files object dynamically but I agree rename could use more explanation in the docs. Patches welcome https://github.com/gruntjs/grunt-docs :)

from grunt-contrib-concat.

mikelyons avatar mikelyons commented on September 13, 2024

So tl;dr is that cwd: doesn't work for dest: ?
I'm having this issue with a different plugin (grunt-contrib-coffee)
Where CWD is just not prepended to dest so I have to add a template string to it redundantly

from grunt-contrib-concat.

shama avatar shama commented on September 13, 2024

@mikelyons cwd is for setting where the src operates from when using expand: true. If you want the path to be included in the dest, don't include that path in the cwd. The good assumption is your src and dest paths are different (and usually should be) and is why it operates that way.

from grunt-contrib-concat.

therazor avatar therazor commented on September 13, 2024

This got me as well.
I agree with the comments above, this doesn't seem to be the most expected behaviour.

from grunt-contrib-concat.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.